use std::cell::RefCell;
use std::collections::HashMap;
use mitsuami_core::Size;
use objc2::rc::Retained;
use objc2::{MainThreadMarker, MainThreadOnly};
use objc2_app_kit::{NSBox, NSBoxType, NSTitlePosition};
use objc2_foundation::{NSPoint, NSRect, NSSize};
use super::ns;
pub(super) fn group_box(mtm: MainThreadMarker, size: NSSize) -> Retained<NSBox> {
let frame = NSBox::initWithFrame(NSBox::alloc(mtm), NSRect::new(NSPoint::new(0.0, 0.0), size));
frame.setBoxType(NSBoxType::Primary);
frame.setTitlePosition(NSTitlePosition::NoTitle);
frame
}
pub(super) fn set_group_title(frame: &NSBox, title: &str) {
frame.setTitle(&ns(title));
frame.setTitlePosition(if title.is_empty() { NSTitlePosition::NoTitle } else { NSTitlePosition::AtTop });
frame.setNeedsLayout(true);
frame.layoutSubtreeIfNeeded();
}
pub(super) fn group_insets(mtm: MainThreadMarker, title: &str) -> mitsuami_core::Insets {
group_probe(mtm, title).0
}
fn group_probe(mtm: MainThreadMarker, title: &str) -> (mitsuami_core::Insets, f32) {
let frame = group_box(mtm, NSSize::new(10000.0, 300.0));
set_group_title(&frame, title);
probe_insets(&frame)
}
#[derive(PartialEq, Eq, Hash)]
pub(super) struct ProbeKey(usize, String, String, u64, usize, u64, u64, u64);
const PROBES_KEPT: usize = 256;
pub(super) fn group_probe_like(
probes: &RefCell<HashMap<ProbeKey, (mitsuami_core::Insets, f32)>>,
shown: &NSBox,
) -> (mitsuami_core::Insets, f32) {
let font = shown.titleFont();
let margins = shown.contentViewMargins();
let key = ProbeKey(
shown.boxType().0,
shown.title().to_string(),
font.fontName().to_string(),
font.pointSize().to_bits(),
shown.titlePosition().0,
shown.borderWidth().to_bits(),
margins.width.to_bits(),
margins.height.to_bits(),
);
if let Some(probe) = probes.borrow().get(&key) {
return *probe;
}
let probe = probe_like(shown);
let mut probes = probes.borrow_mut();
if probes.len() >= PROBES_KEPT {
probes.clear();
}
probes.insert(key, probe);
probe
}
fn probe_like(shown: &NSBox) -> (mitsuami_core::Insets, f32) {
let frame = group_box(MainThreadMarker::from(shown), NSSize::new(10000.0, 300.0));
frame.setBoxType(shown.boxType());
frame.setTitle(&shown.title());
frame.setTitleFont(&shown.titleFont());
frame.setTitlePosition(shown.titlePosition());
frame.setBorderWidth(shown.borderWidth());
frame.setContentViewMargins(shown.contentViewMargins());
probe_insets(&frame)
}
fn probe_insets(frame: &NSBox) -> (mitsuami_core::Insets, f32) {
let heading = frame.titleRect();
let titled = frame.titlePosition() != NSTitlePosition::NoTitle && !frame.title().is_empty();
let heading = if titled { (heading.origin.x * 2.0 + heading.size.width) as f32 } else { 0.0 };
let (bounds, content) = (frame.bounds(), frame.contentView().map_or(frame.bounds(), |v| v.frame()));
let insets = mitsuami_core::Insets::new(
(bounds.size.height - content.origin.y - content.size.height) as f32,
(bounds.size.width - content.origin.x - content.size.width) as f32,
content.origin.y as f32,
content.origin.x as f32,
);
(insets, heading)
}
pub(super) fn group_natural_size(
probes: &RefCell<HashMap<ProbeKey, (mitsuami_core::Insets, f32)>>,
frame: &NSBox,
) -> Size {
let (insets, heading) = group_probe_like(probes, frame);
Size::new(heading.max(insets.left + insets.right).ceil(), insets.top + insets.bottom)
}