use std::cell::Cell;
use mitsuami_core::{HeaderEdge, NodeId};
use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject, NSObjectProtocol};
use objc2::{MainThreadMarker, msg_send, sel};
use objc2_app_kit::{
NSAutoresizingMaskOptions, NSLayoutAttribute, NSLayoutRelation, NSTitlebarAccessoryViewController, NSView, NSWindow,
};
use objc2_foundation::{NSPoint, NSRect, NSSize};
use crate::classes::HostView;
pub(crate) struct Header {
pub(crate) content: Retained<HostView>,
accessory: Retained<NSTitlebarAccessoryViewController>,
pub(crate) on: Cell<bool>,
pub(crate) window: Cell<Option<NodeId>>,
}
impl Header {
pub(crate) fn new(mtm: MainThreadMarker, placeholder: &NSView) -> Header {
let content = HostView::new(mtm);
for child in placeholder.subviews().iter() {
child.removeFromSuperview();
content.addSubview(&child);
}
let accessory = NSTitlebarAccessoryViewController::new(mtm);
accessory.setView(&content);
accessory.setLayoutAttribute(NSLayoutAttribute::Bottom);
let header = Header { content, accessory, on: Cell::new(true), window: Cell::new(None) };
header.fill(placeholder);
header
}
fn fill(&self, placeholder: &NSView) {
self.content.removeFromSuperview();
self.content.setFrame(placeholder.bounds());
self.content.setAutoresizingMask(
NSAutoresizingMaskOptions::ViewWidthSizable | NSAutoresizingMaskOptions::ViewHeightSizable,
);
placeholder.addSubview(&self.content);
}
pub(crate) fn attach(&self, id: NodeId, window: &NSWindow, host: &NSView, height: f64) {
self.content.removeFromSuperview();
self.content.setAutoresizingMask(NSAutoresizingMaskOptions::ViewWidthSizable);
self.content.setFrame(NSRect::new(NSPoint::ZERO, NSSize::new(host.frame().size.width, height)));
window.addTitlebarAccessoryViewController(&self.accessory);
self.window.set(Some(id));
set_host_top(host, -height);
}
pub(crate) fn set_edge(&self, edge: HeaderEdge) {
let Some(class) = AnyClass::get(c"NSScrollEdgeEffectStyle") else { return };
if !self.accessory.respondsToSelector(sel!(setPreferredScrollEdgeEffectStyle:)) {
return;
}
let style: Option<Retained<AnyObject>> = unsafe {
match edge {
HeaderEdge::Automatic => msg_send![class, automaticStyle],
HeaderEdge::Soft => msg_send![class, softStyle],
HeaderEdge::Hard => msg_send![class, hardStyle],
}
};
if let Some(style) = style {
let _: () = unsafe { msg_send![&*self.accessory, setPreferredScrollEdgeEffectStyle: &*style] };
}
}
pub(crate) fn set_height(&self, host: &NSView, height: f64) {
let size = self.content.frame().size;
if size.height != height {
self.content.setFrameSize(NSSize::new(size.width, height));
set_host_top(host, -height);
}
}
pub(crate) fn detach(&self, placeholder: &NSView, host: Option<&NSView>) {
if self.window.take().is_none() {
return;
}
self.accessory.removeFromParentViewController();
if let Some(host) = host {
set_host_top(host, 0.0);
}
self.fill(placeholder);
}
}
fn set_host_top(host: &NSView, offset: f64) {
let Some(frame) = (unsafe { host.superview() }) else { return };
for constraint in frame.constraints().iter() {
let first = unsafe { constraint.firstItem() };
if first.is_some_and(|item| std::ptr::eq(&*item as *const _ as *const NSView, host))
&& constraint.firstAttribute() == NSLayoutAttribute::Top
&& constraint.relation() == NSLayoutRelation::Equal
{
constraint.setConstant(offset);
return;
}
}
}