#![allow(unused_unsafe)]
#![cfg(target_os = "macos")]
mod controls;
mod applications;
mod color;
mod nav_progress;
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::rc::Rc;
use linkme::distributed_slice;
use objc2::Message as _;
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObjectProtocol, ProtocolObject, Sel};
use objc2::{
AllocAnyThread, DefinedClass, MainThreadMarker, MainThreadOnly, define_class, msg_send, sel,
};
use objc2_app_kit::NSAccessibility as _;
use objc2_app_kit::NSAppearanceCustomization as _;
use objc2_app_kit::NSDraggingInfo as _;
use objc2_app_kit::NSSplitViewDelegate;
use objc2_app_kit::NSTextContent as _;
use objc2_app_kit::NSTextInputClient;
use objc2_app_kit::NSUserInterfaceItemIdentification as _;
use objc2_app_kit::{
NSAffineTransformNSAppKitAdditions, NSClickGestureRecognizer, NSGestureRecognizer,
NSGestureRecognizerDelegate, NSGestureRecognizerState, NSPanGestureRecognizer, NSPasteboard,
NSPressGestureRecognizer,
};
use objc2_app_kit::{
NSAnimationContext, NSApplication, NSApplicationActivationPolicy, NSBackingStoreType,
NSBitmapImageFileType, NSBox, NSBoxType, NSButton, NSColor, NSControl, NSControlStateValueOff,
NSControlStateValueOn, NSControlTextEditingDelegate, NSCursor, NSCursorFrameResizeDirections,
NSCursorFrameResizePosition, NSEvent, NSEventModifierFlags, NSEventType, NSFont,
NSGraphicsContext, NSLineBreakMode, NSMenu, NSMenuItem, NSProgressIndicator,
NSProgressIndicatorStyle, NSResponder, NSScrollView, NSSecureTextField, NSSlider, NSSwitch,
NSTabViewDelegate, NSText, NSTextField, NSTextFieldDelegate, NSTextMovement,
NSTextMovementUserInfoKey, NSTrackingArea, NSTrackingAreaOptions, NSView, NSWindow,
NSWindowDelegate, NSWindowStyleMask,
};
use objc2_app_kit::{
NSApplicationDidBecomeActiveNotification, NSApplicationWillResignActiveNotification,
NSApplicationWillTerminateNotification,
};
use objc2_app_kit::{
NSOutlineViewDataSource, NSOutlineViewDelegate, NSTextDelegate, NSTextView, NSTextViewDelegate,
};
use objc2_app_kit::{NSTableColumn, NSTableView, NSTableViewDataSource, NSTableViewDelegate};
use objc2_core_foundation::CGAffineTransform;
use objc2_foundation::{
NSAffineTransform, NSAffineTransformStruct, NSArray, NSData, NSDictionary, NSNotification,
NSNumber, NSObject, NSPoint, NSRange, NSRect, NSSize, NSString,
};
use objc2_quartz_core::CATransaction;
use day_spec::ffi_guard;
use day_spec::present;
use day_spec::props::*;
use day_spec::sidetable::SideTable;
use day_spec::{
A11yProps, AnimSpec, Builtin, Cap, Cursor, Curve, DrawOp, Event, EventSink, Font, InputPurpose,
InputTraits, ListSource, MoveVerdict, NodeId, PieceKind, Platform, Point, Proposal, RawHandle,
Rect, Registry, Renderer, Size, Support, Toolkit, Transform, TreeSource, WINDOW_NODE,
WindowOptions, kinds, props_of,
};
pub type Handle = Retained<NSView>;
pub use toolbar::image_for as toolbar_image;
mod dock;
mod documents;
mod frame;
mod input;
mod motion;
mod picker;
mod scroll;
mod status_item;
mod textarea;
mod toolbar;
mod transfer;
mod window_chrome;
use objc2_app_kit::NSDraggingSource;
pub mod ext;
pub use color::nsink;
pub use ext::*;
type Sink = Rc<dyn Fn(NodeId, Event)>;
day_core::tls_group! {
static SINK: RefCell<Option<Sink>> = const { RefCell::new(None) };
static TARGETS: RefCell<HashMap<usize, Retained<DayTarget>>> = RefCell::new(HashMap::new());
static BUTTON_STYLES: RefCell<HashMap<usize, day_spec::props::ButtonStyleSpec>> =
RefCell::new(HashMap::new());
static BUTTON_IMAGES: SideTable<Retained<objc2_app_kit::NSImage>> = SideTable::new();
static BUTTON_SELECTED: RefCell<HashMap<usize, bool>> = RefCell::new(HashMap::new());
static OPS: SideTable<Vec<DrawOp>> = SideTable::new();
static APP_LABELED: SideTable<()> = SideTable::new();
static IMAGE_MODES: SideTable<ImageMode> = SideTable::new();
static TRANSFORMS: SideTable<Transform> = SideTable::new();
static BASE_FRAMES: SideTable<[f64; 4]> = SideTable::new();
static COVER_SHEETS: SideTable<CoverSheet> = SideTable::new();
static SHEET_NODES: SideTable<NodeId> = SideTable::new();
static SHEET_ESCAPE_MONITOR: RefCell<Option<PanMonitor>> = const { RefCell::new(None) };
static PAN_NODES: SideTable<NodeId> = SideTable::new();
static PAN_MONITOR: RefCell<Option<PanMonitor>> = const { RefCell::new(None) };
static HOVER_NODES: SideTable<NodeId> = SideTable::new();
static ROTATE_NODES: SideTable<NodeId> = SideTable::new();
static ROTATE_ANGLE: SideTable<f64> = SideTable::new();
static KEY_NODES: SideTable<NodeId> = SideTable::new();
static FOCUSABLE_NODES: SideTable<NodeId> = SideTable::new();
static MARKED: SideTable<String> = SideTable::new();
static INTERPRETING: RefCell<Option<Interpreting>> = const { RefCell::new(None) };
static GESTURES: RefCell<HashMap<usize, Vec<Retained<DayGesture>>>> =
RefCell::new(HashMap::new());
static NAV_STATE: RefCell<HashMap<usize, NavState>> = RefCell::new(HashMap::new());
static NAV_PAGES: RefCell<std::collections::HashSet<usize>> =
RefCell::new(std::collections::HashSet::new());
static PAGE_PANE: SideTable<day_spec::props::Pane> = SideTable::new();
static INSPECTOR_STATE: RefCell<HashMap<usize, InspectorState>> = RefCell::new(HashMap::new());
static INSPECTOR_PANES: SideTable<bool> = SideTable::new();
static SPLIT_STATE: RefCell<HashMap<usize, SplitState>> = RefCell::new(HashMap::new());
static SPLIT_PANES: SideTable<bool> = SideTable::new();
static NAV_OUTLINE_MENUS: SideTable<Retained<DayNavMenuData>> = SideTable::new();
static LIST_STATE: RefCell<HashMap<usize, ListEntry>> = RefCell::new(HashMap::new());
static TREE_STATE: RefCell<HashMap<usize, TreeEntry>> = RefCell::new(HashMap::new());
static BITMAPS: RefCell<HashMap<u64, AppKitBitmap>> = RefCell::new(HashMap::new());
static OFFSCREEN_FONTS: RefCell<Vec<((u64, day_spec::CanvasFont), Retained<NSFont>)>> =
const { RefCell::new(Vec::new()) };
static CTX_MENU_FNS: SideTable<day_spec::ContextMenuFn> = SideTable::new();
static BUTTON_MENUS: SideTable<day_spec::ContextMenuFn> = SideTable::new();
static NAV_MENUS: RefCell<HashMap<usize, NavMenuEntry>> = RefCell::new(HashMap::new());
static EDIT_STATE: std::cell::Cell<day_spec::EditState> =
const { std::cell::Cell::new(day_spec::EditState { can_cut: false, can_copy: false, can_paste: false, can_select_all: false }) };
static UNDO_FRONT: std::cell::RefCell<Option<Retained<DayUndoManager>>> =
const { std::cell::RefCell::new(None) };
static PRESENT_ALERTS: RefCell<HashMap<u64, Retained<objc2_app_kit::NSAlert>>> =
RefCell::new(HashMap::new());
static PRESENT_PANELS: RefCell<HashMap<u64, Retained<objc2_app_kit::NSSavePanel>>> =
RefCell::new(HashMap::new());
static MENU_TARGET: std::cell::RefCell<Option<Retained<DayMenuTarget>>> =
const { std::cell::RefCell::new(None) };
static PRIMARY_CONTENT: std::cell::RefCell<Option<Retained<NSView>>> =
const { std::cell::RefCell::new(None) };
}
pub fn emit(id: NodeId, ev: Event) {
let sink = SINK.with(|s| s.borrow().clone());
if let Some(sink) = sink {
sink(id, ev);
}
}
fn ptr_of(v: &NSView) -> usize {
std::ptr::from_ref::<NSView>(v).cast::<()>() as usize
}
fn pop_button_menu(btn: &NSButton, f: &day_spec::ContextMenuFn) {
ffi_guard::contain((), || {
let key = ptr_of(btn);
let Some(items) =
day_spec::resolve_button_menu(f, || BUTTON_MENUS.with(|t| t.get(key)).is_some())
else {
return;
};
let menu = build_ns_menu(btn.mtm(), "", &items);
let bounds = btn.bounds();
let y = if btn.isFlipped() {
bounds.size.height + 2.0
} else {
-2.0
};
unsafe {
btn.highlight(true);
menu.popUpMenuPositioningItem_atLocation_inView(
None,
objc2_foundation::NSPoint::new(0.0, y),
Some(btn),
);
btn.highlight(false);
}
});
}
#[derive(Clone, Copy)]
struct ImageMode {
template: bool,
tinted: bool,
}
impl ImageMode {
fn renders_template(self) -> bool {
self.template || self.tinted
}
}
struct AppKitBitmap {
image: Retained<objc2_app_kit::NSImage>,
rep: Option<Retained<objc2_app_kit::NSBitmapImageRep>>,
info: day_spec::BitmapInfo,
}
fn scaled_rep(
image: &objc2_app_kit::NSImage,
pixels: Size,
scale: f64,
) -> Option<Retained<objc2_app_kit::NSBitmapImageRep>> {
use objc2::AllocAnyThread as _;
let w = (pixels.width * scale).round().max(1.0);
let h = (pixels.height * scale).round().max(1.0);
let rep = unsafe {
objc2_app_kit::NSBitmapImageRep::initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel(
objc2_app_kit::NSBitmapImageRep::alloc(),
std::ptr::null_mut(),
w as isize,
h as isize,
8,
4,
true,
false,
objc2_app_kit::NSDeviceRGBColorSpace,
0,
0,
)
}?;
let ctx = objc2_app_kit::NSGraphicsContext::graphicsContextWithBitmapImageRep(&rep)?;
NSGraphicsContext::saveGraphicsState_class();
objc2_app_kit::NSGraphicsContext::setCurrentContext(Some(&ctx));
unsafe {
image.drawInRect_fromRect_operation_fraction(
NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(w, h)),
NSRect::ZERO,
objc2_app_kit::NSCompositingOperation::SourceOver,
1.0,
);
}
NSGraphicsContext::restoreGraphicsState_class();
Some(rep)
}
fn encode_bitmap(
id: day_spec::BitmapId,
spec: &day_spec::EncodeSpec,
) -> Result<Vec<u8>, day_spec::ImageError> {
use day_spec::ImageFormat as F;
use objc2_app_kit::NSBitmapImageFileType as T;
let file_type = match spec.format {
F::Png => T::PNG,
F::Jpeg => T::JPEG,
F::Tiff => T::TIFF,
F::Bmp => T::BMP,
F::Gif => T::GIF,
_ => return Err(day_spec::ImageError::Encode),
};
let held = BITMAPS.with(|m| {
m.borrow()
.get(&id.0)
.map(|b| (b.rep.clone(), b.image.clone(), b.info))
});
let (rep, image, info) = held.ok_or(day_spec::ImageError::Gone)?;
let rep = rep.ok_or(day_spec::ImageError::Encode)?;
let rep = match spec.fit {
Some(fit) if info.pixels.width > 0.0 && info.pixels.height > 0.0 => {
let scale = (fit.width / info.pixels.width)
.min(fit.height / info.pixels.height)
.min(1.0);
if scale < 1.0 {
scaled_rep(&image, info.pixels, scale).ok_or(day_spec::ImageError::Encode)?
} else {
rep
}
}
_ => rep,
};
let props: Retained<NSDictionary<NSString, objc2::runtime::AnyObject>> = match spec.quality {
Some(q) => {
let key: &NSString = unsafe { objc2_app_kit::NSImageCompressionFactor };
let number = NSNumber::new_f64(q.clamp(0.0, 1.0));
let value: &objc2::runtime::AnyObject = &number;
NSDictionary::from_slices::<NSString>(&[key], &[value])
}
None => NSDictionary::new(),
};
let data = unsafe { rep.representationUsingType_properties(file_type, &props) }
.ok_or(day_spec::ImageError::Encode)?;
Ok(data.to_vec())
}
fn paint_ops(ops: &[DrawOp], base_scale: f64) {
use objc2_core_graphics::CGContext;
let cg = unsafe { NSGraphicsContext::currentContext().map(|c| c.CGContext()) };
let mut replay = Replay::new(base_scale);
let Some(cg) = cg else {
for op in ops {
draw_op(op, &mut replay);
}
return;
};
cg_begin_replay(&cg);
let grouped = ops
.iter()
.any(|op| matches!(op, DrawOp::Blend(mode) if *mode != day_spec::BlendMode::Normal));
if grouped {
CGContext::save_g_state(Some(&cg));
unsafe { CGContext::begin_transparency_layer(Some(&cg), None) };
}
for op in ops {
if !cg_fast_op(&cg, op) {
draw_op(op, &mut replay);
}
}
while replay.restore() {
NSGraphicsContext::restoreGraphicsState_class();
}
if grouped {
CGContext::end_transparency_layer(Some(&cg));
CGContext::restore_g_state(Some(&cg));
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Ink {
shadow: bool,
blend: day_spec::BlendMode,
}
struct Replay {
base_scale: f64,
ink: Ink,
saved: Vec<Ink>,
}
impl Replay {
fn new(base_scale: f64) -> Self {
Replay {
base_scale,
ink: Ink::default(),
saved: Vec::new(),
}
}
fn save(&mut self) {
self.saved.push(self.ink);
}
fn restore(&mut self) -> bool {
match self.saved.pop() {
Some(ink) => {
self.ink = ink;
true
}
None => false,
}
}
}
fn pdf_ops(ops: &[DrawOp]) -> std::borrow::Cow<'_, [DrawOp]> {
use day_spec::BlendMode;
if !ops
.iter()
.any(|op| matches!(op, DrawOp::Blend(BlendMode::Erase)))
{
return std::borrow::Cow::Borrowed(ops);
}
let (mut blend, mut saved) = (BlendMode::Normal, Vec::new());
let mut kept = Vec::with_capacity(ops.len());
for op in ops {
match op {
DrawOp::Blend(mode) => blend = *mode,
DrawOp::Save => saved.push(blend),
DrawOp::Restore => blend = saved.pop().unwrap_or(blend),
DrawOp::Fill(..)
| DrawOp::Stroke(..)
| DrawOp::Text { .. }
| DrawOp::Stamp(_)
| DrawOp::Image { .. }
if blend == BlendMode::Erase =>
{
continue;
}
_ => {}
}
kept.push(op.clone());
}
std::borrow::Cow::Owned(kept)
}
fn cg_blend_mode(mode: day_spec::BlendMode) -> objc2_core_graphics::CGBlendMode {
use day_spec::BlendMode as B;
use objc2_core_graphics::CGBlendMode as M;
match mode {
B::Normal => M::Normal,
B::Multiply => M::Multiply,
B::Screen => M::Screen,
B::Overlay => M::Overlay,
B::Darken => M::Darken,
B::Lighten => M::Lighten,
B::Add => M::PlusLighter,
B::Erase => M::DestinationOut,
}
}
fn ns_compositing(mode: day_spec::BlendMode) -> objc2_app_kit::NSCompositingOperation {
use day_spec::BlendMode as B;
use objc2_app_kit::NSCompositingOperation as O;
match mode {
B::Normal => O::SourceOver,
B::Multiply => O::Multiply,
B::Screen => O::Screen,
B::Overlay => O::Overlay,
B::Darken => O::Darken,
B::Lighten => O::Lighten,
B::Add => O::PlusLighter,
B::Erase => O::DestinationOut,
}
}
fn cg_set_shadow(
cg: &objc2_core_graphics::CGContext,
shadow: Option<&day_spec::Shadow>,
base_scale: f64,
) {
use objc2_core_graphics::{CGColor, CGContext};
match shadow {
Some(s) => {
let c = s.color.resolved();
let color = CGColor::new_srgb(c.r, c.g, c.b, c.a);
CGContext::set_shadow_with_color(
Some(cg),
NSSize::new(s.dx * base_scale, -s.dy * base_scale),
s.blur.max(0.0) * base_scale,
Some(&color),
);
}
None => CGContext::set_shadow_with_color(Some(cg), NSSize::new(0.0, 0.0), 0.0, None),
}
}
fn draw_grouped(replay: &Replay, draw: impl FnOnce()) {
use objc2_core_graphics::CGContext;
let cg = unsafe { NSGraphicsContext::currentContext().map(|c| c.CGContext()) };
let layer = cg.filter(|_| replay.ink.shadow);
NSGraphicsContext::saveGraphicsState_class();
if let Some(cg) = &layer {
unsafe { CGContext::begin_transparency_layer(Some(cg), None) };
}
draw();
if let Some(cg) = &layer {
CGContext::end_transparency_layer(Some(cg));
}
NSGraphicsContext::restoreGraphicsState_class();
}
fn native_conic() -> Option<ConicFn> {
unsafe extern "C" {
fn dlsym(
handle: *mut std::ffi::c_void,
symbol: *const std::ffi::c_char,
) -> *mut std::ffi::c_void;
}
static FOUND: std::sync::OnceLock<Option<ConicFn>> = std::sync::OnceLock::new();
*FOUND.get_or_init(|| {
let default = -2isize as *mut std::ffi::c_void;
let symbol = unsafe { dlsym(default, c"CGContextDrawConicGradient".as_ptr()) };
(!symbol.is_null())
.then(|| unsafe { std::mem::transmute::<*mut std::ffi::c_void, ConicFn>(symbol) })
})
}
type ConicFn = unsafe extern "C-unwind" fn(
&objc2_core_graphics::CGContext,
Option<&objc2_core_graphics::CGGradient>,
NSPoint,
objc2_core_foundation::CGFloat,
);
fn conic_pixels(
gradient: &day_spec::ConicGradient,
center: Point,
origin: Point,
step: f64,
width: usize,
height: usize,
) -> Vec<u8> {
let mut pixels = Vec::with_capacity(width * height * 4);
let byte = |v: f64| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
for j in 0..height {
let dy = origin.y + (j as f64 + 0.5) * step - center.y;
for i in 0..width {
let dx = origin.x + (i as f64 + 0.5) * step - center.x;
let c = gradient.color_at(day_spec::Angle::rad(dy.atan2(dx)));
pixels.extend_from_slice(&[
byte(c.r * c.a),
byte(c.g * c.a),
byte(c.b * c.a),
byte(c.a),
]);
}
}
pixels
}
const CONIC_PIXEL_BUDGET: f64 = 4_000_000.0;
fn draw_conic(
cg: &objc2_core_graphics::CGContext,
gradient: &day_spec::ConicGradient,
bounds: day_spec::Rect,
) {
use objc2_core_graphics::{CGColorSpace, CGGradient};
if gradient.stops.is_empty() {
return;
}
let center = gradient.center.resolve(bounds);
if let Some(draw) = native_conic() {
let components: Vec<objc2_core_foundation::CGFloat> = stops_now(&gradient.stops)
.into_iter()
.flat_map(|(_, c)| [c.r, c.g, c.b, c.a])
.collect();
let locations: Vec<objc2_core_foundation::CGFloat> =
gradient.stops.iter().map(|(at, _)| *at).collect();
let srgb = CGColorSpace::with_name(Some(unsafe { objc2_core_graphics::kCGColorSpaceSRGB }));
let native = unsafe {
CGGradient::with_color_components(
srgb.as_deref(),
components.as_ptr(),
locations.as_ptr(),
locations.len(),
)
};
if let Some(native) = native {
unsafe {
draw(
cg,
Some(&native),
NSPoint::new(center.x, center.y),
gradient.start.radians(),
);
};
return;
}
}
draw_conic_pixels(cg, gradient, center);
}
fn draw_conic_pixels(
cg: &objc2_core_graphics::CGContext,
gradient: &day_spec::ConicGradient,
center: Point,
) {
use objc2_core_graphics::{
CGBitmapContextCreate, CGBitmapContextCreateImage, CGColorSpace, CGContext,
CGImageAlphaInfo,
};
let clip = CGContext::clip_bounding_box(Some(cg));
let (w, h) = (clip.size.width, clip.size.height);
if !(w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0) {
return;
}
let device = CGContext::user_space_to_device_space_transform(Some(cg));
let density = device
.a
.hypot(device.b)
.max(device.c.hypot(device.d))
.clamp(2.0, 16.0)
.min((CONIC_PIXEL_BUDGET / (w * h)).sqrt());
let (pw, ph) = (
((w * density).ceil() as usize).max(1),
((h * density).ceil() as usize).max(1),
);
let origin = Point::new(clip.origin.x, clip.origin.y);
let mut pixels = conic_pixels(gradient, center, origin, 1.0 / density, pw, ph);
let srgb = CGColorSpace::with_name(Some(unsafe { objc2_core_graphics::kCGColorSpaceSRGB }));
let image = unsafe {
CGBitmapContextCreate(
pixels.as_mut_ptr().cast(),
pw,
ph,
8,
pw * 4,
srgb.as_deref(),
CGImageAlphaInfo::PremultipliedLast.0,
)
}
.and_then(|bitmap| CGBitmapContextCreateImage(Some(&bitmap)));
let Some(image) = image else {
return;
};
CGContext::save_g_state(Some(cg));
CGContext::translate_ctm(Some(cg), origin.x, origin.y + ph as f64 / density);
CGContext::scale_ctm(Some(cg), 1.0, -1.0);
CGContext::draw_image(
Some(cg),
NSRect::new(
NSPoint::new(0.0, 0.0),
NSSize::new(pw as f64 / density, ph as f64 / density),
),
Some(&image),
);
CGContext::restore_g_state(Some(cg));
}
fn cg_fast_op(cg: &objc2_core_graphics::CGContext, op: &DrawOp) -> bool {
use objc2_core_graphics::{CGContext, CGImage};
match op {
DrawOp::Fill(shape, day_spec::Paint::Solid(color)) => {
let Some(even_odd) = cg_add_shape(cg, shape) else {
return false;
};
let c = color.resolved();
let c = [c.r, c.g, c.b, c.a];
unsafe { CGContext::set_fill_color(Some(cg), c.as_ptr()) };
if even_odd {
CGContext::eo_fill_path(Some(cg));
} else {
CGContext::fill_path(Some(cg));
}
true
}
DrawOp::Stroke(shape, day_spec::Paint::Solid(color), style) => {
if cg_add_shape(cg, shape).is_none() {
return false;
}
let c = color.resolved();
let c = [c.r, c.g, c.b, c.a];
unsafe { CGContext::set_stroke_color(Some(cg), c.as_ptr()) };
cg_stroke_style(cg, style);
CGContext::stroke_path(Some(cg));
true
}
DrawOp::Clip(shape) => {
let Some(even_odd) = cg_add_shape(cg, shape) else {
return false;
};
if even_odd {
CGContext::eo_clip(Some(cg));
} else {
CGContext::clip(Some(cg));
}
true
}
DrawOp::Image {
image,
rect,
opacity,
source,
} => {
let Some((cg_image, pixels)) = BITMAPS.with(|m| {
m.borrow().get(&image.0).and_then(|b| {
let cg = unsafe { b.rep.as_ref()?.CGImage() }?;
Some((cg, b.info.pixels))
})
}) else {
return false;
};
let (cropped, dest) = match source {
None => (None, *rect),
Some(src) => {
let Some((src, dest)) = day_spec::clamp_image_source(*src, *rect, pixels)
else {
return true;
};
let crop = CGImage::with_image_in_rect(
Some(&cg_image),
NSRect::new(
NSPoint::new(src.origin.x, src.origin.y),
NSSize::new(src.size.width, src.size.height),
),
);
let Some(crop) = crop else {
return true;
};
(Some(crop), dest)
}
};
if dest.size.width <= 0.0 || dest.size.height <= 0.0 {
return true;
}
CGContext::save_g_state(Some(cg));
CGContext::set_alpha(Some(cg), opacity.clamp(0.0, 1.0));
CGContext::translate_ctm(Some(cg), dest.origin.x, dest.origin.y + dest.size.height);
CGContext::scale_ctm(Some(cg), 1.0, -1.0);
CGContext::draw_image(
Some(cg),
NSRect::new(
NSPoint::new(0.0, 0.0),
NSSize::new(dest.size.width, dest.size.height),
),
Some(cropped.as_deref().unwrap_or(&cg_image)),
);
CGContext::restore_g_state(Some(cg));
true
}
_ => false,
}
}
fn cg_begin_replay(cg: &objc2_core_graphics::CGContext) {
use objc2_core_graphics::{CGColorSpace, CGContext};
let srgb = CGColorSpace::with_name(Some(unsafe { objc2_core_graphics::kCGColorSpaceSRGB }));
CGContext::set_fill_color_space(Some(cg), srgb.as_deref());
CGContext::set_stroke_color_space(Some(cg), srgb.as_deref());
}
fn cg_add_shape(cg: &objc2_core_graphics::CGContext, shape: &day_spec::Shape) -> Option<bool> {
use day_spec::{PathSeg, Shape};
use objc2_core_graphics::CGContext;
CGContext::begin_path(Some(cg));
match shape {
Shape::Rect(r) => CGContext::add_rect(Some(cg), ns_rect(*r)),
Shape::Ellipse(r) => CGContext::add_ellipse_in_rect(Some(cg), ns_rect(*r)),
Shape::Line(a, b) => {
CGContext::move_to_point(Some(cg), a.x, a.y);
CGContext::add_line_to_point(Some(cg), b.x, b.y);
}
Shape::Polygon(pts) => {
let (first, rest) = pts.split_first()?;
if rest.is_empty() {
return None;
}
CGContext::move_to_point(Some(cg), first.x, first.y);
for p in rest {
CGContext::add_line_to_point(Some(cg), p.x, p.y);
}
CGContext::close_path(Some(cg));
}
Shape::Path(path) => {
if path.segs.is_empty() {
return None;
}
for seg in &path.segs {
match seg {
PathSeg::Move(a) => CGContext::move_to_point(Some(cg), a.x, a.y),
PathSeg::Line(a) => CGContext::add_line_to_point(Some(cg), a.x, a.y),
PathSeg::Quad(c, a) => {
CGContext::add_quad_curve_to_point(Some(cg), c.x, c.y, a.x, a.y);
}
PathSeg::Cubic(c1, c2, a) => {
CGContext::add_curve_to_point(Some(cg), c1.x, c1.y, c2.x, c2.y, a.x, a.y);
}
PathSeg::Close => CGContext::close_path(Some(cg)),
}
}
return Some(path.rule == day_spec::FillRule::EvenOdd);
}
Shape::RoundedRect(..) | Shape::Arc { .. } => return None,
}
Some(false)
}
fn cg_stroke_style(cg: &objc2_core_graphics::CGContext, style: &day_spec::StrokeStyle) {
use day_spec::{LineCap, LineJoin};
use objc2_core_graphics::{CGContext, CGLineCap, CGLineJoin};
CGContext::set_line_width(Some(cg), style.width);
CGContext::set_line_cap(
Some(cg),
match style.cap {
LineCap::Butt => CGLineCap::Butt,
LineCap::Round => CGLineCap::Round,
LineCap::Square => CGLineCap::Square,
},
);
CGContext::set_line_join(
Some(cg),
match style.join {
LineJoin::Miter => CGLineJoin::Miter,
LineJoin::Round => CGLineJoin::Round,
LineJoin::Bevel => CGLineJoin::Bevel,
},
);
CGContext::set_miter_limit(Some(cg), style.miter_limit);
if style.dash.is_empty() {
unsafe { CGContext::set_line_dash(Some(cg), 0.0, core::ptr::null(), 0) };
} else {
let pattern: Vec<objc2_core_foundation::CGFloat> = style.dash.clone();
unsafe {
CGContext::set_line_dash(Some(cg), style.dash_phase, pattern.as_ptr(), pattern.len());
};
}
}
fn paint_flipped(cg: &objc2_core_graphics::CGContext, height: f64, scale: f64, ops: &[DrawOp]) {
use objc2_core_graphics::CGContext;
CGContext::save_g_state(Some(cg));
CGContext::translate_ctm(Some(cg), 0.0, height);
CGContext::scale_ctm(Some(cg), scale, -scale);
let ns = NSGraphicsContext::graphicsContextWithCGContext_flipped(cg, true);
NSGraphicsContext::saveGraphicsState_class();
NSGraphicsContext::setCurrentContext(Some(&ns));
paint_ops(ops, scale);
NSGraphicsContext::restoreGraphicsState_class();
CGContext::restore_g_state(Some(cg));
}
struct Sendable<T>(T);
unsafe impl<T> Send for Sendable<T> {}
struct OffscreenResources {
images: Vec<(u64, AppKitBitmap)>,
fonts: Vec<((u64, day_spec::CanvasFont), Retained<NSFont>)>,
}
impl OffscreenResources {
fn collect(ops: &[DrawOp]) -> Self {
let mut images: Vec<(u64, AppKitBitmap)> = Vec::new();
let mut fonts: Vec<((u64, day_spec::CanvasFont), Retained<NSFont>)> = Vec::new();
for op in ops {
match op {
DrawOp::Image { image, .. } => {
if images.iter().any(|(id, _)| *id == image.0) {
continue;
}
let held = BITMAPS.with(|m| {
m.borrow().get(&image.0).map(|b| AppKitBitmap {
image: b.image.clone(),
rep: b.rep.clone(),
info: b.info,
})
});
if let Some(bitmap) = held {
images.push((image.0, bitmap));
}
}
DrawOp::Text { size, font, .. } => {
let key = (size.to_bits(), font.clone());
if fonts.iter().any(|(k, _)| *k == key) {
continue;
}
let resolved = canvas_nsfont(*size, font);
fonts.push((key, resolved));
}
_ => {}
}
}
Self { images, fonts }
}
fn with_installed<R>(self, paint: impl FnOnce() -> R) -> R {
let mut added_images = Vec::with_capacity(self.images.len());
BITMAPS.with(|m| {
let mut m = m.borrow_mut();
for (id, bitmap) in self.images {
if let std::collections::hash_map::Entry::Vacant(slot) = m.entry(id) {
slot.insert(bitmap);
added_images.push(id);
}
}
});
let added_fonts = self.fonts.len();
OFFSCREEN_FONTS.with(|t| t.borrow_mut().extend(self.fonts));
let out = paint();
OFFSCREEN_FONTS.with(|t| {
let mut t = t.borrow_mut();
let keep = t.len().saturating_sub(added_fonts);
t.truncate(keep);
});
BITMAPS.with(|m| {
let mut m = m.borrow_mut();
for id in added_images {
m.remove(&id);
}
});
out
}
}
fn render_bitmap_cgimage(
ops: &[DrawOp],
size: Size,
scale: f64,
) -> Result<objc2_core_foundation::CFRetained<objc2_core_graphics::CGImage>, day_spec::ExportError>
{
use objc2_core_graphics::{
CGBitmapContextCreate, CGBitmapContextCreateImage, CGColorSpace, CGImageAlphaInfo,
};
let failed = |what: &str| day_spec::ExportError::Failed(what.to_string());
let (pw, ph) = day_spec::export_pixels(size, scale);
let space = CGColorSpace::with_name(Some(unsafe { objc2_core_graphics::kCGColorSpaceSRGB }))
.ok_or_else(|| failed("CoreGraphics has no sRGB color space"))?;
let cg = unsafe {
CGBitmapContextCreate(
std::ptr::null_mut(),
pw as usize,
ph as usize,
8,
0,
Some(&space),
CGImageAlphaInfo::PremultipliedLast.0,
)
}
.ok_or_else(|| {
day_spec::ExportError::Failed(format!("CGBitmapContextCreate refused {pw}x{ph}"))
})?;
paint_flipped(&cg, f64::from(ph), scale, ops);
CGBitmapContextCreateImage(Some(&cg))
.ok_or_else(|| failed("CGBitmapContextCreateImage returned nothing"))
}
fn bitmap_from_cgimage(cg_image: &objc2_core_graphics::CGImage, scale: f64) -> AppKitBitmap {
use objc2::AllocAnyThread as _;
let rep = unsafe {
objc2_app_kit::NSBitmapImageRep::initWithCGImage(
objc2_app_kit::NSBitmapImageRep::alloc(),
cg_image,
)
};
let (pw, ph) = unsafe { (rep.pixelsWide() as f64, rep.pixelsHigh() as f64) };
let points = NSSize::new(pw / scale, ph / scale);
unsafe { rep.setSize(points) };
let image =
unsafe { objc2_app_kit::NSImage::initWithSize(objc2_app_kit::NSImage::alloc(), points) };
unsafe { image.addRepresentation(&rep) };
AppKitBitmap {
image,
rep: Some(rep),
info: day_spec::BitmapInfo {
pixels: Size::new(pw, ph),
scale,
format: None,
has_alpha: true,
},
}
}
fn render_pdf_bytes(
pages: &[day_spec::PdfPage],
info: &day_spec::PdfInfo,
) -> Result<Vec<u8>, day_spec::ExportError> {
use objc2_core_foundation::{
CFDictionary, CFMutableData, CFRetained, CFString, CGPoint, CGRect, CGSize,
};
use objc2_core_graphics::{CGContext, CGDataConsumer, CGPDFContextClose, CGPDFContextCreate};
let failed = |what: &str| day_spec::ExportError::Failed(what.to_string());
let media =
|size: Size| CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(size.width, size.height));
let first = pages.first().ok_or(day_spec::ExportError::Invalid)?;
let data = CFMutableData::new(None, 0)
.ok_or_else(|| failed("CFDataCreateMutable returned nothing"))?;
let consumer = CGDataConsumer::with_cf_data(Some(&data))
.ok_or_else(|| failed("CGDataConsumerCreateWithCFData returned nothing"))?;
let mut keys: Vec<&CFString> = Vec::new();
let mut values: Vec<CFRetained<CFString>> = Vec::new();
let (title_key, author_key) = unsafe {
(
objc2_core_graphics::kCGPDFContextTitle,
objc2_core_graphics::kCGPDFContextAuthor,
)
};
for (key, value) in [(title_key, &info.title), (author_key, &info.author)] {
if let Some(value) = value {
keys.push(key);
values.push(CFString::from_str(value));
}
}
let values: Vec<&CFString> = values.iter().map(|v| &**v).collect();
let aux = CFDictionary::<CFString, CFString>::from_slices(&keys, &values);
let first_box = media(first.size);
let pdf =
unsafe { CGPDFContextCreate(Some(&consumer), &raw const first_box, Some(aux.as_opaque())) }
.ok_or_else(|| failed("CGPDFContextCreate refused the document"))?;
for page in pages {
let page_box = media(page.size);
unsafe { CGContext::begin_page(Some(&pdf), &raw const page_box) };
paint_flipped(&pdf, page.size.height, 1.0, &pdf_ops(&page.ops));
CGContext::end_page(Some(&pdf));
}
CGPDFContextClose(Some(&pdf));
drop(pdf);
drop(consumer);
Ok(data.to_vec())
}
thread_local! {
static LIST_ITEMS: std::cell::RefCell<std::collections::HashMap<usize, day_spec::props::ListItemProps>> =
std::cell::RefCell::new(std::collections::HashMap::new());
}
fn make_list_item(p: &day_spec::props::ListItemProps, mtm: MainThreadMarker) -> Retained<NSView> {
const ACROSS: objc2_app_kit::NSLayoutConstraintOrientation =
objc2_app_kit::NSLayoutConstraintOrientation::Horizontal;
const STRETCH: f32 = 1.0;
const HOLD: f32 = 750.0;
use objc2_app_kit::{
NSImageView, NSLayoutAttribute, NSStackView, NSUserInterfaceLayoutOrientation,
};
let label = |text: &str, size: f64, secondary: bool| {
let tf = unsafe { NSTextField::labelWithString(&NSString::from_str(text), mtm) };
unsafe {
tf.setFont(Some(&objc2_app_kit::NSFont::systemFontOfSize(size)));
if secondary {
tf.setTextColor(Some(&objc2_app_kit::NSColor::secondaryLabelColor()));
}
tf.setLineBreakMode(objc2_app_kit::NSLineBreakMode::ByTruncatingTail);
}
tf
};
let size = unsafe { objc2_app_kit::NSFont::systemFontSize() };
let small = unsafe { objc2_app_kit::NSFont::smallSystemFontSize() };
let stack = NSStackView::new(mtm);
stack.setOrientation(NSUserInterfaceLayoutOrientation::Horizontal);
stack.setDistribution(objc2_app_kit::NSStackViewDistribution::Fill);
stack.setAlignment(NSLayoutAttribute::CenterY);
stack.setHuggingPriority_forOrientation(HOLD, ACROSS);
stack.setSpacing(6.0);
stack.setEdgeInsets(objc2_foundation::NSEdgeInsets {
top: 4.0,
left: 0.0,
bottom: 4.0,
right: 0.0,
});
let icon = unsafe { NSImageView::new(mtm) };
unsafe {
icon.setImageScaling(objc2_app_kit::NSImageScaling::ScaleProportionallyUpOrDown);
icon.widthAnchor()
.constraintEqualToConstant(16.0)
.setActive(true);
icon.heightAnchor()
.constraintEqualToConstant(16.0)
.setActive(true);
}
stack.addArrangedSubview(&icon);
let texts = NSStackView::new(mtm);
texts.setOrientation(NSUserInterfaceLayoutOrientation::Vertical);
texts.setAlignment(NSLayoutAttribute::Width);
texts.setSpacing(1.0);
texts.setHuggingPriority_forOrientation(
HOLD,
objc2_app_kit::NSLayoutConstraintOrientation::Vertical,
);
let title = label("", size, false);
let subtitle = label("", small, true);
for line in [&title, &subtitle] {
line.setContentHuggingPriority_forOrientation(STRETCH, ACROSS);
line.setContentCompressionResistancePriority_forOrientation(250.0, ACROSS);
}
texts.addArrangedSubview(&title);
texts.addArrangedSubview(&subtitle);
stack.addArrangedSubview(&texts);
let detail = label("", size, true);
detail.setContentHuggingPriority_forOrientation(HOLD, ACROSS);
detail.setContentCompressionResistancePriority_forOrientation(HOLD, ACROSS);
stack.addArrangedSubview(&detail);
let check = unsafe { NSImageView::new(mtm) };
unsafe {
check.setImage(
objc2_app_kit::NSImage::imageWithSystemSymbolName_accessibilityDescription(
&NSString::from_str("checkmark"),
None,
)
.as_deref(),
);
check.setContentTintColor(Some(&objc2_app_kit::NSColor::controlAccentColor()));
}
check.setContentHuggingPriority_forOrientation(HOLD, ACROSS);
check.setContentCompressionResistancePriority_forOrientation(HOLD, ACROSS);
stack.addArrangedSubview(&check);
let view = Retained::from(<NSStackView as AsRef<NSView>>::as_ref(&stack));
LIST_ITEMS.with(|m| m.borrow_mut().insert(ptr_of(&view), p.clone()));
dress_list_item(&view, p, mtm);
view
}
fn dress_list_item(view: &NSView, p: &day_spec::props::ListItemProps, mtm: MainThreadMarker) {
use objc2_app_kit::{NSImageView, NSStackView};
let Some(stack) = view.downcast_ref::<NSStackView>() else {
return;
};
let parts = stack.arrangedSubviews();
if parts.len() < 4 {
return;
}
let icon = parts.objectAtIndex(0);
if let Some(iv) = icon.downcast_ref::<NSImageView>() {
let image = p
.icon
.as_ref()
.and_then(|i| toolbar::image_for(i, &p.title, mtm));
unsafe {
iv.setImage(image.as_deref());
iv.setContentTintColor(p.icon_tint.map(color::nsink).as_deref());
}
}
icon.setHidden(p.icon.is_none());
if let Some(texts) = parts.objectAtIndex(1).downcast_ref::<NSStackView>() {
let lines = texts.arrangedSubviews();
if lines.len() == 2 {
if let Some(tf) = lines.objectAtIndex(0).downcast_ref::<NSTextField>() {
tf.setStringValue(&NSString::from_str(&p.title));
}
let sub = lines.objectAtIndex(1);
if let Some(tf) = sub.downcast_ref::<NSTextField>() {
tf.setStringValue(&NSString::from_str(&p.subtitle));
}
sub.setHidden(p.subtitle.is_empty());
}
}
let detail = parts.objectAtIndex(2);
if let Some(tf) = detail.downcast_ref::<NSTextField>() {
tf.setStringValue(&NSString::from_str(&p.detail));
}
detail.setHidden(p.detail.is_empty());
parts.objectAtIndex(3).setHidden(!p.checked);
let words = [&p.title, &p.subtitle, &p.detail]
.into_iter()
.filter(|w| !w.is_empty())
.map(String::as_str)
.collect::<Vec<_>>()
.join(", ");
unsafe {
view.setAccessibilityElement(true);
view.setAccessibilityLabel(Some(&NSString::from_str(&words)));
view.setAccessibilitySelected(p.checked);
}
}
fn appkit_image_for(source: &day_spec::ImageSource) -> Option<Retained<objc2_app_kit::NSImage>> {
use objc2::AllocAnyThread as _;
match source {
day_spec::ImageSource::Named(name) => day_spec::resource::resolve_vector_svg(name)
.or_else(|| day_spec::resource::resolve_image_file(name))
.and_then(|path| unsafe {
objc2_app_kit::NSImage::initWithContentsOfFile(
objc2_app_kit::NSImage::alloc(),
&NSString::from_str(&path.to_string_lossy()),
)
}),
day_spec::ImageSource::Bytes(bytes) => unsafe {
objc2_app_kit::NSImage::initWithData(
objc2_app_kit::NSImage::alloc(),
&NSData::with_bytes(bytes),
)
},
day_spec::ImageSource::Decoded(id) => {
BITMAPS.with(|m| m.borrow().get(&id.0).map(|b| b.image.clone()))
}
}
}
fn image_rendered(
image: Retained<objc2_app_kit::NSImage>,
template: bool,
) -> Retained<objc2_app_kit::NSImage> {
use objc2_foundation::NSCopying as _;
if image.isTemplate() == template {
return image;
}
let image = image.copy();
image.setTemplate(template);
image
}
fn ns_role(role: day_spec::Role) -> Option<&'static objc2_app_kit::NSAccessibilityRole> {
use day_spec::Role;
use objc2_app_kit::{
NSAccessibilityButtonRole, NSAccessibilityCheckBoxRole, NSAccessibilityGroupRole,
NSAccessibilityHeadingRole, NSAccessibilityImageRole, NSAccessibilityLevelIndicatorRole,
NSAccessibilityOutlineRole, NSAccessibilityRadioButtonRole, NSAccessibilityRowRole,
NSAccessibilitySliderRole, NSAccessibilityTabGroupRole, NSAccessibilityTextFieldRole,
};
unsafe {
Some(match role {
Role::Button => NSAccessibilityButtonRole,
Role::Toggle => NSAccessibilityCheckBoxRole,
Role::Tab => NSAccessibilityRadioButtonRole,
Role::TabList => NSAccessibilityTabGroupRole,
Role::Slider => NSAccessibilitySliderRole,
Role::TextInput => NSAccessibilityTextFieldRole,
Role::Heading(_) => NSAccessibilityHeadingRole,
Role::Image => NSAccessibilityImageRole,
Role::Meter => NSAccessibilityLevelIndicatorRole,
Role::Group => NSAccessibilityGroupRole,
Role::Tree => NSAccessibilityOutlineRole,
Role::TreeItem => NSAccessibilityRowRole,
Role::None => return None,
})
}
}
fn day_role_from_ns(ax: &str) -> day_spec::Role {
use day_spec::Role;
match ax {
"AXButton" => Role::Button,
"AXCheckBox" => Role::Toggle,
"AXSlider" => Role::Slider,
"AXTextField" => Role::TextInput,
"AXStaticText" | "AXHeading" => Role::Heading(0),
"AXOutline" => Role::Tree,
"AXImage" => Role::Image,
"AXLevelIndicator" | "AXProgressIndicator" => Role::Meter,
"AXGroup" => Role::Group,
"AXRadioButton" => Role::Tab,
"AXTabGroup" => Role::TabList,
_ => Role::None,
}
}
struct TargetIvars {
node: NodeId,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DayTarget"]
#[ivars = TargetIvars]
struct DayTarget;
unsafe impl NSObjectProtocol for DayTarget {}
unsafe impl NSTextFieldDelegate for DayTarget {}
unsafe impl NSTabViewDelegate for DayTarget {
#[unsafe(method(tabView:didSelectTabViewItem:))]
fn tab_view_did_select(
&self,
tab_view: &objc2_app_kit::NSTabView,
item: Option<&objc2_app_kit::NSTabViewItem>,
) {
ffi_guard::contain((), || {
if TAB_PICK_SUPPRESS.with(std::cell::Cell::get) {
return;
}
if let Some(item) = item {
let idx = unsafe { tab_view.indexOfTabViewItem(item) };
if idx >= 0 {
emit(self.ivars().node, Event::SelectionChanged(idx as i64));
}
}
});
}
}
impl DayTarget {
#[unsafe(method(action:))]
fn action(&self, sender: &NSControl) {
ffi_guard::contain((), || {
let node = self.ivars().node;
if sender.downcast_ref::<NSSwitch>().is_some() {
emit(node, Event::ToggleChanged(unsafe { sender.integerValue() } != 0));
} else if sender.downcast_ref::<NSSlider>().is_some() {
let value = unsafe { sender.doubleValue() };
emit(node, Event::ValueChanged(value));
if slider_value_settled() {
emit(node, Event::ValueCommitted(value));
}
} else if let (Some(f), Some(btn)) = (
BUTTON_MENUS.with(|t| t.get(ptr_of(sender))),
sender.downcast_ref::<NSButton>(),
) {
pop_button_menu(btn, &f);
} else {
emit(node, Event::Pressed);
let wanted = BUTTON_SELECTED.with(|m| m.borrow().get(&ptr_of(sender)).copied());
if let (Some(on), Some(btn)) = (wanted, sender.downcast_ref::<NSButton>()) {
unsafe {
btn.setState(if on {
NSControlStateValueOn
} else {
NSControlStateValueOff
});
};
}
}
});
}
#[unsafe(method(navBack:))]
fn nav_back(&self, _sender: &NSControl) {
ffi_guard::contain((), || {
emit(
self.ivars().node,
Event::NavBack {
already_popped: false,
},
);
});
}
}
unsafe impl NSControlTextEditingDelegate for DayTarget {
#[unsafe(method(controlTextDidChange:))]
fn control_text_did_change(&self, notification: &NSNotification) {
ffi_guard::contain((), || {
let node = self.ivars().node;
if let Some(obj) = unsafe { notification.object() }
&& let Ok(tf) = obj.downcast::<NSTextField>() {
emit(node, Event::TextChanged(unsafe { tf.stringValue() }.to_string()));
}
});
}
#[unsafe(method(control:textView:doCommandBySelector:))]
fn control_do_command(
&self,
_control: &NSControl,
_tv: &NSTextView,
command: objc2::runtime::Sel,
) -> objc2::runtime::Bool {
ffi_guard::contain(objc2::runtime::Bool::NO, || {
let node = self.ivars().node;
let key = if command == sel!(moveUp:) {
"ArrowUp"
} else if command == sel!(moveDown:) {
"ArrowDown"
} else if command == sel!(cancelOperation:) {
"Escape"
} else {
return objc2::runtime::Bool::NO;
};
let press = NSApplication::sharedApplication(self.mtm())
.currentEvent()
.filter(|e| e.r#type() == NSEventType::KeyDown);
if let Some(press) = press.as_deref()
&& press.isARepeat()
{
if !input::owns_repeat(press) {
return objc2::runtime::Bool::NO;
}
emit(
node,
Event::Key(
day_spec::KeyEvent::named(key, 0)
.with_phase(day_spec::KeyPhase::Repeat),
),
);
return objc2::runtime::Bool::YES;
}
if !day_spec::keys::field_claims(node, key, 0) {
return objc2::runtime::Bool::NO;
}
emit(node, Event::Key(day_spec::KeyEvent::named(key, 0)));
if let Some(press) = press.as_deref() {
input::claim_key(press, node, key.to_string());
}
objc2::runtime::Bool::YES
})
}
#[unsafe(method(controlTextDidEndEditing:))]
fn control_text_did_end_editing(&self, notification: &NSNotification) {
ffi_guard::contain((), || {
let node = self.ivars().node;
let movement = unsafe { notification.userInfo() }
.and_then(|ui| ui.objectForKey(unsafe { NSTextMovementUserInfoKey }.as_ref()))
.and_then(|n| n.downcast::<NSNumber>().ok())
.map_or(NSTextMovement::Other, |n| NSTextMovement(n.integerValue()));
if movement == NSTextMovement::Return {
emit(node, Event::Submitted);
} else {
input::forget_keys(node);
emit(node, Event::FocusChanged(false));
}
});
}
}
);
impl DayTarget {
fn new(mtm: MainThreadMarker, node: NodeId) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(TargetIvars { node });
unsafe { msg_send![super(this), init] }
}
}
struct CursorIvars {
cursor: RefCell<Option<Retained<NSCursor>>>,
hidden: Cell<bool>,
hiding: Cell<bool>,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DayCursorOwner"]
#[ivars = CursorIvars]
struct DayCursorOwner;
unsafe impl NSObjectProtocol for DayCursorOwner {}
impl DayCursorOwner {
#[unsafe(method(cursorUpdate:))]
fn cursor_update(&self, _event: &NSEvent) {
let iv = self.ivars();
if iv.hidden.get() {
if !iv.hiding.replace(true) {
NSCursor::hide();
}
return;
}
if let Some(c) = iv.cursor.borrow().as_ref() {
c.set();
}
}
#[unsafe(method(mouseExited:))]
fn mouse_exited(&self, _event: &NSEvent) {
if self.ivars().hiding.replace(false) {
NSCursor::unhide();
}
}
}
);
impl DayCursorOwner {
fn new(mtm: MainThreadMarker) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(CursorIvars {
cursor: RefCell::new(None),
hidden: Cell::new(false),
hiding: Cell::new(false),
});
unsafe { msg_send![super(this), init] }
}
}
#[allow(deprecated)]
#[expect(
clippy::match_same_arms,
reason = "each macOS 15 cursor keeps its older fallback arm directly below its probe"
)]
fn ns_cursor(c: &Cursor) -> Option<Retained<NSCursor>> {
let has = |sel: objc2::runtime::Sel| <NSCursor as objc2::ClassType>::class().responds_to(sel);
Some(match c {
Cursor::Default | Cursor::Wait | Cursor::Progress | Cursor::Help => NSCursor::arrowCursor(),
Cursor::Pointer => NSCursor::pointingHandCursor(),
Cursor::Text => NSCursor::IBeamCursor(),
Cursor::VerticalText => NSCursor::IBeamCursorForVerticalLayout(),
Cursor::Crosshair | Cursor::Cell => NSCursor::crosshairCursor(),
Cursor::Move | Cursor::Grab => NSCursor::openHandCursor(),
Cursor::Grabbing => NSCursor::closedHandCursor(),
Cursor::NotAllowed => NSCursor::operationNotAllowedCursor(),
Cursor::ContextMenu => NSCursor::contextualMenuCursor(),
Cursor::Copy => NSCursor::dragCopyCursor(),
Cursor::Alias => NSCursor::dragLinkCursor(),
Cursor::ZoomIn if has(sel!(zoomInCursor)) => NSCursor::zoomInCursor(),
Cursor::ZoomOut if has(sel!(zoomOutCursor)) => NSCursor::zoomOutCursor(),
Cursor::ZoomIn | Cursor::ZoomOut => NSCursor::arrowCursor(),
Cursor::NsResize => NSCursor::resizeUpDownCursor(),
Cursor::EwResize => NSCursor::resizeLeftRightCursor(),
Cursor::NeswResize if has(sel!(frameResizeCursorFromPosition:inDirections:)) => {
NSCursor::frameResizeCursorFromPosition_inDirections(
NSCursorFrameResizePosition::TopRight,
NSCursorFrameResizeDirections::All,
)
}
Cursor::NwseResize if has(sel!(frameResizeCursorFromPosition:inDirections:)) => {
NSCursor::frameResizeCursorFromPosition_inDirections(
NSCursorFrameResizePosition::TopLeft,
NSCursorFrameResizeDirections::All,
)
}
Cursor::NeswResize | Cursor::NwseResize => NSCursor::arrowCursor(),
Cursor::ColResize if has(sel!(columnResizeCursor)) => NSCursor::columnResizeCursor(),
Cursor::ColResize => NSCursor::resizeLeftRightCursor(),
Cursor::RowResize if has(sel!(rowResizeCursor)) => NSCursor::rowResizeCursor(),
Cursor::RowResize => NSCursor::resizeUpDownCursor(),
Cursor::None => return None,
Cursor::Native(name) => match &**name {
"disappearingItem" => NSCursor::disappearingItemCursor(),
"dragCopy" => NSCursor::dragCopyCursor(),
"dragLink" => NSCursor::dragLinkCursor(),
"contextualMenu" => NSCursor::contextualMenuCursor(),
"IBeamCursorForVerticalLayout" => NSCursor::IBeamCursorForVerticalLayout(),
_ => return None,
},
})
}
fn slider_value_settled() -> bool {
let Some(mtm) = MainThreadMarker::new() else {
return false;
};
let Some(event) = NSApplication::sharedApplication(mtm).currentEvent() else {
return false;
};
matches!(
unsafe { event.r#type() },
NSEventType::LeftMouseUp | NSEventType::KeyDown | NSEventType::KeyUp
)
}
struct FieldIvars {
node: NodeId,
}
struct SharePickerIvars {
actions: Vec<day_spec::share::ShareAction>,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DaySharePickerDelegate"]
#[ivars = SharePickerIvars]
struct DaySharePickerDelegate;
unsafe impl NSObjectProtocol for DaySharePickerDelegate {}
impl DaySharePickerDelegate {
#[unsafe(method_id(sharingServicePicker:sharingServicesForItems:proposedSharingServices:))]
fn services(
&self,
_picker: &AnyObject,
_items: &AnyObject,
proposed: &objc2_foundation::NSArray<AnyObject>,
) -> Retained<objc2_foundation::NSArray<AnyObject>> {
let mut services: Vec<Retained<AnyObject>> = Vec::new();
for action in &self.ivars().actions {
let id = action.id;
let handler = block2::RcBlock::new(move || {
ffi_guard::contain((), || emit(WINDOW_NODE, Event::MenuAction(id)));
});
let image = action.symbol.and_then(|s| {
objc2_app_kit::NSImage::imageWithSystemSymbolName_accessibilityDescription(
&NSString::from_str(day_spec::sf_symbol_name(s)),
None,
)
});
let service: Option<Retained<AnyObject>> = unsafe {
let alloc: objc2::rc::Allocated<AnyObject> =
msg_send![objc2::class!(NSSharingService), alloc];
msg_send![
alloc,
initWithTitle: &*NSString::from_str(&action.title),
image: image.as_deref(),
alternateImage: None::<&AnyObject>,
handler: &*handler
]
};
services.extend(service);
}
let mut all: Vec<Retained<AnyObject>> = proposed.to_vec();
all.extend(services);
objc2_foundation::NSArray::from_retained_slice(&all)
}
}
);
define_class!(
#[unsafe(super(NSTextField))]
#[thread_kind = MainThreadOnly]
#[name = "DayTextField"]
#[ivars = FieldIvars]
struct DayTextField;
impl DayTextField {
#[unsafe(method(becomeFirstResponder))]
fn become_first_responder(&self) -> bool {
let ok: bool = unsafe { msg_send![super(self), becomeFirstResponder] };
if ok {
field_gained_focus(self.ivars().node);
}
ok
}
}
);
impl DayTextField {
fn new(mtm: MainThreadMarker, node: NodeId) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(FieldIvars { node });
unsafe { msg_send![super(this), init] }
}
}
define_class!(
#[unsafe(super(NSSecureTextField))]
#[thread_kind = MainThreadOnly]
#[name = "DaySecureTextField"]
#[ivars = FieldIvars]
struct DaySecureTextField;
impl DaySecureTextField {
#[unsafe(method(becomeFirstResponder))]
fn become_first_responder(&self) -> bool {
let ok: bool = unsafe { msg_send![super(self), becomeFirstResponder] };
if ok {
field_gained_focus(self.ivars().node);
}
ok
}
}
);
impl DaySecureTextField {
fn new(mtm: MainThreadMarker, node: NodeId) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(FieldIvars { node });
unsafe { msg_send![super(this), init] }
}
}
thread_local! {
static FIELD_SWAP: Cell<bool> = const { Cell::new(false) };
}
fn field_gained_focus(node: NodeId) {
if FIELD_SWAP.with(std::cell::Cell::get) {
return;
}
ffi_guard::contain((), || emit(node, Event::FocusChanged(true)));
}
fn text_field_node(v: &NSView) -> Option<NodeId> {
v.downcast_ref::<DayTextField>()
.map(|f| f.ivars().node)
.or_else(|| {
v.downcast_ref::<DaySecureTextField>()
.map(|f| f.ivars().node)
})
}
fn new_text_field(mtm: MainThreadMarker, node: NodeId, secure: bool) -> Retained<NSTextField> {
let target = DayTarget::new(mtm, node);
let tf: Retained<NSTextField> = if secure {
Retained::into_super(Retained::into_super(DaySecureTextField::new(mtm, node)))
} else {
Retained::into_super(DayTextField::new(mtm, node))
};
unsafe {
tf.setEditable(true);
tf.setBezeled(true);
tf.setDelegate(Some(ProtocolObject::from_ref(&*target)));
}
TARGETS.with(|m| m.borrow_mut().insert(ptr_of(&tf), target));
tf
}
fn owns_first_responder(window: &NSWindow, h: &NSView) -> bool {
window.firstResponder().is_some_and(|fr| {
if Retained::as_ptr(&fr).cast::<()>() as usize == ptr_of(h) {
return true;
}
fr.downcast::<NSText>().is_ok_and(|text| {
unsafe { text.delegate() }
.is_some_and(|d| Retained::as_ptr(&d).cast::<()>() as usize == ptr_of(h))
})
})
}
fn text_content_type(symbol: &std::ffi::CStr) -> Option<&'static NSString> {
unsafe extern "C" {
fn dlsym(
handle: *mut std::ffi::c_void,
symbol: *const std::ffi::c_char,
) -> *mut std::ffi::c_void;
}
const RTLD_DEFAULT: *mut std::ffi::c_void = -2isize as *mut std::ffi::c_void;
unsafe {
let slot = dlsym(RTLD_DEFAULT, symbol.as_ptr()).cast::<*const NSString>();
if slot.is_null() {
None
} else {
(*slot).as_ref()
}
}
}
fn rekey<V>(table: &SideTable<V>, old: usize, new: usize) {
if let Some(v) = table.take(old) {
table.insert(new, v);
}
}
struct LabelIvars {
node: NodeId,
has_links: Cell<bool>,
has_runs: Cell<bool>,
selectable: Cell<bool>,
wraps: Cell<bool>,
paragraph: Cell<day_spec::props::LabelParagraph>,
align: Cell<day_spec::props::TextAlign>,
direction: Cell<Option<day_spec::LayoutDirection>>,
stamped: Cell<bool>,
reports_selection: Cell<bool>,
selection: Cell<Option<(usize, usize)>>,
color: Cell<Option<day_spec::Ink>>,
role: Cell<TextRole>,
}
define_class!(
#[unsafe(super(NSTextField))]
#[thread_kind = MainThreadOnly]
#[name = "DayLabel"]
#[ivars = LabelIvars]
struct DayLabel;
impl DayLabel {
#[unsafe(method_id(hitTest:))]
fn hit_test(&self, point: NSPoint) -> Option<Retained<NSView>> {
let hit: Option<Retained<NSView>> = unsafe { msg_send![super(self), hitTest: point] };
ffi_guard::contain(None, || {
let takes = self.isSelectable() || input::takes_pointer(self);
input::leaf_hit_test(self, hit, takes)
})
}
#[unsafe(method(mouseEntered:))]
fn mouse_entered(&self, event: &NSEvent) {
ffi_guard::contain(false, || {
input::report_hover(self, event, day_spec::DragPhase::Began)
});
let _: () = unsafe { msg_send![super(self), mouseEntered: event] };
}
#[unsafe(method(mouseMoved:))]
fn mouse_moved(&self, event: &NSEvent) {
ffi_guard::contain(false, || {
input::report_hover(self, event, day_spec::DragPhase::Changed)
});
let _: () = unsafe { msg_send![super(self), mouseMoved: event] };
}
#[unsafe(method(mouseExited:))]
fn mouse_exited(&self, event: &NSEvent) {
ffi_guard::contain(false, || {
input::report_hover(self, event, day_spec::DragPhase::Ended)
});
let _: () = unsafe { msg_send![super(self), mouseExited: event] };
}
#[unsafe(method(updateTrackingAreas))]
fn update_tracking_areas(&self) {
let _: () = unsafe { msg_send![super(self), updateTrackingAreas] };
ffi_guard::contain((), || input::refresh_hover_area(self));
}
#[unsafe(method_id(menuForEvent:))]
fn menu_for_event(&self, event: &objc2_app_kit::NSEvent) -> Option<Retained<NSMenu>> {
ffi_guard::contain(None, || {
if !self.isSelectable()
&& let Some(menu) = inherited_context_menu(self, event)
{
return menu;
}
unsafe { msg_send![super(self), menuForEvent: event] }
})
}
#[unsafe(method(drawRect:))]
fn draw_rect(&self, dirty: NSRect) {
let mut ancestor = unsafe { self.superview() };
let mut selection_color = None;
if !self.ivars().has_runs.get() {
while let Some(view) = ancestor {
if let Some(row) = view.downcast_ref::<objc2_app_kit::NSTableRowView>() {
if unsafe { row.isSelected() } {
selection_color = Some(unsafe {
if row.isEmphasized() {
objc2_app_kit::NSColor::alternateSelectedControlTextColor()
} else {
objc2_app_kit::NSColor::unemphasizedSelectedTextColor()
}
});
}
break;
}
ancestor = unsafe { view.superview() };
}
}
let cell = unsafe { self.cell() }
.and_then(|cell| cell.downcast::<objc2_app_kit::NSTextFieldCell>().ok());
if let (Some(color), Some(cell)) = (selection_color, cell) {
let original = unsafe { cell.textColor() };
unsafe {
cell.setTextColor(Some(&color));
let _: () = msg_send![super(self), drawRect: dirty];
cell.setTextColor(original.as_deref());
}
} else {
let _: () = unsafe { msg_send![super(self), drawRect: dirty] };
}
}
}
unsafe impl NSObjectProtocol for DayLabel {}
unsafe impl NSTextDelegate for DayLabel {
#[unsafe(method(textDidEndEditing:))]
fn text_did_end_editing(&self, notification: &NSNotification) {
let _: () = unsafe { msg_send![super(self), textDidEndEditing: notification] };
ffi_guard::contain((), || self.clear_selection());
}
}
unsafe impl NSTextViewDelegate for DayLabel {
#[unsafe(method(textViewDidChangeSelection:))]
fn text_view_did_change_selection(&self, notification: &NSNotification) {
if <NSTextField as objc2::ClassType>::class()
.instance_method(sel!(textViewDidChangeSelection:))
.is_some()
{
let _: () =
unsafe { msg_send![super(self), textViewDidChangeSelection: notification] };
}
ffi_guard::contain((), || self.report_selection(notification));
}
#[unsafe(method(textView:clickedOnLink:atIndex:))]
fn clicked_on_link(
&self,
_tv: &NSTextView,
link: &objc2::runtime::AnyObject,
_index: usize,
) -> bool {
ffi_guard::contain(true, || {
let url: Retained<NSString> = unsafe { msg_send![link, description] };
emit(self.ivars().node, Event::LinkActivated(url.to_string()));
true })
}
}
);
impl DayLabel {
fn new(mtm: MainThreadMarker, node: NodeId, text: &str) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(LabelIvars {
node,
has_links: Cell::new(false),
has_runs: Cell::new(false),
selectable: Cell::new(false),
wraps: Cell::new(true),
paragraph: Cell::new(day_spec::props::LabelParagraph::default()),
align: Cell::new(day_spec::props::TextAlign::Leading),
direction: Cell::new(None),
stamped: Cell::new(false),
reports_selection: Cell::new(false),
selection: Cell::new(None),
color: Cell::new(None),
role: Cell::new(TextRole::default()),
});
let this: Retained<Self> = unsafe { msg_send![super(this), init] };
unsafe {
this.setStringValue(&NSString::from_str(text));
this.setEditable(false);
this.setSelectable(false);
this.setBordered(false);
this.setBezeled(false);
this.setDrawsBackground(false);
}
this
}
fn set_links(&self, has_links: bool) {
self.ivars().has_links.set(has_links);
unsafe {
self.setSelectable(has_links || self.ivars().selectable.get());
self.setAllowsEditingTextAttributes(has_links);
}
}
fn report_selection(&self, notification: &NSNotification) {
if !self.ivars().reports_selection.get() {
return;
}
let Some(editor) = notification
.object()
.and_then(|o| o.downcast::<NSTextView>().ok())
else {
return;
};
let range = unsafe { editor.selectedRange() };
let text = self.stringValue().to_string();
let now = utf16_span_to_bytes(&text, range.location, range.location + range.length);
if let Some((last, (start, end))) = selection_change(self.ivars().selection.get(), now) {
self.ivars().selection.set(last);
emit(self.ivars().node, Event::TextSelected { start, end });
}
}
fn clear_selection(&self) {
if let Some((_, end)) = self.ivars().selection.take()
&& self.ivars().reports_selection.get()
{
let at = end.min(self.stringValue().len());
emit(
self.ivars().node,
Event::TextSelected { start: at, end: at },
);
}
}
}
fn utf16_span_to_bytes(text: &str, start16: usize, end16: usize) -> ByteSpan {
let (start16, end16) = (start16.min(end16), start16.max(end16));
let mut start = text.len();
let mut end = text.len();
let mut at16 = 0;
let mut found_start = false;
for (byte, ch) in text.char_indices() {
let next16 = at16 + ch.len_utf16();
if !found_start && next16 > start16 {
start = byte;
found_start = true;
}
if next16 > end16 {
end = if at16 >= end16 {
byte
} else {
byte + ch.len_utf8()
};
break;
}
at16 = next16;
}
(start.min(end), end)
}
type ByteSpan = (usize, usize);
fn selection_change(last: Option<ByteSpan>, now: ByteSpan) -> Option<(Option<ByteSpan>, ByteSpan)> {
let now_set = (now.0 != now.1).then_some(now);
if now_set == last {
return None;
}
match now_set {
Some(range) => Some((Some(range), range)),
None => Some((None, (now.0, now.0))),
}
}
#[cfg(test)]
mod label_text_tests {
use super::{
label_line_break, label_line_limit, label_line_points, nstextalign, selection_change,
utf16_span_to_bytes,
};
use day_spec::props::{TextAlign, Truncation};
use objc2_app_kit::{NSLineBreakMode, NSTextAlignment};
#[test]
fn utf16_spans_land_on_character_boundaries() {
let text = "Say déjà vu 🌅 now";
let deja = text.find("déjà ").unwrap();
assert_eq!(utf16_span_to_bytes(text, 4, 8), (deja, deja + "déjà ".len()));
let sun = text.find('🌅').unwrap();
assert_eq!(utf16_span_to_bytes(text, 12, 14), (sun, sun + 4));
assert_eq!(utf16_span_to_bytes(text, 13, 14), (sun, sun + 4));
assert_eq!(utf16_span_to_bytes(text, 12, 13), (sun, sun + 4));
assert_eq!(utf16_span_to_bytes(text, 8, 4), (deja, deja + "déjà ".len()));
assert_eq!(utf16_span_to_bytes(text, 2, 2), (2, 2));
assert_eq!(utf16_span_to_bytes(text, 99, 120), (text.len(), text.len()));
assert_eq!(utf16_span_to_bytes("", 0, 0), (0, 0));
}
#[test]
fn selection_reports_once_per_change() {
assert_eq!(selection_change(None, (3, 3)), None);
assert_eq!(selection_change(None, (2, 5)), Some((Some((2, 5)), (2, 5))));
assert_eq!(selection_change(Some((2, 5)), (2, 5)), None);
assert_eq!(
selection_change(Some((2, 5)), (2, 6)),
Some((Some((2, 6)), (2, 6)))
);
assert_eq!(selection_change(Some((2, 6)), (4, 4)), Some((None, (4, 4))));
}
#[test]
fn paragraph_rules() {
assert_eq!(label_line_limit(false, 3), 1);
assert_eq!(label_line_limit(true, 3), 3);
assert_eq!(label_line_limit(true, 0), 0);
assert_eq!(
label_line_break(1, Truncation::Middle),
NSLineBreakMode::ByTruncatingMiddle
);
assert_eq!(
label_line_break(1, Truncation::Head),
NSLineBreakMode::ByTruncatingHead
);
assert_eq!(
label_line_break(2, Truncation::Head),
NSLineBreakMode::ByWordWrapping
);
assert_eq!(
label_line_break(0, Truncation::Tail),
NSLineBreakMode::ByWordWrapping
);
assert_eq!(label_line_points(2.0, 20.0), Some(40.0));
assert_eq!(label_line_points(0.0, 20.0), None);
assert_eq!(label_line_points(-1.0, 20.0), None);
assert_eq!(label_line_points(f64::NAN, 20.0), None);
assert_eq!(
nstextalign(TextAlign::Trailing, false),
NSTextAlignment::Right
);
assert_eq!(
nstextalign(TextAlign::Trailing, true),
NSTextAlignment::Left
);
assert_eq!(
nstextalign(TextAlign::Leading, true),
NSTextAlignment::Natural
);
assert_eq!(
nstextalign(TextAlign::Justified, false),
NSTextAlignment::Justified
);
}
}
struct PanMonitor(Retained<objc2::runtime::AnyObject>);
impl Drop for PanMonitor {
fn drop(&mut self) {
unsafe { NSEvent::removeMonitor(&self.0) };
}
}
fn install_sheet_escape_monitor() {
SHEET_ESCAPE_MONITOR.with(|slot| {
if slot.borrow().is_some() {
return;
}
let block = block2::RcBlock::new(|raw: std::ptr::NonNull<NSEvent>| -> *mut NSEvent {
let event = unsafe { raw.as_ref() };
ffi_guard::contain(raw.as_ptr(), || {
let Some(mtm) = MainThreadMarker::new() else {
return raw.as_ptr();
};
let escape = unsafe { event.charactersIgnoringModifiers() }
.is_some_and(|s| s.to_string() == "\u{1b}");
let Some(window) = event.window(mtm).filter(|_| escape) else {
return raw.as_ptr();
};
let app = NSApplication::sharedApplication(mtm);
if app.modalWindow().is_some() || window.attachedSheet().is_some() {
return raw.as_ptr();
}
match SHEET_NODES.with(|t| t.get(Retained::as_ptr(&window) as usize)) {
Some(node) => {
emit(
node,
Event::NavBack {
already_popped: false,
},
);
std::ptr::null_mut()
}
None => raw.as_ptr(),
}
})
});
let monitor = unsafe {
NSEvent::addLocalMonitorForEventsMatchingMask_handler(
objc2_app_kit::NSEventMask::KeyDown,
&block,
)
};
*slot.borrow_mut() = monitor.map(PanMonitor);
});
}
fn install_container_pan_monitor() {
PAN_MONITOR.with(|slot| {
if slot.borrow().is_some() {
return;
}
let block = block2::RcBlock::new(|raw: std::ptr::NonNull<NSEvent>| -> *mut NSEvent {
let event = unsafe { raw.as_ref() };
ffi_guard::contain(raw.as_ptr(), || {
let dx = unsafe { event.scrollingDeltaX() };
let dy = unsafe { event.scrollingDeltaY() };
if dx.abs() <= dy.abs() {
return raw.as_ptr();
}
let Some(mtm) = MainThreadMarker::new() else {
return raw.as_ptr();
};
let Some(window) = event.window(mtm) else {
return raw.as_ptr();
};
let Some(content) = window.contentView() else {
return raw.as_ptr();
};
let point =
content.convertPoint_fromView(unsafe { event.locationInWindow() }, None);
let mut view = content.hitTest(point);
while let Some(v) = view {
if let Some(node) = PAN_NODES.with(|t| t.get(ptr_of(&v))) {
if v.downcast_ref::<DayCanvas>().is_some() {
return raw.as_ptr();
}
let raw_phase = unsafe { event.phase() };
let phase = if raw_phase.contains(objc2_app_kit::NSEventPhase::Began) {
day_spec::DragPhase::Began
} else if raw_phase.intersects(
objc2_app_kit::NSEventPhase::Ended
| objc2_app_kit::NSEventPhase::Cancelled,
) {
day_spec::DragPhase::Ended
} else {
day_spec::DragPhase::Changed
};
let p = v.convertPoint_fromView(unsafe { event.locationInWindow() }, None);
emit(
node,
Event::Pan {
phase,
delta: Point::new(dx, dy),
location: Point::new(p.x, p.y),
},
);
return std::ptr::null_mut();
}
view = unsafe { v.superview() };
}
raw.as_ptr()
})
});
*slot.borrow_mut() = unsafe {
NSEvent::addLocalMonitorForEventsMatchingMask_handler(
objc2_app_kit::NSEventMask::ScrollWheel,
&block,
)
}
.map(PanMonitor);
});
}
#[derive(Clone, Default)]
struct SurfaceStyle {
fill: Option<day_spec::Paint>,
radius: f64,
corners: day_spec::Corners,
border: Option<day_spec::Border>,
shadow: Option<day_spec::Shadow>,
clips: bool,
}
impl SurfaceStyle {
fn solid(&self) -> Option<day_spec::Ink> {
match self.fill {
Some(day_spec::Paint::Solid(ink)) => Some(ink),
_ => None,
}
}
fn masks(&self) -> bool {
(self.clips || self.radius > 0.0) && self.shadow.is_none()
}
fn outline(&self, rect: NSRect, inset: f64) -> Retained<objc2_app_kit::NSBezierPath> {
use objc2_app_kit::NSBezierPath;
let rect = NSRect::new(
NSPoint::new(rect.origin.x + inset, rect.origin.y + inset),
NSSize::new(
(rect.size.width - 2.0 * inset).max(0.0),
(rect.size.height - 2.0 * inset).max(0.0),
),
);
let radius = (self.radius - inset).max(0.0);
if radius <= 0.0 || self.corners == day_spec::Corners::NONE {
return NSBezierPath::bezierPathWithRect(rect);
}
if self.corners == day_spec::Corners::ALL {
return NSBezierPath::bezierPathWithRoundedRect_xRadius_yRadius(rect, radius, radius);
}
let shape = day_spec::Shape::rounded_rect(
day_spec::Rect::new(
rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height,
),
self.corners.radii(radius),
);
bezier(&shape).unwrap_or_else(|| NSBezierPath::bezierPathWithRect(rect))
}
}
#[derive(Default)]
struct FlippedIvars {
surface: RefCell<Option<SurfaceStyle>>,
section_card: Cell<Option<f64>>,
pane_backdrop: Cell<bool>,
keyboard_focus: Cell<bool>,
layer_fill: Cell<bool>,
fill_run: Cell<u64>,
}
define_class!(
#[unsafe(super(NSView))]
#[thread_kind = MainThreadOnly]
#[name = "DayFlipped"]
#[ivars = FlippedIvars]
struct DayFlipped;
impl DayFlipped {
#[unsafe(method_id(menuForEvent:))]
fn menu_for_event(&self, event: &objc2_app_kit::NSEvent) -> Option<Retained<NSMenu>> {
ffi_guard::contain(None, || {
inherited_context_menu(self, event)
.unwrap_or_else(|| unsafe { msg_send![super(self), menuForEvent: event] })
})
}
#[unsafe(method(isFlipped))]
fn is_flipped(&self) -> bool {
true
}
#[unsafe(method(acceptsFirstResponder))]
fn accepts_first_responder(&self) -> bool {
let ptr = std::ptr::from_ref::<DayFlipped>(self).cast::<NSView>() as usize;
FOCUSABLE_NODES.with(|t| t.get(ptr)).is_some()
&& !ffi_guard::contain(true, || input::subtree_disabled(self))
}
#[unsafe(method(canBecomeKeyView))]
fn can_become_key_view(&self) -> bool {
self.acceptsFirstResponder() && !self.isHiddenOrHasHiddenAncestor()
}
#[unsafe(method_id(hitTest:))]
fn hit_test(&self, point: NSPoint) -> Option<Retained<NSView>> {
ffi_guard::contain(None, || input::container_hit_test(self, point))
}
#[unsafe(method(mouseEntered:))]
fn mouse_entered(&self, event: &NSEvent) {
if !ffi_guard::contain(true, || {
input::report_hover(self, event, day_spec::DragPhase::Began)
}) {
let _: () = unsafe { msg_send![super(self), mouseEntered: event] };
}
}
#[unsafe(method(mouseMoved:))]
fn mouse_moved(&self, event: &NSEvent) {
if !ffi_guard::contain(true, || {
input::report_hover(self, event, day_spec::DragPhase::Changed)
}) {
let _: () = unsafe { msg_send![super(self), mouseMoved: event] };
}
}
#[unsafe(method(mouseExited:))]
fn mouse_exited(&self, event: &NSEvent) {
if !ffi_guard::contain(true, || {
input::report_hover(self, event, day_spec::DragPhase::Ended)
}) {
let _: () = unsafe { msg_send![super(self), mouseExited: event] };
}
}
#[unsafe(method(updateTrackingAreas))]
fn update_tracking_areas(&self) {
let _: () = unsafe { msg_send![super(self), updateTrackingAreas] };
ffi_guard::contain((), || input::refresh_hover_area(self));
}
#[unsafe(method(drawFocusRingMask))]
fn draw_focus_ring_mask(&self) {
let bounds = self.bounds();
let outline = match self.ivars().surface.borrow().as_ref() {
Some(surface) => surface.outline(bounds, 0.0),
None => objc2_app_kit::NSBezierPath::bezierPathWithRect(bounds),
};
outline.fill();
}
#[unsafe(method(focusRingMaskBounds))]
fn focus_ring_mask_bounds(&self) -> NSRect {
if self.ivars().keyboard_focus.get() {
self.bounds()
} else {
NSRect::ZERO
}
}
#[unsafe(method(mouseDown:))]
fn mouse_down(&self, event: &objc2_app_kit::NSEvent) {
if transfer::pressed(self, event) { return; }
let ptr = std::ptr::from_ref::<DayFlipped>(self).cast::<NSView>() as usize;
if FOCUSABLE_NODES.with(|t| t.get(ptr)).is_some()
&& let Some(window) = self.window()
{
window.makeFirstResponder(Some(self));
}
let _: () = unsafe { msg_send![super(self), mouseDown: event] };
}
#[unsafe(method(becomeFirstResponder))]
fn become_first_responder(&self) -> bool {
let ptr = std::ptr::from_ref::<DayFlipped>(self).cast::<NSView>() as usize;
if let Some(node) = FOCUSABLE_NODES.with(|t| t.get(ptr)) {
let by_key = NSApplication::sharedApplication(self.mtm())
.currentEvent()
.is_some_and(|e| e.r#type() == NSEventType::KeyDown);
self.ivars().keyboard_focus.set(by_key);
self.noteFocusRingMaskChanged();
ffi_guard::contain((), || emit(node, Event::FocusChanged(true)));
}
true
}
#[unsafe(method(resignFirstResponder))]
fn resign_first_responder(&self) -> bool {
let ptr = std::ptr::from_ref::<DayFlipped>(self).cast::<NSView>() as usize;
if self.ivars().keyboard_focus.replace(false) {
self.noteFocusRingMaskChanged();
}
if let Some(node) = FOCUSABLE_NODES.with(|t| t.get(ptr)) {
ffi_guard::contain((), || {
input::forget_keys(node);
emit(node, Event::FocusChanged(false));
});
}
true
}
#[unsafe(method(keyDown:))]
fn key_down(&self, event: &objc2_app_kit::NSEvent) {
let ptr = std::ptr::from_ref::<DayFlipped>(self).cast::<NSView>() as usize;
let handled = ffi_guard::contain(false, || {
let Some(node) = FOCUSABLE_NODES.with(|t| t.get(ptr)) else {
return false;
};
short_route_key_down(node, event)
});
if !handled {
let _: () = unsafe { msg_send![super(self), keyDown: event] };
}
}
#[unsafe(method(rightMouseDown:))]
fn right_mouse_down(&self, event: &objc2_app_kit::NSEvent) {
if let Some(menu) = self.menuForEvent(event) {
unsafe {
objc2_app_kit::NSMenu::popUpContextMenu_withEvent_forView(&menu, event, self);
};
} else {
let _: () = unsafe { msg_send![super(self), rightMouseDown: event] };
}
}
#[unsafe(method(drawRect:))]
fn draw_rect(&self, _dirty: NSRect) {
let paint = || {
if self.ivars().pane_backdrop.get() {
let bounds = self.bounds();
unsafe {
NSColor::windowBackgroundColor().setFill();
objc2_app_kit::NSBezierPath::bezierPathWithRect(bounds).fill();
}
}
if let Some(radius) = self.ivars().section_card.get() {
let bounds = self.bounds();
unsafe {
let cls = objc2::class!(NSColor);
let has: bool = msg_send![cls, respondsToSelector: objc2::sel!(quaternarySystemFillColor)];
let color: objc2::rc::Retained<NSColor> = if has {
msg_send![cls, quaternarySystemFillColor]
} else {
msg_send![cls, controlBackgroundColor]
};
color.setFill();
let path = objc2_app_kit::NSBezierPath::bezierPathWithRoundedRect_xRadius_yRadius(
bounds, radius, radius,
);
path.fill();
}
}
if let Some(surface) = self.ivars().surface.borrow().as_ref() {
let bounds = self.bounds();
let outline = surface.outline(bounds, 0.0);
match &surface.fill {
Some(day_spec::Paint::Solid(ink)) => {
if !self.ivars().layer_fill.get() {
color::nsink(*ink).setFill();
outline.fill();
}
}
Some(paint) => {
let area = day_spec::Rect::new(
bounds.origin.x,
bounds.origin.y,
bounds.size.width,
bounds.size.height,
);
NSGraphicsContext::saveGraphicsState_class();
outline.addClip();
draw_gradient_in(paint, area);
NSGraphicsContext::restoreGraphicsState_class();
}
None => {}
}
}
};
match color::standard_appearance(&self.effectiveAppearance()) {
Some(appearance) => {
let block = block2::StackBlock::new(paint);
appearance.performAsCurrentDrawingAppearance(&block);
}
None => paint(),
}
}
#[unsafe(method(viewDidChangeEffectiveAppearance))]
fn view_did_change_effective_appearance(&self) {
let _: () = unsafe { msg_send![super(self), viewDidChangeEffectiveAppearance] };
ffi_guard::contain((), || {
self.sync_layer();
NSView::setNeedsDisplay(self, true);
});
}
}
unsafe impl NSObjectProtocol for DayFlipped {}
unsafe impl NSDraggingSource for DayFlipped {
#[unsafe(method(draggingSession:sourceOperationMaskForDraggingContext:))]
fn source_mask(&self, _session: &objc2_app_kit::NSDraggingSession, _context: objc2_app_kit::NSDraggingContext) -> objc2_app_kit::NSDragOperation { objc2_app_kit::NSDragOperation::Copy }
}
impl DayFlipped {
#[unsafe(method(mouseDragged:))]
fn transfer_dragged(&self, event: &NSEvent) {
if !transfer::dragged(self, event) { let _: () = unsafe { msg_send![super(self), mouseDragged: event] }; }
}
#[unsafe(method(draggingEntered:))]
fn transfer_entered(&self, info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>) -> objc2_app_kit::NSDragOperation { transfer::proposal(self, info) }
#[unsafe(method(draggingUpdated:))]
fn transfer_updated(&self, info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>) -> objc2_app_kit::NSDragOperation { transfer::proposal(self, info) }
#[unsafe(method(performDragOperation:))]
fn transfer_drop(&self, info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>) -> bool { transfer::receive(self, info) }
#[unsafe(method(draggingExited:))]
fn transfer_exited(&self, _info: Option<&ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>>) {
ffi_guard::contain((), || transfer::leave(self));
}
#[unsafe(method(draggingEnded:))]
fn transfer_ended(&self, _info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>) {
ffi_guard::contain((), || transfer::leave(self));
}
#[unsafe(method(setFrameSize:))]
fn set_frame_size(&self, size: NSSize) {
let _: () = unsafe { msg_send![super(self), setFrameSize: size] };
if self.ivars().surface.borrow().is_some() {
NSView::setNeedsDisplay(self, true);
self.sync_layer();
}
}
#[unsafe(method(setBoundsSize:))]
fn set_bounds_size(&self, size: NSSize) {
let _: () = unsafe { msg_send![super(self), setBoundsSize: size] };
self.sync_layer();
}
}
);
impl DayFlipped {
fn new(mtm: MainThreadMarker) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(FlippedIvars::default());
unsafe { msg_send![super(this), init] }
}
fn set_section_card(&self, corner_radius: f64) {
self.ivars().section_card.set(Some(corner_radius));
unsafe {
let _: () = msg_send![self, setNeedsDisplay: true];
}
}
fn set_pane_backdrop(&self) {
self.ivars().pane_backdrop.set(true);
unsafe {
let _: () = msg_send![self, setNeedsDisplay: true];
}
}
fn set_surface(&self, p: &ContainerProps) {
let shadow = p
.shadow
.filter(|s| !matches!(s.color, day_spec::Ink::Fixed(c) if c.a <= 0.0));
*self.ivars().surface.borrow_mut() = Some(SurfaceStyle {
fill: p.fill.clone(),
radius: p.corner_radius,
corners: p.corners,
border: p.border,
shadow,
clips: p.clips,
});
NSView::setNeedsDisplay(self, true);
self.sync_layer();
}
fn sync_layer(&self) {
let Some(s) = self.ivars().surface.borrow().clone() else {
return;
};
let border = s.border.filter(|b| b.width > 0.0);
let masks = s.masks();
if self.layer().is_none() {
if !masks && border.is_none() && s.shadow.is_none() {
return;
}
self.setWantsLayer(true);
}
let Some(layer) = self.layer() else {
return;
};
let size = layer.bounds().size;
let half = (size.width.min(size.height) / 2.0).max(0.0);
layer.setCornerRadius(s.radius.min(half));
layer.setMaskedCorners(corner_mask(s.corners));
layer.setMasksToBounds(masks);
if let Some(b) = border {
layer.setBorderWidth(b.width.min(half));
layer.setBorderColor(Some(&color::cg_for(self, b.color)));
} else {
layer.setBorderWidth(0.0);
layer.setBorderColor(None);
}
match s.shadow {
Some(sh) => {
let native = objc2_app_kit::NSShadow::new();
native.setShadowColor(Some(&nscolor(color::color_for(self, sh.color))));
native.setShadowOffset(NSSize::new(sh.dx, sh.dy));
native.setShadowBlurRadius(sh.blur.max(0.0) / 2.0);
self.setShadow(Some(&native));
let outline = s.outline(self.bounds(), 0.0);
layer.setShadowPath(Some(&*unsafe { outline.CGPath() }));
}
None => {
if self.shadow().is_some() {
self.setShadow(None);
layer.setShadowPath(None);
}
}
}
}
fn set_background(&self, fill: Option<day_spec::Paint>) {
self.ivars()
.surface
.borrow_mut()
.get_or_insert_default()
.fill = fill;
NSView::setNeedsDisplay(self, true);
}
}
fn corner_mask(corners: day_spec::Corners) -> objc2_quartz_core::CACornerMask {
use objc2_quartz_core::CACornerMask;
let mut mask = CACornerMask::empty();
for (corner, layer_corner) in [
(
day_spec::Corners::TOP_LEFT,
CACornerMask::LayerMinXMinYCorner,
),
(
day_spec::Corners::TOP_RIGHT,
CACornerMask::LayerMaxXMinYCorner,
),
(
day_spec::Corners::BOTTOM_RIGHT,
CACornerMask::LayerMaxXMaxYCorner,
),
(
day_spec::Corners::BOTTOM_LEFT,
CACornerMask::LayerMinXMaxYCorner,
),
] {
if corners.contains(corner) {
mask |= layer_corner;
}
}
mask
}
struct CanvasIvars;
define_class!(
#[unsafe(super(NSView))]
#[thread_kind = MainThreadOnly]
#[name = "DayCanvas"]
#[ivars = CanvasIvars]
struct DayCanvas;
impl DayCanvas {
#[unsafe(method(isFlipped))]
fn is_flipped(&self) -> bool {
true
}
#[unsafe(method_id(menuForEvent:))]
fn menu_for_event(
&self,
event: &objc2_app_kit::NSEvent,
) -> Option<Retained<NSMenu>> {
let f = CTX_MENU_FNS.with(|t| t.get(std::ptr::from_ref::<Self>(self) as usize));
let Some(f) = f else {
return unsafe { msg_send![super(self), menuForEvent: event] };
};
ffi_guard::contain(None, || {
let p = self.convertPoint_fromView(unsafe { event.locationInWindow() }, None);
let items = f(day_spec::Point::new(p.x, p.y));
if items.is_empty() {
return None;
}
Some(build_ns_menu(self.mtm(), "", &items))
})
}
#[unsafe(method(acceptsFirstResponder))]
fn accepts_first_responder(&self) -> bool {
!ffi_guard::contain(true, || input::subtree_disabled(self))
}
#[unsafe(method_id(hitTest:))]
fn hit_test(&self, point: NSPoint) -> Option<Retained<NSView>> {
let hit: Option<Retained<NSView>> = unsafe { msg_send![super(self), hitTest: point] };
ffi_guard::contain(None, || {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
let keys = KEY_NODES
.with(|t| t.get(ptr))
.is_some_and(day_spec::keys::handled);
input::leaf_hit_test(self, hit, keys || input::takes_pointer(self))
})
}
#[unsafe(method(updateTrackingAreas))]
fn update_tracking_areas(&self) {
let _: () = unsafe { msg_send![super(self), updateTrackingAreas] };
ffi_guard::contain((), || input::refresh_hover_area(self));
}
#[unsafe(method(mouseDown:))]
fn mouse_down(&self, event: &objc2_app_kit::NSEvent) {
if let Some(window) = self.window() {
window.makeFirstResponder(Some(self));
}
let _: () = unsafe { msg_send![super(self), mouseDown: event] };
}
#[unsafe(method(becomeFirstResponder))]
fn become_first_responder(&self) -> bool {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
if let Some(node) = KEY_NODES.with(|t| t.get(ptr)) {
ffi_guard::contain((), || emit(node, Event::FocusChanged(true)));
}
true
}
#[unsafe(method(resignFirstResponder))]
fn resign_first_responder(&self) -> bool {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
if MARKED.with(|t| t.remove(ptr)).is_some()
&& let Some(context) = self.inputContext()
{
context.discardMarkedText();
}
if let Some(node) = KEY_NODES.with(|t| t.get(ptr)) {
ffi_guard::contain((), || {
input::forget_keys(node);
emit(node, Event::FocusChanged(false));
});
}
true
}
#[unsafe(method(keyDown:))]
fn key_down(&self, event: &objc2_app_kit::NSEvent) {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
let handled = ffi_guard::contain(false, || {
let Some(node) = KEY_NODES.with(|t| t.get(ptr)) else {
return false;
};
if !day_spec::keys::handled(node) {
return false;
}
if day_spec::keys::text(node) {
return full_route_key_down(self, node, event);
}
short_route_key_down(node, event)
});
if !handled {
let _: () = unsafe { msg_send![super(self), keyDown: event] };
}
}
#[unsafe(method_id(inputContext))]
fn input_context(&self) -> Option<Retained<objc2_app_kit::NSTextInputContext>> {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
let takes_keyboard = KEY_NODES
.with(|t| t.get(ptr))
.is_some_and(|node| ffi_guard::contain(false, || day_spec::keys::text(node)));
if takes_keyboard {
unsafe { msg_send![super(self), inputContext] }
} else {
None
}
}
#[unsafe(method(drawRect:))]
fn draw_rect(&self, _dirty: NSRect) {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
let ops = OPS.with(|t| t.get(ptr)).unwrap_or_default();
paint_ops(&ops, 1.0);
}
#[unsafe(method(mouseEntered:))]
fn mouse_entered(&self, event: &objc2_app_kit::NSEvent) {
self.report_hover(event, day_spec::DragPhase::Began);
}
#[unsafe(method(mouseMoved:))]
fn mouse_moved(&self, event: &objc2_app_kit::NSEvent) {
self.report_hover(event, day_spec::DragPhase::Changed);
}
#[unsafe(method(mouseExited:))]
fn mouse_exited(&self, event: &objc2_app_kit::NSEvent) {
self.report_hover(event, day_spec::DragPhase::Ended);
}
#[unsafe(method(rotateWithEvent:))]
fn rotate_with_event(&self, event: &objc2_app_kit::NSEvent) {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
let Some(node) = ROTATE_NODES.with(|t| t.get(ptr)) else {
let _: () = unsafe { msg_send![super(self), rotateWithEvent: event] };
return;
};
ffi_guard::contain((), || {
let tick = -f64::from(event.rotation()).to_radians();
let raw = event.phase();
let (phase, rotation) = if raw.contains(objc2_app_kit::NSEventPhase::Began) {
ROTATE_ANGLE.with(|t| t.insert(ptr, tick));
(day_spec::DragPhase::Began, tick)
} else if raw.intersects(
objc2_app_kit::NSEventPhase::Ended | objc2_app_kit::NSEventPhase::Cancelled,
) {
let total = ROTATE_ANGLE.with(|t| t.remove(ptr)).unwrap_or(0.0) + tick;
(day_spec::DragPhase::Ended, total)
} else if raw.contains(objc2_app_kit::NSEventPhase::Changed) {
let total = ROTATE_ANGLE
.with(|t| {
t.with(ptr, |a| {
*a += tick;
*a
})
})
.unwrap_or(tick);
(day_spec::DragPhase::Changed, total)
} else {
(day_spec::DragPhase::Changed, tick)
};
let win = event.locationInWindow();
let loc = self.convertPoint_fromView(win, None);
emit(
node,
Event::Rotate {
phase,
rotation,
location: Point::new(loc.x, loc.y),
},
);
});
}
#[unsafe(method(scrollWheel:))]
fn scroll_wheel(&self, event: &objc2_app_kit::NSEvent) {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
let Some(node) = PAN_NODES.with(|t| t.get(ptr)) else {
let _: () = unsafe { msg_send![super(self), scrollWheel: event] };
return;
};
ffi_guard::contain((), || {
let raw = unsafe { event.phase() };
let phase = if raw.contains(objc2_app_kit::NSEventPhase::Began) {
day_spec::DragPhase::Began
} else if raw.contains(objc2_app_kit::NSEventPhase::Ended)
|| raw.contains(objc2_app_kit::NSEventPhase::Cancelled)
{
day_spec::DragPhase::Ended
} else {
day_spec::DragPhase::Changed
};
let delta = Point::new(unsafe { event.scrollingDeltaX() }, unsafe {
event.scrollingDeltaY()
});
let win = unsafe { event.locationInWindow() };
let loc = self.convertPoint_fromView(win, None);
emit(
node,
Event::Pan {
phase,
delta,
location: Point::new(loc.x, loc.y),
},
);
});
}
}
unsafe impl NSObjectProtocol for DayCanvas {}
unsafe impl NSTextInputClient for DayCanvas {
#[unsafe(method(insertText:replacementRange:))]
unsafe fn insert_text(&self, string: &AnyObject, _replacement: NSRange) {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
MARKED.with(|t| t.remove(ptr));
let Some(node) = KEY_NODES.with(|t| t.get(ptr)) else {
return;
};
let text = text_of_input_string(string);
if text.is_empty() {
return;
}
ffi_guard::contain((), || {
let interpreting = INTERPRETING.with(|i| {
i.borrow()
.as_ref()
.map(|i| (i.key.clone(), i.mods, i.phase, i.event.clone()))
});
let event = match interpreting {
Some((key, mods, phase, press)) if text.chars().count() == 1 => {
if phase == day_spec::KeyPhase::Down {
input::claim_key(&press, node, key.clone());
}
day_spec::KeyEvent::typed_by(key, text, mods).with_phase(phase)
}
Some((_, mods, _, _)) => day_spec::KeyEvent::typed_text(text, mods),
None => day_spec::KeyEvent::typed_text(text, 0),
};
emit(node, Event::Key(event));
});
}
#[unsafe(method(doCommandBySelector:))]
unsafe fn do_command_by_selector(&self, _selector: Sel) {}
#[unsafe(method(setMarkedText:selectedRange:replacementRange:))]
unsafe fn set_marked_text(&self, string: &AnyObject, _selected: NSRange, _replacement: NSRange) {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
let text = text_of_input_string(string);
if text.is_empty() {
MARKED.with(|t| t.remove(ptr));
} else {
MARKED.with(|t| t.insert(ptr, text));
}
}
#[unsafe(method(unmarkText))]
fn unmark_text(&self) {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
MARKED.with(|t| t.remove(ptr));
}
#[unsafe(method(selectedRange))]
fn selected_range(&self) -> NSRange {
NSRange::new(objc2_foundation::NSNotFound as usize, 0)
}
#[unsafe(method(markedRange))]
fn marked_range(&self) -> NSRange {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
match MARKED.with(|t| t.get(ptr)) {
Some(marked) => NSRange::new(0, marked.encode_utf16().count()),
None => NSRange::new(objc2_foundation::NSNotFound as usize, 0),
}
}
#[unsafe(method(hasMarkedText))]
fn has_marked_text(&self) -> bool {
let ptr = std::ptr::from_ref::<DayCanvas>(self).cast::<NSView>() as usize;
MARKED.with(|t| t.contains(ptr))
}
#[unsafe(method_id(attributedSubstringForProposedRange:actualRange:))]
unsafe fn attributed_substring(
&self,
_range: NSRange,
_actual: objc2_foundation::NSRangePointer,
) -> Option<Retained<objc2_foundation::NSAttributedString>> {
None
}
#[unsafe(method_id(validAttributesForMarkedText))]
fn valid_attributes(&self) -> Retained<NSArray<objc2_foundation::NSAttributedStringKey>> {
NSArray::new()
}
#[unsafe(method(firstRectForCharacterRange:actualRange:))]
unsafe fn first_rect(&self, _range: NSRange, _actual: objc2_foundation::NSRangePointer) -> NSRect {
let bounds = self.bounds();
let in_window = self.convertRect_toView(bounds, None);
match self.window() {
Some(window) => window.convertRectToScreen(in_window),
None => in_window,
}
}
#[unsafe(method(characterIndexForPoint:))]
fn character_index_for_point(&self, _point: NSPoint) -> objc2_foundation::NSUInteger {
objc2_foundation::NSNotFound as objc2_foundation::NSUInteger
}
}
);
impl DayCanvas {
fn new(mtm: MainThreadMarker) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(CanvasIvars);
unsafe { msg_send![super(this), init] }
}
fn report_hover(&self, event: &objc2_app_kit::NSEvent, phase: day_spec::DragPhase) {
ffi_guard::contain(false, || input::report_hover(self, event, phase));
}
}
struct GestureIvars {
node: NodeId,
kind: day_spec::GestureKind,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DayGesture"]
#[ivars = GestureIvars]
struct DayGesture;
unsafe impl NSObjectProtocol for DayGesture {}
unsafe impl NSGestureRecognizerDelegate for DayGesture {
#[unsafe(method(gestureRecognizer:shouldAttemptToRecognizeWithEvent:))]
fn should_attempt(&self, _g: &NSGestureRecognizer, event: &NSEvent) -> bool {
!(self.ivars().kind == day_spec::GestureKind::LongPress
&& event.modifierFlags().contains(NSEventModifierFlags::Control))
}
#[unsafe(method(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:))]
fn simultaneous(&self, _g: &NSGestureRecognizer, other: &NSGestureRecognizer) -> bool {
use day_spec::GestureKind::{DoubleTap, Tap};
let mine = self.ivars().kind;
let theirs = unsafe { other.delegate() }.and_then(|d| {
let obj: &AnyObject = d.as_ref();
obj.downcast_ref::<DayGesture>().map(|g| g.ivars().kind)
});
matches!((mine, theirs), (Tap, Some(DoubleTap)) | (DoubleTap, Some(Tap)))
}
}
impl DayGesture {
#[unsafe(method(fire:))]
fn fire(&self, g: &NSGestureRecognizer) {
ffi_guard::contain((), || {
let node = self.ivars().node;
let view = g.view();
let loc = g.locationInView(view.as_deref());
let at = match view.as_deref() {
Some(v) => input::top_left(v, loc),
None => Point::new(loc.x, loc.y),
};
let up = view.as_deref().is_some_and(|v| !v.isFlipped());
let phase = match g.state() {
NSGestureRecognizerState::Began => day_spec::DragPhase::Began,
NSGestureRecognizerState::Ended
| NSGestureRecognizerState::Cancelled
| NSGestureRecognizerState::Failed => day_spec::DragPhase::Ended,
_ => day_spec::DragPhase::Changed,
};
let obj: &objc2::runtime::AnyObject = g.as_ref();
match self.ivars().kind {
day_spec::GestureKind::Drag => {
let pan = obj.downcast_ref::<NSPanGestureRecognizer>();
let flip = |p: NSPoint| Point::new(p.x, if up { -p.y } else { p.y });
let translation = pan.map_or(Point::ZERO, |pan| {
flip(unsafe { pan.translationInView(view.as_deref()) })
});
if g.state() == NSGestureRecognizerState::Ended
&& let Some(pan) = pan
{
let velocity = flip(unsafe { pan.velocityInView(view.as_deref()) });
emit(
node,
Event::GestureVelocity {
kind: day_spec::GestureKind::Drag,
velocity,
},
);
}
emit(node, Event::Drag { phase, location: at, translation });
}
day_spec::GestureKind::LongPress => {
let key = view.as_deref().map_or(0, ptr_of);
match g.state() {
NSGestureRecognizerState::Began => {
input::LONG_HELD.with(|t| t.insert(key, ()));
emit(node, Event::LongPress(at));
}
NSGestureRecognizerState::Ended
| NSGestureRecognizerState::Cancelled
| NSGestureRecognizerState::Failed => {
<AppKit as Platform>::post(Box::new(move || {
input::LONG_HELD.with(|t| t.take(key));
}));
}
_ => {}
}
}
day_spec::GestureKind::DoubleTap => {
if let Some(v) = view.as_deref() {
input::cancel_held_tap(v);
}
emit(node, Event::DoubleTap(at));
}
day_spec::GestureKind::Pinch => {
let scale = if let Some(m) =
obj.downcast_ref::<objc2_app_kit::NSMagnificationGestureRecognizer>()
{
1.0 + unsafe { m.magnification() }
} else {
1.0
};
emit(node, Event::Pinch { phase, scale, location: at });
}
_ => {
let Some(v) = view.as_deref() else {
emit(node, Event::Tap(at));
return;
};
if input::LONG_HELD.with(|t| t.take(ptr_of(v))).is_some() {
return;
}
let double = GESTURES.with(|m| {
m.borrow().get(&ptr_of(v)).is_some_and(|targets| {
targets
.iter()
.any(|t| t.ivars().kind == day_spec::GestureKind::DoubleTap)
})
});
let clicks = NSApplication::sharedApplication(self.mtm())
.currentEvent()
.map_or(1, |e| {
if matches!(
e.r#type(),
NSEventType::LeftMouseUp | NSEventType::LeftMouseDown
) {
e.clickCount()
} else {
1
}
});
match input::tap_disposition(double, clicks) {
input::TapDisposition::Immediate => emit(node, Event::Tap(at)),
input::TapDisposition::Pending => input::hold_tap(v, node, at),
input::TapDisposition::Drop => {}
}
}
}
});
}
}
);
impl DayGesture {
fn new(mtm: MainThreadMarker, node: NodeId, kind: day_spec::GestureKind) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(GestureIvars { node, kind });
unsafe { msg_send![super(this), init] }
}
}
struct NavHeader {
bar: Retained<NSView>,
title: Retained<NSTextField>,
titles: Vec<String>,
_target: Retained<DayTarget>,
}
const NAV_HEADER_H: f64 = 34.0;
use day_spec::{NAV_LIST_MAX_W, NAV_LIST_MIN_W, NAV_SIDEBAR_MAX_W, NAV_SIDEBAR_MIN_W};
fn take_list_placement(state: &mut NavState) -> Option<f64> {
if !state.list_visible || state.list_positioned {
return None;
}
state.list_positioned = true;
state.list_width
}
const NAV_TABVIEW_INSET: f64 = 12.0;
const NAV_TABVIEW_INSET_TOP: f64 = 8.0;
struct TabHost {
view: Retained<objc2_app_kit::NSTabView>,
target: Option<Retained<DayTarget>>,
}
thread_local! {
static TAB_PICK_SUPPRESS: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
fn sync_tab_labels(tabs: &TabHost, titles: &[String], selected: usize) {
unsafe {
let n = tabs.view.numberOfTabViewItems().max(0) as usize;
for (i, title) in titles.iter().enumerate().take(n) {
tabs.view
.tabViewItemAtIndex(i as isize)
.setLabel(&NSString::from_str(title));
}
if n > 0 {
TAB_PICK_SUPPRESS.with(|s| s.set(true));
tabs.view
.selectTabViewItemAtIndex(selected.min(n - 1) as isize);
TAB_PICK_SUPPRESS.with(|s| s.set(false));
}
}
}
fn adopt_menu_for_tabs(mtm: MainThreadMarker, menu: &NSView) {
let Some((titles, node)) = NAV_MENUS.with(|m| {
m.borrow().get(&ptr_of(menu)).map(|(_, d)| {
let ivars = d.ivars();
(
ivars
.items
.borrow()
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<String>>(),
ivars.node,
)
})
}) else {
return;
};
let mut up = unsafe { menu.superview() };
while let Some(v) = up {
let reached = NAV_STATE.with(|m| {
let mut m = m.borrow_mut();
let Some(s) = m.get_mut(&ptr_of(&v)) else {
return false;
};
let selected = s.selected;
if let Some(tabs) = s.tabs.as_mut() {
if tabs.target.is_none() {
let target = DayTarget::new(mtm, node);
unsafe {
tabs.view
.setDelegate(Some(ProtocolObject::from_ref(&*target)));
};
tabs.target = Some(target);
}
sync_tab_labels(tabs, &titles, selected);
}
true
});
if reached {
return;
}
up = unsafe { v.superview() };
}
}
struct NavState {
sidebar_wrap: Retained<NSView>,
detail_wrap: Retained<NSView>,
split_vc: Retained<objc2_app_kit::NSSplitViewController>,
sidebar_item: Retained<objc2_app_kit::NSSplitViewItem>,
list_wrap: Option<Retained<NSView>>,
list_item: Option<Retained<objc2_app_kit::NSSplitViewItem>>,
list_width: Option<f64>,
list_visible: bool,
list_positioned: bool,
pages: Vec<Retained<NSView>>,
sidebar_page: Option<Retained<NSView>>,
list_page: Option<Retained<NSView>>,
positioned: bool,
presentation: NavPresentation,
selected: usize,
tabs: Option<TabHost>,
header: Option<NavHeader>,
root_title: String,
node: NodeId,
title_strip: f64,
}
fn pane_under_title_bar(mtm: MainThreadMarker, wrap: &NSView) -> Retained<NSView> {
let pane = view_of(&DayFlipped::new(mtm));
unsafe {
wrap.setFrame(pane.bounds());
wrap.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable
| objc2_app_kit::NSAutoresizingMaskOptions::ViewHeightSizable,
);
pane.addSubview(wrap);
}
pane
}
fn place_below_strip(wrap: &NSView, strip: f64) {
let Some(pane) = (unsafe { wrap.superview() }) else {
return;
};
let b = pane.bounds();
unsafe {
wrap.setFrame(NSRect::new(
NSPoint::new(0.0, strip),
NSSize::new(b.size.width, (b.size.height - strip).max(0.0)),
));
};
}
fn build_nav_header(
mtm: MainThreadMarker,
id: NodeId,
detail_wrap: &NSView,
root_title: &str,
) -> NavHeader {
let bar = view_of(&DayFlipped::new(mtm));
let target = DayTarget::new(mtm, id);
let back = unsafe {
let img = objc2_app_kit::NSImage::imageNamed(objc2_app_kit::NSImageNameGoBackTemplate)
.expect("NSGoBackTemplate");
let tobj: &objc2::runtime::AnyObject = target.as_ref();
objc2_app_kit::NSButton::buttonWithImage_target_action(
&img,
Some(tobj),
Some(sel!(navBack:)),
mtm,
)
};
let title = unsafe { NSTextField::labelWithString(&NSString::from_str(root_title), mtm) };
unsafe {
back.setFrame(NSRect::new(
NSPoint::new(6.0, 4.0),
NSSize::new(30.0, NAV_HEADER_H - 8.0),
));
title.setFont(Some(&NSFont::boldSystemFontOfSize(13.0)));
title.setAlignment(objc2_app_kit::NSTextAlignment::Center);
title.setLineBreakMode(objc2_app_kit::NSLineBreakMode::ByTruncatingTail);
title.setFrame(NSRect::new(
NSPoint::new(44.0, 9.0),
NSSize::new((detail_wrap.bounds().size.width - 88.0).max(0.0), 18.0),
));
title.setAutoresizingMask(objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable);
bar.setFrame(NSRect::new(
NSPoint::new(0.0, 0.0),
NSSize::new(detail_wrap.bounds().size.width, NAV_HEADER_H),
));
bar.setAutoresizingMask(objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable);
bar.addSubview(&back);
bar.addSubview(&title);
bar.setHidden(true);
detail_wrap.addSubview(&bar);
}
NavHeader {
bar,
title,
titles: vec![root_title.to_string()],
_target: target,
}
}
fn nav_page_frame(wrap: &NSView, header_visible: bool) -> NSRect {
let b = wrap.bounds();
let top = if header_visible { NAV_HEADER_H } else { 0.0 };
NSRect::new(
NSPoint::new(0.0, top),
NSSize::new(b.size.width, (b.size.height - top).max(0.0)),
)
}
fn nav_menu_rows(page: &NSView) -> Option<(Vec<String>, NodeId)> {
fn walk(v: &NSView, out: &mut Option<(Vec<String>, NodeId)>) {
if out.is_some() {
return;
}
let found = NAV_MENUS.with(|m| {
m.borrow()
.get(&(std::ptr::from_ref::<NSView>(v) as usize))
.map(|(_, d)| {
let ivars = d.ivars();
(
ivars
.items
.borrow()
.iter()
.map(std::string::ToString::to_string)
.collect(),
ivars.node,
)
})
});
if found.is_some() {
*out = found;
return;
}
for sub in &unsafe { v.subviews() } {
walk(&sub, out);
}
}
let mut out = None;
walk(page, &mut out);
out
}
fn apply_sidebar_item(item: &objc2_app_kit::NSSplitViewItem, pres: NavPresentation) {
let shown = matches!(pres, NavPresentation::Split | NavPresentation::Rail);
unsafe {
item.setCanCollapse(shown);
item.setCollapsed(!shown);
match pres {
NavPresentation::Split | NavPresentation::Rail => {
item.setAllowsFullHeightLayout(true);
item.setMinimumThickness(NAV_SIDEBAR_MIN_W);
item.setMaximumThickness(NAV_SIDEBAR_MAX_W);
}
NavPresentation::Stack | NavPresentation::Tabs => {
item.setMinimumThickness(0.0);
item.setMaximumThickness(0.0);
}
}
}
}
pub(crate) fn list_split_view(window: usize) -> Option<Retained<objc2_app_kit::NSSplitView>> {
NAV_STATE.with(|m| {
m.borrow()
.values()
.find(|st| {
st.list_item.is_some()
&& st
.sidebar_wrap
.window()
.is_some_and(|w| Retained::as_ptr(&w) as usize == window)
})
.map(|st| unsafe { st.split_vc.splitView() })
})
}
fn sync_nav_header(hdr: &NavHeader, wrap: &NSView, pages: &[Retained<NSView>]) {
let visible = pages.len() >= 2;
hdr.bar.setHidden(!visible);
unsafe {
hdr.title.setStringValue(&NSString::from_str(
hdr.titles.last().map_or("", String::as_str),
));
}
let frame = nav_page_frame(wrap, visible);
for page in pages {
unsafe { page.setFrame(frame) };
}
}
fn nav_present(mtm: MainThreadMarker, host: &Handle, next: NavPresentation) {
let Some((sidebar_page, detail_wrap, sidebar_wrap, sidebar_item, was, root_title, id, sel)) =
NAV_STATE.with(|m| {
let st = m.borrow();
let s = st.get(&ptr_of(host))?;
Some((
s.sidebar_page.clone(),
s.detail_wrap.clone(),
s.sidebar_wrap.clone(),
s.sidebar_item.clone(),
s.presentation,
s.root_title.clone(),
s.node,
s.selected,
))
})
else {
return;
};
if was == next {
return;
}
apply_sidebar_item(&sidebar_item, next);
NAV_STATE.with(|m| {
if let Some(s) = m.borrow().get(&ptr_of(host)) {
unsafe { s.split_vc.splitView().layoutSubtreeIfNeeded() };
}
});
let stacked = next == NavPresentation::Stack;
let mut header = NAV_STATE.with(|m| m.borrow_mut().get_mut(&ptr_of(host))?.header.take());
if stacked && header.is_none() {
header = Some(build_nav_header(mtm, id, &detail_wrap, &root_title));
}
if let Some(hdr) = header.as_ref() {
hdr.bar.setHidden(true);
}
let mut pages = NAV_STATE.with(|m| {
m.borrow()
.get(&ptr_of(host))
.map_or_default(|s| s.pages.clone())
});
let shown_page = pages.get(sel).cloned();
if let Some(page) = sidebar_page {
pages.retain(|p| ptr_of(p) != ptr_of(&page));
unsafe {
if stacked {
detail_wrap.addSubview(&page);
pages.insert(0, page.clone());
} else {
page.setFrame(sidebar_wrap.bounds());
sidebar_wrap.addSubview(&page);
}
}
page.setHidden(false);
}
let shown = shown_page
.and_then(|p| pages.iter().position(|q| ptr_of(q) == ptr_of(&p)))
.unwrap_or_else(|| pages.len().saturating_sub(1));
for (i, page) in pages.iter().enumerate() {
page.setHidden(i != shown);
}
NAV_STATE.with(|m| {
let mut m = m.borrow_mut();
if let Some(s) = m.get_mut(&ptr_of(host)) {
s.presentation = next;
s.pages = pages;
s.selected = shown;
s.header = header;
if let Some(hdr) = s.header.as_ref() {
if stacked {
sync_nav_header(hdr, &s.detail_wrap, &s.pages);
} else {
hdr.bar.setHidden(true);
}
}
if !stacked {
let frame = nav_page_frame(&s.detail_wrap, false);
for page in &s.pages {
unsafe { page.setFrame(frame) };
}
}
}
});
}
struct CoverSheet(Retained<NSWindow>);
impl Drop for CoverSheet {
fn drop(&mut self) {
let _ = SHEET_NODES.try_with(|t| t.remove(Retained::as_ptr(&self.0) as usize));
let raw = Retained::into_raw(self.0.clone()) as usize;
dispatch2::DispatchQueue::main().exec_async(move || {
ffi_guard::contain((), || unsafe {
let window = Retained::from_raw(raw as *mut NSWindow).unwrap();
if let Some(parent) = window.sheetParent() {
parent.endSheet(&window);
}
window.orderOut(None);
window.setContentView(None);
});
});
}
}
struct NavPageIvars {
node: NodeId,
top_inset: Cell<f64>,
sheet: Cell<bool>,
half: Cell<bool>,
large: Cell<bool>,
fill: Cell<Option<day_spec::Ink>>,
}
define_class!(
#[unsafe(super(NSView))]
#[thread_kind = MainThreadOnly]
#[name = "DayNavPage"]
#[ivars = NavPageIvars]
struct DayNavPage;
impl DayNavPage {
#[unsafe(method(isFlipped))]
fn is_flipped(&self) -> bool {
true
}
#[unsafe(method(setFrameSize:))]
fn set_frame_size(&self, size: NSSize) {
let _: () = unsafe { msg_send![super(self), setFrameSize: size] };
ffi_guard::contain((), || {
let inset = self.ivars().top_inset.get();
emit(
self.ivars().node,
Event::FrameChanged(Size::new(size.width, (size.height - inset).max(0.0))),
);
});
}
#[unsafe(method(viewDidChangeEffectiveAppearance))]
fn view_did_change_effective_appearance(&self) {
let _: () = unsafe { msg_send![super(self), viewDidChangeEffectiveAppearance] };
ffi_guard::contain((), || self.paint_fill());
}
}
);
impl DayNavPage {
fn new(mtm: MainThreadMarker, node: NodeId) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(NavPageIvars {
node,
top_inset: Cell::new(0.0),
sheet: Cell::new(false),
half: Cell::new(false),
large: Cell::new(false),
fill: Cell::new(None),
});
unsafe { msg_send![super(this), init] }
}
fn set_fill(&self, ink: day_spec::Ink) {
self.ivars().fill.set(Some(ink));
self.setWantsLayer(true);
self.paint_fill();
}
fn paint_fill(&self) {
if let (Some(ink), Some(layer)) = (self.ivars().fill.get(), self.layer()) {
layer.setBackgroundColor(Some(&color::cg_for(self, ink)));
}
}
}
struct InspectorState {
content_wrap: Handle,
panel_wrap: Handle,
split_vc: Retained<objc2_app_kit::NSSplitViewController>,
panel_item: Retained<objc2_app_kit::NSSplitViewItem>,
}
struct SplitState {
split: Retained<objc2_app_kit::NSSplitView>,
first_wrap: Handle,
second_wrap: Handle,
delegate: Retained<DaySplitDelegate>,
}
struct SplitDelegateIvars {
node: NodeId,
min_pane: f64,
share: Cell<f64>,
positioned: Cell<bool>,
suppress: Cell<bool>,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DaySplitDelegate"]
#[ivars = SplitDelegateIvars]
struct DaySplitDelegate;
unsafe impl NSObjectProtocol for DaySplitDelegate {}
unsafe impl NSSplitViewDelegate for DaySplitDelegate {
#[unsafe(method(splitViewDidResizeSubviews:))]
fn did_resize(&self, notification: &NSNotification) {
ffi_guard::contain((), || {
let Some(obj) = notification.object() else {
return;
};
let Ok(sv) = obj.downcast::<objc2_app_kit::NSSplitView>() else {
return;
};
let iv = self.ivars();
let usable = split_usable(&sv);
if usable <= 0.0 {
return;
}
if !iv.positioned.replace(true) {
split_place(&sv, iv);
return;
}
if iv.suppress.get() {
return;
}
let Some(first) = sv.subviews().iter().next() else {
return;
};
let f = first.frame();
let a = if sv.isVertical() {
f.size.width
} else {
f.size.height
};
let share = (a / usable).clamp(0.0, 1.0);
if (share - iv.share.get()).abs() > 1e-4 {
iv.share.set(share);
emit(iv.node, Event::ValueChanged(share));
}
});
}
#[unsafe(method(splitView:constrainMinCoordinate:ofSubviewAt:))]
fn constrain_min(
&self,
_sv: &objc2_app_kit::NSSplitView,
proposed: f64,
_index: isize,
) -> f64 {
proposed.max(self.ivars().min_pane)
}
#[unsafe(method(splitView:constrainMaxCoordinate:ofSubviewAt:))]
fn constrain_max(
&self,
sv: &objc2_app_kit::NSSplitView,
proposed: f64,
_index: isize,
) -> f64 {
proposed.min((split_usable(sv) - self.ivars().min_pane).max(0.0))
}
}
);
impl DaySplitDelegate {
fn new(mtm: MainThreadMarker, node: NodeId, min_pane: f64, share: f64) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(SplitDelegateIvars {
node,
min_pane,
share: Cell::new(share.clamp(0.0, 1.0)),
positioned: Cell::new(false),
suppress: Cell::new(false),
});
unsafe { msg_send![super(this), init] }
}
}
fn split_usable(sv: &objc2_app_kit::NSSplitView) -> f64 {
let frame = sv.frame();
let len = if sv.isVertical() {
frame.size.width
} else {
frame.size.height
};
(len - sv.dividerThickness()).max(0.0)
}
fn split_place(sv: &objc2_app_kit::NSSplitView, iv: &SplitDelegateIvars) {
let usable = split_usable(sv);
if usable <= 0.0 {
return;
}
iv.suppress.set(true);
unsafe { sv.setPosition_ofDividerAtIndex(iv.share.get() * usable, 0) };
iv.suppress.set(false);
}
struct NavMenuIvars {
node: NodeId,
items: RefCell<Vec<Retained<NSString>>>,
icons: RefCell<Vec<Option<Retained<objc2_app_kit::NSImage>>>>,
icon_fills: RefCell<Vec<f64>>,
tints: RefCell<Vec<Option<day_spec::Ink>>>,
badges: RefCell<Vec<Option<Retained<NSString>>>>,
icon_progress: RefCell<Vec<(usize, Option<f64>)>>,
badge_icons: RefCell<Vec<Option<Retained<objc2_app_kit::NSImage>>>>,
badge_tints: RefCell<Vec<Option<day_spec::Ink>>>,
menus: RefCell<Vec<Vec<day_spec::MenuItem>>>,
movable: RefCell<Vec<bool>>,
regroup: std::cell::Cell<bool>,
sorted: RefCell<Vec<bool>>,
rows: RefCell<Vec<Option<usize>>>,
row_objects: RefCell<Vec<Retained<NSString>>>,
row_ids: RefCell<Vec<Retained<NSNumber>>>,
suppress: std::cell::Cell<bool>,
}
struct NavCellIvars {
accessory: RefCell<Option<Retained<NSView>>>,
count: RefCell<Option<Retained<NSTextField>>>,
icon_fill: std::cell::Cell<f64>,
}
define_class!(
#[unsafe(super(objc2_app_kit::NSTableCellView))]
#[thread_kind = MainThreadOnly]
#[name = "DayNavCell"]
#[ivars = NavCellIvars]
struct DayNavCell;
unsafe impl NSObjectProtocol for DayNavCell {}
impl DayNavCell {
#[unsafe(method(layout))]
fn layout(&self) {
let _: () = unsafe { msg_send![super(self), layout] };
ffi_guard::contain((), || self.fit_accessories());
}
}
);
impl DayNavCell {
fn new(mtm: MainThreadMarker) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(NavCellIvars {
accessory: RefCell::new(None),
count: RefCell::new(None),
icon_fill: std::cell::Cell::new(1.0),
});
unsafe { msg_send![super(this), init] }
}
fn fit_accessories(&self) {
let label = unsafe { self.textField() };
if let (Some(label), Some(count)) = (&label, self.ivars().count.borrow().as_ref()) {
let size = unsafe { label.font() }.map_or(NSFont::smallSystemFontSize(), |f| {
(f.pointSize() - 2.0).max(9.0)
});
#[expect(
clippy::float_cmp,
reason = "unchanged-value check against the exact size set below"
)]
let resize = unsafe { count.font() }.is_none_or(|f| f.pointSize() != size);
if resize {
unsafe {
count.setFont(Some(&NSFont::monospacedDigitSystemFontOfSize_weight(
size,
objc2_app_kit::NSFontWeightRegular,
)));
}
self.layoutSubtreeIfNeeded();
}
}
if let (Some(label), Some(accessory)) = (label, self.ivars().accessory.borrow().as_ref()) {
let mut frame = label.frame();
let end = accessory.frame().origin.x - 6.0;
if frame.origin.x + frame.size.width > end {
frame.size.width = (end - frame.origin.x).max(0.0);
label.setFrame(frame);
}
}
if let Some(icon) = unsafe { self.imageView() } {
fit_nav_icon(&icon, self.ivars().icon_fill.get());
nav_progress::fit(&icon);
}
}
}
thread_local! {
static NAV_GLYPH_SIDES: RefCell<HashMap<u64, f64>> = RefCell::new(HashMap::new());
}
fn fit_nav_icon(icon: &objc2_app_kit::NSImageView, fill: f64) {
let Some(config) = (unsafe { icon.symbolConfiguration() }) else {
return;
};
let slot = icon.frame();
let key = config.hash() as u64;
let side = NAV_GLYPH_SIDES
.with(|m| m.borrow().get(&key).copied())
.or_else(|| {
let symbol =
objc2_app_kit::NSImage::imageWithSystemSymbolName_accessibilityDescription(
&NSString::from_str("gearshape"),
None,
)?;
let side = symbol.imageWithSymbolConfiguration(&config)?.size().height;
NAV_GLYPH_SIDES.with(|m| m.borrow_mut().insert(key, side));
Some(side)
});
let Some(side) = side
.filter(|s| *s > 0.0)
.map(|s| (s / fill.clamp(0.25, 1.0)).round())
else {
return;
};
if slot.size.width <= side && slot.size.height <= side {
return;
}
icon.setFrame(NSRect::new(
NSPoint::new(
(slot.origin.x + (slot.size.width - side) / 2.0).round(),
(slot.origin.y + (slot.size.height - side) / 2.0).round(),
),
NSSize::new(side, side),
));
}
define_class!(
#[unsafe(super(objc2_app_kit::NSTableRowView))]
#[thread_kind = MainThreadOnly]
#[name = "DayNavRowView"]
struct DayNavRowView;
unsafe impl NSObjectProtocol for DayNavRowView {}
impl DayNavRowView {
#[unsafe(method(isEmphasized))]
fn is_emphasized(&self) -> bool {
true
}
}
);
define_class!(
#[unsafe(super(objc2_app_kit::NSOutlineView))]
#[thread_kind = MainThreadOnly]
#[name = "DayNavOutlineView"]
struct DayNavOutlineView;
unsafe impl NSObjectProtocol for DayNavOutlineView {}
impl DayNavOutlineView {
#[unsafe(method_id(menuForEvent:))]
fn menu_for_event(
&self,
event: &objc2_app_kit::NSEvent,
) -> Option<Retained<NSMenu>> {
ffi_guard::contain(None, || {
let point = self.convertPoint_fromView(unsafe { event.locationInWindow() }, None);
let row = unsafe { self.rowAtPoint(point) };
let data = NAV_OUTLINE_MENUS.with(|t| t.get(std::ptr::from_ref(self) as usize));
match data {
None => None,
Some(d) => {
let items = d
.item_of_row(row)
.and_then(|i| d.ivars().menus.borrow().get(i).cloned())
.unwrap_or_default();
if items.is_empty() {
None
} else {
Some(build_ns_menu(d.mtm(), "", &items))
}
}
}
})
}
}
);
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DayNavMenuData"]
#[ivars = NavMenuIvars]
struct DayNavMenuData;
unsafe impl NSObjectProtocol for DayNavMenuData {}
unsafe impl NSOutlineViewDataSource for DayNavMenuData {
#[unsafe(method(outlineView:numberOfChildrenOfItem:))]
fn number_of_children(
&self,
_ov: &objc2_app_kit::NSOutlineView,
item: Option<&objc2::runtime::AnyObject>,
) -> isize {
if item.is_none() {
self.ivars().row_objects.borrow().len() as isize
} else {
0
}
}
#[unsafe(method_id(outlineView:child:ofItem:))]
fn child_of_item(
&self,
_ov: &objc2_app_kit::NSOutlineView,
index: isize,
_item: Option<&objc2::runtime::AnyObject>,
) -> Retained<objc2::runtime::AnyObject> {
let ids = self.ivars().row_ids.borrow();
let id = ids
.get(index as usize)
.cloned()
.unwrap_or_else(|| NSNumber::new_isize(-1));
unsafe { objc2::rc::Retained::cast_unchecked(id) }
}
#[unsafe(method(outlineView:isItemExpandable:))]
fn is_expandable(
&self,
_ov: &objc2_app_kit::NSOutlineView,
_item: &objc2::runtime::AnyObject,
) -> bool {
false
}
#[unsafe(method_id(outlineView:pasteboardWriterForItem:))]
fn pasteboard_writer_for_item(
&self,
_ov: &objc2_app_kit::NSOutlineView,
item: &objc2::runtime::AnyObject,
) -> Option<Retained<ProtocolObject<dyn objc2_app_kit::NSPasteboardWriting>>> {
let make = || {
let row = Self::outline_row(item);
let index = self.item_of_row(row)?;
if !self.is_movable(index) {
return None;
}
let pbitem = objc2_app_kit::NSPasteboardItem::new();
unsafe {
pbitem.setString_forType(
&NSString::from_str(&row.to_string()),
&NSString::from_str(DAY_NAV_PASTEBOARD_TYPE),
);
}
Some(ProtocolObject::from_retained(pbitem))
};
ffi_guard::contain(None, make)
}
#[unsafe(method(outlineView:validateDrop:proposedItem:proposedChildIndex:))]
fn validate_drop(
&self,
ov: &objc2_app_kit::NSOutlineView,
info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>,
item: Option<&objc2::runtime::AnyObject>,
child_index: isize,
) -> objc2_app_kit::NSDragOperation {
ffi_guard::contain(objc2_app_kit::NSDragOperation::None, || {
let Some(drop) = self.nav_drop(ov, info, item, child_index) else {
return objc2_app_kit::NSDragOperation::None;
};
if drop.retarget {
unsafe { ov.setDropItem_dropChildIndex(None, drop.gap as isize) };
}
objc2_app_kit::NSDragOperation::Move
})
}
#[unsafe(method(outlineView:acceptDrop:item:childIndex:))]
fn accept_drop(
&self,
ov: &objc2_app_kit::NSOutlineView,
info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>,
item: Option<&objc2::runtime::AnyObject>,
child_index: isize,
) -> bool {
ffi_guard::contain(false, || {
let Some(drop) = self.nav_drop(ov, info, item, child_index) else {
return false;
};
match drop.moves {
Some(NavMove::Reorder { from, to }) => {
emit(self.ivars().node, Event::ListReorder { from, to });
}
Some(NavMove::Regroup {
from,
section,
before,
}) => emit(
self.ivars().node,
Event::NavRegroup {
from,
section,
before,
},
),
None => {}
}
true
})
}
}
unsafe impl NSControlTextEditingDelegate for DayNavMenuData {}
unsafe impl NSOutlineViewDelegate for DayNavMenuData {
#[unsafe(method(outlineView:isGroupItem:))]
fn is_group_item(
&self,
_ov: &objc2_app_kit::NSOutlineView,
item: &objc2::runtime::AnyObject,
) -> bool {
let row = Self::outline_row(item);
row >= 0 && self.item_of_row(row).is_none()
}
#[unsafe(method(outlineView:shouldSelectItem:))]
fn should_select(
&self,
_ov: &objc2_app_kit::NSOutlineView,
item: &objc2::runtime::AnyObject,
) -> bool {
self.item_of_row(Self::outline_row(item)).is_some()
}
#[unsafe(method_id(outlineView:viewForTableColumn:item:))]
fn view_for(
&self,
ov: &objc2_app_kit::NSOutlineView,
_col: Option<&objc2_app_kit::NSTableColumn>,
item: &objc2::runtime::AnyObject,
) -> Option<Retained<NSView>> {
let mtm = self.mtm();
let row = Self::outline_row(item);
let text = self
.ivars()
.row_objects
.borrow()
.get(usize::try_from(row).unwrap_or(usize::MAX))
.cloned();
text.map(|text| {
let text: &NSString = &text;
let Some(index) = self.item_of_row(row) else {
let cell = DayNavCell::new(mtm);
let label = unsafe { NSTextField::labelWithString(text, mtm) };
unsafe {
label.cell().inspect(|c| {
c.setLineBreakMode(objc2_app_kit::NSLineBreakMode::ByTruncatingTail);
});
cell.addSubview(&label);
cell.setTextField(Some(&label));
}
return objc2::rc::Retained::into_super(objc2::rc::Retained::into_super(cell));
};
let icon = self
.ivars()
.icons
.borrow()
.get(index)
.and_then(std::clone::Clone::clone);
let badge = self
.ivars()
.badges
.borrow()
.get(index)
.and_then(std::clone::Clone::clone);
let badge_icon = self
.ivars()
.badge_icons
.borrow()
.get(index)
.and_then(std::clone::Clone::clone);
let badge_tint = self
.ivars()
.badge_tints
.borrow()
.get(index)
.copied()
.flatten();
let cell = DayNavCell::new(mtm);
let label = unsafe { NSTextField::labelWithString(text, mtm) };
unsafe {
label.cell().inspect(|c| {
c.setLineBreakMode(objc2_app_kit::NSLineBreakMode::ByTruncatingTail);
});
cell.addSubview(&label);
cell.setTextField(Some(&label));
let accessory = objc2_app_kit::NSStackView::new(mtm);
accessory.setTranslatesAutoresizingMaskIntoConstraints(false);
accessory.setOrientation(
objc2_app_kit::NSUserInterfaceLayoutOrientation::Horizontal,
);
accessory.setSpacing(4.0);
let mut has_accessory = false;
let trailing: Retained<NSView> = match &badge {
Some(text) => {
let b = NSTextField::labelWithString(text, mtm);
b.setTranslatesAutoresizingMaskIntoConstraints(false);
b.setTextColor(Some(&objc2_app_kit::NSColor::secondaryLabelColor()));
b.setAlignment(objc2_app_kit::NSTextAlignment::Right);
b.setFont(Some(
&objc2_app_kit::NSFont::monospacedDigitSystemFontOfSize_weight(
objc2_app_kit::NSFont::smallSystemFontSize(),
objc2_app_kit::NSFontWeightRegular,
),
));
b.setContentCompressionResistancePriority_forOrientation(
objc2_app_kit::NSLayoutPriorityRequired,
objc2_app_kit::NSLayoutConstraintOrientation::Horizontal,
);
*cell.ivars().count.borrow_mut() = Some(b.clone());
accessory.addArrangedSubview(&objc2::rc::Retained::into_super(
objc2::rc::Retained::into_super(b),
));
has_accessory = true;
let v: Retained<NSView> =
objc2::rc::Retained::into_super(accessory.clone());
v
}
None => objc2::rc::Retained::into_super(accessory.clone()),
};
if let Some(img) = &badge_icon {
let iv = objc2_app_kit::NSImageView::new(mtm);
iv.setImage(Some(img));
iv.setTranslatesAutoresizingMaskIntoConstraints(false);
if let Some(c) = badge_tint {
iv.setContentTintColor(Some(&color::nsink(c)));
}
iv.setContentCompressionResistancePriority_forOrientation(
objc2_app_kit::NSLayoutPriorityRequired,
objc2_app_kit::NSLayoutConstraintOrientation::Horizontal,
);
accessory.addArrangedSubview(&objc2::rc::Retained::into_super(iv));
has_accessory = true;
}
cell.addSubview(&trailing);
if has_accessory {
*cell.ivars().accessory.borrow_mut() = Some(trailing.clone());
}
objc2_app_kit::NSLayoutConstraint::activateConstraints(
&objc2_foundation::NSArray::from_retained_slice(&[
trailing
.trailingAnchor()
.constraintEqualToAnchor_constant(&cell.trailingAnchor(), -6.0),
trailing
.centerYAnchor()
.constraintEqualToAnchor(&cell.centerYAnchor()),
]),
);
}
if let Some(img) = icon {
let fill = self.ivars().icon_fills.borrow().get(index).copied();
cell.ivars().icon_fill.set(fill.unwrap_or(1.0));
let tint = self.ivars().tints.borrow().get(index).copied().flatten();
let iv = unsafe { objc2_app_kit::NSImageView::new(mtm) };
unsafe {
iv.setImage(Some(&img));
if let Some(t) = tint {
iv.setContentTintColor(Some(&color::nsink(t)));
}
iv.setImageScaling(
objc2_app_kit::NSImageScaling::ScaleProportionallyUpOrDown,
);
cell.addSubview(&iv);
cell.setImageView(Some(&iv));
let progress = self
.ivars()
.icon_progress
.borrow()
.iter()
.find(|(row, _)| *row == index)
.map(|(_, f)| *f);
nav_progress::update(&iv, progress);
}
}
objc2::rc::Retained::into_super(objc2::rc::Retained::into_super(cell))
})
}
#[unsafe(method_id(outlineView:rowViewForItem:))]
fn row_view_for(
&self,
_ov: &objc2_app_kit::NSOutlineView,
_item: &objc2::runtime::AnyObject,
) -> Retained<objc2_app_kit::NSTableRowView> {
let row: Retained<DayNavRowView> =
unsafe { msg_send![DayNavRowView::alloc(self.mtm()), init] };
objc2::rc::Retained::into_super(row)
}
#[unsafe(method(outlineViewSelectionDidChange:))]
fn selection_did_change(&self, notification: &NSNotification) {
ffi_guard::contain((), || {
if self.ivars().suppress.get() {
return;
}
let Some(obj) = (unsafe { notification.object() }) else {
return;
};
let Ok(ov) = obj.downcast::<objc2_app_kit::NSOutlineView>() else {
return;
};
let row = unsafe { ov.selectedRow() };
if let Some(item) = self.item_of_row(row) {
emit(self.ivars().node, Event::SelectionChanged(item as i64));
}
});
}
}
);
thread_local! {
static NAV_ICON_FILLS: RefCell<HashMap<String, f64>> = RefCell::new(HashMap::new());
}
fn nav_icon_fills(
names: &[Option<String>],
images: &[Option<Retained<objc2_app_kit::NSImage>>],
) -> Vec<f64> {
names
.iter()
.zip(images)
.map(|(name, image)| match (name, image) {
(Some(name), Some(image)) => NAV_ICON_FILLS.with(|m| {
*m.borrow_mut()
.entry(name.clone())
.or_insert_with(|| nav_icon_fill(image))
}),
_ => 1.0,
})
.collect()
}
fn nav_icon_fill(image: &objc2_app_kit::NSImage) -> f64 {
const PX: f64 = 64.0;
let Some(rep) = scaled_rep(image, Size::new(PX, PX), 1.0) else {
return 1.0;
};
let (w, h) = (
rep.pixelsWide().max(0) as usize,
rep.pixelsHigh().max(0) as usize,
);
let stride = rep.bytesPerRow().max(0) as usize;
let data = unsafe { rep.bitmapData() };
if data.is_null() || stride < w * 4 {
return 1.0;
}
let bytes = unsafe { std::slice::from_raw_parts(data, stride * h) };
let (mut x0, mut y0, mut x1, mut y1) = (w, h, 0, 0);
for y in 0..h {
for x in 0..w {
if bytes[y * stride + x * 4 + 3] > 24 {
(x0, x1, y0, y1) = (x0.min(x), x1.max(x + 1), y0.min(y), y1.max(y + 1));
}
}
}
if x1 <= x0 || y1 <= y0 {
return 1.0;
}
let size = image.size();
let (iw, ih) = (size.width.max(1.0), size.height.max(1.0));
let (fx, fy) = ((x1 - x0) as f64 / w as f64, (y1 - y0) as f64 / h as f64);
((fx * iw).max(fy * ih) / iw.max(ih)).clamp(0.25, 1.0)
}
fn resolve_nav_icons(icons: &[Option<String>]) -> Vec<Option<Retained<objc2_app_kit::NSImage>>> {
icons
.iter()
.map(|ic| {
use objc2::AllocAnyThread as _;
let name = ic.as_deref()?;
let path = day_spec::resource::resolve_vector_svg(name)
.or_else(|| day_spec::resource::resolve_image_file(name))?;
let img = unsafe {
objc2_app_kit::NSImage::initWithContentsOfFile(
objc2_app_kit::NSImage::alloc(),
&NSString::from_str(&path.to_string_lossy()),
)
}?;
unsafe { img.setTemplate(!std::path::Path::new(name).is_absolute()) };
Some(img)
})
.collect()
}
impl DayNavMenuData {
#[allow(clippy::too_many_arguments)]
fn new(
mtm: MainThreadMarker,
node: NodeId,
items: &[String],
icons: &[Option<String>],
badges: &[Option<String>],
badge_icons: &[Option<String>],
badge_tints: &[Option<day_spec::Ink>],
sections: &[Option<String>],
tints: &[Option<day_spec::Ink>],
menus: &[Vec<day_spec::MenuItem>],
) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(NavMenuIvars {
node,
items: RefCell::new(items.iter().map(|s| NSString::from_str(s)).collect()),
icon_fills: RefCell::new(nav_icon_fills(icons, &resolve_nav_icons(icons))),
icons: RefCell::new(resolve_nav_icons(icons)),
tints: RefCell::new(tints.to_vec()),
menus: RefCell::new(menus.to_vec()),
movable: RefCell::new(Vec::new()),
regroup: std::cell::Cell::new(false),
sorted: RefCell::new(Vec::new()),
badges: RefCell::new(ns_badges(badges)),
icon_progress: RefCell::new(Vec::new()),
badge_icons: RefCell::new(resolve_nav_icons(badge_icons)),
badge_tints: RefCell::new(badge_tints.to_vec()),
rows: RefCell::new(Vec::new()),
row_objects: RefCell::new(Vec::new()),
row_ids: RefCell::new(Vec::new()),
suppress: std::cell::Cell::new(false),
});
let this: Retained<Self> = unsafe { msg_send![super(this), init] };
this.rebuild_rows(items, sections);
this
}
#[allow(clippy::too_many_arguments)]
fn set_items(
&self,
items: &[String],
icons: &[Option<String>],
badges: &[Option<String>],
badge_icons: &[Option<String>],
badge_tints: &[Option<day_spec::Ink>],
sections: &[Option<String>],
tints: &[Option<day_spec::Ink>],
menus: &[Vec<day_spec::MenuItem>],
) {
*self.ivars().items.borrow_mut() = items.iter().map(|s| NSString::from_str(s)).collect();
let resolved = resolve_nav_icons(icons);
*self.ivars().icon_fills.borrow_mut() = nav_icon_fills(icons, &resolved);
*self.ivars().icons.borrow_mut() = resolved;
*self.ivars().tints.borrow_mut() = tints.to_vec();
*self.ivars().menus.borrow_mut() = menus.to_vec();
*self.ivars().badges.borrow_mut() = ns_badges(badges);
self.ivars().icon_progress.borrow_mut().clear();
*self.ivars().badge_icons.borrow_mut() = resolve_nav_icons(badge_icons);
*self.ivars().badge_tints.borrow_mut() = badge_tints.to_vec();
self.rebuild_rows(items, sections);
}
fn rebuild_rows(&self, items: &[String], sections: &[Option<String>]) {
let mut rows = Vec::with_capacity(items.len());
let mut objects = Vec::with_capacity(items.len());
for (i, label) in items.iter().enumerate() {
if let Some(Some(header)) = sections.get(i) {
rows.push(None);
objects.push(NSString::from_str(header));
}
rows.push(Some(i));
objects.push(NSString::from_str(label));
}
let ids = (0..objects.len())
.map(|i| NSNumber::new_isize(i as isize))
.collect();
*self.ivars().rows.borrow_mut() = rows;
*self.ivars().row_objects.borrow_mut() = objects;
*self.ivars().row_ids.borrow_mut() = ids;
}
fn outline_row(item: &objc2::runtime::AnyObject) -> isize {
item.downcast_ref::<NSNumber>()
.map_or(-1, objc2_foundation::NSNumber::integerValue)
}
fn item_of_row(&self, row: isize) -> Option<usize> {
if row < 0 {
return None;
}
self.ivars()
.rows
.borrow()
.get(row as usize)
.copied()
.flatten()
}
fn row_of_item(&self, item: usize) -> Option<usize> {
self.ivars()
.rows
.borrow()
.iter()
.position(|r| *r == Some(item))
}
fn is_movable(&self, item: usize) -> bool {
self.ivars()
.movable
.borrow()
.get(item)
.copied()
.unwrap_or(false)
}
fn nav_drop(
&self,
ov: &objc2_app_kit::NSOutlineView,
info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>,
item: Option<&objc2::runtime::AnyObject>,
child_index: isize,
) -> Option<NavDrop> {
let src = unsafe { info.draggingSource() }?;
if Retained::as_ptr(&src).cast::<std::ffi::c_void>()
!= std::ptr::from_ref(ov).cast::<std::ffi::c_void>()
{
return None;
}
let pb = unsafe { info.draggingPasteboard() };
let from_row = unsafe { pb.stringForType(&NSString::from_str(DAY_NAV_PASTEBOARD_TYPE)) }?
.to_string()
.parse::<usize>()
.ok()?;
let rows = self.ivars().rows.borrow();
let proposed = match item {
Some(item) => Self::outline_row(item),
None => child_index,
};
let proposed = usize::try_from(proposed).unwrap_or(rows.len());
if self.ivars().regroup.get() {
regroup_gap(
&rows,
&self.ivars().movable.borrow(),
&self.ivars().sorted.borrow(),
from_row,
proposed,
item.is_some(),
)
} else {
reorder_gap(
&rows,
&self.ivars().movable.borrow(),
from_row,
proposed,
item.is_some(),
)
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct NavDrop {
gap: usize,
retarget: bool,
moves: Option<NavMove>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum NavMove {
Reorder { from: usize, to: usize },
Regroup {
from: usize,
section: usize,
before: Option<usize>,
},
}
fn reorder_gap(
rows: &[Option<usize>],
movable: &[bool],
from_row: usize,
proposed: usize,
onto: bool,
) -> Option<NavDrop> {
let is_movable = |i: usize| movable.get(i).copied().unwrap_or(false);
let from = (*rows.get(from_row)?).filter(|i| is_movable(*i))?;
let in_run = |r: usize| rows[r].is_some_and(is_movable);
let first = (0..from_row)
.rev()
.take_while(|r| in_run(*r))
.last()
.unwrap_or(from_row);
let last = (from_row + 1..rows.len())
.take_while(|r| in_run(*r))
.last()
.unwrap_or(from_row);
let gap = proposed.clamp(first, last + 1);
let target_row = if gap > from_row { gap - 1 } else { gap };
let moves = (target_row != from_row)
.then(|| rows[target_row].map(|to| NavMove::Reorder { from, to }))
.flatten();
Some(NavDrop {
gap,
retarget: gap != proposed || onto,
moves,
})
}
fn regroup_gap(
rows: &[Option<usize>],
movable: &[bool],
sorted: &[bool],
from_row: usize,
proposed: usize,
onto: bool,
) -> Option<NavDrop> {
let flag = |v: &[bool], i: usize| v.get(i).copied().unwrap_or(false);
let from = (*rows.get(from_row)?).filter(|i| flag(movable, *i))?;
let gap = match rows.get(proposed) {
Some(None) if onto => proposed + 1,
_ => proposed.min(rows.len()),
};
let section_start = |gap: usize| match (0..gap).rev().find(|r| rows[*r].is_none()) {
Some(header) => Some(header + 1),
None => rows.first().copied().flatten().map(|_| 0),
};
let first = section_start(gap)?;
let end = (first..rows.len())
.find(|r| rows[*r].is_none())
.unwrap_or(rows.len());
let section = rows.get(first).copied().flatten()?;
if !rows[first..end]
.iter()
.all(|r| r.is_some_and(|i| flag(movable, i)))
{
return None;
}
let own = section_start(from_row) == Some(first);
if own && flag(sorted, from) {
return None;
}
let moves = (!own || (gap != from_row && gap != from_row + 1)).then(|| NavMove::Regroup {
from,
section,
before: rows.get(gap).copied().flatten(),
});
Some(NavDrop {
gap,
retarget: gap != proposed || onto,
moves,
})
}
#[cfg(test)]
mod nav_gap_tests {
use super::{NavDrop, NavMove, regroup_gap, reorder_gap};
const ROWS: [Option<usize>; 10] = [
Some(0),
None,
Some(1),
Some(2),
Some(3),
None,
Some(4),
Some(5),
None,
Some(6),
];
const MOVABLE: [bool; 7] = [false, true, true, true, true, true, false];
const SORTED: [bool; 7] = [false, true, true, true, true, true, false];
const UNSORTED: [bool; 7] = [false; 7];
fn regroup(from: usize, section: usize, before: Option<usize>) -> NavMove {
NavMove::Regroup {
from,
section,
before,
}
}
#[test]
fn regroup_gaps_name_the_section_above_and_the_row_below() {
let at = |from_row, gap| regroup_gap(&ROWS, &MOVABLE, &SORTED, from_row, gap, false);
assert_eq!(
at(2, 6),
Some(NavDrop {
gap: 6,
retarget: false,
moves: Some(regroup(1, 4, Some(4))),
})
);
assert_eq!(at(6, 5).and_then(|d| d.moves), Some(regroup(4, 1, None)));
assert_eq!(at(2, 7).and_then(|d| d.moves), Some(regroup(1, 4, Some(5))));
assert_eq!(at(2, 8).and_then(|d| d.moves), Some(regroup(1, 4, None)));
}
#[test]
fn regroup_gaps_skip_fixed_sections_fixed_rows_and_a_sorted_rows_own_section() {
let at = |from_row, gap| regroup_gap(&ROWS, &MOVABLE, &SORTED, from_row, gap, false);
for gap in [0, 1, 9, 10] {
assert_eq!(at(2, gap), None, "gap {gap}");
}
for gap in [2, 3, 4, 5] {
assert_eq!(at(2, gap), None, "gap {gap}");
}
assert_eq!(at(0, 6), None);
assert_eq!(at(1, 6), None);
assert_eq!(at(9, 6), None);
}
#[test]
fn regroup_gaps_reorder_an_unsorted_row_in_its_own_section() {
let at = |from_row, gap| regroup_gap(&ROWS, &MOVABLE, &UNSORTED, from_row, gap, false);
assert_eq!(at(2, 4).and_then(|d| d.moves), Some(regroup(1, 1, Some(3))));
assert_eq!(at(2, 5).and_then(|d| d.moves), Some(regroup(1, 1, None)));
for gap in [2, 3] {
let drop = at(2, gap).expect("offered");
assert_eq!(drop.moves, None, "gap {gap}");
}
}
#[test]
fn regroup_drops_onto_a_row_move_to_a_gap() {
assert_eq!(
regroup_gap(&ROWS, &MOVABLE, &SORTED, 2, 5, true),
Some(NavDrop {
gap: 6,
retarget: true,
moves: Some(regroup(1, 4, Some(4))),
})
);
assert_eq!(
regroup_gap(&ROWS, &MOVABLE, &SORTED, 2, 7, true),
Some(NavDrop {
gap: 7,
retarget: true,
moves: Some(regroup(1, 4, Some(5))),
})
);
}
#[test]
fn regroup_offers_nothing_above_a_leading_header() {
let rows = [None, Some(0), None, Some(1)];
let movable = [true, true];
let at = |gap| regroup_gap(&rows, &movable, &[false, false], 1, gap, false);
assert_eq!(at(0), None);
assert_eq!(at(1).map(|d| d.moves), Some(None));
assert_eq!(at(2).map(|d| d.moves), Some(None));
assert_eq!(at(3).and_then(|d| d.moves), Some(regroup(0, 1, Some(1))));
assert_eq!(at(4).and_then(|d| d.moves), Some(regroup(0, 1, None)));
}
#[test]
fn reorder_gaps_clamp_to_the_run() {
assert_eq!(
reorder_gap(&ROWS, &MOVABLE, 2, 9, false),
Some(NavDrop {
gap: 5,
retarget: true,
moves: Some(NavMove::Reorder { from: 1, to: 3 }),
})
);
assert_eq!(
reorder_gap(&ROWS, &MOVABLE, 2, 3, false).map(|d| d.moves),
Some(None)
);
assert_eq!(
reorder_gap(&ROWS, &MOVABLE, 3, 0, false).and_then(|d| d.moves),
Some(NavMove::Reorder { from: 2, to: 1 })
);
assert_eq!(reorder_gap(&ROWS, &MOVABLE, 0, 3, false), None);
}
}
const DAY_NAV_PASTEBOARD_TYPE: &str = "dev.daybrite.day.nav-row";
fn ns_badges(badges: &[Option<String>]) -> Vec<Option<Retained<NSString>>> {
badges
.iter()
.map(|b| b.as_deref().map(NSString::from_str))
.collect()
}
fn post_realize_visible_rows(key: usize) {
<AppKit as Platform>::post(Box::new(move || {
if let Some((table, data)) = list_entry(key) {
data.apply_held(&table);
{
let _pass = RowPass::enter();
unsafe { table.layoutSubtreeIfNeeded() };
let range = unsafe { table.rowsInRect(table.visibleRect()) };
for row in range.location..range.location + range.length {
let cell =
unsafe { table.viewAtColumn_row_makeIfNecessary(0, row as isize, true) };
if let Some(cell) = cell {
let raw = Retained::as_ptr(&cell) as RawHandle;
let skipped = data.ivars().unbound.borrow_mut().remove(&(raw as usize));
if (skipped || cell.subviews().is_empty())
&& let Some(source) = data.ivars().source.borrow().clone()
&& let Some(index) = data.source_row(row as isize)
{
(source.bind_row)(index, raw);
data.note_row_height(&table, &source, row as isize, raw);
}
}
}
}
data.report_viewport(&table);
}
day_core::pump_events();
}));
}
fn post_settle_list_drop(key: usize) {
<AppKit as Platform>::post(Box::new(move || {
day_core::pump_events();
let Some((table, data)) = list_entry(key) else {
return;
};
data.apply_held(&table);
let Some(scroll) = table.enclosingScrollView() else {
return;
};
let clip = scroll.contentView();
let origin = clip.bounds().origin;
let selected = table.selectedRowIndexes();
let suppressed = data.ivars().suppress.replace(true);
table.reloadData();
table.layoutSubtreeIfNeeded();
let restored = objc2_foundation::NSMutableIndexSet::new();
for row in 0..table.numberOfRows().max(0) as usize {
if selected.containsIndex(row) {
restored.addIndex(row);
}
}
table.selectRowIndexes_byExtendingSelection(&restored, false);
let mut bounds = clip.bounds();
bounds.origin = origin;
clip.scrollToPoint(clip.constrainBoundsRect(bounds).origin);
scroll.reflectScrolledClipView(&clip);
data.ivars().suppress.set(suppressed);
post_realize_visible_rows(key);
}));
}
define_class!(
#[unsafe(super(NSTableView))]
#[thread_kind = MainThreadOnly]
#[name = "DayListView"]
struct DayListView;
unsafe impl NSObjectProtocol for DayListView {}
impl DayListView {
#[unsafe(method_id(menuForEvent:))]
fn menu_for_event(
&self,
event: &objc2_app_kit::NSEvent,
) -> Option<Retained<NSMenu>> {
ffi_guard::contain(None, || {
let point = self.convertPoint_fromView(unsafe { event.locationInWindow() }, None);
let row = unsafe { self.rowAtPoint(point) };
let provider = if row >= 0 {
unsafe { self.viewAtColumn_row_makeIfNecessary(0, row, false) }
.and_then(|cell| list_row_menu_provider(&cell))
} else {
None
};
match provider {
Some((view, provider)) => {
let point = view.convertPoint_fromView(
unsafe { event.locationInWindow() }, None,
);
let items = provider(day_spec::Point::new(point.x, point.y));
if items.is_empty() {
None
} else {
Some(build_ns_menu(self.mtm(), "", &items))
}
}
None => unsafe { msg_send![super(self), menuForEvent: event] },
}
})
}
#[unsafe(method(drawGridInClipRect:))]
fn draw_grid_in_clip_rect(&self, clip: NSRect) {
ffi_guard::contain((), || {
let data = unsafe { self.target() }
.and_then(|t| t.downcast::<DayListData>().ok())
.filter(|d| d.ivars().supplements.get());
let Some(data) = data else {
let _: () = unsafe { msg_send![super(self), drawGridInClipRect: clip] };
return;
};
if unsafe { self.gridStyleMask() }.is_empty() {
return;
}
let rows = unsafe { self.numberOfRows() };
let shown = unsafe { self.rowsInRect(clip) };
unsafe { self.gridColor() }.setFill();
for row in shown.location..shown.location + shown.length {
let row = row as isize;
if data.is_supplement(row) || (row + 1 < rows && data.is_supplement(row + 1)) {
continue;
}
let rect = unsafe { self.rectOfRow(row) };
objc2_app_kit::NSBezierPath::fillRect(NSRect::new(
NSPoint::new(clip.origin.x, rect.origin.y + rect.size.height - 1.0),
NSSize::new(clip.size.width, 1.0),
));
}
});
}
#[unsafe(method(becomeFirstResponder))]
fn become_first_responder(&self) -> bool {
let accepted: bool = unsafe { msg_send![super(self), becomeFirstResponder] };
if accepted {
self.report_focus(true);
}
accepted
}
#[unsafe(method(resignFirstResponder))]
fn resign_first_responder(&self) -> bool {
let accepted: bool = unsafe { msg_send![super(self), resignFirstResponder] };
if accepted {
self.report_focus(false);
}
accepted
}
#[unsafe(method(keyDown:))]
fn key_down(&self, event: &objc2_app_kit::NSEvent) {
ffi_guard::contain((), || {
let key = unsafe { event.keyCode() };
if (key == 36 || key == 76) && unsafe { self.doubleAction() }.is_some() {
let row = unsafe { self.selectedRow() };
if row >= 0 && let Some(target) = unsafe { self.target() } {
let _: () = unsafe { msg_send![&*target, activateRow: self] };
return;
}
}
let data = unsafe { self.target() }
.and_then(|t| t.downcast::<DayListData>().ok());
if let Some(data) = &data {
if PAGE_KEYS.contains(&key)
&& let Some(scroll) = self.enclosingScrollView()
{
scroll::stop_clip_animation(&scroll);
}
data.ivars().input.set(true);
}
let _: () = unsafe { msg_send![super(self), keyDown: event] };
if let Some(data) = &data {
data.ivars().input.set(false);
}
});
}
#[unsafe(method(layout))]
fn layout(&self) {
let _pass = RowPass::enter();
let _: () = unsafe { msg_send![super(self), layout] };
}
#[unsafe(method(prepareContentInRect:))]
fn prepare_content_in_rect(&self, rect: NSRect) {
let _pass = RowPass::enter();
let _: () = unsafe { msg_send![super(self), prepareContentInRect: rect] };
}
}
);
const PAGE_KEYS: [u16; 4] = [115, 116, 119, 121];
define_class!(
#[unsafe(super(NSScrollView))]
#[thread_kind = MainThreadOnly]
#[name = "DayListScrollView"]
struct DayListScrollView;
unsafe impl NSObjectProtocol for DayListScrollView {}
impl DayListScrollView {
#[unsafe(method(isCompatibleWithResponsiveScrolling))]
fn compatible_with_responsive_scrolling() -> bool {
true
}
#[unsafe(method(scrollWheel:))]
fn scroll_wheel(&self, event: &NSEvent) {
ffi_guard::contain((), || {
let entry = list_entry(ptr_of(self));
if let Some((table, data)) = &entry {
let phase = unsafe { event.phase() };
let notch = phase.is_empty() && unsafe { event.momentumPhase() }.is_empty();
if notch || phase.contains(objc2_app_kit::NSEventPhase::Began) {
scroll::stop_clip_animation(self);
}
if unsafe { event.scrollingDeltaY() } > 0.0
&& unsafe { self.contentView() }.bounds().origin.y > 0.0
{
data.report_reader_leaving(table);
}
data.ivars().input.set(true);
data.ivars().wheel.set(true);
}
let _: () = unsafe { msg_send![super(self), scrollWheel: event] };
if let Some((_, data)) = &entry {
data.ivars().input.set(false);
data.ivars().wheel.set(false);
}
});
}
}
);
fn list_row_menu_provider(view: &NSView) -> Option<(Retained<NSView>, day_spec::ContextMenuFn)> {
if let Some(provider) =
CTX_MENU_FNS.with(|t| t.get(std::ptr::from_ref::<NSView>(view) as usize))
{
return Some((view.retain(), provider));
}
view.subviews()
.iter()
.find_map(|view| list_row_menu_provider(&view))
}
#[expect(
clippy::option_option,
reason = "`None` defers to AppKit's own menu while `Some(None)` is an owner's deliberate no-menu answer"
)]
fn inherited_context_menu(
view: &NSView,
event: &objc2_app_kit::NSEvent,
) -> Option<Option<Retained<NSMenu>>> {
let mut current = Some(view.retain());
while let Some(view) = current {
if let Some(provider) = CTX_MENU_FNS.with(|t| t.get(Retained::as_ptr(&view) as usize)) {
let point = view.convertPoint_fromView(unsafe { event.locationInWindow() }, None);
let items = provider(day_spec::Point::new(point.x, point.y));
return Some((!items.is_empty()).then(|| build_ns_menu(view.mtm(), "", &items)));
}
current = unsafe { view.superview() };
}
None
}
impl DayListView {
fn report_focus(&self, focused: bool) {
ffi_guard::contain((), || {
if let Some(target) = unsafe { self.target() }
&& let Some(data) = target.downcast_ref::<DayListData>()
{
emit(data.ivars().node, Event::FocusChanged(focused));
}
});
}
}
struct ListIvars {
node: NodeId,
source: RefCell<Option<ListSource>>,
selectable: std::cell::Cell<bool>,
multi: std::cell::Cell<bool>,
suppress: std::cell::Cell<bool>,
tokens: RefCell<Vec<u64>>,
automatic: bool,
sections: bool,
header_estimate: std::cell::Cell<f64>,
heights: RefCell<std::collections::HashMap<u64, f64>>,
cell_tokens: RefCell<std::collections::HashMap<usize, u64>>,
estimate: std::cell::Cell<f64>,
heights_pending: std::cell::Cell<bool>,
supplements: std::cell::Cell<bool>,
unbound: RefCell<std::collections::HashSet<usize>>,
splice: RefCell<Option<SpliceRows>>,
live: std::cell::Cell<bool>,
input: std::cell::Cell<bool>,
wheel: std::cell::Cell<bool>,
held: RefCell<Option<HeldRows>>,
held_posted: std::cell::Cell<bool>,
applying: std::cell::Cell<bool>,
}
thread_local! {
static LIST_ROW_PASS: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
}
struct RowPass;
impl RowPass {
fn enter() -> Self {
LIST_ROW_PASS.with(|depth| depth.set(depth.get() + 1));
Self
}
fn active() -> bool {
LIST_ROW_PASS.with(|depth| depth.get() > 0)
}
}
impl Drop for RowPass {
fn drop(&mut self) {
LIST_ROW_PASS.with(|depth| depth.set(depth.get().saturating_sub(1)));
}
}
struct HeldRows {
deltas: Option<Vec<day_spec::props::RowDelta>>,
selected: Option<Vec<u64>>,
}
struct SpliceRows {
entries: Vec<usize>,
finals: Vec<Option<usize>>,
next: usize,
}
impl SpliceRows {
fn new(shown: usize, deltas: &[day_spec::props::RowDelta]) -> Self {
let mut replay = Self {
entries: (0..shown).collect(),
finals: Vec::new(),
next: shown,
};
for delta in deltas {
replay.apply(*delta);
}
let mut finals = vec![None; replay.next];
for (row, entry) in replay.entries.iter().enumerate() {
finals[*entry] = Some(row);
}
Self {
entries: (0..shown).collect(),
finals,
next: shown,
}
}
fn apply(&mut self, delta: day_spec::props::RowDelta) {
use day_spec::props::RowDelta;
let len = self.entries.len();
match delta {
RowDelta::Insert(i) if i <= len => {
self.entries.insert(i, self.next);
self.next += 1;
}
RowDelta::Remove(i) if i < len => {
self.entries.remove(i);
}
RowDelta::Move(from, to) if from < len && to < len => {
let entry = self.entries.remove(from);
self.entries.insert(to, entry);
}
_ => {}
}
}
fn source_row(&self, row: usize) -> Option<usize> {
self.entries
.get(row)
.and_then(|entry| self.finals.get(*entry).copied().flatten())
}
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DayListData"]
#[ivars = ListIvars]
struct DayListData;
unsafe impl NSObjectProtocol for DayListData {}
unsafe impl NSControlTextEditingDelegate for DayListData {}
impl DayListData {
#[unsafe(method(respondsToSelector:))]
fn responds_to_selector(&self, selector: objc2::runtime::Sel) -> bool {
if selector == objc2::sel!(tableView:heightOfRow:)
&& !self.ivars().automatic
&& !self.ivars().sections
&& !self.ivars().supplements.get()
{
false
} else {
unsafe { msg_send![super(self), respondsToSelector: selector] }
}
}
#[unsafe(method(listViewportChanged:))]
fn list_viewport_changed(&self, notification: &NSNotification) {
ffi_guard::contain((), || {
if let Some(object) = notification.object()
&& let Some(clip) = object.downcast_ref::<objc2_app_kit::NSClipView>()
&& let Some(document) = clip.documentView()
&& let Some(table) = document.downcast_ref::<NSTableView>() {
if &*notification.name()
== unsafe { objc2_app_kit::NSViewBoundsDidChangeNotification }
{
self.note_reader_scroll(table);
}
self.report_viewport(table);
}
});
}
#[unsafe(method(listLiveScrollStarted:))]
fn list_live_scroll_started(&self, notification: &NSNotification) {
ffi_guard::contain((), || {
let Some(object) = notification.object() else { return };
let Some(scroll) = object.downcast_ref::<NSScrollView>() else { return };
scroll::stop_clip_animation(scroll);
let app = NSApplication::sharedApplication(self.mtm());
let held = self.ivars().wheel.get()
|| app.currentEvent().is_some_and(|event| match unsafe { event.r#type() } {
NSEventType::ScrollWheel => {
!unsafe { event.phase() }.is_empty()
|| !unsafe { event.momentumPhase() }.is_empty()
}
NSEventType::LeftMouseDown | NSEventType::LeftMouseDragged => true,
_ => false,
});
if held {
self.ivars().live.set(true);
} else if let Some(document) = unsafe { scroll.documentView() }
&& let Some(table) = document.downcast_ref::<NSTableView>()
{
self.report_reader_scroll(table);
}
});
}
#[unsafe(method(listLiveScrollEnded:))]
fn list_live_scroll_ended(&self, _notification: &NSNotification) {
ffi_guard::contain((), || self.ivars().live.set(false));
}
#[unsafe(method(activateRow:))]
fn activate_row(&self, table: &NSTableView) {
ffi_guard::contain((), || {
let row = unsafe { table.selectedRow() };
if row >= 0 { emit(self.ivars().node, Event::ListActivated(row as usize)); }
});
}
#[unsafe(method(doubleClickRow:))]
fn double_click_row(&self, table: &NSTableView) {
ffi_guard::contain((), || {
let row = unsafe { table.clickedRow() };
if row >= 0 { emit(self.ivars().node, Event::ListActivated(row as usize)); }
});
}
}
unsafe impl NSTableViewDataSource for DayListData {
#[unsafe(method(numberOfRowsInTableView:))]
fn number_of_rows(&self, _tv: &NSTableView) -> isize {
ffi_guard::contain(0, || {
if let Some(splice) = self.ivars().splice.borrow().as_ref() {
return splice.entries.len() as isize;
}
self.ivars()
.source
.borrow()
.as_ref()
.map_or(0, |s| (s.len)() as isize)
})
}
#[unsafe(method_id(tableView:pasteboardWriterForRow:))]
fn pasteboard_writer_for_row(
&self,
_tv: &NSTableView,
row: isize,
) -> Option<Retained<ProtocolObject<dyn objc2_app_kit::NSPasteboardWriting>>> {
let make = || {
let reorderable = self
.ivars()
.source
.borrow()
.as_ref()
.is_some_and(|s| s.reorder.is_some());
if !reorderable || row < 0 || self.is_header(row) || self.is_supplement(row) {
return None;
}
let item = objc2_app_kit::NSPasteboardItem::new();
unsafe {
item.setString_forType(
&NSString::from_str(&row.to_string()),
&NSString::from_str(DAY_ROW_PASTEBOARD_TYPE),
);
}
Some(ProtocolObject::from_retained(item))
};
ffi_guard::contain(None, make)
}
#[unsafe(method(tableView:validateDrop:proposedRow:proposedDropOperation:))]
fn validate_drop(
&self,
tv: &NSTableView,
info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>,
row: isize,
_op: objc2_app_kit::NSTableViewDropOperation,
) -> objc2_app_kit::NSDragOperation {
ffi_guard::contain(objc2_app_kit::NSDragOperation::None, || {
let Some((from, len)) = self.drag_context(tv, info) else {
return objc2_app_kit::NSDragOperation::None;
};
let ins = row.clamp(0, len as isize) as usize;
let to = insertion_to_target(from, ins, len);
let accepted = self.can_move(from, to);
if accepted < 0 {
return objc2_app_kit::NSDragOperation::None;
}
let accepted = (accepted as usize).min(len.saturating_sub(1));
if accepted != to {
unsafe {
tv.setDropRow_dropOperation(
target_to_insertion(from, accepted) as isize,
objc2_app_kit::NSTableViewDropOperation::Above,
);
}
}
objc2_app_kit::NSDragOperation::Move
})
}
#[unsafe(method(tableView:acceptDrop:row:dropOperation:))]
fn accept_drop(
&self,
tv: &NSTableView,
info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>,
row: isize,
_op: objc2_app_kit::NSTableViewDropOperation,
) -> bool {
let drop = || {
let Some((from, len)) = self.drag_context(tv, info) else {
return false;
};
let ins = row.clamp(0, len as isize) as usize;
let to = insertion_to_target(from, ins, len);
let accepted = self.can_move(from, to);
if accepted < 0 {
return false;
}
let accepted = (accepted as usize).min(len.saturating_sub(1));
if accepted == from {
return true; }
let mv = self
.ivars()
.source
.borrow()
.as_ref()
.and_then(|s| s.reorder.as_ref().map(|r| r.move_row.clone()));
let Some(mv) = mv else { return false };
mv(from, accepted);
{
let mut toks = self.ivars().tokens.borrow_mut();
if from < toks.len() && accepted < toks.len() {
let t = toks.remove(from);
toks.insert(accepted, t);
}
}
unsafe { tv.moveRowAtIndex_toIndex(from as isize, accepted as isize) };
if let Some(scroll) = tv.enclosingScrollView() {
post_settle_list_drop(ptr_of(&scroll));
}
true
};
ffi_guard::contain(false, drop)
}
}
unsafe impl NSTableViewDelegate for DayListData {
#[unsafe(method(tableView:heightOfRow:))]
fn height_of_row(&self, tv: &NSTableView, row: isize) -> f64 {
ffi_guard::contain(44.0, || {
let ivars = self.ivars();
let header = self.is_header(row);
let supplement = self.is_supplement(row);
if !header && !supplement && !ivars.automatic {
return unsafe { tv.rowHeight() };
}
let token = self.source_row(row).and_then(|row| {
ivars
.source
.borrow()
.as_ref()
.filter(|s| row < (s.len)())
.map(|s| (s.token_at)(row))
});
token
.and_then(|t| ivars.heights.borrow().get(&t).copied())
.unwrap_or_else(|| {
if header {
ivars.header_estimate.get()
} else if supplement && !ivars.automatic {
unsafe { tv.rowHeight() }
} else {
ivars.estimate.get()
}
})
})
}
#[unsafe(method(tableView:isGroupRow:))]
fn is_group_row(&self, _tv: &NSTableView, row: isize) -> bool {
ffi_guard::contain(false, || self.is_header(row))
}
#[unsafe(method(tableView:shouldSelectRow:))]
fn should_select_row(&self, _tv: &NSTableView, row: isize) -> bool {
ffi_guard::contain(true, || !self.is_header(row) && !self.is_supplement(row))
}
#[unsafe(method_id(tableView:viewForTableColumn:row:))]
fn view_for_row(
&self,
tv: &NSTableView,
_col: Option<&NSTableColumn>,
row: isize,
) -> Option<Retained<NSView>> {
ffi_guard::contain(None, || {
let _pass = RowPass::enter();
let mtm = self.mtm();
let ident = NSString::from_str(if self.is_header(row) {
DAY_HEADER_CELL
} else if self.is_supplement(row) {
DAY_EXTRA_CELL
} else {
DAY_ROW_CELL
});
let cell: Retained<NSView> =
unsafe { tv.makeViewWithIdentifier_owner(&ident, None) }.unwrap_or_else(|| {
let v: Retained<NSView> = Retained::into_super(DayFlipped::new(mtm));
unsafe { v.setIdentifier(Some(&ident)) };
v
});
let source = self.ivars().source.borrow().clone();
if let Some(source) = source
&& let Some(index) = self.source_row(row)
{
let raw = Retained::as_ptr(&cell) as RawHandle;
let busy = day_core::try_with_tree(|_| ()).is_none();
(source.bind_row)(index, raw);
if busy {
self.ivars().unbound.borrow_mut().insert(raw as usize);
} else {
self.ivars().unbound.borrow_mut().remove(&(raw as usize));
self.note_row_height(tv, &source, row, raw);
}
}
Some(cell)
})
}
#[unsafe(method(tableView:didRemoveRowView:forRow:))]
fn did_remove_row_view(
&self,
_tv: &NSTableView,
row_view: &objc2_app_kit::NSTableRowView,
_row: isize,
) {
ffi_guard::contain((), || {
let Some(cell) = row_view.subviews().iter().find(|sub| {
sub.identifier()
.is_some_and(|i| {
let i = i.to_string();
i == DAY_ROW_CELL || i == DAY_HEADER_CELL || i == DAY_EXTRA_CELL
})
}) else {
return;
};
let cell = Retained::into_raw(cell) as usize;
let Some(this) = (unsafe {
Retained::retain(std::ptr::from_ref::<DayListData>(self).cast_mut())
}) else {
return;
};
let this = Retained::into_raw(this) as usize;
<AppKit as Platform>::post(Box::new(move || {
let this = unsafe { Retained::from_raw(this as *mut DayListData) };
let cell = unsafe { Retained::from_raw(cell as *mut NSView) };
let (Some(this), Some(cell)) = (this, cell) else {
return;
};
if cell.window().is_some() {
return; }
let recycle = this
.ivars()
.source
.borrow()
.as_ref()
.map(|s| s.recycle.clone());
if let Some(recycle) = recycle {
recycle(Retained::as_ptr(&cell) as RawHandle);
}
}));
});
}
#[unsafe(method(tableViewSelectionDidChange:))]
fn selection_did_change(&self, notification: &NSNotification) {
ffi_guard::contain((), || {
if self.ivars().suppress.get() || !self.ivars().selectable.get() {
return;
}
let Some(obj) = (unsafe { notification.object() }) else {
return;
};
let Ok(tv) = obj.downcast::<NSTableView>() else {
return;
};
if self.ivars().multi.get() {
let idx = unsafe { tv.selectedRowIndexes() };
let mut rows = Vec::with_capacity(idx.count());
let mut i = idx.firstIndex();
while i != objc2_foundation::NSNotFound as usize {
rows.push(i as i64);
i = unsafe { idx.indexGreaterThanIndex(i) };
}
emit(self.ivars().node, Event::SelectionSet(rows));
} else {
let row = unsafe { tv.selectedRow() };
if row >= 0 {
emit(self.ivars().node, Event::SelectionChanged(row as i64));
} else {
emit(self.ivars().node, Event::SelectionSet(Vec::new()));
}
}
});
}
#[unsafe(method_id(tableView:rowActionsForRow:edge:))]
fn row_actions_for_row(
&self,
tv: &NSTableView,
row: isize,
edge: objc2_app_kit::NSTableRowActionEdge,
) -> Retained<NSArray<objc2_app_kit::NSTableViewRowAction>> {
ffi_guard::contain(NSArray::new(), || {
let sw = self
.ivars()
.source
.borrow()
.as_ref()
.and_then(|s| s.swipe.clone());
let Some(sw) = sw else {
return NSArray::new();
};
if row < 0 {
return NSArray::new();
}
let day_edge = if edge == objc2_app_kit::NSTableRowActionEdge::Leading {
day_spec::SwipeEdge::Leading
} else {
day_spec::SwipeEdge::Trailing
};
let offer = (sw.actions_at)(row as usize, day_edge);
let actions: Vec<Retained<objc2_app_kit::NSTableViewRowAction>> = offer
.iter()
.enumerate()
.map(|(i, a)| {
let style = if a.destructive {
objc2_app_kit::NSTableViewRowActionStyle::Destructive
} else {
objc2_app_kit::NSTableViewRowActionStyle::Regular
};
let perform = sw.perform.clone();
let table = tv.retain();
let handler = block2::RcBlock::new(
move |_action: std::ptr::NonNull<
objc2_app_kit::NSTableViewRowAction,
>,
row: isize| {
ffi_guard::contain((), || {
perform(row as usize, day_edge, i);
unsafe { table.setRowActionsVisible(false) };
});
},
);
let action =
objc2_app_kit::NSTableViewRowAction::rowActionWithStyle_title_handler(
style,
&NSString::from_str(&a.label),
&handler,
);
if let Some(t) = a.tint {
unsafe { action.setBackgroundColor(Some(&color::nsink(t))) };
}
if let Some(sym) = a.symbol {
let img = objc2_app_kit::NSImage::imageWithSystemSymbolName_accessibilityDescription(
&NSString::from_str(day_spec::sf_symbol_name(sym)),
None,
);
if let Some(img) = img {
unsafe { action.setImage(Some(&img)) };
}
}
action
})
.collect();
NSArray::from_retained_slice(&actions)
})
}
}
);
impl DayListData {
fn new(
mtm: MainThreadMarker,
node: NodeId,
selectable: bool,
multi: bool,
automatic: bool,
sections: bool,
) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(ListIvars {
node,
source: RefCell::new(None),
selectable: std::cell::Cell::new(selectable),
multi: std::cell::Cell::new(multi),
suppress: std::cell::Cell::new(false),
tokens: RefCell::new(Vec::new()),
automatic,
sections,
header_estimate: std::cell::Cell::new(28.0),
heights: RefCell::new(std::collections::HashMap::new()),
cell_tokens: RefCell::new(std::collections::HashMap::new()),
estimate: std::cell::Cell::new(44.0),
heights_pending: std::cell::Cell::new(false),
supplements: std::cell::Cell::new(false),
unbound: RefCell::new(std::collections::HashSet::new()),
splice: RefCell::new(None),
live: std::cell::Cell::new(false),
input: std::cell::Cell::new(false),
wheel: std::cell::Cell::new(false),
held: RefCell::new(None),
held_posted: std::cell::Cell::new(false),
applying: std::cell::Cell::new(false),
});
unsafe { msg_send![super(this), init] }
}
fn note_row_height(
&self,
table: &NSTableView,
source: &ListSource,
row: isize,
cell: RawHandle,
) {
let header = self.is_header(row);
let supplement = self.is_supplement(row);
if !self.ivars().automatic && !header && !supplement {
return;
}
let height = (source.cell_height)(cell);
let Some(index) = self.source_row(row).filter(|_| height > 0.0) else {
return;
};
let token = (source.token_at)(index);
self.ivars()
.cell_tokens
.borrow_mut()
.insert(cell as usize, token);
self.ivars().heights.borrow_mut().insert(token, height);
if header {
self.ivars().header_estimate.set(height);
} else if !supplement {
self.ivars().estimate.set(height);
}
let drawn = unsafe { table.rectOfRow(row) }.size.height;
if (drawn - height).abs() > 0.5 {
self.post_row_heights(table);
}
}
fn source_row(&self, row: isize) -> Option<usize> {
let row = usize::try_from(row).ok()?;
match self.ivars().splice.borrow().as_ref() {
Some(splice) => splice.source_row(row),
None => Some(row),
}
}
fn is_header(&self, row: isize) -> bool {
self.source_row(row).is_some_and(|row| {
self.ivars()
.source
.borrow()
.as_ref()
.and_then(|s| s.is_header.clone())
.is_some_and(|is_header| is_header(row))
})
}
fn splice_fits(&self, table: &NSTableView, deltas: &[day_spec::props::RowDelta]) -> bool {
let shown = unsafe { table.numberOfRows() }.max(0) as usize;
let now = self
.ivars()
.source
.borrow()
.as_ref()
.map_or(0, |s| (s.len)());
splice_fits(shown, deltas, now)
}
fn reload_keeping_offset(&self, table: &NSTableView) {
let selected = unsafe { table.selectedRowIndexes() };
let tokens: Vec<u64> = self
.ivars()
.tokens
.borrow()
.iter()
.enumerate()
.filter(|(row, _)| selected.containsIndex(*row))
.map(|(_, token)| *token)
.collect();
let scroll = table.enclosingScrollView();
let origin = scroll
.as_ref()
.map(|s| unsafe { s.contentView() }.bounds().origin);
self.ivars().suppress.set(true);
unsafe { table.reloadData() };
if let Some(source) = self.ivars().source.borrow().as_ref() {
let n = (source.len)();
*self.ivars().tokens.borrow_mut() = (0..n).map(|i| (source.token_at)(i)).collect();
}
let restored = objc2_foundation::NSMutableIndexSet::new();
for (row, token) in self.ivars().tokens.borrow().iter().enumerate() {
if tokens.contains(token) {
restored.addIndex(row);
}
}
unsafe { table.selectRowIndexes_byExtendingSelection(&restored, false) };
self.ivars().suppress.set(false);
if let (Some(scroll), Some(origin)) = (scroll, origin) {
let clip = unsafe { scroll.contentView() };
if clip.bounds().origin != origin {
scroll::scroll_clip_to(&scroll, origin, false, None);
}
}
}
fn change_rows(
&self,
key: usize,
table: &NSTableView,
deltas: Option<&[day_spec::props::RowDelta]>,
) {
let applying = self.ivars().applying.get();
if !RowPass::active() && !applying {
match deltas {
Some(deltas) => self.splice_rows(table, deltas),
None => self.reload_rows(table),
}
return;
}
{
let mut held = self.ivars().held.borrow_mut();
let held = held.get_or_insert_with(|| HeldRows {
deltas: Some(Vec::new()),
selected: None,
});
match (deltas, held.deltas.as_mut()) {
(Some(deltas), Some(all)) => all.extend_from_slice(deltas),
_ => held.deltas = None,
}
}
if !applying {
self.map_held(table);
}
self.post_held(key);
}
fn select(&self, table: &NSTableView, rows: &[usize]) {
if self.ivars().held.borrow().is_none() {
self.select_rows(table, rows);
return;
}
let tokens: Vec<u64> =
self.ivars()
.source
.borrow()
.as_ref()
.map_or_else(Vec::new, |source| {
let n = (source.len)();
rows.iter()
.filter(|row| **row < n)
.map(|row| (source.token_at)(*row))
.collect()
});
if let Some(held) = self.ivars().held.borrow_mut().as_mut() {
held.selected = Some(tokens);
}
}
fn map_held(&self, table: &NSTableView) {
let shown = unsafe { table.numberOfRows() }.max(0) as usize;
let map = self
.ivars()
.held
.borrow()
.as_ref()
.and_then(|held| held.deltas.as_ref())
.map(|deltas| SpliceRows::new(shown, deltas));
*self.ivars().splice.borrow_mut() = map;
}
fn post_held(&self, key: usize) {
if self.ivars().held_posted.replace(true) {
return;
}
<AppKit as Platform>::post(Box::new(move || {
let Some((table, data)) = list_entry(key) else {
return;
};
data.ivars().held_posted.set(false);
if RowPass::active() || data.ivars().applying.get() {
data.post_held(key);
} else {
data.apply_held(&table);
}
}));
}
fn apply_held(&self, table: &NSTableView) {
if RowPass::active() || self.ivars().applying.get() {
return;
}
let Some(held) = self.ivars().held.borrow_mut().take() else {
return;
};
self.ivars().splice.borrow_mut().take();
match &held.deltas {
Some(deltas) => self.splice_rows(table, deltas),
None => self.reload_rows(table),
}
if let Some(tokens) = held.selected {
let rows: Vec<usize> =
self.ivars()
.source
.borrow()
.as_ref()
.map_or_else(Vec::new, |source| {
(0..(source.len)())
.filter(|row| tokens.contains(&(source.token_at)(*row)))
.collect()
});
self.select_rows(table, &rows);
}
if let Some(scroll) = table.enclosingScrollView() {
post_realize_visible_rows(ptr_of(&scroll));
}
}
fn splice_rows(&self, table: &NSTableView, deltas: &[day_spec::props::RowDelta]) {
use objc2_app_kit::NSTableViewAnimationOptions;
if !self.splice_fits(table, deltas) {
log::warn!(
"day-toolkit-appkit: a list splice of {} rows does not fit the {} rows shown; reloading",
deltas.len(),
unsafe { table.numberOfRows() }
);
let applying = self.ivars().applying.replace(true);
self.reload_keeping_offset(table);
self.ivars().applying.set(applying);
self.map_held(table);
return;
}
let quiet = deltas.len() > SPLICE_ANIMATED_MAX;
let (insert, remove) = if quiet {
(
NSTableViewAnimationOptions::EffectNone,
NSTableViewAnimationOptions::EffectNone,
)
} else {
(
NSTableViewAnimationOptions::SlideDown,
NSTableViewAnimationOptions::SlideUp,
)
};
if quiet {
NSAnimationContext::beginGrouping();
NSAnimationContext::currentContext().setDuration(0.0);
}
let shown = unsafe { table.numberOfRows() }.max(0) as usize;
*self.ivars().splice.borrow_mut() = Some(SpliceRows::new(shown, deltas));
{
let applying = self.ivars().applying.replace(true);
let _done = std::mem::DropGuard::new((), |()| {
self.ivars().applying.set(applying);
});
for d in deltas {
if let Some(splice) = self.ivars().splice.borrow_mut().as_mut() {
splice.apply(*d);
}
unsafe {
table.beginUpdates();
match d {
day_spec::props::RowDelta::Insert(i) => table
.insertRowsAtIndexes_withAnimation(
&objc2_foundation::NSIndexSet::indexSetWithIndex(*i),
insert,
),
day_spec::props::RowDelta::Remove(i) => table
.removeRowsAtIndexes_withAnimation(
&objc2_foundation::NSIndexSet::indexSetWithIndex(*i),
remove,
),
day_spec::props::RowDelta::Move(from, to) => {
table.moveRowAtIndex_toIndex(*from as isize, *to as isize);
}
}
table.endUpdates();
}
}
}
self.ivars().splice.borrow_mut().take();
if quiet {
NSAnimationContext::endGrouping();
}
if let Some(source) = self.ivars().source.borrow().as_ref() {
let n = (source.len)();
*self.ivars().tokens.borrow_mut() = (0..n).map(|i| (source.token_at)(i)).collect();
}
self.map_held(table);
}
fn reload_rows(&self, table: &NSTableView) {
let selected = unsafe { table.selectedRowIndexes() };
let tokens: Vec<u64> = self
.ivars()
.tokens
.borrow()
.iter()
.enumerate()
.filter(|(row, _)| selected.containsIndex(*row))
.map(|(_, token)| *token)
.collect();
self.ivars().suppress.set(true);
{
let applying = self.ivars().applying.replace(true);
let _done = std::mem::DropGuard::new((), |()| {
self.ivars().applying.set(applying);
});
if let Some(moves) = self.permutation_moves(table) {
unsafe {
table.beginUpdates();
for (from, to) in moves {
table.moveRowAtIndex_toIndex(from as isize, to as isize);
}
table.endUpdates();
}
} else {
unsafe { table.reloadData() };
}
}
let restored = objc2_foundation::NSMutableIndexSet::new();
for (row, token) in self.ivars().tokens.borrow().iter().enumerate() {
if tokens.contains(token) {
restored.addIndex(row);
}
}
unsafe { table.selectRowIndexes_byExtendingSelection(&restored, false) };
self.ivars().suppress.set(false);
self.map_held(table);
}
fn select_rows(&self, table: &NSTableView, rows: &[usize]) {
self.ivars().suppress.set(true);
unsafe {
if rows.is_empty() {
table.deselectAll(None);
} else {
let set = objc2_foundation::NSMutableIndexSet::new();
let count = table.numberOfRows().max(0) as usize;
for r in rows.iter().copied().filter(|r| *r < count) {
set.addIndex(r);
}
table.selectRowIndexes_byExtendingSelection(&set, false);
}
}
self.ivars().suppress.set(false);
}
fn is_supplement(&self, row: isize) -> bool {
self.source_row(row).is_some_and(|row| {
self.ivars()
.source
.borrow()
.as_ref()
.and_then(|s| s.is_supplement.clone())
.is_some_and(|is_supplement| is_supplement(row))
})
}
fn report_viewport(&self, table: &NSTableView) {
let Some(source) = self.ivars().source.borrow().clone() else {
return;
};
let n = (source.len)();
if n == 0 || unsafe { table.numberOfRows() } != n as isize {
return;
}
let visible = table.visibleRect();
let shown = unsafe { table.rowsInRect(visible) };
if let Some(report) = &source.viewport {
let top = visible.origin.y;
let bottom = top + visible.size.height;
let count = shown.length.min(n.saturating_sub(shown.location));
let span = scroll::visible_span(top, bottom, shown.location, count, |row| {
let rect = unsafe { table.rectOfRow(row as isize) };
(rect.origin.y, rect.origin.y + rect.size.height)
});
let Some((first, last)) = span else {
return;
};
let end = unsafe { table.rectOfRow(n as isize - 1) };
report(day_spec::ListViewport {
first,
last,
end_distance: (end.origin.y + end.size.height - bottom).max(0.0),
});
} else if let Some(report) = &source.first_visible {
report(shown.location.min(n - 1));
}
}
fn note_reader_scroll(&self, table: &NSTableView) {
let ivars = self.ivars();
if ivars.live.get() || ivars.input.get() {
self.report_reader_scroll(table);
}
}
fn report_reader_scroll(&self, table: &NSTableView) {
let rows = unsafe { table.numberOfRows() };
if rows <= 0 {
return;
}
let end = unsafe { table.rectOfRow(rows - 1) };
let visible = table.visibleRect();
if end.origin.y + end.size.height - (visible.origin.y + visible.size.height) > 0.0 {
self.report_reader_leaving(table);
}
}
fn report_reader_leaving(&self, table: &NSTableView) {
let report = self
.ivars()
.source
.borrow()
.as_ref()
.and_then(|s| s.reader_scroll.clone());
if let Some(report) = report
&& unsafe { table.numberOfRows() } > 0
{
report();
}
}
fn row_resized(&self, table: &NSTableView, cell: usize, height: f64) {
let token = self.ivars().cell_tokens.borrow().get(&cell).copied();
if let Some(token) = token {
self.ivars().heights.borrow_mut().insert(token, height);
self.post_row_heights(table);
}
}
fn post_row_heights(&self, table: &NSTableView) {
if self.ivars().heights_pending.replace(true) {
return;
}
let Some(this) = (unsafe { Retained::retain(std::ptr::from_ref::<Self>(self).cast_mut()) })
else {
return;
};
let this = Retained::into_raw(this) as usize;
let table = Retained::into_raw(table.retain()) as usize;
<AppKit as Platform>::post(Box::new(move || {
let this = unsafe { Retained::from_raw(this as *mut DayListData) };
let table = unsafe { Retained::from_raw(table as *mut NSTableView) };
let (Some(this), Some(table)) = (this, table) else {
return;
};
this.ivars().heights_pending.set(false);
ffi_guard::contain((), || this.apply_row_heights(&table));
}));
}
fn apply_row_heights(&self, table: &NSTableView) {
self.apply_held(table);
let Some(source) = self.ivars().source.borrow().clone() else {
return;
};
let count = unsafe { table.numberOfRows() }.max(0) as usize;
let shown = unsafe { table.rowsInRect(table.visibleRect()) };
let first = shown.location.saturating_sub(8);
let last = (shown.location + shown.length + 8)
.min(count)
.min((source.len)());
let changed = objc2_foundation::NSMutableIndexSet::new();
{
let heights = self.ivars().heights.borrow();
for row in first..last {
if let Some(&height) = heights.get(&(source.token_at)(row)) {
let drawn = unsafe { table.rectOfRow(row as isize) }.size.height;
if (drawn - height).abs() > 0.5 {
unsafe { changed.addIndex(row) };
}
}
}
}
if changed.count() == 0 {
return;
}
NSAnimationContext::beginGrouping();
NSAnimationContext::currentContext().setDuration(0.0);
unsafe { table.noteHeightOfRowsWithIndexesChanged(&changed) };
NSAnimationContext::endGrouping();
self.report_viewport(table);
}
fn permutation_moves(&self, table: &NSTableView) -> Option<Vec<(usize, usize)>> {
let source = self.ivars().source.borrow();
let source = source.as_ref()?;
let n = (source.len)();
let new: Vec<u64> = (0..n).map(|i| (source.token_at)(i)).collect();
let old = std::mem::replace(&mut *self.ivars().tokens.borrow_mut(), new.clone());
if old.len() != n || n == 0 || old == new {
return None;
}
let (mut so, mut sn) = (old.clone(), new.clone());
so.sort_unstable();
sn.sort_unstable();
if so != sn {
return None;
}
for i in 0..n {
if old[i] != new[i]
&& unsafe { table.rowViewAtRow_makeIfNecessary(i as isize, false) }.is_none()
{
return None;
}
}
let mut work = old;
let mut moves = Vec::new();
for i in 0..n {
if work[i] == new[i] {
continue;
}
let j = (i + 1..n).find(|&j| work[j] == new[i])?;
let t = work.remove(j);
work.insert(i, t);
moves.push((j, i));
}
Some(moves)
}
fn drag_context(
&self,
tv: &NSTableView,
info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>,
) -> Option<(usize, usize)> {
let src = unsafe { info.draggingSource() }?;
if Retained::as_ptr(&src).cast::<std::ffi::c_void>()
!= std::ptr::from_ref::<NSTableView>(tv).cast::<std::ffi::c_void>()
{
return None;
}
let pb = unsafe { info.draggingPasteboard() };
let from = unsafe { pb.stringForType(&NSString::from_str(DAY_ROW_PASTEBOARD_TYPE)) }?
.to_string()
.parse::<usize>()
.ok()?;
let len = self.ivars().source.borrow().as_ref().map(|s| (s.len)())?;
(from < len).then_some((from, len))
}
fn can_move(&self, from: usize, to: usize) -> i64 {
self.ivars()
.source
.borrow()
.as_ref()
.and_then(|s| s.reorder.as_ref().map(|r| (r.can_move)(from, to)))
.unwrap_or(-1)
}
}
const DAY_ROW_CELL: &str = "day.cell";
const DAY_HEADER_CELL: &str = "day.header";
const DAY_EXTRA_CELL: &str = "day.extra";
const DAY_ROW_PASTEBOARD_TYPE: &str = "dev.daybrite.day.row";
const SPLICE_ANIMATED_MAX: usize = 64;
fn insertion_to_target(from: usize, ins: usize, len: usize) -> usize {
let to = if ins > from { ins - 1 } else { ins };
to.min(len.saturating_sub(1))
}
fn splice_fits(shown: usize, deltas: &[day_spec::props::RowDelta], now: usize) -> bool {
use day_spec::props::RowDelta;
let mut count = shown;
for delta in deltas {
match *delta {
RowDelta::Insert(i) if i <= count => count += 1,
RowDelta::Remove(i) if i < count => count -= 1,
RowDelta::Move(from, to) if from < count && to < count => {}
_ => return false,
}
}
count == now
}
fn target_to_insertion(from: usize, to: usize) -> usize {
if to > from { to + 1 } else { to }
}
type ListEntry = (Retained<NSTableView>, Retained<DayListData>);
fn list_entry(key: usize) -> Option<ListEntry> {
LIST_STATE.with(|m| m.borrow().get(&key).cloned())
}
struct TreeIvars {
node: NodeId,
source: RefCell<Option<TreeSource>>,
selectable: std::cell::Cell<bool>,
suppress: std::cell::Cell<bool>,
items: RefCell<HashMap<u64, Retained<NSNumber>>>,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DayTreeData"]
#[ivars = TreeIvars]
struct DayTreeData;
unsafe impl NSObjectProtocol for DayTreeData {}
unsafe impl NSOutlineViewDataSource for DayTreeData {
#[unsafe(method(outlineView:numberOfChildrenOfItem:))]
fn number_of_children(
&self,
_ov: &objc2_app_kit::NSOutlineView,
item: Option<&objc2::runtime::AnyObject>,
) -> isize {
ffi_guard::contain(0, || {
let parent = item.and_then(Self::token_of_item);
if item.is_some() && parent.is_none() {
return 0; }
self.ivars()
.source
.borrow()
.as_ref()
.map_or(0, |s| (s.children_len)(parent) as isize)
})
}
#[unsafe(method_id(outlineView:child:ofItem:))]
fn child_of_item(
&self,
_ov: &objc2_app_kit::NSOutlineView,
index: isize,
item: Option<&objc2::runtime::AnyObject>,
) -> Retained<objc2::runtime::AnyObject> {
let token = ffi_guard::contain(0, || {
let parent = item.and_then(Self::token_of_item);
self.ivars()
.source
.borrow()
.as_ref()
.map_or(0, |s| (s.child_token)(parent, index.max(0) as usize))
});
let ns = self.intern(token);
unsafe { objc2::rc::Retained::cast_unchecked(ns) }
}
#[unsafe(method(outlineView:isItemExpandable:))]
fn is_expandable(
&self,
_ov: &objc2_app_kit::NSOutlineView,
item: &objc2::runtime::AnyObject,
) -> bool {
ffi_guard::contain(false, || {
let Some(token) = Self::token_of_item(item) else {
return false;
};
self.ivars()
.source
.borrow()
.as_ref()
.is_some_and(|s| (s.expandable)(token))
})
}
#[unsafe(method_id(outlineView:pasteboardWriterForItem:))]
fn pasteboard_writer_for_item(
&self,
_ov: &objc2_app_kit::NSOutlineView,
item: &objc2::runtime::AnyObject,
) -> Option<Retained<ProtocolObject<dyn objc2_app_kit::NSPasteboardWriting>>> {
let make = || {
let movable = self
.ivars()
.source
.borrow()
.as_ref()
.is_some_and(|s| s.moves.is_some());
let token = Self::token_of_item(item)?;
if !movable {
return None;
}
let pbitem = objc2_app_kit::NSPasteboardItem::new();
unsafe {
pbitem.setString_forType(
&NSString::from_str(&token.to_string()),
&NSString::from_str(DAY_TREE_PASTEBOARD_TYPE),
);
}
Some(ProtocolObject::from_retained(pbitem))
};
ffi_guard::contain(None, make)
}
#[unsafe(method(outlineView:validateDrop:proposedItem:proposedChildIndex:))]
fn validate_drop(
&self,
ov: &objc2_app_kit::NSOutlineView,
info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>,
item: Option<&objc2::runtime::AnyObject>,
child_index: isize,
) -> objc2_app_kit::NSDragOperation {
ffi_guard::contain(
objc2_app_kit::NSDragOperation::None,
|| match Self::drop_proposal(ov, info, item, child_index) {
Some((token, parent, index)) if self.can_move(token, parent, index) => {
objc2_app_kit::NSDragOperation::Move
}
_ => objc2_app_kit::NSDragOperation::None,
},
)
}
#[unsafe(method(outlineView:acceptDrop:item:childIndex:))]
fn accept_drop(
&self,
ov: &objc2_app_kit::NSOutlineView,
info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>,
item: Option<&objc2::runtime::AnyObject>,
child_index: isize,
) -> bool {
let drop = || {
let Some((token, parent, index)) = Self::drop_proposal(ov, info, item, child_index)
else {
return false;
};
if !self.can_move(token, parent, index) {
return false;
}
let mv = self
.ivars()
.source
.borrow()
.as_ref()
.and_then(|s| s.moves.as_ref().map(|m| m.move_node.clone()));
let Some(mv) = mv else { return false };
mv(token, parent, index);
true
};
ffi_guard::contain(false, drop)
}
}
unsafe impl NSControlTextEditingDelegate for DayTreeData {}
unsafe impl NSOutlineViewDelegate for DayTreeData {
#[unsafe(method(outlineView:isGroupItem:))]
fn is_group_item(
&self,
_ov: &objc2_app_kit::NSOutlineView,
item: &objc2::runtime::AnyObject,
) -> bool {
Self::token_of_item(item).is_some_and(|t| {
self.ivars()
.source
.borrow()
.as_ref()
.is_some_and(|s| (s.section_header)(t))
})
}
#[unsafe(method(outlineView:shouldSelectItem:))]
fn should_select_item(
&self,
_ov: &objc2_app_kit::NSOutlineView,
item: &objc2::runtime::AnyObject,
) -> bool {
Self::token_of_item(item).is_some_and(|t| {
self.ivars()
.source
.borrow()
.as_ref()
.is_some_and(|s| !(s.section_header)(t))
})
}
#[unsafe(method_id(outlineView:viewForTableColumn:item:))]
fn view_for_item(
&self,
ov: &objc2_app_kit::NSOutlineView,
_col: Option<&NSTableColumn>,
item: &objc2::runtime::AnyObject,
) -> Option<Retained<NSView>> {
ffi_guard::contain(None, || {
let mtm = self.mtm();
let token = Self::token_of_item(item)?;
let ident = NSString::from_str("day.tree.cell");
let cell: Retained<NSView> =
unsafe { ov.makeViewWithIdentifier_owner(&ident, None) }.unwrap_or_else(|| {
let v: Retained<NSView> =
Retained::into_super(Retained::into_super(DayTreeCell::new(mtm)));
unsafe { v.setIdentifier(Some(&ident)) };
v
});
if let Some(source) = self.ivars().source.borrow().as_ref() {
let raw = Retained::as_ptr(&cell) as RawHandle;
if let Some(cell) = cell.downcast_ref::<DayTreeCell>() {
unsafe {
cell.setTextField(None);
}
}
(source.bind_row)(token, raw);
if (source.section_header)(token) {
fn label_in(view: &NSView) -> Option<Retained<NSTextField>> {
for child in &unsafe { view.subviews() } {
if let Ok(label) = child.clone().downcast::<NSTextField>() {
return Some(label);
}
if let Some(label) = label_in(&child) {
return Some(label);
}
}
None
}
if let Some(label) = label_in(&cell) {
let cell = cell.downcast_ref::<DayTreeCell>()?;
unsafe {
cell.setTextField(Some(&label));
}
}
}
}
Some(cell)
})
}
#[unsafe(method_id(outlineView:typeSelectStringForTableColumn:item:))]
fn type_select_string(
&self,
_ov: &objc2_app_kit::NSOutlineView,
_col: Option<&NSTableColumn>,
item: &objc2::runtime::AnyObject,
) -> Option<Retained<NSString>> {
ffi_guard::contain(None, || {
let token = Self::token_of_item(item)?;
let text = self
.ivars()
.source
.borrow()
.as_ref()
.map_or_default(|s| (s.type_select_text)(token));
(!text.is_empty()).then(|| NSString::from_str(&text))
})
}
#[unsafe(method_id(outlineView:rowViewForItem:))]
fn row_view_for_item(
&self,
ov: &objc2_app_kit::NSOutlineView,
_item: &objc2::runtime::AnyObject,
) -> Retained<objc2_app_kit::NSTableRowView> {
if unsafe { ov.style() } == objc2_app_kit::NSTableViewStyle::SourceList {
unsafe { objc2_app_kit::NSTableRowView::new(self.mtm()) }
} else {
let rv: Retained<DayTreeRowView> =
unsafe { msg_send![DayTreeRowView::alloc(self.mtm()), init] };
Retained::into_super(rv)
}
}
#[unsafe(method(outlineViewSelectionDidChange:))]
fn selection_did_change(&self, notification: &NSNotification) {
ffi_guard::contain((), || {
if self.ivars().suppress.get() || !self.ivars().selectable.get() {
return;
}
let Some(obj) = (unsafe { notification.object() }) else {
return;
};
let Ok(ov) = obj.downcast::<objc2_app_kit::NSOutlineView>() else {
return;
};
let idx = unsafe { ov.selectedRowIndexes() };
let mut tokens = Vec::with_capacity(idx.count());
let mut i = idx.firstIndex();
while i != objc2_foundation::NSNotFound as usize {
if let Some(item) = unsafe { ov.itemAtRow(i as isize) }
&& let Some(tok) = Self::token_of_item(&item)
{
tokens.push(tok);
}
i = unsafe { idx.indexGreaterThanIndex(i) };
}
emit(self.ivars().node, Event::TreeSelection(tokens));
});
}
#[unsafe(method(outlineView:didRemoveRowView:forRow:))]
fn did_remove_row_view(
&self,
ov: &objc2_app_kit::NSOutlineView,
row_view: &objc2_app_kit::NSTableRowView,
_row: isize,
) {
ffi_guard::contain((), || {
let mut cell_ptr: Option<usize> = None;
for sub in &row_view.subviews() {
let is_cell = sub
.identifier()
.is_some_and(|i| i.to_string() == "day.tree.cell");
if is_cell {
cell_ptr = Some(Retained::as_ptr(&sub) as usize);
}
}
let Some(cell) = cell_ptr else { return };
let Some(scroll) = (unsafe { ov.enclosingScrollView() }) else {
return;
};
let key = Retained::as_ptr(&scroll) as usize;
let retained =
unsafe { Retained::into_raw(Retained::retain(cell as *mut NSView).unwrap()) }
as usize;
<AppKit as Platform>::post(Box::new(move || {
let cell_view: Retained<NSView> =
match unsafe { Retained::from_raw(retained as *mut NSView) } {
Some(v) => v,
None => return,
};
let recycle = tree_entry(key).and_then(|(_, data)| {
data.ivars()
.source
.borrow()
.as_ref()
.map(|s| s.recycle.clone())
});
let Some(recycle) = recycle else { return };
if cell_view.window().is_some() {
return; }
recycle(cell as RawHandle);
}));
});
}
#[unsafe(method(outlineViewItemDidExpand:))]
fn item_did_expand(&self, notification: &NSNotification) {
self.report_disclosure(notification, true);
}
#[unsafe(method(outlineViewItemDidCollapse:))]
fn item_did_collapse(&self, notification: &NSNotification) {
self.report_disclosure(notification, false);
}
}
);
impl DayTreeData {
fn new(mtm: MainThreadMarker, node: NodeId, selectable: bool) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(TreeIvars {
node,
source: RefCell::new(None),
selectable: std::cell::Cell::new(selectable),
suppress: std::cell::Cell::new(false),
items: RefCell::new(HashMap::new()),
});
unsafe { msg_send![super(this), init] }
}
fn intern(&self, token: u64) -> Retained<NSNumber> {
self.ivars()
.items
.borrow_mut()
.entry(token)
.or_insert_with(|| NSNumber::new_u64(token))
.clone()
}
fn token_of_item(item: &objc2::runtime::AnyObject) -> Option<u64> {
item.downcast_ref::<NSNumber>()
.map(objc2_foundation::NSNumber::as_u64)
}
fn report_disclosure(&self, notification: &NSNotification, expanded: bool) {
ffi_guard::contain((), || {
if self.ivars().suppress.get() {
return;
}
let token = unsafe { notification.userInfo() }
.and_then(|ui| ui.objectForKey(&*NSString::from_str("NSObject")))
.and_then(|item| Self::token_of_item(&item));
if let Some(token) = token {
emit(self.ivars().node, Event::TreeExpanded { token, expanded });
}
});
}
fn drop_proposal(
ov: &objc2_app_kit::NSOutlineView,
info: &ProtocolObject<dyn objc2_app_kit::NSDraggingInfo>,
item: Option<&objc2::runtime::AnyObject>,
child_index: isize,
) -> Option<(u64, Option<u64>, Option<usize>)> {
let src = unsafe { info.draggingSource() }?;
if Retained::as_ptr(&src).cast::<std::ffi::c_void>()
!= std::ptr::from_ref::<objc2_app_kit::NSOutlineView>(ov).cast::<std::ffi::c_void>()
{
return None;
}
let pb = unsafe { info.draggingPasteboard() };
let token = unsafe { pb.stringForType(&NSString::from_str(DAY_TREE_PASTEBOARD_TYPE)) }?
.to_string()
.parse::<u64>()
.ok()?;
let parent = match item {
Some(i) => Some(Self::token_of_item(i)?), None => None,
};
let index = (child_index >= 0).then_some(child_index as usize);
Some((token, parent, index))
}
fn can_move(&self, token: u64, parent: Option<u64>, index: Option<usize>) -> bool {
self.ivars()
.source
.borrow()
.as_ref()
.and_then(|s| s.moves.as_ref().map(|m| (m.can_move)(token, parent, index)))
.is_some_and(|v| v == MoveVerdict::Allow)
}
}
define_class!(
#[unsafe(super(objc2_app_kit::NSOutlineView))]
#[thread_kind = MainThreadOnly]
#[name = "DayOutlineView"]
struct DayOutlineView;
impl DayOutlineView {
#[unsafe(method(layout))]
fn layout(&self) {
sync_tree_width(self);
let _: () = unsafe { msg_send![super(self), layout] };
}
#[unsafe(method_id(menuForEvent:))]
fn menu_for_event(
&self,
event: &objc2_app_kit::NSEvent,
) -> Option<Retained<NSMenu>> {
ffi_guard::contain(None, || {
let scroll = unsafe { self.enclosingScrollView() }?;
let (_, data) = tree_entry(Retained::as_ptr(&scroll) as usize)?;
let row_menu = data
.ivars()
.source
.borrow()
.as_ref()
.and_then(|s| s.row_menu.clone())?;
let p = self.convertPoint_fromView(unsafe { event.locationInWindow() }, None);
let row = unsafe { self.rowAtPoint(p) };
if row < 0 {
return None;
}
let token =
unsafe { self.itemAtRow(row) }.and_then(|i| DayTreeData::token_of_item(&i))?;
let items = row_menu(token);
if items.is_empty() {
return None;
}
Some(build_ns_menu(self.mtm(), "", &items))
})
}
}
);
define_class!(
#[unsafe(super(objc2_app_kit::NSTableRowView))]
#[thread_kind = MainThreadOnly]
#[name = "DayTreeRowView"]
struct DayTreeRowView;
impl DayTreeRowView {
#[unsafe(method(drawSelectionInRect:))]
fn draw_selection_in_rect(&self, _dirty: NSRect) {
if !unsafe { self.isSelected() } {
return;
}
let color = if unsafe { self.isEmphasized() } {
unsafe { objc2_app_kit::NSColor::selectedContentBackgroundColor() }
} else {
unsafe { objc2_app_kit::NSColor::unemphasizedSelectedContentBackgroundColor() }
};
let b = self.bounds();
let pill = NSRect::new(
NSPoint::new(b.origin.x + 5.0, b.origin.y + 1.0),
NSSize::new(
(b.size.width - 10.0).max(0.0),
(b.size.height - 2.0).max(0.0),
),
);
unsafe {
color.setFill();
objc2_app_kit::NSBezierPath::bezierPathWithRoundedRect_xRadius_yRadius(
pill, 5.0, 5.0,
)
.fill();
}
}
}
);
define_class!(
#[unsafe(super(objc2_app_kit::NSTableCellView))]
#[thread_kind = MainThreadOnly]
#[name = "DayTreeCell"]
struct DayTreeCell;
impl DayTreeCell {
#[unsafe(method(isFlipped))]
fn is_flipped(&self) -> bool {
true
}
#[unsafe(method(layout))]
fn layout(&self) {
let _: () = unsafe { msg_send![super(self), layout] };
ffi_guard::contain((), || {
let width = self.bounds().size.width;
if width <= 0.0 {
return;
}
let mut cur = unsafe { self.superview() };
let mut outline: Option<Retained<objc2_app_kit::NSOutlineView>> = None;
while let Some(v) = cur {
cur = unsafe { v.superview() };
if let Ok(ov) = v.downcast::<objc2_app_kit::NSOutlineView>() {
outline = Some(ov);
break;
}
}
let Some(ov) = outline else { return };
let Some(scroll) = (unsafe { ov.enclosingScrollView() }) else {
return;
};
let f = tree_entry(Retained::as_ptr(&scroll) as usize)
.and_then(|(_, data)| {
data.ivars()
.source
.borrow()
.as_ref()
.map(|s| s.layout_cell_size.clone())
});
if let Some(f) = f {
f(std::ptr::from_ref::<Self>(self) as RawHandle, Size::new(width, self.bounds().size.height));
}
});
}
}
);
impl DayTreeCell {
fn new(mtm: MainThreadMarker) -> Retained<Self> {
unsafe { msg_send![Self::alloc(mtm), init] }
}
}
const DAY_TREE_PASTEBOARD_TYPE: &str = "dev.daybrite.day.tree.node";
type TreeEntry = (
Retained<objc2_app_kit::NSOutlineView>,
Retained<DayTreeData>,
);
fn tree_entry(key: usize) -> Option<TreeEntry> {
TREE_STATE.with(|m| m.borrow().get(&key).cloned())
}
fn sync_tree_width(outline: &objc2_app_kit::NSOutlineView) {
let Some(scroll) = (unsafe { outline.enclosingScrollView() }) else {
return;
};
let clip_w = unsafe { scroll.contentView() }.bounds().size.width;
let f = outline.frame();
if clip_w > 0.0 && (f.size.width - clip_w).abs() > 0.5 {
unsafe {
outline.setFrameSize(NSSize::new(clip_w, f.size.height));
outline.sizeLastColumnToFit();
}
}
}
fn post_realize_visible_tree_rows(key: usize) {
<AppKit as Platform>::post(Box::new(move || {
if let Some((outline, _)) = tree_entry(key) {
let range = unsafe { outline.rowsInRect(outline.visibleRect()) };
for row in range.location..range.location + range.length {
let _ = unsafe { outline.viewAtColumn_row_makeIfNecessary(0, row as isize, true) };
}
}
day_core::pump_events();
}));
}
type NavMenuEntry = (
Retained<objc2_app_kit::NSOutlineView>,
Retained<DayNavMenuData>,
);
fn ns_rect(r: day_spec::Rect) -> NSRect {
NSRect::new(
NSPoint::new(r.origin.x, r.origin.y),
NSSize::new(r.size.width, r.size.height),
)
}
fn draw_op(op: &DrawOp, replay: &mut Replay) {
unsafe {
match op {
DrawOp::Stamp(st) => {
let batch = unsafe { objc2_app_kit::NSBezierPath::bezierPath() };
for p in &st.at {
if let Some(copy) = bezier(&st.shape.translated(p.x, p.y)) {
unsafe { batch.appendBezierPath(©) };
}
}
let day_spec::Paint::Solid(color) = &st.paint else {
let Some(bounds) = stamp_bounds(st) else {
return;
};
draw_grouped(replay, || {
match &st.stroke {
None => unsafe { batch.addClip() },
Some(style) => clip_to_stroke(&batch, style),
}
draw_gradient_in(&st.paint, bounds);
});
return;
};
match &st.stroke {
None => {
nscolor((*color).resolved()).setFill();
unsafe { batch.fill() };
}
Some(style) => {
nscolor((*color).resolved()).setStroke();
apply_stroke_style(&batch, style);
unsafe { batch.stroke() };
}
}
}
DrawOp::Fill(shape, paint) => match paint {
day_spec::Paint::Solid(color) => {
nscolor((*color).resolved()).setFill();
if let Some(p) = bezier(shape) {
p.fill();
}
}
gradient => {
if let Some(p) = bezier(shape) {
draw_grouped(replay, || {
p.addClip();
draw_gradient_in(gradient, shape.bounds());
});
}
}
},
DrawOp::Stroke(shape, paint, style) => {
if let Some(p) = bezier(shape) {
apply_stroke_style(&p, style);
if let day_spec::Paint::Solid(color) = paint {
nscolor((*color).resolved()).setStroke();
p.stroke();
} else {
draw_grouped(replay, || {
clip_to_stroke(&p, style);
draw_gradient_in(paint, shape.bounds());
});
}
}
}
DrawOp::Clip(shape) => {
if let Some(p) = bezier(shape) {
p.addClip();
}
}
DrawOp::Text {
text,
at,
size,
color,
anchor,
font,
} => {
let font = canvas_nsfont(*size, font);
let attrs = canvas_text_attrs(&font, *color);
let ns = NSString::from_str(text);
let mut origin = NSPoint::new(at.x, at.y);
if *anchor != day_spec::TextAnchor::LEADING {
let sz: NSSize = msg_send![&ns, sizeWithAttributes: &*attrs];
let (dx, dy) = anchor.offset(sz.width, sz.height, font.ascender());
origin.x += dx;
origin.y += dy;
}
let _: () = msg_send![&ns, drawAtPoint: origin, withAttributes: &*attrs];
}
DrawOp::Image {
image,
rect,
opacity,
source,
} => {
let held = BITMAPS.with(|m| {
m.borrow()
.get(&image.0)
.map(|b| (b.image.clone(), b.info.pixels))
});
if let Some((img, pixels)) = held {
let region = match source {
None => Some((NSRect::ZERO, *rect)),
Some(src) => {
day_spec::clamp_image_source(*src, *rect, pixels).map(|(src, dest)| {
let sz = img.size();
let (rx, ry) = (sz.width / pixels.width, sz.height / pixels.height);
let from = NSRect::new(
NSPoint::new(
src.origin.x * rx,
(pixels.height - src.origin.y - src.size.height) * ry,
),
NSSize::new(src.size.width * rx, src.size.height * ry),
);
(from, dest)
})
}
};
if let Some((from, dest)) = region {
let dest = NSRect::new(
NSPoint::new(dest.origin.x, dest.origin.y),
NSSize::new(dest.size.width, dest.size.height),
);
img.drawInRect_fromRect_operation_fraction_respectFlipped_hints(
dest,
from,
ns_compositing(replay.ink.blend),
*opacity,
true,
None,
);
}
}
}
DrawOp::Shadow(shadow) => {
let shadow = shadow.as_ref().filter(|s| s.color.resolved().a > 0.0);
replay.ink.shadow = shadow.is_some();
if let Some(ctx) = NSGraphicsContext::currentContext() {
cg_set_shadow(&ctx.CGContext(), shadow, replay.base_scale);
}
}
DrawOp::Blend(mode) => {
replay.ink.blend = *mode;
if let Some(ctx) = NSGraphicsContext::currentContext() {
objc2_core_graphics::CGContext::set_blend_mode(
Some(&ctx.CGContext()),
cg_blend_mode(*mode),
);
}
}
DrawOp::Save => {
replay.save();
NSGraphicsContext::saveGraphicsState_class();
}
DrawOp::Restore => {
if replay.restore() {
NSGraphicsContext::restoreGraphicsState_class();
}
}
DrawOp::Concat(m) => {
let t = NSAffineTransform::new();
t.setTransformStruct(NSAffineTransformStruct {
m11: m.a,
m12: m.b,
m21: m.c,
m22: m.d,
tX: m.tx,
tY: m.ty,
});
t.concat();
}
}
}
}
fn clip_to_stroke(path: &objc2_app_kit::NSBezierPath, style: &day_spec::StrokeStyle) {
use objc2_core_graphics::CGContext;
let Some(ctx) = (unsafe { NSGraphicsContext::currentContext() }) else {
return;
};
let cg = ctx.CGContext();
cg_stroke_style(&cg, style);
CGContext::begin_path(Some(&cg));
CGContext::add_path(Some(&cg), Some(&*unsafe { path.CGPath() }));
CGContext::replace_path_with_stroked_path(Some(&cg));
CGContext::clip(Some(&cg));
}
fn stamp_bounds(stamp: &day_spec::Stamp) -> Option<day_spec::Rect> {
let (first, rest) = stamp.at.split_first()?;
let (mut min, mut max) = (*first, *first);
for p in rest {
min = Point::new(min.x.min(p.x), min.y.min(p.y));
max = Point::new(max.x.max(p.x), max.y.max(p.y));
}
let b = stamp.shape.bounds();
Some(day_spec::Rect::new(
b.origin.x + min.x,
b.origin.y + min.y,
b.size.width + (max.x - min.x),
b.size.height + (max.y - min.y),
))
}
fn nsgradient(
stops: &[(f64, day_spec::Color)],
) -> Option<objc2::rc::Retained<objc2_app_kit::NSGradient>> {
if stops.is_empty() {
return None;
}
let colors = objc2_foundation::NSArray::from_retained_slice(
&stops.iter().map(|(_, c)| nscolor(*c)).collect::<Vec<_>>(),
);
let locations: Vec<f64> = stops.iter().map(|(o, _)| *o).collect();
unsafe {
objc2_app_kit::NSGradient::initWithColors_atLocations_colorSpace(
objc2_app_kit::NSGradient::alloc(),
&colors,
locations.as_ptr(),
&objc2_app_kit::NSColorSpace::sRGBColorSpace(),
)
}
}
fn concat_unit_to_bounds(b: day_spec::Rect) {
let t = NSAffineTransform::new();
t.setTransformStruct(NSAffineTransformStruct {
m11: b.size.width,
m12: 0.0,
m21: 0.0,
m22: b.size.height,
tX: b.origin.x,
tY: b.origin.y,
});
unsafe { t.concat() };
}
fn apply_stroke_style(p: &objc2_app_kit::NSBezierPath, style: &day_spec::StrokeStyle) {
use day_spec::{LineCap, LineJoin};
unsafe {
p.setLineWidth(style.width);
p.setLineCapStyle(match style.cap {
LineCap::Butt => objc2_app_kit::NSLineCapStyle::Butt,
LineCap::Round => objc2_app_kit::NSLineCapStyle::Round,
LineCap::Square => objc2_app_kit::NSLineCapStyle::Square,
});
if style.is_plain() {
return;
}
p.setLineJoinStyle(match style.join {
LineJoin::Miter => objc2_app_kit::NSLineJoinStyle::Miter,
LineJoin::Round => objc2_app_kit::NSLineJoinStyle::Round,
LineJoin::Bevel => objc2_app_kit::NSLineJoinStyle::Bevel,
});
p.setMiterLimit(style.miter_limit);
if !style.dash.is_empty() {
let pattern: Vec<objc2_core_foundation::CGFloat> = style.dash.clone();
p.setLineDash_count_phase(pattern.as_ptr(), pattern.len() as isize, style.dash_phase);
}
}
}
fn stops_now(stops: &[(f64, day_spec::Ink)]) -> Vec<(f64, day_spec::Color)> {
stops
.iter()
.map(|(at, ink)| (*at, color::ink_now(*ink)))
.collect()
}
fn draw_gradient_in(paint: &day_spec::Paint, bounds: day_spec::Rect) {
let opts = objc2_app_kit::NSGradientDrawingOptions::DrawsBeforeStartingLocation
| objc2_app_kit::NSGradientDrawingOptions::DrawsAfterEndingLocation;
match paint {
day_spec::Paint::Linear(g) => {
if let Some(grad) = nsgradient(&stops_now(&g.stops)) {
let (s, e) = (g.start.resolve(bounds), g.end.resolve(bounds));
unsafe {
grad.drawFromPoint_toPoint_options(
NSPoint::new(s.x, s.y),
NSPoint::new(e.x, e.y),
opts,
);
};
}
}
day_spec::Paint::Radial(g) => {
if let Some(grad) = nsgradient(&stops_now(&g.stops)) {
NSGraphicsContext::saveGraphicsState_class();
concat_unit_to_bounds(bounds);
let c = NSPoint::new(g.center.x, g.center.y);
unsafe {
grad.drawFromCenter_radius_toCenter_radius_options(c, 0.0, c, g.radius, opts);
};
NSGraphicsContext::restoreGraphicsState_class();
}
}
day_spec::Paint::Conic(g) => {
if let Some(ctx) = unsafe { NSGraphicsContext::currentContext() } {
draw_conic(&ctx.CGContext(), g, bounds);
}
}
day_spec::Paint::Solid(_) => {}
}
}
fn apply_button_content(
btn: &NSButton,
title: &str,
icon: Option<&day_spec::Icon>,
icon_only: bool,
) {
let image = icon.and_then(|i| crate::toolbar::image_for(i, title, btn.mtm()));
if let Some(image) = &image {
let size = image.size();
let longest = size.width.max(size.height);
if longest > 0.0 {
image.setSize(objc2_foundation::NSSize::new(
size.width * 20.0 / longest,
size.height * 20.0 / longest,
));
}
}
let style = BUTTON_STYLES
.with(|m| m.borrow().get(&ptr_of(btn)).copied())
.unwrap_or_default();
BUTTON_IMAGES.with(|images| {
let _ = images.remove(ptr_of(btn));
if let Some(image) = &image {
images.insert(ptr_of(btn), image.clone());
}
});
set_button_title(btn, title, style);
unsafe {
btn.setImagePosition(if image.is_none() {
objc2_app_kit::NSCellImagePosition::NoImage
} else if icon_only {
objc2_app_kit::NSCellImagePosition::ImageOnly
} else {
objc2_app_kit::NSCellImagePosition::ImageLeading
});
}
set_button_image(btn, style);
unsafe {
if !APP_LABELED.with(|t| t.contains(ptr_of(btn))) {
btn.setAccessibilityLabel(Some(&NSString::from_str(title)));
}
let tooltip = (icon_only && image.is_some()).then(|| NSString::from_str(title));
btn.setToolTip(tooltip.as_deref());
}
}
fn set_button_image(btn: &NSButton, style: day_spec::props::ButtonStyleSpec) {
use day_spec::props::ButtonStyleSpec as S;
let image = BUTTON_IMAGES.with(|images| images.with(ptr_of(btn), |image| image.clone()));
let image = image.map(|source| {
let S::Tinted(fill) = style else {
return source;
};
let size = source.size();
let ink = color::nsink(color::map_ink(fill, S::on_tint));
let draw = block2::RcBlock::new(move |rect: NSRect| {
source.drawInRect_fromRect_operation_fraction(
rect,
NSRect::ZERO,
objc2_app_kit::NSCompositingOperation::SourceOver,
1.0,
);
unsafe {
ink.setFill();
objc2_app_kit::NSRectFillUsingOperation(
rect,
objc2_app_kit::NSCompositingOperation::SourceIn,
);
}
objc2::runtime::Bool::YES
});
objc2_app_kit::NSImage::imageWithSize_flipped_drawingHandler(size, false, &draw)
});
unsafe {
let position = btn.imagePosition();
let image = image.map(|source| {
if position == objc2_app_kit::NSCellImagePosition::ImageOnly {
return source;
}
let size = source.size();
let template = source.isTemplate();
let padded = NSSize::new(size.width + 8.0, size.height);
let draw = block2::RcBlock::new(move |_rect: NSRect| {
source.drawInRect_fromRect_operation_fraction(
NSRect::new(NSPoint::new(0.0, 0.0), size),
NSRect::ZERO,
objc2_app_kit::NSCompositingOperation::SourceOver,
1.0,
);
objc2::runtime::Bool::YES
});
let out =
objc2_app_kit::NSImage::imageWithSize_flipped_drawingHandler(padded, false, &draw);
out.setTemplate(template);
out
});
btn.setImage(image.as_deref());
btn.setImagePosition(position);
btn.setImageHugsTitle(true);
}
}
fn set_button_selected(btn: &objc2_app_kit::NSButton, on: bool) {
BUTTON_SELECTED.with(|m| m.borrow_mut().insert(ptr_of(btn), on));
unsafe {
if on {
btn.setButtonType(objc2_app_kit::NSButtonType::PushOnPushOff);
}
btn.setState(if on {
NSControlStateValueOn
} else {
NSControlStateValueOff
});
}
}
const TINT_CORNER_RADIUS: f64 = 6.0;
fn apply_button_style(btn: &objc2_app_kit::NSButton, style: day_spec::props::ButtonStyleSpec) {
use day_spec::props::ButtonStyleSpec as S;
let title = unsafe { btn.title() }.to_string();
unsafe {
btn.setKeyEquivalent(&NSString::from_str(""));
btn.setBezelColor(None);
btn.setBordered(!matches!(style, S::Plain));
match style {
S::Prominent => btn.setKeyEquivalent(&NSString::from_str("\r")),
S::Tinted(c) => btn.setBezelColor(Some(&color::nsink(c))),
S::Bordered | S::Automatic | S::Compact | S::Plain => {}
}
btn.setWantsLayer(true);
if let Some(layer) = btn.layer() {
match style {
S::Tinted(c) => {
layer.setBackgroundColor(Some(&color::cg_for(btn, c)));
layer.setCornerRadius(TINT_CORNER_RADIUS);
if c.is_dynamic() {
color::track_tinted(btn);
}
}
_ => layer.setBackgroundColor(None),
}
}
}
BUTTON_STYLES.with(|m| m.borrow_mut().insert(ptr_of(btn), style));
let ink = match style {
S::Tinted(c) => Some(color::nsink(color::map_ink(c, S::on_tint))),
_ => None,
};
unsafe {
btn.setContentTintColor(ink.as_deref());
}
set_button_title(btn, &title, style);
set_button_image(btn, style);
}
fn set_button_title(
btn: &objc2_app_kit::NSButton,
title: &str,
style: day_spec::props::ButtonStyleSpec,
) {
use day_spec::props::ButtonStyleSpec as S;
let position = unsafe { btn.imagePosition() };
let S::Tinted(fill) = style else {
unsafe {
btn.setTitle(&NSString::from_str(title));
btn.setImagePosition(position);
}
return;
};
unsafe {
let col = color::nsink(color::map_ink(fill, S::on_tint));
let font = btn
.font()
.unwrap_or_else(|| NSFont::systemFontOfSize(NSFont::systemFontSize()));
let para = objc2_app_kit::NSMutableParagraphStyle::new();
para.setAlignment(objc2_app_kit::NSTextAlignment::Center);
let keys: [&NSString; 3] = [
objc2_app_kit::NSFontAttributeName,
objc2_app_kit::NSForegroundColorAttributeName,
objc2_app_kit::NSParagraphStyleAttributeName,
];
let objs: [&objc2::runtime::AnyObject; 3] = [
font.as_ref() as &objc2::runtime::AnyObject,
col.as_ref() as &objc2::runtime::AnyObject,
para.as_ref() as &objc2::runtime::AnyObject,
];
let attrs = objc2_foundation::NSDictionary::from_slices::<NSString>(&keys, &objs);
let s = objc2_foundation::NSAttributedString::initWithString_attributes(
objc2_foundation::NSAttributedString::alloc(),
&NSString::from_str(title),
Some(&attrs),
);
btn.setAttributedTitle(&s);
btn.setImagePosition(position);
}
}
define_class!(
#[unsafe(super(objc2_app_kit::NSImageView))]
#[thread_kind = MainThreadOnly]
#[name = "DayFillImageView"]
struct FillImageView;
impl FillImageView {
#[unsafe(method(drawRect:))]
fn draw_rect(&self, dirty: NSRect) {
let bounds = self.bounds();
let image = unsafe { self.image() };
let Some(size) = image.map(|image| image.size()).filter(|size| {
size.width > 0.0 && size.height > 0.0
&& bounds.size.width > 0.0 && bounds.size.height > 0.0
}) else {
let _: () = unsafe { msg_send![super(self), drawRect: dirty] };
return;
};
let x = bounds.size.width / size.width;
let y = bounds.size.height / size.height;
let scale = x.max(y) / x.min(y);
let cx = bounds.origin.x + bounds.size.width / 2.0;
let cy = bounds.origin.y + bounds.size.height / 2.0;
NSGraphicsContext::saveGraphicsState_class();
objc2_app_kit::NSBezierPath::bezierPathWithRect(bounds).addClip();
let transform = NSAffineTransform::transform();
transform.translateXBy_yBy(cx, cy);
transform.scaleBy(scale);
transform.translateXBy_yBy(-cx, -cy);
transform.concat();
let _: () = unsafe { msg_send![super(self), drawRect: bounds] };
NSGraphicsContext::restoreGraphicsState_class();
}
#[unsafe(method(setFrameSize:))]
fn set_frame_size(&self, size: NSSize) {
let _: () = unsafe { msg_send![super(self), setFrameSize: size] };
NSView::setNeedsDisplay(self, true);
}
}
);
fn bezier(shape: &day_spec::Shape) -> Option<objc2::rc::Retained<objc2_app_kit::NSBezierPath>> {
use day_spec::Shape;
use objc2_app_kit::NSBezierPath;
unsafe {
Some(match shape {
Shape::Rect(r) => NSBezierPath::bezierPathWithRect(ns_rect(*r)),
Shape::RoundedRect(r, rad) => {
NSBezierPath::bezierPathWithRoundedRect_xRadius_yRadius(ns_rect(*r), *rad, *rad)
}
Shape::Ellipse(r) => NSBezierPath::bezierPathWithOvalInRect(ns_rect(*r)),
Shape::Arc { rect, start, sweep } => {
let (start_deg, sweep_deg) = (&start.degrees(), &sweep.degrees());
let p = NSBezierPath::new();
let center = NSPoint::new(
rect.origin.x + rect.size.width / 2.0,
rect.origin.y + rect.size.height / 2.0,
);
let radius = rect.size.width.min(rect.size.height) / 2.0;
p.appendBezierPathWithArcWithCenter_radius_startAngle_endAngle(
center,
radius,
*start_deg,
*start_deg + *sweep_deg,
);
p
}
Shape::Line(a, b) => {
let p = NSBezierPath::new();
p.moveToPoint(NSPoint::new(a.x, a.y));
p.lineToPoint(NSPoint::new(b.x, b.y));
p
}
Shape::Polygon(pts) => {
if pts.len() < 2 {
return None;
}
let p = NSBezierPath::new();
p.moveToPoint(NSPoint::new(pts[0].x, pts[0].y));
for pt in &pts[1..] {
p.lineToPoint(NSPoint::new(pt.x, pt.y));
}
p.closePath();
p
}
Shape::Path(path) => {
use day_spec::PathSeg;
if path.segs.is_empty() {
return None;
}
let p = NSBezierPath::new();
let mut cur = NSPoint::new(0.0, 0.0);
for seg in &path.segs {
match seg {
PathSeg::Move(a) => {
cur = NSPoint::new(a.x, a.y);
p.moveToPoint(cur);
}
PathSeg::Line(a) => {
cur = NSPoint::new(a.x, a.y);
p.lineToPoint(cur);
}
PathSeg::Quad(c, a) => {
let (end, ctl) = (NSPoint::new(a.x, a.y), NSPoint::new(c.x, c.y));
let c1 = NSPoint::new(
cur.x + 2.0 / 3.0 * (ctl.x - cur.x),
cur.y + 2.0 / 3.0 * (ctl.y - cur.y),
);
let c2 = NSPoint::new(
end.x + 2.0 / 3.0 * (ctl.x - end.x),
end.y + 2.0 / 3.0 * (ctl.y - end.y),
);
p.curveToPoint_controlPoint1_controlPoint2(end, c1, c2);
cur = end;
}
PathSeg::Cubic(c1, c2, a) => {
cur = NSPoint::new(a.x, a.y);
p.curveToPoint_controlPoint1_controlPoint2(
cur,
NSPoint::new(c1.x, c1.y),
NSPoint::new(c2.x, c2.y),
);
}
PathSeg::Close => p.closePath(),
}
}
p.setWindingRule(match path.rule {
day_spec::FillRule::NonZero => objc2_app_kit::NSWindingRule::NonZero,
day_spec::FillRule::EvenOdd => objc2_app_kit::NSWindingRule::EvenOdd,
});
p
}
})
}
}
struct UndoIvars {
state: std::cell::RefCell<day_spec::UndoState>,
}
define_class!(
#[unsafe(super(objc2_foundation::NSUndoManager))]
#[thread_kind = MainThreadOnly]
#[name = "DayUndoManager"]
#[ivars = UndoIvars]
struct DayUndoManager;
impl DayUndoManager {
#[unsafe(method(canUndo))]
fn can_undo(&self) -> bool {
self.ivars().state.borrow().can_undo
}
#[unsafe(method(canRedo))]
fn can_redo(&self) -> bool {
self.ivars().state.borrow().can_redo
}
#[unsafe(method(undo))]
fn do_undo(&self) {
ffi_guard::contain((), || emit(WINDOW_NODE, Event::Undo { redo: false }));
}
#[unsafe(method(redo))]
fn do_redo(&self) {
ffi_guard::contain((), || emit(WINDOW_NODE, Event::Undo { redo: true }));
}
#[unsafe(method_id(undoMenuItemTitle))]
fn undo_menu_item_title(&self) -> Retained<NSString> {
let label = self.ivars().state.borrow().undo_label.clone();
unsafe { self.undoMenuTitleForUndoActionName(&NSString::from_str(&label)) }
}
#[unsafe(method_id(redoMenuItemTitle))]
fn redo_menu_item_title(&self) -> Retained<NSString> {
let label = self.ivars().state.borrow().redo_label.clone();
unsafe { self.redoMenuTitleForUndoActionName(&NSString::from_str(&label)) }
}
}
);
fn undo_front(mtm: MainThreadMarker) -> Retained<DayUndoManager> {
UNDO_FRONT.with(|u| {
u.borrow_mut()
.get_or_insert_with(|| {
let this = DayUndoManager::alloc(mtm).set_ivars(UndoIvars {
state: std::cell::RefCell::new(day_spec::UndoState::default()),
});
unsafe { msg_send![super(this), init] }
})
.clone()
})
}
struct WinIvars {
node: Option<NodeId>,
group: Option<String>,
last_state: Cell<i32>,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DayWinDelegate"]
#[ivars = WinIvars]
struct DayWinDelegate;
unsafe impl NSObjectProtocol for DayWinDelegate {}
impl DayWinDelegate {
#[unsafe(method(undo:))]
fn edit_undo(&self, _sender: Option<&objc2::runtime::AnyObject>) {
ffi_guard::contain((), || {
if let Some(m) = self.acting_undo_manager() {
unsafe { m.undo() };
}
});
}
#[unsafe(method(redo:))]
fn edit_redo(&self, _sender: Option<&objc2::runtime::AnyObject>) {
ffi_guard::contain((), || {
if let Some(m) = self.acting_undo_manager() {
unsafe { m.redo() };
}
});
}
#[unsafe(method(cut:))]
fn edit_cut(&self, _sender: Option<&objc2::runtime::AnyObject>) {
ffi_guard::contain((), || emit(WINDOW_NODE, Event::Edit(day_spec::EditOp::Cut)));
}
#[unsafe(method(copy:))]
fn edit_copy(&self, _sender: Option<&objc2::runtime::AnyObject>) {
ffi_guard::contain((), || emit(WINDOW_NODE, Event::Edit(day_spec::EditOp::Copy)));
}
#[unsafe(method(paste:))]
fn edit_paste(&self, _sender: Option<&objc2::runtime::AnyObject>) {
ffi_guard::contain((), || emit(WINDOW_NODE, Event::Edit(day_spec::EditOp::Paste)));
}
#[unsafe(method(selectAll:))]
fn edit_select_all(&self, _sender: Option<&objc2::runtime::AnyObject>) {
ffi_guard::contain((), || {
emit(WINDOW_NODE, Event::Edit(day_spec::EditOp::SelectAll));
});
}
#[unsafe(method(validateMenuItem:))]
fn validate_menu_item(&self, item: &NSMenuItem) -> bool {
ffi_guard::contain(false, || {
let state = EDIT_STATE.with(std::cell::Cell::get);
match unsafe { item.action() } {
Some(a) if a == sel!(undo:) => match self.acting_undo_manager() {
Some(m) => {
item.setTitle(unsafe { m.undoMenuItemTitle() }.as_ref());
unsafe { m.canUndo() }
}
None => false,
},
Some(a) if a == sel!(redo:) => match self.acting_undo_manager() {
Some(m) => {
item.setTitle(unsafe { m.redoMenuItemTitle() }.as_ref());
unsafe { m.canRedo() }
}
None => false,
},
Some(a) if a == sel!(selectAll:) => state.can_select_all,
Some(a) if a == sel!(cut:) => state.can_cut,
Some(a) if a == sel!(copy:) => state.can_copy,
Some(a) if a == sel!(paste:) => {
state.can_paste && {
let pb = unsafe { NSPasteboard::generalPasteboard() };
pb.types().is_some_and(|types| !types.is_empty())
}
}
_ => true,
}
})
}
}
unsafe impl NSWindowDelegate for DayWinDelegate {
#[unsafe(method(windowDidResize:))]
fn window_did_resize(&self, notification: &NSNotification) {
ffi_guard::contain((), || {
if let Some(obj) = unsafe { notification.object() }
&& let Ok(win) = obj.downcast::<NSWindow>()
&& let Some(size) = pin_below_title_bar(&win)
{
let target = self.ivars().node.unwrap_or(WINDOW_NODE);
emit(target, Event::WindowResized(size));
}
self.report_state_of(notification);
});
}
#[unsafe(method(windowShouldClose:))]
fn window_should_close(&self, _window: &NSWindow) -> bool {
ffi_guard::contain(false, || day_core::windows::native_close_requested(self.ivars().node.unwrap_or(WINDOW_NODE)))
}
#[unsafe(method(windowWillClose:))]
fn window_will_close(&self, _notification: &NSNotification) {
ffi_guard::contain((), || {
match self.ivars().node {
Some(node) => emit(node, Event::WindowClosed),
None if status_item::keeps_running() => emit(WINDOW_NODE, Event::WindowClosed),
None => {
let app = NSApplication::sharedApplication(self.mtm());
unsafe { app.terminate(None) };
}
}
});
}
#[unsafe(method_id(windowWillReturnUndoManager:))]
fn window_will_return_undo_manager(
&self,
_window: &NSWindow,
) -> Option<Retained<objc2_foundation::NSUndoManager>> {
ffi_guard::contain(None, || {
UNDO_FRONT.with(|u| {
u.borrow()
.as_ref()
.map(|m| Retained::into_super(m.clone()))
})
})
}
#[unsafe(method(windowDidBecomeKey:))]
fn window_did_become_key(&self, _notification: &NSNotification) {
ffi_guard::contain((), || {
if let Some(node) = self.ivars().node {
emit(node, Event::WindowFocused(true));
}
});
}
#[unsafe(method(windowDidResignKey:))]
fn window_did_resign_key(&self, _notification: &NSNotification) {
ffi_guard::contain((), || {
if let Some(node) = self.ivars().node {
emit(node, Event::WindowFocused(false));
}
});
}
#[unsafe(method(windowDidMiniaturize:))]
fn window_did_miniaturize(&self, notification: &NSNotification) {
ffi_guard::contain((), || self.report_state_of(notification));
}
#[unsafe(method(windowDidDeminiaturize:))]
fn window_did_deminiaturize(&self, notification: &NSNotification) {
ffi_guard::contain((), || self.report_state_of(notification));
}
#[unsafe(method(windowDidEnterFullScreen:))]
fn window_did_enter_full_screen(&self, notification: &NSNotification) {
ffi_guard::contain((), || self.report_state_of(notification));
}
#[unsafe(method(windowDidExitFullScreen:))]
fn window_did_exit_full_screen(&self, notification: &NSNotification) {
ffi_guard::contain((), || self.report_state_of(notification));
}
}
impl DayWinDelegate {
#[unsafe(method(newWindowForTab:))]
fn new_window_for_tab(&self, _sender: Option<&NSObject>) {
ffi_guard::contain((), || {
let _ = match self.ivars().group.as_deref() {
Some(group) => day_core::windows::open_new_window_for_group(group),
None => day_core::windows::open_new_window(),
};
});
}
}
);
impl DayWinDelegate {
fn report_state_of(&self, notification: &NSNotification) {
if let Some(obj) = unsafe { notification.object() }
&& let Ok(win) = obj.downcast::<NSWindow>()
{
self.report_state(&win);
}
}
}
fn key_name(event: &objc2_app_kit::NSEvent) -> Option<&'static str> {
match unsafe { event.keyCode() } {
123 => Some("ArrowLeft"),
124 => Some("ArrowRight"),
125 => Some("ArrowDown"),
126 => Some("ArrowUp"),
_ => {
let held = NSEventModifierFlags::Command
| NSEventModifierFlags::Option
| NSEventModifierFlags::Control;
if event.modifierFlags().intersects(held) {
return None;
}
let typed = event.characters()?.to_string();
let mut chars = typed.chars();
match (chars.next(), chars.next()) {
(Some(c), None) => day_spec::KeyEvent::digit_name(c),
_ => None,
}
}
}
}
fn short_route_key_down(node: NodeId, event: &objc2_app_kit::NSEvent) -> bool {
let Some(key) = key_name(event) else {
return false;
};
if !day_spec::keys::handled(node) {
return false;
}
let mods = key_modifiers(event.modifierFlags());
if event.isARepeat() {
if !input::owns_repeat(event) {
return false;
}
emit(
node,
Event::Key(day_spec::KeyEvent::named(key, mods).with_phase(day_spec::KeyPhase::Repeat)),
);
return true;
}
emit(node, Event::Key(day_spec::KeyEvent::named(key, mods)));
input::claim_key(event, node, key.to_string());
true
}
fn named_key(event: &objc2_app_kit::NSEvent) -> Option<&'static str> {
Some(match unsafe { event.keyCode() } {
123 => "ArrowLeft",
124 => "ArrowRight",
125 => "ArrowDown",
126 => "ArrowUp",
36 | 76 => "Enter",
48 => "Tab",
53 => "Escape",
51 => "Backspace",
117 => "Delete",
115 => "Home",
119 => "End",
116 => "PageUp",
121 => "PageDown",
114 => "Insert",
122 => "F1",
120 => "F2",
99 => "F3",
118 => "F4",
96 => "F5",
97 => "F6",
98 => "F7",
100 => "F8",
101 => "F9",
109 => "F10",
103 => "F11",
111 => "F12",
_ => return None,
})
}
fn full_route_key_down(view: &DayCanvas, node: NodeId, event: &objc2_app_kit::NSEvent) -> bool {
let ptr = std::ptr::from_ref::<DayCanvas>(view).cast::<NSView>() as usize;
let flags = event.modifierFlags();
let mods = key_modifiers(flags);
if flags.contains(NSEventModifierFlags::Command) {
return false;
}
let composing = MARKED.with(|t| t.contains(ptr));
let phase = if event.isARepeat() {
if !composing && !input::owns_repeat(event) {
return true;
}
day_spec::KeyPhase::Repeat
} else {
day_spec::KeyPhase::Down
};
let claim = |key: String| {
if phase == day_spec::KeyPhase::Down {
input::claim_key(event, node, key);
}
};
if !composing && let Some(named) = named_key(event) {
emit(
node,
Event::Key(day_spec::KeyEvent::named(named, mods).with_phase(phase)),
);
claim(named.to_string());
return true;
}
let unmodified =
unsafe { event.charactersIgnoringModifiers() }.map_or_default(|s| s.to_string());
let typeable = !unmodified.is_empty()
&& !unmodified
.chars()
.any(|c| c.is_control() || ('\u{f700}'..='\u{f8ff}').contains(&c));
if !composing && flags.contains(NSEventModifierFlags::Control) {
if typeable {
emit(
node,
Event::Key(day_spec::KeyEvent::named(unmodified.clone(), mods).with_phase(phase)),
);
claim(unmodified);
}
return true;
}
if !composing && !typeable {
return true;
}
INTERPRETING.with(|i| {
*i.borrow_mut() = Some(Interpreting {
key: unmodified,
mods,
phase,
event: event.retain(),
});
});
view.interpretKeyEvents(&NSArray::from_slice(&[event]));
INTERPRETING.with(|i| *i.borrow_mut() = None);
true
}
struct Interpreting {
key: String,
mods: u8,
phase: day_spec::KeyPhase,
event: Retained<NSEvent>,
}
fn text_of_input_string(string: &AnyObject) -> String {
if let Some(s) = string.downcast_ref::<NSString>() {
s.to_string()
} else if let Some(s) = string.downcast_ref::<objc2_foundation::NSAttributedString>() {
s.string().to_string()
} else {
String::new()
}
}
fn key_modifiers(f: NSEventModifierFlags) -> u8 {
let mut modifiers = 0u8;
if f.contains(NSEventModifierFlags::Shift) {
modifiers |= day_spec::Modifiers::SHIFT.bits();
}
if f.contains(NSEventModifierFlags::Command) {
modifiers |= day_spec::Modifiers::PRIMARY.bits();
}
if f.contains(NSEventModifierFlags::Option) {
modifiers |= day_spec::Modifiers::ALT.bits();
}
if f.contains(NSEventModifierFlags::Control) {
modifiers |= day_spec::Modifiers::CONTROL.bits();
}
modifiers
}
impl DayWinDelegate {
fn acting_undo_manager(&self) -> Option<Retained<objc2_foundation::NSUndoManager>> {
let responder = NSApplication::sharedApplication(self.mtm())
.keyWindow()
.and_then(|w| w.firstResponder());
responder
.and_then(|r| unsafe { r.undoManager() })
.or_else(|| {
UNDO_FRONT.with(|u| u.borrow().as_ref().map(|m| Retained::into_super(m.clone())))
})
}
}
impl DayWinDelegate {
fn new(mtm: MainThreadMarker, node: Option<NodeId>, group: Option<String>) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(WinIvars {
node,
group,
last_state: Cell::new(day_spec::WindowState::Normal.code()),
});
unsafe { msg_send![super(this), init] }
}
fn report_state(&self, window: &NSWindow) {
let state = ns_window_state(window);
if self.ivars().last_state.replace(state.code()) != state.code() {
log::debug!("appkit: window state now {state:?}");
let target = self.ivars().node.unwrap_or(WINDOW_NODE);
emit(target, Event::WindowStateChanged(state));
}
}
}
fn ns_window_state(window: &NSWindow) -> day_spec::WindowState {
if window.isMiniaturized() {
day_spec::WindowState::Minimized
} else if window
.styleMask()
.contains(objc2_app_kit::NSWindowStyleMask::FullScreen)
{
day_spec::WindowState::Fullscreen
} else if window.isZoomed() {
day_spec::WindowState::Maximized
} else {
day_spec::WindowState::Normal
}
}
fn set_ns_window_state(window: &NSWindow, state: day_spec::WindowState) {
use day_spec::WindowState as S;
let current = ns_window_state(window);
if current == state {
return;
}
unsafe {
match current {
S::Minimized => window.deminiaturize(None),
S::Fullscreen => {
window.toggleFullScreen(None);
return;
}
S::Maximized if state != S::Minimized => window.zoom(None),
_ => {}
}
match state {
S::Normal => {}
S::Minimized => window.miniaturize(None),
S::Maximized => {
if !window.isZoomed() {
window.zoom(None);
}
}
S::Fullscreen => window.toggleFullScreen(None),
}
}
}
#[distributed_slice]
pub static RENDERERS: [fn() -> Renderer<AppKit>];
struct SecondaryWin {
window: Retained<NSWindow>,
#[allow(dead_code)] delegate: Retained<DayWinDelegate>,
content: Handle,
}
struct MenuEscapeMonitor(Retained<objc2::runtime::AnyObject>);
impl Drop for MenuEscapeMonitor {
fn drop(&mut self) {
unsafe { NSEvent::removeMonitor(&self.0) };
}
}
pub struct AppKit {
menu_escape_monitor: Option<MenuEscapeMonitor>,
sharing: Option<objc2::rc::Retained<objc2::runtime::AnyObject>>,
sharing_delegate: Option<Retained<DaySharePickerDelegate>>,
mtm: MainThreadMarker,
registry: Registry<AppKit>,
window: Option<Retained<NSWindow>>,
content: Option<Handle>,
secondary: Vec<SecondaryWin>,
app_name: String,
cascade: Option<NSPoint>,
cursors: HashMap<usize, (Retained<DayCursorOwner>, Retained<NSTrackingArea>)>,
}
impl AppKit {
fn swap_text_field(
&mut self,
h: &Handle,
old: &NSTextField,
node: NodeId,
secure: bool,
) -> Retained<NSTextField> {
let new = new_text_field(self.mtm, node, secure);
let (old_key, new_key) = (ptr_of(h), ptr_of(&new));
let window = h.window();
let focused = window
.as_deref()
.is_some_and(|w| owns_first_responder(w, h));
let selection = old.currentEditor().map(|e| e.selectedRange());
unsafe {
new.setStringValue(&old.stringValue());
new.setPlaceholderString(old.placeholderString().as_deref());
new.setEnabled(old.isEnabled());
new.setFont(old.font().as_deref());
new.setFrame(h.frame());
new.setHidden(h.isHidden());
new.setMenu(h.menu().as_deref());
new.setToolTip(h.toolTip().as_deref());
if h.wantsLayer() {
new.setWantsLayer(true);
new.setAlphaValue(h.alphaValue());
let from: *mut objc2::runtime::AnyObject = msg_send![h, layer];
let to: *mut objc2::runtime::AnyObject = msg_send![&*new, layer];
if !from.is_null() && !to.is_null() {
let t: CGAffineTransform = msg_send![from, affineTransform];
let _: () = msg_send![to, setAffineTransform: t];
}
}
if let Some(id) = h.accessibilityIdentifier().filter(|s| !s.is_empty()) {
new.setAccessibilityIdentifier(Some(&id));
}
if let Some(label) = h.accessibilityLabel().filter(|s| !s.is_empty()) {
new.setAccessibilityLabel(Some(&label));
}
if let Some(help) = h.accessibilityHelp().filter(|s| !s.is_empty()) {
new.setAccessibilityHelp(Some(&help));
}
if !h.isAccessibilityElement() {
new.setAccessibilityElement(false);
}
}
for r in &unsafe { h.gestureRecognizers() } {
unsafe {
h.removeGestureRecognizer(&r);
new.addGestureRecognizer(&r);
}
}
GESTURES.with(|m| {
let mut m = m.borrow_mut();
if let Some(targets) = m.remove(&old_key) {
m.insert(new_key, targets);
}
});
if let Some((owner, area)) = self.cursors.remove(&old_key) {
unsafe {
h.removeTrackingArea(&area);
new.addTrackingArea(&area);
}
self.cursors.insert(new_key, (owner, area));
}
PAN_NODES.with(|t| rekey(t, old_key, new_key));
HOVER_NODES.with(|t| rekey(t, old_key, new_key));
ROTATE_NODES.with(|t| rekey(t, old_key, new_key));
ROTATE_ANGLE.with(|t| rekey(t, old_key, new_key));
KEY_NODES.with(|t| rekey(t, old_key, new_key));
FOCUSABLE_NODES.with(|t| rekey(t, old_key, new_key));
CTX_MENU_FNS.with(|t| rekey(t, old_key, new_key));
transfer::moved(h, &new);
unsafe { old.setDelegate(None) };
TARGETS.with(|m| {
m.borrow_mut().remove(&old_key);
});
if let Some(parent) = unsafe { h.superview() } {
unsafe {
parent.addSubview_positioned_relativeTo(
&new,
objc2_app_kit::NSWindowOrderingMode::Above,
Some(h),
);
h.removeFromSuperview();
}
}
if focused && let Some(window) = window {
FIELD_SWAP.with(|s| s.set(true));
let taken = window.makeFirstResponder(Some(&new));
FIELD_SWAP.with(|s| s.set(false));
if taken && let Some(editor) = new.currentEditor() {
let len = unsafe { new.stringValue() }.length();
let range = selection
.filter(|r| r.location.saturating_add(r.length) <= len)
.unwrap_or(objc2_foundation::NSRange::new(len, 0));
editor.setSelectedRange(range);
}
}
day_spec::sidetable::sweep(old_key);
new
}
#[must_use]
pub fn new() -> Self {
let mtm =
MainThreadMarker::new().expect("day-toolkit-appkit must start on the main thread");
let mut registry = Registry::default();
for f in RENDERERS {
registry.register(f());
}
AppKit {
menu_escape_monitor: None,
mtm,
registry,
window: None,
sharing: None,
sharing_delegate: None,
content: None,
secondary: Vec::new(),
app_name: "Day".into(),
cascade: None,
cursors: HashMap::new(),
}
}
#[must_use]
pub fn mtm(&self) -> MainThreadMarker {
self.mtm
}
fn shown_document_content(&self, content: &NSView) -> Option<Handle> {
if content.window().is_none_or(|w| w.isVisible()) {
return None;
}
let main = NSApplication::sharedApplication(self.mtm).mainWindow();
let anchor = main
.filter(|m| {
self.secondary
.iter()
.any(|w| std::ptr::eq(&raw const *w.window, &raw const **m))
})
.or_else(|| {
self.secondary
.iter()
.find(|w| w.window.isVisible())
.map(|w| w.window.clone())
})?;
let shown = anchor
.tabGroup()
.and_then(|g| g.selectedWindow())
.unwrap_or(anchor);
self.secondary
.iter()
.find(|w| std::ptr::eq(&raw const *w.window, &raw const *shown) && w.window.isVisible())
.map(|w| w.content.clone())
}
fn ns_window_for(&self, host: &Handle) -> Option<Retained<NSWindow>> {
if let Some(w) = self
.secondary
.iter()
.find(|w| ptr_of(&w.content) == ptr_of(host))
{
return Some(w.window.clone());
}
self.content
.as_ref()
.filter(|c| ptr_of(c) == ptr_of(host))
.and(self.window.clone())
}
fn make_window(
&self,
options: &WindowOptions,
prefs_style: bool,
node: Option<NodeId>,
) -> (Retained<NSWindow>, Retained<DayWinDelegate>, Handle) {
use day_spec::WindowTabbing as T;
use objc2_app_kit::NSWindowTabbingMode as M;
let mtm = self.mtm;
let (title, size, min_size, tabbing) = (
options.title.as_str(),
options.size,
options.min_size,
&options.tabbing,
);
let content_rect =
NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(size.width, size.height));
let style = window_chrome::style_mask(options, prefs_style);
let window = window_chrome::alloc_window(
mtm,
content_rect,
style,
options.chrome == day_spec::WindowChrome::Frameless,
);
unsafe { window.setReleasedWhenClosed(false) };
window.setTitle(&NSString::from_str(title));
if let Some(min) = min_size {
unsafe { window.setContentMinSize(NSSize::new(min.width, min.height)) };
}
if let Some(max) = options.max_size {
unsafe { window.setContentMaxSize(NSSize::new(max.width, max.height)) };
}
let group = match tabbing {
T::Group(g) | T::Preferred(g) => Some(g.clone()),
T::Automatic | T::Disallowed => None,
};
match tabbing {
_ if prefs_style => window.setTabbingMode(M::Disallowed),
T::Disallowed => window.setTabbingMode(M::Disallowed),
T::Automatic => unsafe {
window.setTabbingIdentifier(&NSString::from_str("day.normal"));
},
T::Group(g) => unsafe { window.setTabbingIdentifier(&NSString::from_str(g)) },
T::Preferred(g) => {
unsafe { window.setTabbingIdentifier(&NSString::from_str(g)) };
window.setTabbingMode(M::Preferred);
}
}
if group.is_some() {
unsafe { NSWindow::setAllowsAutomaticWindowTabbing(true, mtm) };
}
let delegate = DayWinDelegate::new(mtm, node, group);
window.setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
let content = view_of(&DayFlipped::new(mtm));
window.setContentView(Some(&content));
window_chrome::apply(&window, &content, options);
window.setAutorecalculatesKeyViewLoop(true);
(window, delegate, content)
}
}
impl Default for AppKit {
fn default() -> Self {
Self::new()
}
}
fn view_of<T: AsRef<NSView>>(x: &Retained<T>) -> Handle {
Retained::from((**x).as_ref())
}
fn nscolor(c: day_spec::Color) -> Retained<NSColor> {
unsafe { NSColor::colorWithSRGBRed_green_blue_alpha(c.r, c.g, c.b, c.a) }
}
fn ns_text_style(f: Font) -> Option<&'static objc2_app_kit::NSFontTextStyle> {
use objc2_app_kit::*;
unsafe {
Some(match f {
Font::LargeTitle => NSFontTextStyleLargeTitle,
Font::Title => NSFontTextStyleTitle1,
Font::Title2 => NSFontTextStyleTitle2,
Font::Title3 => NSFontTextStyleTitle3,
Font::Headline => NSFontTextStyleHeadline,
Font::Subheadline => NSFontTextStyleSubheadline,
Font::Body => NSFontTextStyleBody,
Font::Callout => NSFontTextStyleCallout,
Font::Footnote => NSFontTextStyleFootnote,
Font::Caption => NSFontTextStyleCaption1,
Font::Caption2 => NSFontTextStyleCaption2,
Font::System(_) | Font::Custom(..) => return None,
})
}
}
fn ns_weight(w: day_spec::FontWeight) -> objc2_app_kit::NSFontWeight {
use day_spec::FontWeight as W;
use objc2_app_kit::*;
unsafe {
match w {
W::UltraLight => NSFontWeightUltraLight,
W::Thin => NSFontWeightThin,
W::Light => NSFontWeightLight,
W::Regular => NSFontWeightRegular,
W::Medium => NSFontWeightMedium,
W::Semibold => NSFontWeightSemibold,
W::Bold => NSFontWeightBold,
W::Heavy => NSFontWeightHeavy,
W::Black => NSFontWeightBlack,
}
}
}
fn nsfm_weight(w: day_spec::FontWeight) -> isize {
use day_spec::FontWeight as W;
match w {
W::UltraLight => 2,
W::Thin => 3,
W::Light => 4,
W::Regular => 5,
W::Medium => 6,
W::Semibold => 8,
W::Bold => 9,
W::Heavy => 10,
W::Black => 12,
}
}
fn canvas_nsfont(size: f64, font: &day_spec::CanvasFont) -> Retained<NSFont> {
use objc2_app_kit::*;
let weight = font.weight.unwrap_or(day_spec::FontWeight::Regular);
let prepared = OFFSCREEN_FONTS.with(|t| {
t.borrow()
.iter()
.find(|(key, _)| key.0 == size.to_bits() && key.1 == *font)
.map(|(_, f)| f.clone())
});
if let Some(f) = prepared {
return f;
}
let Some(mtm) = objc2::MainThreadMarker::new() else {
return unsafe { NSFont::systemFontOfSize_weight(size, ns_weight(weight)) };
};
let manager = unsafe { NSFontManager::sharedFontManager(mtm) };
let mut traits = NSFontTraitMask::empty();
if weight >= day_spec::FontWeight::Semibold {
traits |= NSFontTraitMask::BoldFontMask;
}
if font.italic {
traits |= NSFontTraitMask::ItalicFontMask;
}
let Some(family) = font.family.as_deref() else {
let base = unsafe { NSFont::systemFontOfSize_weight(size, ns_weight(weight)) };
return if font.italic {
unsafe { manager.convertFont_toHaveTrait(&base, NSFontTraitMask::ItalicFontMask) }
} else {
base
};
};
let ns_family = NSString::from_str(family);
if let Some(f) = unsafe {
manager.fontWithFamily_traits_weight_size(&ns_family, traits, nsfm_weight(weight), size)
} {
return f;
}
if let Some(f) = unsafe { NSFont::fontWithName_size(&ns_family, size) } {
return if traits.is_empty() {
f
} else {
unsafe { manager.convertFont_toHaveTrait(&f, traits) }
};
}
log::warn!("unknown font family {family:?} — drawing canvas text in the system font");
unsafe { NSFont::systemFontOfSize_weight(size, ns_weight(weight)) }
}
fn canvas_text_attrs(
font: &NSFont,
color: day_spec::Color,
) -> Retained<objc2_foundation::NSDictionary<NSString, objc2::runtime::AnyObject>> {
let col = nscolor(color);
let keys: [&NSString; 2] = unsafe {
[
objc2_app_kit::NSFontAttributeName,
objc2_app_kit::NSForegroundColorAttributeName,
]
};
let objs: [&objc2::runtime::AnyObject; 2] = [
font as &objc2::runtime::AnyObject,
col.as_ref() as &objc2::runtime::AnyObject,
];
objc2_foundation::NSDictionary::from_slices::<NSString>(&keys, &objs)
}
fn nsfont(spec: day_spec::FontSpec) -> Retained<NSFont> {
use objc2_app_kit::*;
let base: Retained<NSFont> = match spec.style {
Font::System(pt) => {
let w = spec
.weight
.map_or(unsafe { NSFontWeightRegular }, ns_weight);
unsafe { NSFont::systemFontOfSize_weight(pt, w) }
}
Font::Custom(name, pt) => {
if let Some(f) = unsafe { NSFont::fontWithName_size(&NSString::from_str(name), pt) } {
if spec
.weight
.is_some_and(|w| w >= day_spec::FontWeight::Semibold)
{
let mtm =
objc2::MainThreadMarker::new().expect("labels realize on the main thread");
unsafe {
NSFontManager::sharedFontManager(mtm)
.convertFont_toHaveTrait(&f, NSFontTraitMask::BoldFontMask)
}
} else {
f
}
} else {
log::warn!(
"unknown font family {name:?} — falling back to the system font \
(is the file in the project's fonts/ directory?)"
);
let w = spec
.weight
.map_or(unsafe { NSFontWeightRegular }, ns_weight);
unsafe { NSFont::systemFontOfSize_weight(pt, w) }
}
}
style => {
let ts = ns_text_style(style).expect("semantic style");
let opts = objc2_foundation::NSDictionary::new();
let f = unsafe { NSFont::preferredFontForTextStyle_options(ts, &opts) };
match spec.weight {
Some(w) => unsafe { NSFont::systemFontOfSize_weight(f.pointSize(), ns_weight(w)) },
None => f,
}
}
};
let base = if spec.tabular && !matches!(spec.style, Font::Custom(..)) {
let w = spec
.weight
.map_or(unsafe { NSFontWeightRegular }, ns_weight);
unsafe { NSFont::monospacedDigitSystemFontOfSize_weight(base.pointSize(), w) }
} else {
base
};
let base = if spec.monospace && !matches!(spec.style, Font::Custom(..)) {
let w = spec
.weight
.map_or(unsafe { NSFontWeightRegular }, ns_weight);
unsafe { NSFont::monospacedSystemFontOfSize_weight(base.pointSize(), w) }
} else {
base
};
#[expect(
clippy::float_cmp,
reason = "1.0 is the exact unscaled default of `FontSpec::scale`"
)]
let base = if spec.scale == 1.0 {
base
} else {
let mtm = objc2::MainThreadMarker::new().expect("labels realize on the main thread");
let pts = spec.resolved_points(base.pointSize());
unsafe { NSFontManager::sharedFontManager(mtm).convertFont_toSize(&base, pts) }
};
let base = if spec.italic {
let mtm = objc2::MainThreadMarker::new().expect("labels realize on the main thread");
unsafe {
NSFontManager::sharedFontManager(mtm)
.convertFont_toHaveTrait(&base, NSFontTraitMask::ItalicFontMask)
}
} else {
base
};
with_font_features(base, spec.small_caps, spec.oldstyle_figures)
}
fn with_font_features(
font: Retained<NSFont>,
small_caps: bool,
oldstyle: bool,
) -> Retained<NSFont> {
use objc2_app_kit::{
NSFontFeatureSelectorIdentifierKey, NSFontFeatureSettingsAttribute,
NSFontFeatureTypeIdentifierKey,
};
let mut pairs: Vec<(i64, i64)> = Vec::new();
if small_caps {
pairs.push((37, 1));
}
if oldstyle {
pairs.push((21, 0));
}
if pairs.is_empty() {
return font;
}
let settings: Vec<Retained<NSDictionary<NSString, AnyObject>>> = pairs
.into_iter()
.map(|(kind, selector)| {
let kind: Retained<AnyObject> = NSNumber::new_i64(kind).into();
let selector: Retained<AnyObject> = NSNumber::new_i64(selector).into();
unsafe {
NSDictionary::from_retained_objects(
&[
NSFontFeatureTypeIdentifierKey,
NSFontFeatureSelectorIdentifierKey,
],
&[kind, selector],
)
}
})
.collect();
let settings: Retained<AnyObject> = NSArray::from_retained_slice(&settings).into();
let attrs = NSDictionary::from_retained_objects(
&[unsafe { NSFontFeatureSettingsAttribute }],
&[settings],
);
let descriptor = unsafe {
font.fontDescriptor()
.fontDescriptorByAddingAttributes(&attrs)
};
NSFont::fontWithDescriptor_size(&descriptor, font.pointSize()).unwrap_or(font)
}
fn label_line_limit(wraps: bool, max_lines: u32) -> u32 {
if wraps { max_lines } else { 1 }
}
fn label_line_break(lines: u32, mode: day_spec::props::Truncation) -> NSLineBreakMode {
use day_spec::props::Truncation;
if lines != 1 {
return NSLineBreakMode::ByWordWrapping;
}
match mode {
Truncation::Tail => NSLineBreakMode::ByTruncatingTail,
Truncation::Head => NSLineBreakMode::ByTruncatingHead,
Truncation::Middle => NSLineBreakMode::ByTruncatingMiddle,
}
}
fn label_line_points(multiple: f64, font_points: f64) -> Option<f64> {
(multiple.is_finite() && multiple > 0.0).then_some(multiple * font_points)
}
fn natural_line_height(font: &NSFont) -> f64 {
thread_local! {
static METRICS: Retained<objc2_app_kit::NSLayoutManager> =
objc2_app_kit::NSLayoutManager::new();
}
METRICS.with(|metrics| metrics.defaultLineHeightForFont(font))
}
fn apply_label_paragraph(label: &DayLabel) {
let iv = label.ivars();
let p = iv.paragraph.get();
let lines = label_line_limit(iv.wraps.get(), p.max_lines);
let mode = label_line_break(lines, p.truncation);
let rtl = match iv.direction.get() {
Some(d) => d == day_spec::LayoutDirection::Rtl,
None => {
label.userInterfaceLayoutDirection()
== objc2_app_kit::NSUserInterfaceLayoutDirection::RightToLeft
}
};
let writing = match iv.direction.get() {
None => objc2_app_kit::NSWritingDirection::Natural,
Some(_) if rtl => objc2_app_kit::NSWritingDirection::RightToLeft,
Some(_) => objc2_app_kit::NSWritingDirection::LeftToRight,
};
let align = nstextalign(iv.align.get(), rtl);
unsafe {
label.setMaximumNumberOfLines(lines as isize);
if let Some(cell) = label.cell() {
cell.setWraps(lines != 1);
cell.setUsesSingleLineMode(lines == 1);
cell.setTruncatesLastVisibleLine(lines > 0);
}
label.setLineBreakMode(mode);
label.setAlignment(align);
label.setBaseWritingDirection(writing);
}
let needs_string = p.letter_spacing != 0.0 || label_line_points(p.line_height, 1.0).is_some();
if !iv.has_runs.get() && !needs_string && !iv.stamped.get() {
return;
}
if !iv.has_runs.get() {
iv.stamped.set(true);
}
stamp_paragraph(label, &p, mode, align, writing);
}
fn stamp_paragraph(
label: &DayLabel,
p: &day_spec::props::LabelParagraph,
mode: NSLineBreakMode,
align: objc2_app_kit::NSTextAlignment,
writing: objc2_app_kit::NSWritingDirection,
) {
use objc2_foundation::{NSMutableAttributedString, NSRange};
let font = unsafe { label.font() }.unwrap_or_else(|| nsfont(day_spec::FontSpec::default()));
let points = font.pointSize();
unsafe {
let cur = label.attributedStringValue();
let m = NSMutableAttributedString::initWithAttributedString(
NSMutableAttributedString::alloc(),
&cur,
);
let whole = NSRange::new(0, cur.length());
if !label.ivars().has_runs.get() {
let fg = label
.textColor()
.unwrap_or_else(objc2_app_kit::NSColor::labelColor);
m.addAttribute_value_range(objc2_app_kit::NSFontAttributeName, &font, whole);
m.addAttribute_value_range(objc2_app_kit::NSForegroundColorAttributeName, &fg, whole);
}
let style = objc2_app_kit::NSMutableParagraphStyle::new();
style.setAlignment(align);
style.setLineBreakMode(mode);
style.setBaseWritingDirection(writing);
let line = label_line_points(p.line_height, points);
if let Some(h) = line {
style.setMinimumLineHeight(h);
style.setMaximumLineHeight(h);
}
m.addAttribute_value_range(objc2_app_kit::NSParagraphStyleAttributeName, &style, whole);
if p.letter_spacing == 0.0 {
m.removeAttribute_range(objc2_app_kit::NSKernAttributeName, whole);
} else {
let kern = NSNumber::new_f64(p.letter_spacing * points);
m.addAttribute_value_range(objc2_app_kit::NSKernAttributeName, &kern, whole);
}
match line {
Some(h) => {
let offset = NSNumber::new_f64((h - natural_line_height(&font)) / 2.0);
m.addAttribute_value_range(
objc2_app_kit::NSBaselineOffsetAttributeName,
&offset,
whole,
);
}
None => m.removeAttribute_range(objc2_app_kit::NSBaselineOffsetAttributeName, whole),
}
label.setAttributedStringValue(&m);
}
}
fn attributed_label(
text: &str,
base_font: &NSFont,
color: Option<day_spec::Ink>,
role: day_spec::props::TextRole,
runs: &[day_spec::TextRun],
) -> Retained<objc2_foundation::NSAttributedString> {
use objc2_foundation::{NSMutableAttributedString, NSRange};
let ns = NSString::from_str(text);
let s = unsafe {
NSMutableAttributedString::initWithString(NSMutableAttributedString::alloc(), &ns)
};
let whole = NSRange::new(0, ns.length());
unsafe {
s.addAttribute_value_range(objc2_app_kit::NSFontAttributeName, base_font, whole);
let fg = color.map_or_else(
|| match role {
day_spec::props::TextRole::Secondary => {
objc2_app_kit::NSColor::secondaryLabelColor()
}
day_spec::props::TextRole::Primary => objc2_app_kit::NSColor::labelColor(),
},
color::nsink,
);
s.addAttribute_value_range(objc2_app_kit::NSForegroundColorAttributeName, &fg, whole);
}
for r in runs {
let Some(range) = utf16_range(text, &r.range) else {
continue;
};
unsafe {
s.addAttribute_value_range(objc2_app_kit::NSFontAttributeName, &nsfont(r.font), range);
if let Some(c) = r.color {
s.addAttribute_value_range(
objc2_app_kit::NSForegroundColorAttributeName,
&color::nsink(c),
range,
);
}
if let Some(c) = r.background {
s.addAttribute_value_range(
objc2_app_kit::NSBackgroundColorAttributeName,
&color::nsink(c),
range,
);
}
for (line, style_key, color_key) in [
(
r.underline,
objc2_app_kit::NSUnderlineStyleAttributeName,
objc2_app_kit::NSUnderlineColorAttributeName,
),
(
r.strikethrough,
objc2_app_kit::NSStrikethroughStyleAttributeName,
objc2_app_kit::NSStrikethroughColorAttributeName,
),
] {
let Some(d) = line else { continue };
let style = objc2_foundation::NSNumber::new_i64(ns_underline(Some(d)));
s.addAttribute_value_range(style_key, &style, range);
if let Some(c) = d.color {
s.addAttribute_value_range(color_key, &color::nsink(c), range);
}
}
if let Some(url) = r.link.as_deref() {
let value = NSString::from_str(url);
s.addAttribute_value_range(objc2_app_kit::NSLinkAttributeName, &value, range);
}
}
}
s.into_super()
}
fn ns_underline(u: Option<day_spec::Decoration>) -> i64 {
use day_spec::DecorationStyle as U;
match u.map(|d| d.style) {
None => 0,
Some(U::Single) => 0x01,
Some(U::Double) => 0x09,
Some(U::Dotted) => 0x01 | 0x0100,
Some(U::Wavy) => 0x01 | 0x0400,
}
}
fn utf16_range(text: &str, r: &std::ops::Range<usize>) -> Option<objc2_foundation::NSRange> {
let start = text.get(..r.start)?.encode_utf16().count();
let len = text.get(r.clone())?.encode_utf16().count();
Some(objc2_foundation::NSRange::new(start, len))
}
fn register_bundled_fonts() {
#[link(name = "CoreText", kind = "framework")]
unsafe extern "C" {
fn CTFontManagerRegisterFontsForURL(
font_url: *const std::ffi::c_void,
scope: u32, error: *mut *const std::ffi::c_void,
) -> bool;
}
for path in day_spec::fonts::bundled_fonts() {
let url = unsafe {
objc2_foundation::NSURL::fileURLWithPath(&NSString::from_str(&path.to_string_lossy()))
};
let ok = unsafe {
CTFontManagerRegisterFontsForURL(
Retained::as_ptr(&url).cast::<std::ffi::c_void>(),
1,
std::ptr::null_mut(),
)
};
if !ok {
log::warn!("could not register bundled font {}", path.display());
}
}
}
fn nstextalign(a: day_spec::props::TextAlign, rtl: bool) -> objc2_app_kit::NSTextAlignment {
match a {
day_spec::props::TextAlign::Leading => objc2_app_kit::NSTextAlignment::Natural,
day_spec::props::TextAlign::Justified => objc2_app_kit::NSTextAlignment::Justified,
day_spec::props::TextAlign::Center => objc2_app_kit::NSTextAlignment::Center,
day_spec::props::TextAlign::Trailing if rtl => objc2_app_kit::NSTextAlignment::Left,
day_spec::props::TextAlign::Trailing => objc2_app_kit::NSTextAlignment::Right,
}
}
fn content_of(parent: &Handle) -> Retained<NSView> {
if let Some(sv) = parent.downcast_ref::<NSScrollView>()
&& let Some(doc) = unsafe { sv.documentView() }
{
return doc;
}
parent.clone()
}
fn warn_missing_renderer(kind: PieceKind) {
day_spec::placeholder::report(kind, "appkit");
}
pub(crate) fn placeholder_view(mtm: MainThreadMarker, kind: PieceKind) -> Handle {
view_of(&unsafe {
NSTextField::labelWithString(&NSString::from_str(&format!("⟨{kind}⟩")), mtm)
})
}
impl Toolkit for AppKit {
fn ui_idle(&mut self) -> bool {
toolbar::chrome_settled()
}
type Handle = Handle;
fn system_color(&mut self, role: day_spec::SystemColor) -> day_spec::Color {
let result = std::cell::Cell::new(role.fallback(self.dark_mode()));
let block = block2::StackBlock::new(|| {
if let Some(rgb) = color::srgb(&color::role_color(role)) {
result.set(rgb);
}
});
NSApplication::sharedApplication(self.mtm())
.effectiveAppearance()
.performAsCurrentDrawingAppearance(&block);
result.get()
}
fn dark_mode(&mut self) -> bool {
let app = NSApplication::sharedApplication(self.mtm());
app.effectiveAppearance()
.name()
.to_string()
.contains("Dark")
}
fn reduce_motion(&mut self) -> bool {
objc2_app_kit::NSWorkspace::sharedWorkspace().accessibilityDisplayShouldReduceMotion()
}
fn set_app_badge(&mut self, badge: &day_spec::AppBadge) {
use day_spec::AppBadge;
let label = match badge {
AppBadge::None | AppBadge::Count(0) => None,
AppBadge::Count(n) => Some(n.to_string()),
AppBadge::Text(t) if t.is_empty() => None,
AppBadge::Text(t) => Some(t.clone()),
AppBadge::Dot => Some("\u{2022}".to_string()),
};
let tile = NSApplication::sharedApplication(self.mtm()).dockTile();
let ns = label.map(|l| objc2_foundation::NSString::from_str(&l));
unsafe { tile.setBadgeLabel(ns.as_deref()) };
tile.display();
}
fn set_appearance(&mut self, dark: Option<bool>) {
let app = NSApplication::sharedApplication(self.mtm());
let appearance = dark.and_then(|d| {
objc2_app_kit::NSAppearance::appearanceNamed(unsafe {
if d {
objc2_app_kit::NSAppearanceNameDarkAqua
} else {
objc2_app_kit::NSAppearanceNameAqua
}
})
});
unsafe { app.setAppearance(appearance.as_deref()) };
color::refresh_layers_soon();
}
fn attach_refresh(&mut self, handle: &Self::Handle, host: NodeId) {
controls::refresh_gesture::attach(handle, host);
}
fn set_presented(&mut self, handle: &Self::Handle, visible: bool) {
unsafe {
handle.setHidden(!visible);
}
}
#[expect(
clippy::match_same_arms,
reason = "one arm per capability, each documented with the AppKit mechanism behind it"
)]
fn capability(&self, cap: Cap) -> Support {
match cap {
Cap::ShareActions => Support::Native,
Cap::SearchField => Support::Native,
Cap::ComboBox => Support::Native,
Cap::Stepper => Support::Native,
Cap::DateTimePicker => Support::Native,
Cap::ColorPicker => Support::Native,
Cap::PullRefresh => Support::Emulated,
Cap::Pager => Support::Emulated,
Cap::PullRefreshGesture => Support::Emulated,
Cap::Cursor => Support::Native,
Cap::FontList => Support::Native,
Cap::ListItem => Support::Native,
Cap::ImageDecode | Cap::ImageEncode | Cap::ImageProperties => Support::Native,
Cap::CanvasBitmap | Cap::CanvasPdf => Support::Native,
Cap::Announce => Support::Native,
Cap::ReduceMotion => Support::Native,
Cap::Snapshot
| Cap::NativeSymbols
| Cap::NavTabs
| Cap::NavSplit
| Cap::NavRepresent
| Cap::NavContentList
| Cap::Dialogs
| Cap::FileDialogs
| Cap::FolderDialog
| Cap::Animation
| Cap::TextEditable
| Cap::TextSelectable
| Cap::TextSpellCheck
| Cap::NavReorder | Cap::ListReorder
| Cap::ListRecycling
| Cap::ListSwipeActions
| Cap::ListSections
| Cap::Tree
| Cap::TreeSections
| Cap::TreeMove
| Cap::MultiWindow
| Cap::WindowStates
| Cap::WindowFullscreen
| Cap::ContentProtection
| Cap::WindowTabbing
| Cap::DockMenu
| Cap::FramelessWindow
| Cap::OverlayTitleBar
| Cap::TransparentWindow
| Cap::WindowMaterial
| Cap::DragRegion
| Cap::StatusItem
| Cap::AppProgress
| Cap::DockVisibility
| Cap::WindowGeometry
| Cap::WindowPosition
| Cap::WindowLevel
| Cap::WindowAppearance
| Cap::RequestAttention
| Cap::Monitors
| Cap::DynamicShortcuts
| Cap::Toolbar
| Cap::ToolbarSearch
| Cap::AppMenu
| Cap::AppBadgeCount
| Cap::AppBadgeText
| Cap::UndoBridge
| Cap::DragDrop | Cap::DragExternalImport | Cap::DragExternalExport | Cap::DragMultipleItems | Cap::DragFileReferences | Cap::EditBridge
| Cap::AppBadgeDot
| Cap::Appearance
| Cap::BaselineAlignment
| Cap::Inspector
| Cap::Split
| Cap::TextRuns
| Cap::TextLinks => Support::Native,
Cap::Cover => Support::Emulated,
Cap::CoverSheet => Support::Native,
Cap::Focusable => Support::Native,
Cap::KeyPhases => Support::Native,
Cap::HitSlop => Support::Native,
Cap::LabelSelection => Support::Native,
Cap::ScrollOffset => Support::Native,
Cap::ScrollPhases => Support::Native,
Cap::ScrollSnap => Support::Emulated,
Cap::ScrollZoom => Support::Native,
Cap::ListViewport | Cap::ListScrollAlign => Support::Native,
Cap::ListReaderScroll => Support::Native,
Cap::ScrollKeyboardDismiss | Cap::PagerProgress => Support::Unsupported,
_ => Support::Unsupported,
}
}
fn realize(&mut self, kind: PieceKind, props: &dyn Any, id: NodeId) -> Handle {
let mtm = self.mtm;
match Builtin::from_key(kind) {
Some(Builtin::Container) => {
let v = DayFlipped::new(mtm);
if let Some(p) = props.downcast_ref::<ContainerProps>() {
if p.role == Some(day_spec::SurfaceRole::SectionCard) {
v.set_section_card(p.corner_radius);
} else if p.fill.is_some()
|| p.border.is_some()
|| p.shadow.is_some()
|| p.corner_radius > 0.0
|| p.clips
{
v.set_surface(p);
}
}
view_of(&v)
}
Some(Builtin::Scroll) => {
let p = props
.downcast_ref::<day_spec::props::ScrollProps>()
.cloned()
.unwrap_or_default();
scroll::realize(mtm, id, &p)
}
Some(Builtin::Label) => {
let Some(p) = props_of::<LabelProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
let tf = DayLabel::new(mtm, id, &p.text);
tf.ivars().wraps.set(p.wraps);
tf.ivars().paragraph.set(p.paragraph());
tf.ivars().align.set(p.align);
tf.ivars().reports_selection.set(p.reports_selection);
tf.ivars().color.set(p.color);
tf.ivars().role.set(p.role);
unsafe { tf.setFont(Some(&nsfont(p.font))) };
match (p.color, p.role) {
(Some(c), _) => unsafe { tf.setTextColor(Some(&color::nsink(c))) },
(None, day_spec::props::TextRole::Secondary) => unsafe {
tf.setTextColor(Some(&objc2_app_kit::NSColor::secondaryLabelColor()));
},
(None, day_spec::props::TextRole::Primary) => {}
}
if !p.runs.is_empty() {
let s = attributed_label(&p.text, &nsfont(p.font), p.color, p.role, &p.runs);
unsafe { tf.setAttributedStringValue(&s) };
}
tf.ivars().has_runs.set(!p.runs.is_empty());
tf.set_links(p.runs.iter().any(|r| r.link.is_some()));
apply_label_paragraph(&tf);
view_of(&Retained::into_super(tf))
}
Some(Builtin::Button) => {
let Some(p) = props_of::<ButtonProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
let target = DayTarget::new(mtm, id);
let btn = unsafe {
NSButton::buttonWithTitle_target_action(
&NSString::from_str(&p.title),
Some(&*target),
Some(sel!(action:)),
mtm,
)
};
apply_button_style(&btn, p.style);
if p.icon.is_some() {
apply_button_content(&btn, &p.title, p.icon.as_ref(), p.icon_only);
}
unsafe { btn.setEnabled(p.enabled) };
set_button_selected(&btn, p.selected);
let view = view_of(&btn);
TARGETS.with(|m| m.borrow_mut().insert(ptr_of(&view), target));
view
}
Some(Builtin::Toggle) => {
let Some(p) = props_of::<ToggleProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
let target = DayTarget::new(mtm, id);
let sw = unsafe { NSSwitch::new(mtm) };
unsafe {
sw.setState(isize::from(p.on));
sw.setEnabled(p.enabled);
sw.setTarget(Some(&*target));
sw.setAction(Some(sel!(action:)));
}
let view = view_of(&sw);
TARGETS.with(|m| m.borrow_mut().insert(ptr_of(&view), target));
view
}
Some(Builtin::Slider) => {
let Some(p) = props_of::<SliderProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
let target = DayTarget::new(mtm, id);
let sl = unsafe {
NSSlider::sliderWithValue_minValue_maxValue_target_action(
p.value,
p.min,
p.max,
Some(&*target),
Some(sel!(action:)),
mtm,
)
};
unsafe { sl.setContinuous(true) };
let view = view_of(&sl);
TARGETS.with(|m| m.borrow_mut().insert(ptr_of(&view), target));
view
}
Some(Builtin::Picker) => picker::realize_any(self, props, id),
Some(Builtin::TextArea) => textarea::realize_any(self, props, id),
Some(Builtin::TextField) => {
let Some(p) = props_of::<TextFieldProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
let tf = new_text_field(mtm, id, false);
unsafe {
tf.setStringValue(&NSString::from_str(&p.text));
tf.setPlaceholderString(Some(&NSString::from_str(&p.placeholder)));
}
view_of(&tf)
}
Some(Builtin::Divider) => {
let b = unsafe { NSBox::new(mtm) };
unsafe { b.setBoxType(NSBoxType::Separator) };
view_of(&b)
}
Some(Builtin::Progress) => {
let Some(p) = props_of::<ProgressProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
if p.value.is_none() {
return crate::controls::spinner::make(self, p, id);
}
let pi = unsafe { NSProgressIndicator::new(mtm) };
unsafe {
if let Some(v) = p.value {
pi.setStyle(NSProgressIndicatorStyle::Bar);
pi.setIndeterminate(false);
pi.setMinValue(0.0);
pi.setMaxValue(1.0);
pi.setDoubleValue(v);
} else {
pi.setStyle(NSProgressIndicatorStyle::Spinning);
pi.setIndeterminate(true);
pi.startAnimation(None);
}
}
view_of(&pi)
}
Some(Builtin::Canvas) => {
let canvas = DayCanvas::new(mtm);
KEY_NODES.with(|t| t.insert(Retained::as_ptr(&canvas) as usize, id));
view_of(&canvas)
}
Some(Builtin::Inspector) => {
let (visible, width, leading) =
props
.downcast_ref::<InspectorProps>()
.map_or((false, 280.0, false), |p| {
(
p.visible,
p.width,
p.edge == day_spec::props::PaneEdge::Leading,
)
});
let content_wrap = view_of(&DayFlipped::new(mtm));
let panel_flipped = DayFlipped::new(mtm);
panel_flipped.set_pane_backdrop();
let panel_wrap = view_of(&panel_flipped);
let split_vc = unsafe { objc2_app_kit::NSSplitViewController::new(mtm) };
let _ = unsafe { split_vc.view() };
let content_vc = unsafe { objc2_app_kit::NSViewController::new(mtm) };
let panel_vc = unsafe { objc2_app_kit::NSViewController::new(mtm) };
unsafe {
content_vc.setView(&content_wrap);
panel_vc.setView(&panel_wrap);
}
let content_item = unsafe {
objc2_app_kit::NSSplitViewItem::splitViewItemWithViewController(&content_vc)
};
let panel_item = unsafe {
if leading {
objc2_app_kit::NSSplitViewItem::splitViewItemWithViewController(&panel_vc)
} else {
objc2_app_kit::NSSplitViewItem::inspectorWithViewController(&panel_vc)
}
};
unsafe {
panel_item.setCanCollapse(false);
panel_item.setCollapsed(!visible);
panel_item.setMinimumThickness(width);
panel_item.setMaximumThickness(width);
panel_item.setAllowsFullHeightLayout(true);
if leading {
split_vc.addSplitViewItem(&panel_item);
split_vc.addSplitViewItem(&content_item);
} else {
split_vc.addSplitViewItem(&content_item);
split_vc.addSplitViewItem(&panel_item);
}
}
let split = unsafe { split_vc.splitView() };
unsafe {
split.setVertical(true);
split.setDividerStyle(objc2_app_kit::NSSplitViewDividerStyle::Thin);
split.setTranslatesAutoresizingMaskIntoConstraints(true);
}
let view = view_of(&split);
INSPECTOR_STATE.with(|m| {
m.borrow_mut().insert(
ptr_of(&view),
InspectorState {
content_wrap,
panel_wrap,
split_vc,
panel_item,
},
)
});
view
}
Some(Builtin::InspectorPane) => {
let pane = view_of(&DayNavPage::new(mtm, id));
NAV_PAGES.with(|set| set.borrow_mut().insert(ptr_of(&pane)));
let panel = props
.downcast_ref::<InspectorPaneProps>()
.is_some_and(|p| p.panel);
INSPECTOR_PANES.with(|t| t.insert(ptr_of(&pane), panel));
pane
}
Some(Builtin::Split) => {
let (axis, fraction, min_pane) = props
.downcast_ref::<SplitProps>()
.map_or((day_spec::props::SplitAxis::Horizontal, 0.5, 80.0), |p| {
(p.axis, p.fraction, p.min_pane)
});
let wrap = view_of(&DayFlipped::new(mtm));
let split = unsafe { objc2_app_kit::NSSplitView::new(mtm) };
let first_wrap = view_of(&DayFlipped::new(mtm));
let second_wrap = view_of(&DayFlipped::new(mtm));
unsafe {
split.setVertical(axis == day_spec::props::SplitAxis::Horizontal);
split.setDividerStyle(objc2_app_kit::NSSplitViewDividerStyle::Thin);
split.addArrangedSubview(&first_wrap);
split.addArrangedSubview(&second_wrap);
split.setFrame(wrap.bounds());
split.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable
| objc2_app_kit::NSAutoresizingMaskOptions::ViewHeightSizable,
);
wrap.addSubview(&split);
}
let delegate = DaySplitDelegate::new(mtm, id, min_pane, fraction);
split.setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
SPLIT_STATE.with(|m| {
m.borrow_mut().insert(
ptr_of(&wrap),
SplitState {
split,
first_wrap,
second_wrap,
delegate,
},
)
});
wrap
}
Some(Builtin::SplitPane) => {
let pane = view_of(&DayNavPage::new(mtm, id));
NAV_PAGES.with(|set| set.borrow_mut().insert(ptr_of(&pane)));
let second = props
.downcast_ref::<SplitPaneProps>()
.is_some_and(|p| p.second);
SPLIT_PANES.with(|t| t.insert(ptr_of(&pane), second));
pane
}
Some(Builtin::Nav) => {
let presentation = props
.downcast_ref::<NavProps>()
.map_or(NavPresentation::Split, |p| p.presentation);
let is_split = presentation.is_split();
let sidebar_wrap = view_of(&DayFlipped::new(mtm));
let detail_wrap = view_of(&DayFlipped::new(mtm));
let split_vc = unsafe { objc2_app_kit::NSSplitViewController::new(mtm) };
let _ = unsafe { split_vc.view() };
let sidebar_vc = unsafe { objc2_app_kit::NSViewController::new(mtm) };
let detail_vc = unsafe { objc2_app_kit::NSViewController::new(mtm) };
unsafe {
sidebar_vc.setView(&pane_under_title_bar(mtm, &sidebar_wrap));
detail_vc.setView(&pane_under_title_bar(mtm, &detail_wrap));
}
let sidebar_item = unsafe {
objc2_app_kit::NSSplitViewItem::sidebarWithViewController(&sidebar_vc)
};
let detail_item = unsafe {
objc2_app_kit::NSSplitViewItem::splitViewItemWithViewController(&detail_vc)
};
let list_width = props.downcast_ref::<NavProps>().and_then(|p| p.list_width);
let list_visible = props
.downcast_ref::<NavProps>()
.is_none_or(|p| p.list_visible);
let (list_wrap, list_item) = match list_width {
Some(w) => {
let wrap = view_of(&DayFlipped::new(mtm));
let vc = unsafe { objc2_app_kit::NSViewController::new(mtm) };
unsafe { vc.setView(&pane_under_title_bar(mtm, &wrap)) };
let item = unsafe {
objc2_app_kit::NSSplitViewItem::contentListWithViewController(&vc)
};
unsafe {
item.setCanCollapse(true);
item.setMinimumThickness(NAV_LIST_MIN_W.min(w));
item.setMaximumThickness(NAV_LIST_MAX_W.max(w));
item.setCollapsed(!list_visible);
}
(Some(wrap), Some(item))
}
None => (None, None),
};
unsafe {
sidebar_item.setCanCollapse(is_split);
sidebar_item.setCollapsed(!is_split);
if is_split {
sidebar_item.setAllowsFullHeightLayout(true);
sidebar_item.setMinimumThickness(NAV_SIDEBAR_MIN_W);
sidebar_item.setMaximumThickness(NAV_SIDEBAR_MAX_W);
} else {
sidebar_item.setMinimumThickness(0.0);
sidebar_item.setMaximumThickness(0.0);
}
split_vc.addSplitViewItem(&sidebar_item);
if let Some(item) = list_item.as_ref() {
split_vc.addSplitViewItem(item);
}
split_vc.addSplitViewItem(&detail_item);
}
let split = unsafe { split_vc.splitView() };
unsafe {
let after = split.nextResponder();
split.setNextResponder(Some(&split_vc));
let own_view = split_vc.view();
let vc_resp: *const objc2_app_kit::NSResponder = {
let r: &objc2_app_kit::NSResponder = &split_vc;
r
};
let view_resp: *const objc2_app_kit::NSResponder = {
let r: &objc2_app_kit::NSResponder = &own_view;
r
};
let upstream = after.filter(|a| {
let p: *const objc2_app_kit::NSResponder = &raw const **a;
p != vc_resp && p != view_resp
});
split_vc.setNextResponder(upstream.as_deref());
}
unsafe {
split.setVertical(true);
split.setDividerStyle(objc2_app_kit::NSSplitViewDividerStyle::Thin);
split.setTranslatesAutoresizingMaskIntoConstraints(true);
}
let root_title = props
.downcast_ref::<NavProps>()
.map_or_default(|p| p.title.clone());
let header = if presentation == NavPresentation::Stack {
Some(build_nav_header(mtm, id, &detail_wrap, &root_title))
} else {
None
};
let tabs = (presentation == NavPresentation::Tabs).then(|| {
let tv = unsafe { objc2_app_kit::NSTabView::new(mtm) };
let documents = props
.downcast_ref::<NavProps>()
.is_some_and(|p| p.documents.is_some());
let (inset, inset_top) = if documents {
(0.0, 0.0)
} else {
(NAV_TABVIEW_INSET, NAV_TABVIEW_INSET_TOP)
};
let b = detail_wrap.bounds();
let frame = NSRect::new(
NSPoint::new(inset, inset_top),
NSSize::new(
(b.size.width - 2.0 * inset).max(0.0),
(b.size.height - inset_top - inset).max(0.0),
),
);
unsafe {
tv.setTabViewType(if documents {
objc2_app_kit::NSTabViewType::NoTabsNoBorder
} else {
objc2_app_kit::NSTabViewType::TopTabsBezelBorder
});
tv.setFrame(frame);
tv.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable
| objc2_app_kit::NSAutoresizingMaskOptions::ViewHeightSizable,
);
detail_wrap.addSubview(&tv);
}
TabHost {
view: tv,
target: None,
}
});
let view = view_of(&split);
NAV_STATE.with(|m| {
m.borrow_mut().insert(
ptr_of(&view),
NavState {
sidebar_wrap,
detail_wrap,
split_vc,
sidebar_item,
list_wrap,
list_item,
pages: Vec::new(),
list_page: None,
sidebar_page: None,
list_width,
list_visible,
list_positioned: false,
positioned: false,
presentation,
selected: 0,
tabs,
header,
root_title,
node: id,
title_strip: 0.0,
},
)
});
view
}
Some(Builtin::NavPage) => {
let page = view_of(&DayNavPage::new(mtm, id));
NAV_PAGES.with(|set| set.borrow_mut().insert(ptr_of(&page)));
if let Some(p) = props.downcast_ref::<day_spec::props::NavPageProps>() {
PAGE_PANE.with(|t| t.insert(ptr_of(&page), p.pane));
}
page
}
Some(Builtin::Cover) => {
let page = DayNavPage::new(mtm, id);
let cover_props = props.downcast_ref::<CoverProps>();
page.ivars()
.sheet
.set(cover_props.is_some_and(|p| p.sheet || p.half));
page.ivars().half.set(cover_props.is_some_and(|p| p.half));
page.ivars().large.set(cover_props.is_some_and(|p| p.large));
unsafe { page.setHidden(true) };
view_of(&page)
}
Some(Builtin::NavMenu) => {
let Some(p) = props_of::<NavMenuProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
let data = DayNavMenuData::new(
mtm,
id,
&p.items,
&p.icons,
&p.badges,
&p.badge_icons,
&p.badge_tints,
&p.sections,
&p.tints,
&p.menus,
);
(*data.ivars().movable.borrow_mut()).clone_from(&p.movable);
data.ivars().regroup.set(p.regroup);
(*data.ivars().sorted.borrow_mut()).clone_from(&p.sorted);
let outline: Retained<objc2_app_kit::NSOutlineView> = {
let o: Retained<DayNavOutlineView> =
unsafe { msg_send![DayNavOutlineView::alloc(mtm), init] };
NAV_OUTLINE_MENUS
.with(|t| t.insert(Retained::as_ptr(&o) as usize, data.clone()));
unsafe { msg_send![&*o, self] }
};
let col = unsafe {
objc2_app_kit::NSTableColumn::initWithIdentifier(
objc2_app_kit::NSTableColumn::alloc(mtm),
&NSString::from_str("item"),
)
};
unsafe {
outline.addTableColumn(&col);
outline.setOutlineTableColumn(Some(&col));
outline.setHeaderView(None);
outline.setStyle(objc2_app_kit::NSTableViewStyle::SourceList);
outline.setRowSizeStyle(objc2_app_kit::NSTableViewRowSizeStyle::Default);
outline.setFloatsGroupRows(true);
outline.setIndentationPerLevel(0.0);
outline.registerForDraggedTypes(
&objc2_foundation::NSArray::from_retained_slice(&[NSString::from_str(
DAY_NAV_PASTEBOARD_TYPE,
)]),
);
outline.setDraggingSourceOperationMask_forLocal(
objc2_app_kit::NSDragOperation::Move,
true,
);
outline.setDraggingSourceOperationMask_forLocal(
objc2_app_kit::NSDragOperation::None,
false,
);
outline.setDraggingDestinationFeedbackStyle(
objc2_app_kit::NSTableViewDraggingDestinationFeedbackStyle::Gap,
);
outline.setDataSource(Some(ProtocolObject::from_ref(&*data)));
outline.setDelegate(Some(ProtocolObject::from_ref(&*data)));
outline.reloadData();
}
let scroll = unsafe { NSScrollView::new(mtm) };
unsafe {
scroll.setDrawsBackground(false);
scroll.setHasVerticalScroller(true);
scroll.setScrollerStyle(objc2_app_kit::NSScrollerStyle::Overlay);
scroll.setHasHorizontalScroller(false);
outline.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable,
);
outline.setColumnAutoresizingStyle(
objc2_app_kit::NSTableViewColumnAutoresizingStyle::UniformColumnAutoresizingStyle,
);
scroll.setDocumentView(Some(&outline));
}
let view = view_of(&scroll);
if let Some(row) = p.selected.and_then(|sel| data.row_of_item(sel)) {
data.ivars().suppress.set(true);
unsafe {
outline.selectRowIndexes_byExtendingSelection(
&objc2_foundation::NSIndexSet::indexSetWithIndex(row),
false,
);
};
data.ivars().suppress.set(false);
}
NAV_MENUS.with(|m| m.borrow_mut().insert(ptr_of(&view), (outline, data)));
view
}
Some(Builtin::List) => {
let Some(p) = props_of::<ListProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
let table: Retained<DayListView> =
unsafe { msg_send![DayListView::alloc(mtm), init] };
let table = Retained::into_super(table);
if p.inset_grouped {
unsafe { table.setStyle(objc2_app_kit::NSTableViewStyle::Inset) };
}
let col = unsafe {
NSTableColumn::initWithIdentifier(
NSTableColumn::alloc(mtm),
&NSString::from_str("day.list.col"),
)
};
let data = DayListData::new(
mtm,
id,
p.selectable,
p.multi_select,
p.row_height == RowHeight::Automatic,
p.sections,
);
unsafe {
if p.multi_select {
table.setAllowsMultipleSelection(true);
}
table.addTableColumn(&col);
table.setHeaderView(None);
table.setStyle(objc2_app_kit::NSTableViewStyle::Plain);
table.setIntercellSpacing(NSSize::new(0.0, 0.0));
if p.separators == Some(true) {
table.setGridStyleMask(
objc2_app_kit::NSTableViewGridLineStyle::SolidHorizontalGridLineMask,
);
table.setGridColor(&objc2_app_kit::NSColor::separatorColor());
}
table.setColumnAutoresizingStyle(
objc2_app_kit::NSTableViewColumnAutoresizingStyle::UniformColumnAutoresizingStyle,
);
table.setFloatsGroupRows(p.sections);
match p.row_height {
RowHeight::Uniform(h) => table.setRowHeight(h),
RowHeight::Automatic => table.setRowHeight(44.0),
}
if !p.selectable {
table.setSelectionHighlightStyle(
objc2_app_kit::NSTableViewSelectionHighlightStyle::None,
);
}
table.setBackgroundColor(&objc2_app_kit::NSColor::clearColor());
table.setDataSource(Some(ProtocolObject::from_ref(&*data)));
table.setDelegate(Some(ProtocolObject::from_ref(&*data)));
table.setTarget(Some(&*data));
if p.activatable {
table.setDoubleAction(Some(sel!(doubleClickRow:)));
}
if p.reorderable {
table.registerForDraggedTypes(
&objc2_foundation::NSArray::from_retained_slice(&[NSString::from_str(
DAY_ROW_PASTEBOARD_TYPE,
)]),
);
table.setDraggingSourceOperationMask_forLocal(
objc2_app_kit::NSDragOperation::Move,
true,
);
table.setDraggingSourceOperationMask_forLocal(
objc2_app_kit::NSDragOperation::None,
false,
);
table.setDraggingDestinationFeedbackStyle(
objc2_app_kit::NSTableViewDraggingDestinationFeedbackStyle::Gap,
);
}
}
let scroll: Retained<DayListScrollView> =
unsafe { msg_send![DayListScrollView::alloc(mtm), init] };
let scroll = Retained::into_super(scroll);
unsafe {
scroll.setDrawsBackground(false);
scroll.setHasVerticalScroller(true);
scroll.setScrollerStyle(objc2_app_kit::NSScrollerStyle::Overlay);
scroll.setDocumentView(Some(&table));
let clip = scroll.contentView();
clip.setPostsBoundsChangedNotifications(true);
clip.setPostsFrameChangedNotifications(true);
let center = objc2_foundation::NSNotificationCenter::defaultCenter();
for name in [
objc2_app_kit::NSViewBoundsDidChangeNotification,
objc2_app_kit::NSViewFrameDidChangeNotification,
] {
center.addObserver_selector_name_object(
&data,
sel!(listViewportChanged:),
Some(name),
Some(&clip),
);
}
for (name, selector) in [
(
objc2_app_kit::NSScrollViewWillStartLiveScrollNotification,
sel!(listLiveScrollStarted:),
),
(
objc2_app_kit::NSScrollViewDidEndLiveScrollNotification,
sel!(listLiveScrollEnded:),
),
] {
center.addObserver_selector_name_object(
&data,
selector,
Some(name),
Some(&scroll),
);
}
}
let view = view_of(&scroll);
LIST_STATE.with(|m| m.borrow_mut().insert(ptr_of(&view), (table, data)));
view
}
Some(Builtin::Tree) => {
let Some(p) = props_of::<TreeProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
let outline: Retained<objc2_app_kit::NSOutlineView> = {
let o: Retained<DayOutlineView> =
unsafe { msg_send![DayOutlineView::alloc(mtm), init] };
unsafe { msg_send![&*o, self] }
};
let col = unsafe {
NSTableColumn::initWithIdentifier(
NSTableColumn::alloc(mtm),
&NSString::from_str("day.tree.col"),
)
};
let data = DayTreeData::new(mtm, id, p.selectable);
unsafe {
if p.multi_select {
outline.setAllowsMultipleSelection(true);
}
outline.addTableColumn(&col);
outline.setOutlineTableColumn(Some(&col));
outline.setHeaderView(None);
if p.section_headers {
outline.setStyle(objc2_app_kit::NSTableViewStyle::SourceList);
outline.setFloatsGroupRows(true);
} else {
outline.setStyle(objc2_app_kit::NSTableViewStyle::FullWidth);
outline.setIntercellSpacing(NSSize::new(0.0, 0.0));
}
outline.setColumnAutoresizingStyle(
objc2_app_kit::NSTableViewColumnAutoresizingStyle::UniformColumnAutoresizingStyle,
);
outline.setAutoresizesOutlineColumn(true);
if let Some(indent) = p.indent {
outline.setIndentationPerLevel(indent);
}
match p.row_height {
RowHeight::Uniform(h) => outline.setRowHeight(h),
RowHeight::Automatic if !p.section_headers => outline.setRowHeight(24.0),
RowHeight::Automatic => {}
}
if !p.selectable {
outline.setSelectionHighlightStyle(
objc2_app_kit::NSTableViewSelectionHighlightStyle::None,
);
}
outline.setBackgroundColor(&objc2_app_kit::NSColor::clearColor());
outline.setDataSource(Some(ProtocolObject::from_ref(&*data)));
outline.setDelegate(Some(ProtocolObject::from_ref(&*data)));
if p.movable {
outline.registerForDraggedTypes(
&objc2_foundation::NSArray::from_retained_slice(&[NSString::from_str(
DAY_TREE_PASTEBOARD_TYPE,
)]),
);
outline.setDraggingSourceOperationMask_forLocal(
objc2_app_kit::NSDragOperation::Move,
true,
);
outline.setDraggingSourceOperationMask_forLocal(
objc2_app_kit::NSDragOperation::None,
false,
);
}
}
let scroll = unsafe { NSScrollView::new(mtm) };
unsafe {
scroll.setDrawsBackground(false);
scroll.setHasVerticalScroller(true);
scroll.setHasHorizontalScroller(false);
scroll.setScrollerStyle(objc2_app_kit::NSScrollerStyle::Overlay);
outline.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable,
);
scroll.setDocumentView(Some(&outline));
}
let view = view_of(&scroll);
TREE_STATE.with(|m| m.borrow_mut().insert(ptr_of(&view), (outline, data)));
view
}
Some(Builtin::Image) => {
let Some(p) = props_of::<ImageProps>(kind, "appkit", props) else {
return placeholder_view(mtm, kind);
};
let iv: Retained<objc2_app_kit::NSImageView> =
if p.content_mode == ContentMode::Fill {
let view: Retained<FillImageView> =
unsafe { msg_send![FillImageView::alloc(mtm), init] };
Retained::from(
<FillImageView as AsRef<objc2_app_kit::NSImageView>>::as_ref(&view),
)
} else {
unsafe { objc2_app_kit::NSImageView::new(mtm) }
};
let scaling = match p.content_mode {
ContentMode::Stretch => objc2_app_kit::NSImageScaling::ScaleAxesIndependently,
_ => objc2_app_kit::NSImageScaling::ScaleProportionallyUpOrDown,
};
unsafe { iv.setImageScaling(scaling) };
if let Some(img) = appkit_image_for(&p.source) {
let img = image_rendered(img, p.tint.is_some() || p.template);
unsafe { iv.setImage(Some(&img)) };
}
if let Some(t) = p.tint {
unsafe { iv.setContentTintColor(Some(&color::nsink(t))) };
}
IMAGE_MODES.with(|t| {
t.insert(
ptr_of(&iv),
ImageMode {
template: p.template,
tinted: p.tint.is_some(),
},
)
});
view_of(&iv)
}
Some(Builtin::ListItem) => {
let Some(p) = props_of::<day_spec::props::ListItemProps>(kind, "appkit", props)
else {
return placeholder_view(mtm, kind);
};
make_list_item(p, mtm)
}
Some(Builtin::ListCell) | None => {
if let Some(make) = self.registry.get(kind).map(|r| r.make) {
return make(self, props, id);
}
warn_missing_renderer(kind);
placeholder_view(mtm, kind)
}
}
}
fn update(&mut self, h: &Handle, kind: PieceKind, patch: &dyn Any, anim: Option<&AnimSpec>) {
match kind {
kinds::LIST_ITEM => {
let Some(p) = patch.downcast_ref::<day_spec::props::ListItemPatch>() else {
return;
};
let props = LIST_ITEMS.with(|m| {
use day_spec::props::ListItemPatch as P;
let mut m = m.borrow_mut();
let props = m.get_mut(&ptr_of(h))?;
match p {
P::Title(t) => props.title.clone_from(t),
P::Subtitle(t) => props.subtitle.clone_from(t),
P::Detail(t) => props.detail.clone_from(t),
P::Icon(i) => props.icon.clone_from(i),
P::IconTint(c) => props.icon_tint = *c,
P::Checked(on) => props.checked = *on,
}
Some(props.clone())
});
if let Some(props) = props {
dress_list_item(h, &props, self.mtm());
}
}
kinds::IMAGE => {
if let Some(p) = patch.downcast_ref::<day_spec::props::ImagePatch>() {
match p {
day_spec::props::ImagePatch::Tint(c) => {
if let Ok(iv) = h.clone().downcast::<objc2_app_kit::NSImageView>() {
let key = ptr_of(&iv);
let template = IMAGE_MODES
.with(|t| {
t.with(key, |m| {
m.tinted = c.is_some();
m.renders_template()
})
})
.unwrap_or(c.is_some());
if let Some(img) = unsafe { iv.image() } {
let img = image_rendered(img, template);
unsafe { iv.setImage(Some(&img)) };
}
let tint = c.map(color::nsink);
unsafe { iv.setContentTintColor(tint.as_deref()) };
}
}
day_spec::props::ImagePatch::Source(source) => {
if let Ok(iv) = h.clone().downcast::<objc2_app_kit::NSImageView>() {
if let Some(img) = appkit_image_for(source) {
let template = IMAGE_MODES
.with(|t| t.get(ptr_of(&iv)))
.is_some_and(ImageMode::renders_template);
let img = image_rendered(img, template);
unsafe { iv.setImage(Some(&img)) };
}
}
}
}
}
}
kinds::CONTAINER => {
let (Some(patch), Ok(v)) = (
patch.downcast_ref::<ContainerPatch>(),
h.clone().downcast::<DayFlipped>(),
) else {
return;
};
match patch {
ContainerPatch::Fill(fill) => motion::set_background(&v, fill.clone(), anim),
ContainerPatch::Surface(p) => {
if v.ivars().section_card.get().is_some() {
v.set_section_card(p.corner_radius);
return;
}
let fill = v.ivars().surface.borrow().as_ref().map(|s| s.fill.clone());
if fill.as_ref() != Some(&p.fill) {
motion::set_background(&v, p.fill.clone(), anim);
}
v.set_surface(p);
}
}
}
kinds::LABEL => {
if let (Some(p), Ok(tf)) = (
patch.downcast_ref::<LabelPatch>(),
h.clone().downcast::<NSTextField>(),
) {
let label = h.downcast_ref::<DayLabel>();
match p {
LabelPatch::Paragraph(para) => {
if let Some(label) = label {
label.ivars().paragraph.set(*para);
}
}
LabelPatch::Text(t) => {
unsafe { tf.setStringValue(&NSString::from_str(t)) };
if let Some(label) = label {
label.clear_selection();
label.ivars().has_runs.set(false);
label.set_links(false);
}
}
LabelPatch::Color(c) => {
if let Some(label) = label {
label.ivars().color.set(*c);
}
unsafe { tf.setTextColor(c.map(color::nsink).as_deref()) };
}
LabelPatch::Font(f) => unsafe { tf.setFont(Some(&nsfont(*f))) },
LabelPatch::Runs(text, runs) => {
let base = unsafe { tf.font() };
let (own, role) = label.map_or((None, TextRole::default()), |l| {
(l.ivars().color.get(), l.ivars().role.get())
});
let s = match base.as_deref() {
Some(f) => attributed_label(text, f, own, role, runs),
None => attributed_label(
text,
&nsfont(day_spec::FontSpec::default()),
own,
role,
runs,
),
};
unsafe { tf.setAttributedStringValue(&s) };
if let Some(label) = label {
label.clear_selection();
label.ivars().has_runs.set(!runs.is_empty());
label.set_links(runs.iter().any(|r| r.link.is_some()));
}
}
}
if let Some(label) = label {
apply_label_paragraph(label);
}
}
}
kinds::BUTTON => {
if let (Some(p), Ok(btn)) = (
patch.downcast_ref::<ButtonPatch>(),
h.clone().downcast::<NSButton>(),
) {
match p {
ButtonPatch::Content(c) => {
apply_button_content(&btn, &c.title, c.icon.as_ref(), c.icon_only);
}
ButtonPatch::Title(t) => {
let style = BUTTON_STYLES
.with(|m| m.borrow().get(&ptr_of(&btn)).copied())
.unwrap_or_default();
set_button_title(&btn, t, style);
}
ButtonPatch::Enabled(e) => unsafe { btn.setEnabled(*e) },
ButtonPatch::Selected(on) => set_button_selected(&btn, *on),
ButtonPatch::Style(s) => apply_button_style(&btn, *s),
}
}
}
kinds::TOGGLE => {
if let (Some(p), Ok(sw)) = (
patch.downcast_ref::<TogglePatch>(),
h.clone().downcast::<NSSwitch>(),
) {
match p {
TogglePatch::On(on) => {
let want = isize::from(*on);
if unsafe { sw.state() } != want {
unsafe { sw.setState(want) };
}
}
TogglePatch::Enabled(e) => unsafe { sw.setEnabled(*e) },
}
}
}
kinds::SLIDER => {
if let (Some(p), Ok(sl)) = (
patch.downcast_ref::<SliderPatch>(),
h.clone().downcast::<NSSlider>(),
) {
match p {
SliderPatch::Value(v) => {
if (unsafe { sl.doubleValue() } - v).abs() > 0.001 {
unsafe { sl.setDoubleValue(*v) };
}
}
SliderPatch::Enabled(e) => unsafe { sl.setEnabled(*e) },
}
}
}
kinds::PROGRESS => {
if let Some(p @ ProgressPatch::Animating(_)) = patch.downcast_ref::<ProgressPatch>()
{
crate::controls::spinner::update(self, h, p);
return;
}
if let (Some(ProgressPatch::Value(v)), Ok(pi)) = (
patch.downcast_ref::<ProgressPatch>(),
h.clone().downcast::<NSProgressIndicator>(),
) {
unsafe {
if let Some(val) = v {
if pi.isIndeterminate() {
pi.stopAnimation(None);
pi.setIndeterminate(false);
pi.setStyle(NSProgressIndicatorStyle::Bar);
pi.setMinValue(0.0);
pi.setMaxValue(1.0);
}
if (pi.doubleValue() - val).abs() > 0.0001 {
pi.setDoubleValue(*val);
}
} else {
pi.setIndeterminate(true);
pi.setStyle(NSProgressIndicatorStyle::Spinning);
pi.startAnimation(None);
}
}
}
}
kinds::NAV_MENU => {
if let Some(NavMenuPatch::Items {
items,
icons,
badges,
badge_icons,
badge_tints,
sections,
tints,
menus,
movable,
sorted,
selected,
rows_changed,
..
}) = patch.downcast_ref::<NavMenuPatch>()
{
NAV_MENUS.with(|m| {
let m = m.borrow();
let Some((outline, data)) = m.get(&ptr_of(h)) else {
return;
};
(*data.ivars().movable.borrow_mut()).clone_from(movable);
(*data.ivars().sorted.borrow_mut()).clone_from(sorted);
data.set_items(
items,
icons,
badges,
badge_icons,
badge_tints,
sections,
tints,
menus,
);
data.ivars().suppress.set(true);
if let Some(rows) = rows_changed {
let set = objc2_foundation::NSMutableIndexSet::new();
for row in rows.iter().filter_map(|i| data.row_of_item(*i)) {
unsafe { set.addIndex(row) };
}
unsafe {
outline.reloadDataForRowIndexes_columnIndexes(
&set,
&objc2_foundation::NSIndexSet::indexSetWithIndex(0),
);
};
data.ivars().suppress.set(false);
return;
}
unsafe {
outline.reloadData();
match selected.and_then(|i| data.row_of_item(i)) {
Some(row) => outline.selectRowIndexes_byExtendingSelection(
&objc2_foundation::NSIndexSet::indexSetWithIndex(row),
false,
),
None => outline.deselectAll(None),
}
}
data.ivars().suppress.set(false);
});
adopt_menu_for_tabs(self.mtm(), h);
} else if let Some(NavMenuPatch::IconProgress(progress)) =
patch.downcast_ref::<NavMenuPatch>()
{
NAV_MENUS.with(|menus| {
let menus = menus.borrow();
let Some((outline, data)) = menus.get(&ptr_of(h)) else {
return;
};
let previous = data.ivars().icon_progress.replace(progress.clone());
for (row, item) in data.ivars().rows.borrow().iter().enumerate() {
let Some(index) = item else {
continue;
};
let value = progress.iter().find(|(i, _)| i == index).map(|(_, v)| *v);
if previous.iter().find(|(i, _)| i == index).map(|(_, v)| *v) == value {
continue;
}
if let Some(view) = unsafe {
outline.viewAtColumn_row_makeIfNecessary(0, row as isize, false)
} && let Some(cell) =
view.downcast_ref::<objc2_app_kit::NSTableCellView>()
&& let Some(icon) = unsafe { cell.imageView() }
{
nav_progress::update(&icon, value);
}
}
});
} else if let Some(NavMenuPatch::Selected(sel)) =
patch.downcast_ref::<NavMenuPatch>()
{
NAV_MENUS.with(|m| {
let m = m.borrow();
let Some((outline, data)) = m.get(&ptr_of(h)) else {
return;
};
data.ivars().suppress.set(true);
unsafe {
match sel.and_then(|i| data.row_of_item(i)) {
Some(row) => outline.selectRowIndexes_byExtendingSelection(
&objc2_foundation::NSIndexSet::indexSetWithIndex(row),
false,
),
None => outline.deselectAll(None),
}
}
data.ivars().suppress.set(false);
});
}
}
kinds::INSPECTOR => {
if let Some(InspectorPatch::Visible(v)) = patch.downcast_ref::<InspectorPatch>() {
INSPECTOR_STATE.with(|m| {
if let Some(state) = m.borrow().get(&ptr_of(h)) {
state.panel_item.setCollapsed(!*v);
}
});
}
}
kinds::SPLIT => {
if let Some(p) = patch.downcast_ref::<SplitPatch>() {
SPLIT_STATE.with(|m| {
let m = m.borrow();
let Some(state) = m.get(&ptr_of(h)) else {
return;
};
let iv = state.delegate.ivars();
match p {
SplitPatch::Fraction(f) => {
iv.share.set(f.clamp(0.0, 1.0));
if iv.positioned.get() {
split_place(&state.split, iv);
}
}
SplitPatch::Axis(axis) => unsafe {
state
.split
.setVertical(*axis == day_spec::props::SplitAxis::Horizontal);
state.split.adjustSubviews();
if iv.positioned.get() {
split_place(&state.split, iv);
}
},
}
});
}
}
kinds::NAV => {
if let Some(NavPatch::Presentation(next)) = patch.downcast_ref::<NavPatch>() {
nav_present(self.mtm(), h, *next);
return;
}
if let Some(p) = patch.downcast_ref::<NavPatch>() {
NAV_STATE.with(|m| {
let mut m = m.borrow_mut();
let Some(state) = m.get_mut(&ptr_of(h)) else {
return;
};
match p {
NavPatch::Pushed { title, .. } => {
let last = state.pages.len().saturating_sub(1);
state.selected = last;
for (i, page) in state.pages.iter().enumerate() {
page.setHidden(i != last);
}
if let Some(hdr) = state.header.as_mut() {
hdr.titles.push(title.clone());
sync_nav_header(hdr, &state.detail_wrap, &state.pages);
}
}
NavPatch::Popped => {
let n = state.pages.len();
if let Some(top) = state.pages.last() {
top.setHidden(true);
}
if n >= 2 {
state.pages[n - 2].setHidden(false);
}
state.selected = n.saturating_sub(2);
if let Some(hdr) = state.header.as_mut() {
if hdr.titles.len() > 1 {
hdr.titles.pop();
}
let after: Vec<Retained<NSView>> = state
.pages
.iter()
.take(n.saturating_sub(1))
.cloned()
.collect();
sync_nav_header(hdr, &state.detail_wrap, &after);
}
}
NavPatch::Title(t) => {
if let Some(hdr) = state.header.as_mut() {
if let Some(last) = hdr.titles.last_mut() {
last.clone_from(t);
}
unsafe {
hdr.title.setStringValue(&NSString::from_str(t));
}
}
}
#[expect(
clippy::match_same_arms,
reason = "each no-op patch keeps its own arm and the comment saying why it is one"
)]
NavPatch::GuardTop(_) => {}
NavPatch::Reorder(order) => {
let old = state.pages.clone();
state.pages =
order.iter().filter_map(|i| old.get(*i).cloned()).collect();
if let Some(tabs) = &state.tabs {
TAB_PICK_SUPPRESS.with(|s| s.set(true));
unsafe {
let items = tabs.view.tabViewItems();
for item in &items {
tabs.view.removeTabViewItem(&item);
}
for i in order {
if let Some(item) = items.iter().nth(*i) {
tabs.view.addTabViewItem(&item);
}
}
}
TAB_PICK_SUPPRESS.with(|s| s.set(false));
}
}
#[expect(
clippy::match_same_arms,
reason = "each no-op patch keeps its own arm and the comment saying why it is one"
)]
NavPatch::Presentation(_) => {}
NavPatch::Select(i) => {
state.selected = *i;
match state.tabs.as_ref() {
Some(tabs) => {
let n = unsafe { tabs.view.numberOfTabViewItems() };
if (*i as isize) < n {
TAB_PICK_SUPPRESS.with(|s| s.set(true));
unsafe {
tabs.view.selectTabViewItemAtIndex(*i as isize);
};
TAB_PICK_SUPPRESS.with(|s| s.set(false));
}
}
None => {
for (n, page) in state.pages.iter().enumerate() {
page.setHidden(n != *i);
}
}
}
}
NavPatch::ListVisible(v) => {
state.list_visible = *v;
if let Some(item) = state.list_item.as_ref() {
item.setCollapsed(!*v);
let split = unsafe { state.split_vc.splitView() };
unsafe { split.layoutSubtreeIfNeeded() };
if let Some(w) = take_list_placement(state) {
unsafe {
split.setPosition_ofDividerAtIndex(
day_spec::NAV_SIDEBAR_WIDTH + w,
1,
);
}
}
if *v
&& let Some(wrap) = state.list_wrap.as_ref()
&& let Some(page) = state.list_page.as_ref()
{
unsafe {
wrap.layoutSubtreeIfNeeded();
page.setFrame(wrap.bounds());
}
}
}
}
NavPatch::ListInStack(_) => {}
}
});
}
}
kinds::COVER => {
if let (Some(p), Ok(page)) = (
patch.downcast_ref::<CoverPatch>(),
h.clone().downcast::<DayNavPage>(),
) {
let node = page.ivars().node;
match p {
CoverPatch::Present { background, .. } => {
if page.ivars().sheet.get() {
if let Some(parent) =
primary_content().and_then(|v| unsafe { v.window() })
{
let bounds = parent.frame().size;
let max = if page.ivars().large.get() {
day_spec::props::LARGE_SHEET_MAX
} else {
day_spec::props::SHEET_MAX
};
let height = if page.ivars().half.get() {
(bounds.height * 0.5).clamp(160.0, max.height)
} else {
(bounds.height - 80.0).clamp(300.0, max.height)
};
let size = NSSize::new(
(bounds.width - 40.0).clamp(320.0, max.width),
height,
);
let window = unsafe {
NSWindow::initWithContentRect_styleMask_backing_defer(
NSWindow::alloc(MainThreadMarker::new().unwrap()),
NSRect::new(NSPoint::new(0.0, 0.0), size),
NSWindowStyleMask::Titled,
NSBackingStoreType::Buffered,
false,
)
};
unsafe {
window.setReleasedWhenClosed(false);
page.removeFromSuperview();
page.setHidden(false);
page.setFrame(NSRect::new(NSPoint::new(0.0, 0.0), size));
window.setContentView(Some(&page));
}
let key = ptr_of(h);
SHEET_NODES.with(|t| {
t.insert(Retained::as_ptr(&window) as usize, node)
});
install_sheet_escape_monitor();
COVER_SHEETS.with(|t| t.insert(key, CoverSheet(window)));
dispatch2::DispatchQueue::main().exec_async(move || {
ffi_guard::contain((), || {
let window =
COVER_SHEETS.with(|t| t.with(key, |s| s.0.clone()));
if let (Some(window), Some(parent)) = (
window,
primary_content()
.and_then(|v| unsafe { v.window() }),
) {
unsafe {
parent.beginSheet_completionHandler(
&window, None,
);
}
}
});
});
emit(
node,
Event::FrameChanged(Size::new(size.width, size.height)),
);
}
return;
}
page.set_fill(
background.unwrap_or(day_spec::SystemColor::Background.into()),
);
if let Some(content) = primary_content() {
if let Some(window) = unsafe { content.window() } {
toolbar::cover_presented(ptr_of(h), &window);
}
let top = title_bar_inset(&content);
let full = content.frame().size;
page.ivars().top_inset.set(top);
unsafe {
page.removeFromSuperview();
content.addSubview(&page);
page.setFrame(NSRect::new(
NSPoint::new(0.0, -top),
NSSize::new(full.width, full.height),
));
page.setBoundsOrigin(NSPoint::new(0.0, -top));
page.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable
| objc2_app_kit::NSAutoresizingMaskOptions::ViewHeightSizable,
);
page.setHidden(false);
}
emit(
node,
Event::FrameChanged(Size::new(
full.width,
(full.height - top).max(0.0),
)),
);
}
}
CoverPatch::DismissDisabled(_) => {}
CoverPatch::Dismiss => {
COVER_SHEETS.with(|t| drop(t.remove(ptr_of(h))));
toolbar::cover_dismissed(ptr_of(h));
unsafe {
page.setHidden(true);
page.removeFromSuperview();
}
emit(node, Event::CoverHidden);
}
}
}
}
kinds::PICKER => picker::update_any(self, h, patch),
kinds::TEXT_AREA => textarea::update_any(self, h, patch),
kinds::TEXT_FIELD => {
if let (Some(p), Ok(tf)) = (
patch.downcast_ref::<TextFieldPatch>(),
h.clone().downcast::<NSTextField>(),
) {
match p {
TextFieldPatch::Text { text, from_native } => {
if !*from_native && unsafe { tf.stringValue() }.to_string() != *text {
unsafe { tf.setStringValue(&NSString::from_str(text)) };
}
}
TextFieldPatch::Placeholder(t) => unsafe {
tf.setPlaceholderString(Some(&NSString::from_str(t)));
},
TextFieldPatch::Enabled(e) => unsafe { tf.setEnabled(*e) },
TextFieldPatch::Select(sel) => {
if let Some(editor) = tf.currentEditor() {
let (start, end) = sel.utf16(&editor.string().to_string());
editor.setSelectedRange(objc2_foundation::NSRange::new(
start,
end - start,
));
}
}
}
}
}
kinds::SCROLL => {
if let Some(ScrollPatch::Zoom { scale, animated }) =
patch.downcast_ref::<ScrollPatch>()
{
scroll::zoom(h, *scale, *animated);
}
}
kinds::LIST => {
let key = ptr_of(h);
let entry = list_entry(key);
if let Some((table, data)) = &entry {
data.apply_held(table);
}
match patch.downcast_ref::<ListPatch>() {
Some(ListPatch::Splice(deltas)) => {
if let Some((table, data)) = &entry {
data.change_rows(key, table, Some(deltas));
}
post_realize_visible_rows(key);
}
Some(ListPatch::Reload) => {
if let Some((table, data)) = &entry {
data.change_rows(key, table, None);
}
post_realize_visible_rows(key);
}
Some(ListPatch::ScrollToRow(row)) => {
let row = *row;
<AppKit as Platform>::post(Box::new(move || {
if let Some((table, data)) = list_entry(key) {
data.apply_held(&table);
let rows = unsafe { table.numberOfRows() };
if rows > 0 {
unsafe {
table.scrollRowToVisible((row as isize).min(rows - 1));
};
}
}
}));
post_realize_visible_rows(key);
}
Some(ListPatch::ScrollToEnd) => {
<AppKit as Platform>::post(Box::new(move || {
if let Some((table, data)) = list_entry(key) {
data.apply_held(&table);
let rows = unsafe { table.numberOfRows() };
if rows > 0 {
unsafe { table.scrollRowToVisible(rows - 1) };
}
}
}));
post_realize_visible_rows(key);
}
Some(ListPatch::Selected(rows)) => {
if let Some((table, data)) = &entry {
data.select(table, rows);
}
}
Some(ListPatch::RowSizeInvalidated { cell, height }) => {
if let Some((table, data)) = &entry {
data.row_resized(table, *cell, *height);
}
}
Some(ListPatch::Editing(_)) | None => {}
}
}
kinds::TREE => match patch.downcast_ref::<TreePatch>() {
Some(TreePatch::Reload) => {
if let Some((outline, _)) = tree_entry(ptr_of(h)) {
unsafe { outline.reloadData() };
}
post_realize_visible_tree_rows(ptr_of(h));
}
Some(TreePatch::Expand(token, on)) => {
let (key, token, on) = (ptr_of(h), *token, *on);
<AppKit as Platform>::post(Box::new(move || {
if let Some((outline, data)) = tree_entry(key) {
let item = data.intern(token);
data.ivars().suppress.set(true);
unsafe {
if on {
outline.expandItem(Some(&item));
} else {
outline.collapseItem(Some(&item));
}
}
data.ivars().suppress.set(false);
}
day_core::pump_events();
}));
post_realize_visible_tree_rows(ptr_of(h));
}
Some(TreePatch::Selected(tokens)) => {
if let Some((outline, data)) = tree_entry(ptr_of(h)) {
data.ivars().suppress.set(true);
unsafe {
if tokens.is_empty() {
outline.deselectAll(None);
} else {
let set = objc2_foundation::NSMutableIndexSet::new();
for t in tokens {
let row = outline.rowForItem(Some(&data.intern(*t)));
if row >= 0 {
set.addIndex(row as usize);
}
}
outline.selectRowIndexes_byExtendingSelection(&set, false);
}
}
data.ivars().suppress.set(false);
}
}
Some(TreePatch::Reveal(token)) => {
let (key, token) = (ptr_of(h), *token);
<AppKit as Platform>::post(Box::new(move || {
if let Some((outline, data)) = tree_entry(key) {
let row = unsafe { outline.rowForItem(Some(&data.intern(token))) };
if row >= 0 {
unsafe { outline.scrollRowToVisible(row) };
}
}
}));
post_realize_visible_tree_rows(ptr_of(h));
}
None => {}
},
_ => {
if let Some(update) = self.registry.get(kind).map(|r| r.update) {
update(self, h, patch);
}
}
}
}
fn release_piece(&mut self, kind: day_spec::PieceKind, h: &Self::Handle) {
let f = self.registry.get(kind).and_then(|r| r.release);
if let Some(f) = f {
f(self, h);
}
}
fn release(&mut self, h: Handle) {
LIST_ITEMS.with(|m| m.borrow_mut().remove(&ptr_of(&h)));
self.secondary.retain(|w| {
if ptr_of(&w.content) == ptr_of(&h) {
w.window.close();
day_spec::sidetable::sweep(Retained::as_ptr(&w.window) as usize);
false
} else {
true
}
});
TARGETS.with(|m| {
m.borrow_mut().remove(&ptr_of(&h));
});
LIST_STATE.with(|m| {
m.borrow_mut().remove(&ptr_of(&h));
});
TREE_STATE.with(|m| {
m.borrow_mut().remove(&ptr_of(&h));
});
GESTURES.with(|m| {
m.borrow_mut().remove(&ptr_of(&h));
});
NAV_STATE.with(|m| {
if let Some(nav) = m.borrow_mut().remove(&ptr_of(&h)) {
unsafe {
nav.split_vc.setNextResponder(None);
let split = nav.split_vc.splitView();
if split.nextResponder().is_some_and(|r| {
core::ptr::eq::<objc2::runtime::AnyObject>(
r.as_ref(),
nav.split_vc.as_ref(),
)
}) {
split.setNextResponder(None);
}
}
}
});
NAV_PAGES.with(|set| {
set.borrow_mut().remove(&ptr_of(&h));
});
SPLIT_STATE.with(|m| {
if let Some(state) = m.borrow_mut().remove(&ptr_of(&h)) {
state.split.setDelegate(None);
}
});
INSPECTOR_STATE.with(|m| {
if let Some(insp) = m.borrow_mut().remove(&ptr_of(&h)) {
unsafe {
insp.split_vc.setNextResponder(None);
let split = insp.split_vc.splitView();
if split.nextResponder().is_some_and(|r| {
core::ptr::eq::<objc2::runtime::AnyObject>(
r.as_ref(),
insp.split_vc.as_ref(),
)
}) {
split.setNextResponder(None);
}
}
}
});
NAV_MENUS.with(|m| {
if let Some((outline, _)) = m.borrow_mut().remove(&ptr_of(&h)) {
day_spec::sidetable::sweep(Retained::as_ptr(&outline) as usize);
}
});
input::forget_disabled_root(ptr_of(&h));
day_spec::sidetable::sweep(ptr_of(&h));
unsafe { h.removeFromSuperview() };
}
fn insert(&mut self, parent: &Handle, child: &Handle, _index: usize) {
if NAV_MENUS.with(|m| m.borrow().contains_key(&ptr_of(child))) {
unsafe { content_of(parent).addSubview(child) };
adopt_menu_for_tabs(self.mtm(), child);
return;
}
let page_pane = PAGE_PANE.with(|t| t.get(ptr_of(child)));
let is_sidebar_page = page_pane == Some(day_spec::props::Pane::Sidebar);
let handled = NAV_STATE.with(|m| {
let mut m = m.borrow_mut();
let Some(state) = m.get_mut(&ptr_of(parent)) else {
return false;
};
if page_pane == Some(day_spec::props::Pane::List)
&& let Some(wrap) = state.list_wrap.as_ref()
{
state.list_page = Some(child.clone());
unsafe {
child.setFrame(wrap.bounds());
child.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable
| objc2_app_kit::NSAutoresizingMaskOptions::ViewHeightSizable,
);
wrap.addSubview(child);
}
return true;
}
if is_sidebar_page {
state.sidebar_page = Some(child.clone());
}
let to_pane = is_sidebar_page && state.presentation != NavPresentation::Stack;
if !to_pane && let Some(tabs) = state.tabs.as_ref() {
state.pages.push(child.clone());
let item = unsafe { objc2_app_kit::NSTabViewItem::new() };
unsafe {
child.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable
| objc2_app_kit::NSAutoresizingMaskOptions::ViewHeightSizable,
);
item.setView(Some(child));
tabs.view.addTabViewItem(&item);
}
if let Some((titles, _)) = state.sidebar_page.as_deref().and_then(nav_menu_rows) {
sync_tab_labels(tabs, &titles, state.selected);
}
return true;
}
let (wrap, frame) = if to_pane {
(&state.sidebar_wrap, state.sidebar_wrap.bounds())
} else {
state.pages.push(child.clone());
let visible = state.header.as_ref().is_some_and(|h| !h.bar.isHidden());
(
&state.detail_wrap,
nav_page_frame(&state.detail_wrap, visible),
)
};
unsafe {
child.setFrame(frame);
child.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable
| objc2_app_kit::NSAutoresizingMaskOptions::ViewHeightSizable,
);
wrap.addSubview(child);
}
true
});
if handled {
return;
}
let split_pane = SPLIT_STATE.with(|m| {
let m = m.borrow();
let Some(state) = m.get(&ptr_of(parent)) else {
return false;
};
let second = SPLIT_PANES.with(|t| t.get(ptr_of(child))).unwrap_or(false);
let wrap = if second {
&state.second_wrap
} else {
&state.first_wrap
};
unsafe {
child.setFrame(wrap.bounds());
child.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable
| objc2_app_kit::NSAutoresizingMaskOptions::ViewHeightSizable,
);
wrap.addSubview(child);
}
true
});
if split_pane {
return;
}
let inspected = INSPECTOR_STATE.with(|m| {
let m = m.borrow();
let Some(state) = m.get(&ptr_of(parent)) else {
return false;
};
let panel = INSPECTOR_PANES
.with(|t| t.get(ptr_of(child)))
.unwrap_or(false);
let wrap = if panel {
&state.panel_wrap
} else {
&state.content_wrap
};
unsafe {
child.setFrame(wrap.bounds());
child.setAutoresizingMask(
objc2_app_kit::NSAutoresizingMaskOptions::ViewWidthSizable
| objc2_app_kit::NSAutoresizingMaskOptions::ViewHeightSizable,
);
wrap.addSubview(child);
}
true
});
if !inspected {
unsafe { content_of(parent).addSubview(child) };
}
}
fn remove(&mut self, parent: &Handle, child: &Handle) {
PAGE_PANE.with(|t| t.remove(ptr_of(child)));
let mtm = self.mtm();
NAV_STATE.with(|m| {
if let Some(state) = m.borrow_mut().get_mut(&ptr_of(parent)) {
if let Some(tabs) = state.tabs.as_ref() {
unsafe {
let n = tabs.view.numberOfTabViewItems();
for i in 0..n {
let item = tabs.view.tabViewItemAtIndex(i);
if item.view(mtm).is_some_and(|v| ptr_of(&v) == ptr_of(child)) {
tabs.view.removeTabViewItem(&item);
break;
}
}
}
}
state.pages.retain(|p| ptr_of(p) != ptr_of(child));
if state.sidebar_page.as_deref().map(ptr_of) == Some(ptr_of(child)) {
state.sidebar_page = None;
}
}
});
unsafe { child.removeFromSuperview() };
}
fn move_child(&mut self, parent: &Handle, child: &Handle, _to: usize) {
unsafe { content_of(parent).addSubview(child) };
}
fn measure(&mut self, h: &Handle, kind: PieceKind, p: Proposal) -> Size {
match kind {
kinds::LABEL => {
if let Some(tf) = h.downcast_ref::<NSTextField>() {
let w = p.width.unwrap_or(1.0e6);
let s = tf.sizeThatFits(NSSize::new(w, 1.0e6));
let mut height = s.height.ceil();
if tf.maximumNumberOfLines() == 1
&& let Some(font) = unsafe { tf.font() }
{
let fixed = h.downcast_ref::<DayLabel>().and_then(|l| {
label_line_points(
l.ivars().paragraph.get().line_height,
font.pointSize(),
)
});
let line = fixed.unwrap_or_else(|| natural_line_height(&font));
height = height.min(line.ceil());
}
return Size::new(s.width.ceil().min(w), height);
}
Size::ZERO
}
kinds::BUTTON | kinds::TOGGLE => {
let s = unsafe { h.fittingSize() };
Size::new(s.width.ceil(), s.height.ceil())
}
kinds::SLIDER => {
let s = unsafe { h.fittingSize() };
Size::new(p.width.unwrap_or(180.0), s.height.max(21.0).ceil())
}
kinds::PICKER => picker::measure_any(self, h, p),
kinds::TEXT_AREA => textarea::measure_any(self, h, p),
kinds::TEXT_FIELD => {
let s = unsafe { h.fittingSize() };
Size::new(
p.width.unwrap_or(s.width.max(160.0)),
s.height.max(22.0).ceil(),
)
}
kinds::DIVIDER => Size::new(p.width.unwrap_or(0.0), 5.0),
kinds::LIST_ITEM => {
let s = h.fittingSize();
Size::new(p.width.unwrap_or(s.width.ceil()), s.height.ceil().max(28.0))
}
kinds::PROGRESS => {
let indeterminate = h
.clone()
.downcast::<NSProgressIndicator>()
.is_ok_and(|pi| unsafe { pi.isIndeterminate() });
if indeterminate {
let large = h
.downcast_ref::<NSProgressIndicator>()
.is_some_and(|pi| pi.controlSize() == objc2_app_kit::NSControlSize::Large);
let side = if large { 32.0 } else { 20.0 };
Size::new(side, side)
} else {
Size::new(p.width.unwrap_or(180.0), 20.0)
}
}
kinds::NAV_MENU => {
let rows = NAV_MENUS.with(|m| {
m.borrow()
.get(&ptr_of(h))
.map_or(0, |(_, d)| d.ivars().items.borrow().len())
});
Size::new(
p.width.unwrap_or(220.0),
p.height.unwrap_or(rows as f64 * 32.0 + 12.0),
)
}
kinds::LIST | kinds::TREE => Size::new(p.width.unwrap_or(0.0), p.height.unwrap_or(0.0)),
_ => {
if let Some(measure) = self.registry.get(kind).and_then(|r| r.measure) {
measure(self, h, p)
} else {
let s = unsafe { h.fittingSize() };
Size::new(p.width.unwrap_or(s.width), p.height.unwrap_or(s.height))
}
}
}
}
fn set_opacity(&mut self, h: &Handle, opacity: f64, anim: Option<&AnimSpec>) {
motion::set_opacity(h, opacity, anim);
}
fn set_transform(&mut self, h: &Handle, t: Transform, _size: Size, anim: Option<&AnimSpec>) {
let from = motion::shown_geometry(h);
TRANSFORMS.with(|m| m.insert(ptr_of(h), t));
if let Some(base) = BASE_FRAMES.with(|m| m.get(ptr_of(h))) {
motion::move_geometry(h, from, (base, t), anim, || apply_geometry(h, base, t));
}
}
fn read_displayed(&self, h: &Handle) -> day_spec::Displayed {
motion::displayed(h)
}
fn text_scale(&mut self, _style: Font) -> f64 {
1.0
}
fn set_layout_direction(
&mut self,
h: &Handle,
kind: PieceKind,
direction: day_spec::LayoutDirection,
) {
use objc2_app_kit::{NSUserInterfaceLayoutDirection as Ui, NSWritingDirection as Writing};
let rtl = direction == day_spec::LayoutDirection::Rtl;
let (ui, writing) = if rtl {
(Ui::RightToLeft, Writing::RightToLeft)
} else {
(Ui::LeftToRight, Writing::LeftToRight)
};
if kind == kinds::TEXT_AREA {
textarea::set_direction(h, ui, writing);
return;
}
if !matches!(
kind,
kinds::LABEL
| kinds::BUTTON
| kinds::TOGGLE
| kinds::SLIDER
| kinds::TEXT_FIELD
| kinds::PICKER
| kinds::PROGRESS
) {
return;
}
h.setUserInterfaceLayoutDirection(ui);
if let Some(label) = h.downcast_ref::<DayLabel>() {
label.ivars().direction.set(Some(direction));
apply_label_paragraph(label);
return;
}
if let Some(control) = h.downcast_ref::<NSControl>() {
if let Some(cell) = unsafe { control.cell() } {
cell.setUserInterfaceLayoutDirection(ui);
}
if control.isKindOfClass(<NSTextField as objc2::ClassType>::class()) {
unsafe { control.setBaseWritingDirection(writing) };
}
}
}
fn set_selectable(&mut self, h: &Handle, selectable: bool) -> Option<Handle> {
if let Some(label) = h.downcast_ref::<DayLabel>() {
label.ivars().selectable.set(selectable);
label.set_links(label.ivars().has_links.get());
} else if let Some(tf) = h.downcast_ref::<NSTextField>() {
unsafe { tf.setSelectable(selectable) };
}
None
}
fn set_input_traits(&mut self, h: &Handle, traits: &InputTraits) -> Option<Handle> {
let node = text_field_node(h)?;
let old = h.downcast_ref::<NSTextField>()?;
let swapped = (h.downcast_ref::<DaySecureTextField>().is_some() != traits.secure)
.then(|| self.swap_text_field(h, old, node, traits.secure));
let tf = swapped.as_deref().unwrap_or(old);
unsafe {
tf.setEditable(!traits.read_only);
tf.setSelectable(true);
}
let content_type = match traits.purpose {
InputPurpose::Text | InputPurpose::Number | InputPurpose::Decimal => None,
InputPurpose::Name => text_content_type(c"NSTextContentTypeName"),
InputPurpose::Email => text_content_type(c"NSTextContentTypeEmailAddress"),
InputPurpose::Url => text_content_type(c"NSTextContentTypeURL"),
InputPurpose::Phone => text_content_type(c"NSTextContentTypeTelephoneNumber"),
InputPurpose::Username => text_content_type(c"NSTextContentTypeUsername"),
InputPurpose::Password => text_content_type(c"NSTextContentTypePassword"),
InputPurpose::NewPassword => text_content_type(c"NSTextContentTypeNewPassword"),
InputPurpose::OneTimeCode => text_content_type(c"NSTextContentTypeOneTimeCode"),
};
unsafe {
tf.setContentType(content_type);
tf.setAutomaticTextCompletionEnabled(traits.purpose == InputPurpose::Text);
}
swapped.as_ref().map(view_of)
}
fn set_cursor(&mut self, h: &Handle, cursor: Cursor) {
let key = Retained::as_ptr(h) as usize;
if cursor == Cursor::Default {
if let Some((owner, area)) = self.cursors.remove(&key) {
unsafe { h.removeTrackingArea(&area) };
if owner.ivars().hiding.replace(false) {
NSCursor::unhide();
}
}
return;
}
let hidden = cursor == Cursor::None;
let shape = if hidden { None } else { ns_cursor(&cursor) };
let mtm = self.mtm;
let (owner, _) = self.cursors.entry(key).or_insert_with(|| {
let owner = DayCursorOwner::new(mtm);
let opts = NSTrackingAreaOptions::CursorUpdate
| NSTrackingAreaOptions::MouseEnteredAndExited
| NSTrackingAreaOptions::ActiveInKeyWindow
| NSTrackingAreaOptions::InVisibleRect;
let any: &objc2::runtime::AnyObject = &owner;
let area = unsafe {
NSTrackingArea::initWithRect_options_owner_userInfo(
NSTrackingArea::alloc(),
NSRect::ZERO,
opts,
Some(any),
None,
)
};
unsafe { h.addTrackingArea(&area) };
(owner, area)
});
let changed = {
let iv = owner.ivars();
let same = match (iv.cursor.borrow().as_ref(), shape.as_ref()) {
(Some(a), Some(b)) => Retained::as_ptr(a) == Retained::as_ptr(b),
(None, None) => true,
_ => false,
};
*iv.cursor.borrow_mut() = shape;
let was_hidden = iv.hidden.replace(hidden);
!same || was_hidden != hidden
};
if changed && let Some(w) = h.window() {
unsafe { w.invalidateCursorRectsForView(h) };
}
}
fn first_baseline(&mut self, h: &Handle, kind: PieceKind, size: Size) -> Option<f64> {
if !day_spec::kind_has_baseline(kind) {
return None;
}
let current = unsafe { h.frame() };
if (current.size.height - size.height).abs() > 0.5 {
unsafe { h.setFrameSize(NSSize::new(current.size.width.max(size.width), size.height)) };
}
let insets = unsafe { h.alignmentRectInsets() };
let reported = unsafe { h.firstBaselineOffsetFromTop() } + insets.top;
let font = h
.downcast_ref::<NSControl>()
.and_then(|c| unsafe { c.font() });
let offset = match font {
Some(_) if kind == kinds::TEXT_AREA => reported,
Some(f) => {
let (ascender, descender) = unsafe { (f.ascender(), f.descender()) };
let line = ascender - descender;
let align_h = (size.height - insets.top - insets.bottom).max(0.0);
insets.top + ((align_h - line) / 2.0).max(0.0) + ascender
}
None => reported,
};
(offset > 0.0 && offset < size.height).then_some(offset)
}
fn set_frame(&mut self, h: &Handle, frame: Rect, anim: Option<&AnimSpec>) {
if NAV_PAGES.with(|set| set.borrow().contains(&ptr_of(h))) {
return;
}
let r = NSRect::new(
NSPoint::new(frame.origin.x, frame.origin.y),
NSSize::new(frame.size.width, frame.size.height),
);
if let Some(split) = h.downcast_ref::<objc2_app_kit::NSSplitView>() {
let strip = unsafe { split.superview() }
.and_then(|sup| {
let content = unsafe { sup.window() }?.contentView()?;
let mut ancestor = Some(sup.clone());
while let Some(view) = ancestor {
if std::ptr::eq(&raw const *view, &raw const *content) {
break;
}
if view.downcast_ref::<objc2_app_kit::NSSplitView>().is_some() {
return None; }
ancestor = unsafe { view.superview() };
}
let point = unsafe {
sup.convertPoint_toView(
NSPoint::new(frame.origin.x, frame.origin.y),
Some(&content),
)
};
(point.y.abs() <= 0.5).then(|| title_bar_inset(&content))
})
.unwrap_or(0.0);
let r = NSRect::new(
NSPoint::new(r.origin.x, r.origin.y - strip),
NSSize::new(r.size.width, r.size.height + strip),
);
let wraps = NAV_STATE.with(|m| {
m.borrow_mut().get_mut(&ptr_of(h)).and_then(|s| {
#[expect(
clippy::float_cmp,
reason = "unchanged-value check against the strip stored last time"
)]
let changed = s.title_strip != strip;
changed.then(|| {
s.title_strip = strip;
let mut w = vec![s.sidebar_wrap.clone(), s.detail_wrap.clone()];
w.extend(s.list_wrap.clone());
w
})
})
});
let first = NAV_STATE.with(|m| {
m.borrow_mut()
.get_mut(&ptr_of(h))
.map_or((false, None), |s| {
if !s.presentation.is_split() {
return (false, None);
}
let sidebar = !std::mem::replace(&mut s.positioned, true);
(sidebar, take_list_placement(s))
})
});
unsafe {
split.setFrame(r);
split.layoutSubtreeIfNeeded();
}
for wrap in wraps.iter().flatten() {
place_below_strip(wrap, strip);
}
unsafe {
if first.0 {
split.setPosition_ofDividerAtIndex(day_spec::NAV_SIDEBAR_WIDTH, 0);
}
if let Some(w) = first.1 {
split.setPosition_ofDividerAtIndex(day_spec::NAV_SIDEBAR_WIDTH + w, 1);
}
}
} else {
let base = [
frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height,
];
let from = motion::shown_geometry(h);
BASE_FRAMES.with(|m| m.insert(ptr_of(h), base));
let t = TRANSFORMS.with(|m| m.get(ptr_of(h)));
let to = (base, t.unwrap_or(Transform::IDENTITY));
motion::move_geometry(h, from, to, anim, || match t {
Some(t) => apply_geometry(h, base, t),
None => h.setFrame(r),
});
}
}
fn set_scroll_content(&mut self, h: &Handle, content: Size) {
if let Some(sv) = h.downcast_ref::<NSScrollView>()
&& let Some(doc) = unsafe { sv.documentView() }
{
unsafe { doc.setFrameSize(NSSize::new(content.width, content.height)) };
}
}
fn list_row_metrics(&mut self, _h: &Handle) -> day_spec::RowMetrics {
day_spec::RowMetrics {
insets: day_spec::Insets {
top: 4.0,
leading: 8.0,
bottom: 4.0,
trailing: 8.0,
},
min_height: 28.0,
}
}
fn scroll_to(&mut self, h: &Handle, target: Rect, animated: bool) {
if let Some(sv) = h.downcast_ref::<NSScrollView>()
&& unsafe { sv.documentView() }.is_some()
{
let bounds = unsafe { sv.contentView() }.bounds();
let to = NSPoint::new(
scroll::reveal_origin(
bounds.origin.x,
bounds.size.width,
target.origin.x,
target.size.width,
),
scroll::reveal_origin(
bounds.origin.y,
bounds.size.height,
target.origin.y,
target.size.height,
),
);
scroll::scroll_clip_to(sv, to, animated, None);
}
}
fn scroll_offset(&mut self, h: &Handle) -> Point {
scroll::offset(h)
}
fn list_scroll(&mut self, h: &Handle, to: day_spec::ListScroll) -> bool {
let key = ptr_of(h);
if list_entry(key).is_none() {
return false;
}
<AppKit as Platform>::post(Box::new(move || {
let Some((table, data)) = list_entry(key) else {
return;
};
if data.ivars().live.get() {
return;
}
data.apply_held(&table);
let rows = unsafe { table.numberOfRows() };
let Some(scroll) = table.enclosingScrollView() else {
return;
};
if rows <= 0 {
return;
}
let bounds = unsafe { scroll.contentView() }.bounds();
let y = match to.row {
Some(row) => {
let rect = unsafe { table.rectOfRow((row as isize).min(rows - 1)) };
scroll::row_origin(
bounds.origin.y,
bounds.size.height,
rect.origin.y,
rect.origin.y + rect.size.height,
to.align,
)
}
None => table.frame().size.height - bounds.size.height,
};
let done: Box<dyn FnOnce()> = Box::new(move || post_realize_visible_rows(key));
scroll::scroll_clip_to(
&scroll,
NSPoint::new(bounds.origin.x, y),
to.animated,
Some(done),
);
}));
true
}
fn focus(&mut self, h: &Handle, _node: NodeId, focused: bool) {
let Some(window) = h.window() else { return };
let list = list_entry(ptr_of(h));
let responder: &NSResponder = match &list {
Some((table, _)) => table,
None => h,
};
let responder_ptr = std::ptr::from_ref::<NSResponder>(responder) as usize;
if focused {
window.makeFirstResponder(Some(responder));
return;
}
let owns = window
.firstResponder()
.is_some_and(|fr| Retained::as_ptr(&fr) as usize == responder_ptr)
|| owns_first_responder(&window, h);
if owns {
window.makeFirstResponder(None);
}
}
fn set_focusable(&mut self, h: &Handle, node: NodeId, focusable: bool) {
if focusable {
FOCUSABLE_NODES.with(|t| t.insert(ptr_of(h), node));
} else {
FOCUSABLE_NODES.with(|t| t.remove(ptr_of(h)));
}
}
fn set_event_sink(&mut self, sink: EventSink) {
SINK.with(|s| *s.borrow_mut() = Some(Rc::from(sink)));
input::install_monitor();
}
fn set_edit_state(&mut self, state: &day_spec::EditState) {
EDIT_STATE.with(|s| s.set(*state));
}
fn modifiers(&mut self) -> day_spec::Modifiers {
let f = NSEvent::modifierFlags_class();
day_spec::Modifiers::NONE
.with(
day_spec::Modifiers::SHIFT,
f.contains(NSEventModifierFlags::Shift),
)
.with(
day_spec::Modifiers::PRIMARY,
f.contains(NSEventModifierFlags::Command),
)
.with(
day_spec::Modifiers::ALT,
f.contains(NSEventModifierFlags::Option),
)
}
fn set_undo_state(&mut self, state: &day_spec::UndoState) {
let front = undo_front(self.mtm);
*front.ivars().state.borrow_mut() = state.clone();
}
fn attach_list(&mut self, host: &Handle, source: ListSource) {
let key = ptr_of(host);
if let Some((table, data)) = list_entry(key) {
let supplements = source.is_supplement.is_some();
data.ivars().source.replace(Some(source));
data.ivars().held.replace(None);
data.ivars().splice.replace(None);
if data.ivars().supplements.replace(supplements) != supplements {
unsafe {
table.setDelegate(None);
table.setDelegate(Some(ProtocolObject::from_ref(&*data)));
}
}
unsafe { table.reloadData() };
}
post_realize_visible_rows(key);
}
fn attach_tree(&mut self, host: &Handle, source: TreeSource) {
let key = ptr_of(host);
if let Some((outline, data)) = tree_entry(key) {
data.ivars().source.replace(Some(source));
unsafe { outline.reloadData() };
}
<AppKit as Platform>::post(Box::new(move || {
if let Some((outline, _)) = tree_entry(key) {
unsafe { outline.layoutSubtreeIfNeeded() };
}
}));
}
fn adopt(&mut self, raw: RawHandle) -> Handle {
let ptr = raw.cast::<NSView>();
unsafe { Retained::retain(ptr) }.expect("adopt: null list cell handle")
}
fn edit_toolbar(&mut self, h: &Handle, ops: &[day_spec::ToolbarOp]) -> bool {
self.edit_toolbar(h, ops)
}
fn update_toolbar(&mut self, h: &Handle, patch: &day_spec::ToolbarPatch) {
Self::patch_toolbar(h, patch);
}
fn set_app_menu(&mut self, items: &[day_spec::MenuItem]) {
let mtm = self.mtm;
if self.menu_escape_monitor.is_none() {
let block = block2::RcBlock::new(move |event: std::ptr::NonNull<NSEvent>| {
ffi_guard::contain(event.as_ptr(), || {
let event_ref = unsafe { event.as_ref() };
let app = NSApplication::sharedApplication(mtm);
if app.modalWindow().is_none()
&& app.keyWindow().is_some_and(|w| w.attachedSheet().is_none())
&& unsafe { event_ref.charactersIgnoringModifiers() }
.is_some_and(|s| s.to_string() == "\u{1b}")
&& app
.mainMenu()
.is_some_and(|menu| menu.performKeyEquivalent(event_ref))
{
std::ptr::null_mut()
} else {
event.as_ptr()
}
})
});
self.menu_escape_monitor = unsafe {
NSEvent::addLocalMonitorForEventsMatchingMask_handler(
objc2_app_kit::NSEventMask::KeyDown,
&block,
)
}
.map(MenuEscapeMonitor);
}
let app = NSApplication::sharedApplication(mtm);
let menubar = NSMenu::new(mtm);
let mut items = items.to_vec();
let about = extract_app_role(&mut items, day_spec::MenuRole::About);
let updates = extract_app_role(&mut items, day_spec::MenuRole::CheckForUpdates);
let prefs = extract_app_role(&mut items, day_spec::MenuRole::Preferences);
let quit = extract_app_role(&mut items, day_spec::MenuRole::Quit);
let services =
install_application_menu(mtm, &menubar, &self.app_name, about, updates, prefs, quit);
let items =
day_core::menu::standard_menu_bar_for(day_core::menu::MenuBarStyle::Macos, items);
let mut help_menu: Option<(String, Retained<NSMenu>)> = None;
for item in &items {
match item {
day_spec::MenuItem::Submenu { label, items, role } => {
let sub = build_ns_menu(mtm, label, items);
let it = NSMenuItem::new(mtm);
if *role == Some(day_spec::MenuBarRole::Help)
|| *label == day_localization::t(day_localization::key::day_help)
{
help_menu = Some((label.clone(), sub));
continue;
}
it.setTitle(&NSString::from_str(label));
it.setSubmenu(Some(&sub));
menubar.addItem(&it);
}
other => {
let sub = build_ns_menu(mtm, "", std::slice::from_ref(other));
let it = NSMenuItem::new(mtm);
it.setSubmenu(Some(&sub));
menubar.addItem(&it);
}
}
}
if !model_has_role(&items, day_spec::MenuRole::Minimize) {
install_windows_menu(mtm, &app, &menubar);
}
if let Some((label, help)) = help_menu {
let it = NSMenuItem::new(mtm);
it.setTitle(&NSString::from_str(&label));
it.setSubmenu(Some(&help));
menubar.addItem(&it);
unsafe { app.setHelpMenu(Some(&help)) };
}
app.setMainMenu(Some(&menubar));
unsafe { app.setServicesMenu(Some(&services)) };
}
fn set_context_menu(&mut self, h: &Handle, _node: NodeId, items: &[day_spec::MenuItem]) {
fn apply_to_subviews(v: &NSView, menu: &NSMenu) {
for sub in v.subviews() {
unsafe { sub.setMenu(Some(menu)) };
apply_to_subviews(&sub, menu);
}
}
let Some(view) = h.downcast_ref::<NSView>() else {
return;
};
if items.is_empty() {
unsafe { view.setMenu(None) };
return;
}
let menu = build_ns_menu(self.mtm, "", items);
unsafe { view.setMenu(Some(&menu)) };
apply_to_subviews(view, &menu);
}
fn set_drag_source(&mut self, h: &Handle, source: day_spec::transfer::Source) {
transfer::source(h, source);
}
fn set_drop_target(&mut self, h: &Handle, target: day_spec::transfer::Target) {
transfer::target(h, target);
}
fn set_context_menu_fn(&mut self, h: &Handle, _node: NodeId, f: day_spec::ContextMenuFn) {
CTX_MENU_FNS.with(|t| t.insert(ptr_of(h), f));
}
fn set_button_menu(&mut self, h: &Handle, _node: NodeId, f: day_spec::ContextMenuFn) {
let Some(btn) = h.downcast_ref::<NSButton>() else {
return;
};
unsafe { btn.sendActionOn(objc2_app_kit::NSEventMask::LeftMouseDown) };
BUTTON_MENUS.with(|t| t.insert(ptr_of(h), f));
}
fn enable_gesture(&mut self, h: &Handle, node: NodeId, kind: day_spec::GestureKind) {
let key = ptr_of(h);
if kind == day_spec::GestureKind::Pan {
PAN_NODES.with(|t| t.insert(key, node));
install_container_pan_monitor();
return;
}
if kind == day_spec::GestureKind::Rotate {
ROTATE_NODES.with(|t| t.insert(key, node));
return;
}
if kind == day_spec::GestureKind::Hover {
let first = HOVER_NODES.with(|t| t.get(key)).is_none();
HOVER_NODES.with(|t| t.insert(key, node));
if first {
input::install_hover_area(h);
}
return;
}
if kind == day_spec::GestureKind::Press {
input::PRESS_NODES.with(|t| t.insert(key, node));
input::install_monitor();
return;
}
if kind == day_spec::GestureKind::Touches {
return;
}
let already = GESTURES.with(|m| {
m.borrow()
.get(&key)
.is_some_and(|v| v.iter().any(|t| t.ivars().kind == kind))
});
if already {
return;
}
let mtm = self.mtm;
let target = DayGesture::new(mtm, node, kind);
let recognizer: Retained<NSGestureRecognizer> = unsafe {
match kind {
day_spec::GestureKind::Drag => {
Retained::into_super(NSPanGestureRecognizer::initWithTarget_action(
NSPanGestureRecognizer::alloc(mtm),
Some(&target),
Some(sel!(fire:)),
))
}
day_spec::GestureKind::Pinch => Retained::into_super(
objc2_app_kit::NSMagnificationGestureRecognizer::initWithTarget_action(
objc2_app_kit::NSMagnificationGestureRecognizer::alloc(mtm),
Some(&target),
Some(sel!(fire:)),
),
),
day_spec::GestureKind::LongPress => {
let press = NSPressGestureRecognizer::initWithTarget_action(
NSPressGestureRecognizer::alloc(mtm),
Some(&target),
Some(sel!(fire:)),
);
press.setDelaysPrimaryMouseButtonEvents(false);
press.setDelegate(Some(ProtocolObject::from_ref(&*target)));
Retained::into_super(press)
}
day_spec::GestureKind::DoubleTap => {
let double = NSClickGestureRecognizer::initWithTarget_action(
NSClickGestureRecognizer::alloc(mtm),
Some(&target),
Some(sel!(fire:)),
);
double.setNumberOfClicksRequired(2);
double.setDelaysPrimaryMouseButtonEvents(false);
double.setDelegate(Some(ProtocolObject::from_ref(&*target)));
Retained::into_super(double)
}
_ => {
let click = NSClickGestureRecognizer::initWithTarget_action(
NSClickGestureRecognizer::alloc(mtm),
Some(&target),
Some(sel!(fire:)),
);
click.setDelegate(Some(ProtocolObject::from_ref(&*target)));
Retained::into_super(click)
}
}
};
unsafe { h.addGestureRecognizer(&recognizer) };
GESTURES.with(|m| m.borrow_mut().entry(key).or_default().push(target));
}
fn gesture_support(&self, kind: day_spec::GestureKind) -> Support {
match kind {
day_spec::GestureKind::Touches => Support::Unsupported,
_ => Support::Native,
}
}
fn set_hit_slop(&mut self, h: &Handle, _node: NodeId, slop: day_spec::Insets) {
input::set_hit_slop(h, slop);
}
fn set_drag_axis(&mut self, _h: &Handle, _node: NodeId, _axis: Option<day_spec::DragAxis>) {}
fn set_subtree_enabled(&mut self, h: &Handle, _node: NodeId, enabled: bool) {
input::set_subtree_enabled(h, enabled);
}
fn primary_pointer(&self) -> day_spec::PointerKind {
input::pointer_kind()
}
fn set_a11y(&mut self, h: &Handle, a11y: &A11yProps) {
unsafe {
if let Some(id) = &a11y.identifier {
h.setAccessibilityIdentifier(Some(&NSString::from_str(id)));
}
if let Some(label) = &a11y.label {
h.setAccessibilityLabel(Some(&NSString::from_str(label)));
APP_LABELED.with(|t| t.insert(ptr_of(h), ()));
} else {
APP_LABELED.with(|t| t.remove(ptr_of(h)));
}
if let Some(hint) = &a11y.hint {
h.setAccessibilityHelp(Some(&NSString::from_str(hint)));
}
if let Some(value) = &a11y.value {
let ns = NSString::from_str(value);
h.setAccessibilityValue(Some(ns.as_ref() as &objc2::runtime::AnyObject));
}
if let Some(role) = ns_role(a11y.role) {
h.setAccessibilityRole(Some(role));
}
if a11y.role == day_spec::Role::Tab {
h.setAccessibilitySubrole(Some(objc2_app_kit::NSAccessibilityTabButtonSubrole));
}
if let Some(on) = a11y.selected {
h.setAccessibilitySelected(on);
}
if a11y.hidden {
h.setAccessibilityElement(false);
h.setAccessibilityChildren(Some(&NSArray::new()));
}
}
}
fn announce(&mut self, text: &str, urgent: bool) {
use objc2_app_kit::{
NSAccessibilityAnnouncementKey, NSAccessibilityAnnouncementRequestedNotification,
NSAccessibilityPostNotificationWithUserInfo, NSAccessibilityPriorityKey,
NSAccessibilityPriorityLevel,
};
let app = NSApplication::sharedApplication(self.mtm());
let window = primary_content().and_then(|content| content.window());
let element: &objc2::runtime::AnyObject = match &window {
Some(window) => window,
None => &app,
};
let priority = if urgent {
NSAccessibilityPriorityLevel::High
} else {
NSAccessibilityPriorityLevel::Medium
};
let announcement = NSString::from_str(text);
let priority = NSNumber::new_isize(priority.0);
let values: [&objc2::runtime::AnyObject; 2] = [&announcement, &priority];
unsafe {
let user_info = NSDictionary::from_slices::<NSString>(
&[NSAccessibilityAnnouncementKey, NSAccessibilityPriorityKey],
&values,
);
NSAccessibilityPostNotificationWithUserInfo(
element,
NSAccessibilityAnnouncementRequestedNotification,
Some(&user_info),
);
}
}
fn read_native(&self, h: &Handle) -> day_spec::NativeSnapshot {
unsafe {
let role = h
.accessibilityRole()
.map_or(day_spec::Role::None, |r| day_role_from_ns(&r.to_string()));
let picker = picker::read_native(h);
day_spec::NativeSnapshot {
found: true,
role,
label: h.accessibilityLabel().map(|s| s.to_string()),
value: h.accessibilityValue().and_then(|v| {
v.downcast_ref::<NSString>()
.map(std::string::ToString::to_string)
}),
identifier: h
.accessibilityIdentifier()
.map(|s| s.to_string())
.filter(|s| !s.is_empty()),
text: match &picker {
Some((text, _)) => text.clone(),
None => native_text(h),
},
number: native_number(h),
checked: if let Some(sw) = h.downcast_ref::<NSSwitch>() {
match sw.state() {
s if s == NSControlStateValueOn => Some(true),
s if s == NSControlStateValueOff => Some(false),
_ => None,
}
} else if let Some(btn) = h.downcast_ref::<NSButton>() {
BUTTON_SELECTED
.with(|m| m.borrow().contains_key(&ptr_of(h)))
.then(|| btn.state() == NSControlStateValueOn)
} else {
None
},
enabled: match (&picker, h.downcast_ref::<NSControl>()) {
(Some((_, on)), _) => *on,
(None, Some(_)) if h.downcast_ref::<DayLabel>().is_some() => None,
(None, Some(c)) => Some(c.isEnabled()),
(None, None) => None,
},
visible: Some(h.window().is_some() && !h.isHiddenOrHasHiddenAncestor()),
frame: native_frame(h),
}
}
}
fn request_frame(
&mut self,
host: &Handle,
cb: day_spec::FrameCallback,
) -> day_spec::CancelFrame {
frame::request(host, cb)
}
fn replay(&mut self, h: &Handle, ops: &[DrawOp], _size: Size) {
OPS.with(|t| t.insert(ptr_of(h), ops.to_vec()));
unsafe { h.setNeedsDisplay(true) };
}
fn toggle_sidebar(&mut self, host: &Self::Handle) -> bool {
NAV_STATE.with(|m| {
let m = m.borrow();
let Some(st) = m.get(&ptr_of(host)) else {
return false;
};
if !matches!(
st.presentation,
NavPresentation::Split | NavPresentation::Rail
) {
return false;
}
let item = &st.sidebar_item;
let collapsed = unsafe { item.isCollapsed() };
unsafe { item.setCollapsed(!collapsed) };
true
})
}
fn snapshot_window(&mut self) -> Result<Vec<u8>, String> {
let content = self.content.as_ref().ok_or("no window content")?;
match self.shown_document_content(content) {
Some(shown) => snapshot_view(&shown),
None => snapshot_view(content),
}
}
fn decode_image(&mut self, req: u64, id: day_spec::BitmapId, bytes: &[u8]) {
use objc2::AllocAnyThread as _;
let data = NSData::with_bytes(bytes);
let Some(rep) = (unsafe { objc2_app_kit::NSBitmapImageRep::imageRepWithData(&data) })
else {
emit(
WINDOW_NODE,
Event::ImageDecoded {
req,
result: Err(day_spec::ImageError::Decode),
},
);
return;
};
let (w, h) = unsafe { (rep.pixelsWide() as f64, rep.pixelsHigh() as f64) };
let info = day_spec::BitmapInfo {
pixels: Size::new(w, h),
scale: 1.0,
format: day_spec::ImageFormat::sniff(bytes),
has_alpha: unsafe { rep.hasAlpha() },
};
let image = unsafe {
objc2_app_kit::NSImage::initWithSize(objc2_app_kit::NSImage::alloc(), NSSize::new(w, h))
};
unsafe { image.addRepresentation(&rep) };
BITMAPS.with(|m| {
m.borrow_mut().insert(
id.0,
AppKitBitmap {
image,
rep: Some(rep),
info,
},
)
});
emit(
WINDOW_NODE,
Event::ImageDecoded {
req,
result: Ok(info),
},
);
}
fn image_info(&mut self, id: day_spec::BitmapId) -> Option<day_spec::BitmapInfo> {
BITMAPS.with(|m| m.borrow().get(&id.0).map(|b| b.info))
}
fn image_properties(&mut self, id: day_spec::BitmapId) -> Option<day_spec::ImageProperties> {
let rep = BITMAPS.with(|m| m.borrow().get(&id.0).and_then(|b| b.rep.clone()))?;
let mut out = day_spec::ImageProperties::default();
let size = unsafe { rep.size() };
if size.width > 0.0 {
let dpi = unsafe { rep.pixelsWide() } as f64 / size.width * 72.0;
if (dpi - 72.0).abs() > 0.5 {
out.dpi = Some(dpi);
}
}
if let Some(exif) = rep.valueForProperty(unsafe { objc2_app_kit::NSImageEXIFData }) {
let keys: Retained<NSArray<NSString>> = unsafe { msg_send![&*exif, allKeys] };
for key in &keys {
let value: Option<Retained<objc2::runtime::AnyObject>> =
unsafe { msg_send![&*exif, objectForKey: &*key] };
let Some(value) = value else { continue };
let text: Retained<NSString> = unsafe { msg_send![&*value, description] };
let (k, v) = (key.to_string(), text.to_string());
match k.as_str() {
"Orientation" => out.orientation = v.parse().ok(),
"DateTimeOriginal" => out.created = Some(v.clone()),
_ => {}
}
out.extra.push((k, v));
}
}
Some(out)
}
fn encode_formats(&mut self) -> Vec<day_spec::ImageFormat> {
use day_spec::ImageFormat::{Bmp, Gif, Jpeg, Png, Tiff};
vec![Png, Jpeg, Tiff, Bmp, Gif]
}
fn encode_image(&mut self, req: u64, id: day_spec::BitmapId, spec: &day_spec::EncodeSpec) {
let result = encode_bitmap(id, spec);
emit(WINDOW_NODE, Event::ImageEncoded { req, result });
}
fn release_image(&mut self, id: day_spec::BitmapId) {
release_bitmap(id);
}
fn render_bitmap(
&mut self,
req: u64,
id: day_spec::BitmapId,
ops: &[DrawOp],
size: Size,
scale: f64,
) {
let resources = Sendable(OffscreenResources::collect(ops));
let ops = ops.to_vec();
day_spec::offscreen::spawn(move || {
let resources = resources;
let painted = day_spec::ffi_guard::contain(
Err(day_spec::ExportError::Failed(
"the offscreen painter panicked".into(),
)),
|| {
let started = std::time::Instant::now();
let painted = resources
.0
.with_installed(|| render_bitmap_cgimage(&ops, size, scale));
log::debug!(
"day-toolkit-appkit: bitmap {}x{} painted in {:.1} ms ({} ops)",
size.width * scale,
size.height * scale,
started.elapsed().as_secs_f64() * 1000.0,
ops.len()
);
painted
},
);
let painted = Sendable(painted);
<AppKit as Platform>::post(Box::new(move || {
let painted = painted;
let result = painted.0.map(|cg_image| {
let bitmap = bitmap_from_cgimage(&cg_image, scale);
let info = bitmap.info;
BITMAPS.with(|m| m.borrow_mut().insert(id.0, bitmap));
info
});
emit(WINDOW_NODE, Event::BitmapRendered { req, result });
}));
});
}
fn render_pdf(&mut self, req: u64, pages: &[day_spec::PdfPage], info: &day_spec::PdfInfo) {
let result = render_pdf_bytes(pages, info);
emit(WINDOW_NODE, Event::PdfRendered { req, result });
}
fn font_families(&mut self) -> Vec<day_spec::FontFamilyInfo> {
use day_spec::FontWeight as W;
let mtm =
objc2::MainThreadMarker::new().expect("day-toolkit-appkit runs on the main thread");
let manager = unsafe { objc2_app_kit::NSFontManager::sharedFontManager(mtm) };
let mut out = Vec::new();
for family in &unsafe { manager.availableFontFamilies() } {
let name = family.to_string();
if name.starts_with('.') {
continue;
}
let mut faces = Vec::new();
if let Some(members) = unsafe { manager.availableMembersOfFontFamily(&family) } {
for member in &members {
if member.count() < 4 {
continue;
}
let style = member
.objectAtIndex(1)
.downcast_ref::<NSString>()
.map_or_default(std::string::ToString::to_string);
let weight = member
.objectAtIndex(2)
.downcast_ref::<objc2_foundation::NSNumber>()
.map_or(5, objc2_foundation::NSNumber::integerValue);
let traits = member
.objectAtIndex(3)
.downcast_ref::<objc2_foundation::NSNumber>()
.map_or(0, objc2_foundation::NSNumber::integerValue);
let weight = match weight {
..=2 => W::UltraLight,
3 => W::Thin,
4 => W::Light,
5 => W::Regular,
6 => W::Medium,
7 | 8 => W::Semibold,
9 => W::Bold,
10 | 11 => W::Heavy,
_ => W::Black,
};
let italic = traits & 0x1 != 0; faces.push(day_spec::FontFace {
name: if style.is_empty() {
day_spec::FontFace::synthesized_name(weight, italic)
} else {
style
},
weight,
italic,
});
}
}
out.push(day_spec::FontFamilyInfo {
family: name,
faces,
});
}
out
}
fn measure_text(
&mut self,
text: &str,
size: f64,
font: &day_spec::CanvasFont,
) -> Option<day_spec::TextMetrics> {
let nsfont = canvas_nsfont(size, font);
let attrs = canvas_text_attrs(&nsfont, day_spec::Color::BLACK);
let ns = NSString::from_str(text);
let sz: NSSize = unsafe { msg_send![&ns, sizeWithAttributes: &*attrs] };
let unbounded = NSSize::new(f64::MAX, f64::MAX);
let ink: NSRect = unsafe {
msg_send![
&ns,
boundingRectWithSize: unbounded,
options: objc2_app_kit::NSStringDrawingOptions::UsesDeviceMetrics,
attributes: &*attrs,
]
};
Some(day_spec::TextMetrics::from_baseline(
sz.width,
nsfont.ascender(),
sz.height - nsfont.ascender(),
nsfont.capHeight(),
day_spec::Rect::new(
ink.origin.x,
-(ink.origin.y + ink.size.height),
ink.size.width,
ink.size.height,
),
))
}
fn snapshot_window_chrome(&mut self) -> Result<Vec<u8>, String> {
let content = self.content.as_ref().ok_or("no window content")?;
match self.shown_document_content(content) {
Some(shown) => snapshot_view_chrome(&shown),
None => snapshot_view_chrome(content),
}
}
fn snapshot_window_of(&mut self, host: &Handle) -> Result<Vec<u8>, String> {
snapshot_view(host)
}
fn open_window(
&mut self,
id: NodeId,
options: &WindowOptions,
kind: day_spec::WindowKind,
) -> day_spec::WindowOpenReply<Handle> {
let prefs = kind.is_panel();
let (window, delegate, content) = self.make_window(options, prefs, Some(id));
if prefs {
window.center();
unsafe { window.setExcludedFromWindowsMenu(true) };
} else {
let from = match self.cascade {
Some(p) => p,
None => self.window.as_ref().map_or(NSPoint::new(0.0, 0.0), |w| {
let f = w.frame();
let top_left = NSPoint::new(f.origin.x, f.origin.y + f.size.height);
unsafe { w.cascadeTopLeftFromPoint(top_left) }
}),
};
self.cascade = Some(unsafe { window.cascadeTopLeftFromPoint(from) });
}
window_chrome::place(&window, options.placement);
if prefs || matches!(options.placement, day_spec::WindowPlacement::Centered) {
center_on_screen_of(&window, self.window.as_deref());
}
let _ = pin_below_title_bar(&window);
if !options.start_hidden {
window.makeKeyAndOrderFront(None);
}
mark_tree_needs_display(&content);
self.secondary.push(SecondaryWin {
window,
delegate,
content: content.clone(),
});
day_spec::WindowOpenReply::Open(content)
}
fn native_window_tabs(&self) -> bool {
true
}
fn group_windows(&mut self, hosts: &[Handle]) {
let windows: Vec<_> = hosts.iter().filter_map(|h| self.ns_window_for(h)).collect();
let Some(first) = windows.first() else {
return;
};
unsafe {
NSWindow::setAllowsAutomaticWindowTabbing(true, self.mtm);
}
for window in windows.iter().skip(1) {
unsafe {
first.addTabbedWindow_ordered(window, objc2_app_kit::NSWindowOrderingMode::Above);
}
}
if let Some(group) = first.tabGroup() {
if !group.isTabBarVisible() {
unsafe {
first.toggleTabBar(None);
}
}
for (i, window) in windows.iter().enumerate() {
group.insertWindow_atIndex(window, i as isize);
}
}
for window in &windows {
if let Some(size) = pin_below_title_bar(window) {
let node = window
.delegate()
.and_then(|d| d.downcast::<DayWinDelegate>().ok())
.and_then(|d| d.ivars().node)
.unwrap_or(WINDOW_NODE);
emit(node, Event::WindowResized(size));
}
}
}
fn window_tab_order(&self, host: &Handle) -> Vec<NodeId> {
let Some(window) = self.ns_window_for(host) else {
return Vec::new();
};
let windows = window.tabGroup().map(|g| g.windows());
let Some(windows) = windows else {
return Vec::new();
};
windows
.iter()
.filter_map(|window| {
self.secondary
.iter()
.find(|w| std::ptr::eq(&raw const *w.window, &raw const *window))
.and_then(|w| w.delegate.ivars().node)
})
.collect()
}
fn close_window(&mut self, host: &Handle) {
if let Some(w) = self
.secondary
.iter()
.find(|w| ptr_of(&w.content) == ptr_of(host))
{
w.window.close();
}
}
fn focus_window(&mut self, host: &Handle) {
if let Some(w) = self
.secondary
.iter()
.find(|w| ptr_of(&w.content) == ptr_of(host))
{
w.window.makeKeyAndOrderFront(None);
}
}
fn fit_window(&mut self, host: &Handle, size: Size) {
let Some(w) = self
.secondary
.iter()
.find(|w| ptr_of(&w.content) == ptr_of(host))
else {
return;
};
unsafe {
w.window
.setContentSize(NSSize::new(size.width, size.height));
let mask = w.window.styleMask();
w.window
.setStyleMask(mask & !objc2_app_kit::NSWindowStyleMask::Resizable);
w.window.center();
}
center_on_screen_of(&w.window, self.window.as_deref());
}
fn set_window_title(&mut self, host: &Handle, title: &str) {
if let Some(w) = self
.secondary
.iter()
.find(|w| ptr_of(&w.content) == ptr_of(host))
{
w.window.setTitle(&NSString::from_str(title));
return;
}
if self
.content
.as_ref()
.is_some_and(|c| ptr_of(c) == ptr_of(host))
&& let Some(w) = self.window.as_ref()
{
w.setTitle(&NSString::from_str(title));
}
}
fn apply_window(&mut self, host: &Handle, change: &day_spec::WindowChange) {
use day_spec::WindowChange as C;
let Some(window) = self.ns_window_for(host) else {
return;
};
match change {
C::State(state) => set_ns_window_state(&window, *state),
C::ContentProtected(on) => window.setSharingType(if *on {
objc2_app_kit::NSWindowSharingType::None
} else {
objc2_app_kit::NSWindowSharingType::ReadOnly
}),
other => window_chrome::apply_property(&window, other),
}
}
fn window_frame(&self, host: &Handle) -> Option<day_spec::Rect> {
self.ns_window_for(host)
.map(|w| window_chrome::frame_of(&w))
}
fn monitors(&self) -> Vec<day_spec::Monitor> {
window_chrome::monitors(self.mtm)
}
fn set_launcher_shortcuts(&mut self, _shortcuts: &[day_spec::LauncherShortcut]) {}
fn set_dock_menu(&mut self, items: &[day_spec::MenuItem]) {
dock::set_app_items(self.mtm, items);
}
fn set_drag_region(&mut self, h: &Handle, drag: bool) {
window_chrome::set_drag_region(h, drag);
}
fn set_status_items(&mut self, items: &[day_spec::StatusItemSpec]) {
status_item::set_items(self.mtm, items);
}
fn set_app_progress(&mut self, progress: day_spec::AppProgress) {
status_item::set_progress(self.mtm, progress);
}
fn set_dock_visible(&mut self, visible: bool) {
status_item::set_dock_visible(self.mtm, visible);
}
fn set_keep_running(&mut self, keep: bool) {
status_item::set_keep_running(keep);
}
fn quit_app(&mut self) {
let app = NSApplication::sharedApplication(self.mtm);
unsafe { app.terminate(None) };
}
fn present(&mut self, req: u64, spec: &present::PresentSpec) {
use present::PresentSpec;
let mtm = self.mtm;
let app = NSApplication::sharedApplication(mtm);
let key = app.keyWindow().filter(|k| {
let mut owner = k.clone();
while let Some(parent) = owner.sheetParent() {
owner = parent;
}
self.window
.as_deref()
.is_some_and(|w| std::ptr::eq(w, &raw const *owner))
|| self
.secondary
.iter()
.any(|s| std::ptr::eq(&raw const *s.window, &raw const *owner))
});
let Some(mut window) = key.or_else(|| self.window.clone()) else {
emit(
WINDOW_NODE,
Event::PresentResult {
req,
result: present::PresentResult::Dismissed,
},
);
return;
};
while let Some(sheet) = window.attachedSheet() {
window = sheet;
}
match spec {
PresentSpec::OpenFile {
filters, multiple, ..
} => {
let panel = unsafe { objc2_app_kit::NSOpenPanel::openPanel(mtm) };
unsafe {
panel.setCanChooseFiles(true);
panel.setCanChooseDirectories(false);
panel.setAllowsMultipleSelection(*multiple);
panel.setMessage(Some(&NSString::from_str(spec.title())));
}
apply_allowed_file_types(&panel, filters);
let p = panel.clone();
let handler: block2::RcBlock<dyn Fn(isize)> =
block2::RcBlock::new(move |resp: isize| {
ffi_guard::contain((), || emit_open_panel_result(req, resp, &p));
});
unsafe { panel.beginSheetModalForWindow_completionHandler(&window, &handler) };
PRESENT_PANELS.with(|m| m.borrow_mut().insert(req, Retained::into_super(panel)));
return;
}
PresentSpec::OpenFolder { .. } => {
let panel = unsafe { objc2_app_kit::NSOpenPanel::openPanel(mtm) };
unsafe {
panel.setCanChooseFiles(false);
panel.setCanChooseDirectories(true);
panel.setCanCreateDirectories(true);
panel.setAllowsMultipleSelection(false);
panel.setMessage(Some(&NSString::from_str(spec.title())));
}
let p = panel.clone();
let handler: block2::RcBlock<dyn Fn(isize)> =
block2::RcBlock::new(move |resp: isize| {
ffi_guard::contain((), || emit_open_panel_result(req, resp, &p));
});
unsafe { panel.beginSheetModalForWindow_completionHandler(&window, &handler) };
PRESENT_PANELS.with(|m| m.borrow_mut().insert(req, Retained::into_super(panel)));
return;
}
PresentSpec::SaveFile { suggested_name, .. } => {
let panel = unsafe { objc2_app_kit::NSSavePanel::savePanel(mtm) };
unsafe {
panel.setMessage(Some(&NSString::from_str(spec.title())));
panel.setNameFieldStringValue(&NSString::from_str(suggested_name));
}
apply_allowed_file_types(&panel, spec.filters());
let p = panel.clone();
let handler: block2::RcBlock<dyn Fn(isize)> =
block2::RcBlock::new(move |resp: isize| {
ffi_guard::contain((), || emit_panel_result(req, resp, &p));
});
unsafe { panel.beginSheetModalForWindow_completionHandler(&window, &handler) };
PRESENT_PANELS.with(|m| m.borrow_mut().insert(req, panel));
return;
}
_ => {}
}
let alert = unsafe { objc2_app_kit::NSAlert::new(mtm) };
unsafe { alert.setMessageText(&NSString::from_str(spec.title())) };
if let Some(msg) = spec.message() {
unsafe { alert.setInformativeText(&NSString::from_str(msg)) };
}
let handler: block2::RcBlock<dyn Fn(isize)> = match spec {
PresentSpec::Dialog { buttons, .. } => {
if buttons
.iter()
.any(|b| b.role == present::ButtonRole::Destructive)
{
unsafe { alert.setAlertStyle(objc2_app_kit::NSAlertStyle::Warning) };
}
for b in buttons {
unsafe { alert.addButtonWithTitle(&NSString::from_str(&b.label)) };
}
block2::RcBlock::new(move |resp: isize| {
ffi_guard::contain((), || {
let idx = resp - 1000;
emit(
WINDOW_NODE,
Event::PresentResult {
req,
result: present::PresentResult::Button(idx as i64),
},
);
PRESENT_ALERTS.with(|m| {
m.borrow_mut().remove(&req);
});
});
})
}
PresentSpec::Prompt {
placeholder,
initial,
ok,
cancel,
..
} => {
let tf = unsafe { NSTextField::new(mtm) };
unsafe {
tf.setFrame(NSRect::new(
NSPoint::new(0.0, 0.0),
NSSize::new(260.0, 24.0),
));
tf.setEditable(true);
tf.setBezeled(true);
tf.setStringValue(&NSString::from_str(initial));
tf.setPlaceholderString(Some(&NSString::from_str(placeholder)));
alert.setAccessoryView(Some(&tf));
alert.addButtonWithTitle(&NSString::from_str(ok)); alert.addButtonWithTitle(&NSString::from_str(cancel)); }
block2::RcBlock::new(move |resp: isize| {
ffi_guard::contain((), || {
let result = if resp == 1000 {
present::PresentResult::Text(unsafe { tf.stringValue() }.to_string())
} else {
present::PresentResult::Dismissed
};
emit(WINDOW_NODE, Event::PresentResult { req, result });
PRESENT_ALERTS.with(|m| {
m.borrow_mut().remove(&req);
});
});
})
}
PresentSpec::OpenFile { .. }
| PresentSpec::OpenFolder { .. }
| PresentSpec::SaveFile { .. } => unreachable!(),
};
unsafe {
alert.beginSheetModalForWindow_completionHandler(&window, Some(&handler));
}
PRESENT_ALERTS.with(|m| m.borrow_mut().insert(req, alert));
}
fn dismiss(&mut self, req: u64) {
let alert = PRESENT_ALERTS.with(|m| m.borrow_mut().remove(&req));
if let Some(alert) = alert {
let sheet = alert.window();
if let Some(parent) = sheet.sheetParent() {
unsafe { parent.endSheet(&sheet) };
}
}
let panel = PRESENT_PANELS.with(|m| m.borrow_mut().remove(&req));
if let Some(panel) = panel
&& let Some(parent) = panel.sheetParent()
{
unsafe { parent.endSheet(&panel) };
}
}
fn application_handlers(
&mut self,
query: &day_spec::applications::HandlerQuery,
) -> Result<day_spec::applications::ApplicationHandlers, day_spec::applications::ApplicationError>
{
applications::handlers(query)
}
fn open_url_with_application(
&mut self,
url: &str,
application: &str,
completion: day_spec::applications::OpenApplicationCompletion,
) {
applications::open(url, application, completion);
}
fn share_support(&self) -> day_spec::Support {
day_spec::Support::Native
}
fn share(&mut self, spec: &day_spec::share::ShareSpec, anchor: Option<&Self::Handle>) -> bool {
use objc2::{msg_send, runtime::AnyObject};
use objc2_foundation::{NSArray, NSPoint, NSRect, NSSize, NSString, NSURL};
let Some(mtm) = objc2::MainThreadMarker::new() else {
return false;
};
let Some(url) = NSURL::URLWithString(&NSString::from_str(&spec.url)) else {
return false;
};
let app = objc2_app_kit::NSApplication::sharedApplication(mtm);
let Some(window_view) = app.keyWindow().and_then(|w| w.contentView()) else {
return false;
};
unsafe {
let items = NSArray::from_retained_slice(&[url]);
let picker: objc2::rc::Allocated<AnyObject> =
msg_send![objc2::class!(NSSharingServicePicker), alloc];
let picker: objc2::rc::Retained<AnyObject> = msg_send![picker, initWithItems: &*items];
let delegate = DaySharePickerDelegate::alloc(mtm).set_ivars(SharePickerIvars {
actions: spec.actions.clone(),
});
let delegate: Retained<DaySharePickerDelegate> = msg_send![super(delegate), init];
let _: () = msg_send![&*picker, setDelegate: &*delegate];
let (view, rect, edge) = if let Some(view) = anchor {
(view.clone(), view.bounds(), 1usize)
} else {
let bounds = window_view.bounds();
let y = if window_view.isFlipped() {
30.0
} else {
bounds.size.height - 30.0
};
let rect = NSRect::new(
NSPoint::new(bounds.size.width - 30.0, y),
NSSize::new(1.0, 1.0),
);
(window_view, rect, 3usize)
};
let _: () =
msg_send![&*picker, showRelativeToRect: rect, ofView: &*view, preferredEdge: edge];
self.sharing = Some(picker);
self.sharing_delegate = Some(delegate);
}
true
}
fn open_url(&mut self, url: &str) {
let nsurl = unsafe { objc2_foundation::NSURL::URLWithString(&NSString::from_str(url)) };
if let Some(nsurl) = nsurl {
unsafe { objc2_app_kit::NSWorkspace::sharedWorkspace().openURL(&nsurl) };
}
}
}
#[allow(deprecated)]
fn apply_allowed_file_types(
panel: &objc2_app_kit::NSSavePanel,
filters: &[day_spec::present::FileFilter],
) {
let exts: Vec<Retained<NSString>> = filters
.iter()
.flat_map(|f| f.extensions.iter())
.map(|e| NSString::from_str(e))
.collect();
if !exts.is_empty() {
let refs: Vec<&NSString> = exts.iter().map(|r| &**r).collect();
let arr = objc2_foundation::NSArray::from_slice(&refs);
unsafe { panel.setAllowedFileTypes(Some(&arr)) };
}
}
fn emit_open_panel_result(req: u64, resp: isize, panel: &objc2_app_kit::NSOpenPanel) {
let mut paths = Vec::new();
if resp == 1 {
for url in &unsafe { panel.URLs() } {
if let Some(p) = unsafe { url.path() } {
paths.push(p.to_string());
}
}
}
let result = if paths.is_empty() {
present::PresentResult::Dismissed
} else {
present::PresentResult::Files(paths)
};
emit(WINDOW_NODE, Event::PresentResult { req, result });
PRESENT_PANELS.with(|m| {
m.borrow_mut().remove(&req);
});
}
fn emit_panel_result(req: u64, resp: isize, panel: &objc2_app_kit::NSSavePanel) {
let result = if resp == 1 {
unsafe { panel.URL() }
.and_then(|url| unsafe { url.path() })
.map_or(present::PresentResult::Dismissed, |p| {
present::PresentResult::Files(vec![p.to_string()])
})
} else {
present::PresentResult::Dismissed
};
emit(WINDOW_NODE, Event::PresentResult { req, result });
PRESENT_PANELS.with(|m| {
m.borrow_mut().remove(&req);
});
}
impl Platform for AppKit {
const TARGET: &'static str = "macos-appkit";
const TOOLKIT: &'static str = "appkit";
fn run(mut self, options: WindowOptions, ready: Box<dyn FnOnce(Self, Handle, Size)>) {
let mtm = self.mtm;
self.app_name = options
.app_name
.clone()
.unwrap_or_else(|| options.title.clone());
unsafe {
objc2_foundation::NSProcessInfo::processInfo()
.setProcessName(&NSString::from_str(&self.app_name));
let bundle = objc2_foundation::NSBundle::mainBundle();
let info: Option<Retained<objc2_foundation::NSDictionary>> =
msg_send![&bundle, infoDictionary];
if let Some(info) = info {
let key = NSString::from_str("CFBundleName");
let value = NSString::from_str(&self.app_name);
if info.respondsToSelector(objc2::sel!(setObject:forKey:)) {
let _: () = msg_send![&info, setObject: &*value, forKey: &*key];
}
}
}
if day_core::layout_direction() == day_spec::LayoutDirection::Rtl {
unsafe {
use objc2_foundation::{NSDictionary, NSNumber, NSUserDefaults};
let yes: objc2::rc::Retained<objc2::runtime::AnyObject> = NSNumber::new_bool(true)
.into_super()
.into_super()
.into_super();
let dict = NSDictionary::from_retained_objects(
&[
&*NSString::from_str("AppleTextDirection"),
&*NSString::from_str("NSForceRightToLeftWritingDirection"),
],
&[yes.clone(), yes],
);
NSUserDefaults::standardUserDefaults().registerDefaults(&dict);
}
}
let app = NSApplication::sharedApplication(mtm);
let menu_bar_app = std::env::var("DAY_MACOS_DOCK").is_ok_and(|v| v == "0")
|| unsafe {
let bundle = objc2_foundation::NSBundle::mainBundle();
let value: Option<Retained<objc2::runtime::AnyObject>> = msg_send![
&bundle,
objectForInfoDictionaryKey: &*NSString::from_str("LSUIElement")
];
value.is_some_and(|v| {
let on: bool = msg_send![&v, boolValue];
on
})
};
app.setActivationPolicy(if menu_bar_app {
NSApplicationActivationPolicy::Accessory
} else {
NSApplicationActivationPolicy::Regular
});
let document_delegate = documents::delegate(mtm);
app.setDelegate(Some(ProtocolObject::from_ref(&*document_delegate)));
if let Ok(theme) = std::env::var("DAY_THEME") {
let name = match theme.as_str() {
"dark" => Some(unsafe { objc2_app_kit::NSAppearanceNameDarkAqua }),
"light" => Some(unsafe { objc2_app_kit::NSAppearanceNameAqua }),
_ => None,
};
if let Some(name) = name {
let appearance = objc2_app_kit::NSAppearance::appearanceNamed(name);
unsafe { app.setAppearance(appearance.as_deref()) };
}
}
register_bundled_fonts();
let app_name = self.app_name.clone();
install_main_menu(mtm, &app, &app_name);
install_lifecycle_observers();
install_appearance_observer();
install_reduce_motion_observer();
let (window, delegate, content) = self.make_window(&options, false, None);
let appearance_name = match std::env::var("DAY_APPEARANCE").ok().as_deref() {
Some("light") => Some(unsafe { objc2_app_kit::NSAppearanceNameAqua }),
Some("dark") => Some(unsafe { objc2_app_kit::NSAppearanceNameDarkAqua }),
_ => None,
};
if let Some(name) = appearance_name
&& let Some(appearance) = unsafe { objc2_app_kit::NSAppearance::appearanceNamed(name) }
{
window.setAppearance(Some(&appearance));
}
std::mem::forget(delegate);
self.window = Some(window.clone());
self.content = Some(content.clone());
PRIMARY_CONTENT.with(|c| *c.borrow_mut() = Some(content.clone()));
let layout = pin_below_title_bar(&window)
.unwrap_or(Size::new(options.size.width, options.size.height));
ready(self, content, layout);
if day_core::windows::new_window_action_id() == 0
&& options.tabbing == day_spec::WindowTabbing::Automatic
{
unsafe { NSWindow::setAllowsAutomaticWindowTabbing(false, mtm) };
}
if !day_core::has_app_menu() {
install_main_menu(mtm, &app, &app_name);
}
let deterministic =
std::env::var_os("DAY_SCRIPT").is_some() || std::env::var_os("DAY_WINDOW").is_some();
let autosave = NSString::from_str("day.main");
if deterministic || !unsafe { window.setFrameUsingName(&autosave) } {
window.center();
window_chrome::place(&window, options.placement);
}
if let Some(screen) = capture_screen(mtm) {
let (visible, frame) = (screen.visibleFrame(), window.frame());
window.setFrameOrigin(NSPoint::new(
visible.origin.x + ((visible.size.width - frame.size.width) / 2.0).max(0.0),
visible.origin.y + ((visible.size.height - frame.size.height) / 2.0).max(0.0),
));
}
if !deterministic {
unsafe { window.setFrameAutosaveName(&autosave) };
}
if !options.start_hidden {
window.makeKeyAndOrderFront(None);
app.activate();
}
if let Some(content) = window.contentView() {
mark_tree_needs_display(&content);
}
if std::env::var_os("DAY_DUMP").is_some() {
std::thread::spawn(|| {
std::thread::sleep(std::time::Duration::from_millis(2000));
Self::post(Box::new(|| {
if let Some(content) = primary_content() {
let desc: Retained<NSString> =
unsafe { msg_send![&*content, _subtreeDescription] };
log::warn!("{desc}");
}
}));
});
}
app.run();
}
fn post(f: Box<dyn FnOnce() + Send>) {
dispatch2::DispatchQueue::main().exec_async(move || ffi_guard::contain((), f));
}
fn post_delayed(ms: u32, f: Box<dyn FnOnce() + Send>) {
let when =
dispatch2::DispatchTime::try_from(std::time::Duration::from_millis(u64::from(ms)))
.unwrap_or(dispatch2::DispatchTime::FOREVER);
if let Err(e) = dispatch2::DispatchQueue::main().after(when, move || {
ffi_guard::contain((), f);
}) {
log::error!("day-toolkit-appkit: dispatch_after refused a timer: {e:?}");
}
}
fn locale_hints(&self) -> Vec<String> {
objc2_foundation::NSLocale::preferredLanguages()
.iter()
.map(|s| s.to_string())
.collect()
}
}
unsafe extern "C" {
fn CGWindowListCreateImage(
bounds: objc2_core_foundation::CGRect,
list_option: u32,
window_id: u32,
image_option: u32,
) -> *mut objc2_core_graphics::CGImage;
}
const CG_WINDOW_LIST_INCLUDING_WINDOW: u32 = 1 << 3;
const CG_WINDOW_IMAGE_IGNORE_FRAMING: u32 = 1 << 0;
fn png_of_rep(rep: &objc2_app_kit::NSBitmapImageRep) -> Result<Vec<u8>, String> {
let srgb = rep.bitmapImageRepByConvertingToColorSpace_renderingIntent(
&objc2_app_kit::NSColorSpace::sRGBColorSpace(),
objc2_app_kit::NSColorRenderingIntent::Default,
);
let rep = srgb.as_deref().unwrap_or(rep);
let data = unsafe {
rep.representationUsingType_properties(NSBitmapImageFileType::PNG, &NSDictionary::new())
}
.ok_or("png encode failed")?;
Ok(data.to_vec())
}
fn snapshot_via_window_server(content: &NSView, chrome: bool) -> Result<Vec<u8>, String> {
let window = content.window().ok_or("view is not in a window")?;
if let Some(sheet) = window.attachedSheet().and_then(|s| s.contentView()) {
return snapshot_via_window_server(&sheet, chrome);
}
let number = window.windowNumber();
if number <= 0 {
return Err(format!("window {number} is not on screen"));
}
unsafe { content.layoutSubtreeIfNeeded() };
window.displayIfNeeded();
CATransaction::flush();
let null_rect = objc2_core_foundation::CGRect::new(
objc2_core_foundation::CGPoint::new(f64::INFINITY, f64::INFINITY),
objc2_core_foundation::CGSize::new(0.0, 0.0),
);
let shot = unsafe {
CGWindowListCreateImage(
null_rect,
CG_WINDOW_LIST_INCLUDING_WINDOW,
number as u32,
CG_WINDOW_IMAGE_IGNORE_FRAMING,
)
};
if shot.is_null() {
return Err("the window server has no image for this window".into());
}
let shot = unsafe {
objc2_core_foundation::CFRetained::from_raw(std::ptr::NonNull::new_unchecked(shot))
};
if chrome {
let rep = unsafe {
objc2_app_kit::NSBitmapImageRep::initWithCGImage(
objc2_app_kit::NSBitmapImageRep::alloc(),
&shot,
)
};
return png_of_rep(&rep);
}
let frame = window.frame();
if frame.size.width <= 0.0 || frame.size.height <= 0.0 {
return Err("zero-size window".into());
}
let scale = objc2_core_graphics::CGImage::width(Some(&shot)) as f64 / frame.size.width;
let in_window = unsafe { content.convertRect_toView(content.bounds(), None) };
let from_top = frame.size.height - (in_window.origin.y + in_window.size.height);
let crop = objc2_core_foundation::CGRect::new(
objc2_core_foundation::CGPoint::new(
(in_window.origin.x * scale).round(),
(from_top * scale).round(),
),
objc2_core_foundation::CGSize::new(
(in_window.size.width * scale).round(),
(in_window.size.height * scale).round(),
),
);
let cropped = objc2_core_graphics::CGImage::with_image_in_rect(Some(&shot), crop)
.ok_or("crop to the content view failed")?;
let rep = unsafe {
objc2_app_kit::NSBitmapImageRep::initWithCGImage(
objc2_app_kit::NSBitmapImageRep::alloc(),
&cropped,
)
};
png_of_rep(&rep)
}
fn apply_geometry(v: &NSView, base: [f64; 4], t: Transform) {
let [x, y, w, h] = base;
let (sw, sh) = (w * t.sx, h * t.sy);
let frame = NSRect::new(
NSPoint::new(x + (w - sw) / 2.0 + t.tx, y + (h - sh) / 2.0 + t.ty),
NSSize::new(sw, sh),
);
unsafe {
v.setFrameCenterRotation(0.0);
v.setFrame(frame);
v.setBoundsSize(NSSize::new(w, h));
if t.rotation.degrees() != 0.0 {
v.setFrameCenterRotation(t.rotation.degrees());
}
}
}
fn snapshot_view(content: &NSView) -> Result<Vec<u8>, String> {
objc2::rc::autoreleasepool(|_| {
let modal = content
.window()
.and_then(|w| w.attachedSheet())
.and_then(|s| s.contentView());
let content = modal.as_deref().unwrap_or(content);
match snapshot_via_window_server(content, false) {
Ok(bytes) => Ok(bytes),
Err(_) => snapshot_view_cache(content),
}
})
}
fn snapshot_view_chrome(content: &NSView) -> Result<Vec<u8>, String> {
objc2::rc::autoreleasepool(|_| {
let modal = content
.window()
.and_then(|w| w.attachedSheet())
.and_then(|s| s.contentView());
let content = modal.as_deref().unwrap_or(content);
match snapshot_via_window_server(content, true) {
Ok(bytes) => Ok(bytes),
Err(_) => snapshot_view_cache(content),
}
})
}
fn snapshot_view_cache(content: &NSView) -> Result<Vec<u8>, String> {
let bounds = content.bounds();
let rep =
unsafe { content.bitmapImageRepForCachingDisplayInRect(bounds) }.ok_or("no bitmap rep")?;
let ctx =
NSGraphicsContext::graphicsContextWithBitmapImageRep(&rep).ok_or("no graphics context")?;
NSGraphicsContext::saveGraphicsState_class();
NSGraphicsContext::setCurrentContext(Some(&ctx));
content
.effectiveAppearance()
.performAsCurrentDrawingAppearance(&block2::StackBlock::new(|| unsafe {
NSColor::windowBackgroundColor().setFill();
objc2_app_kit::NSRectFill(bounds);
}));
NSGraphicsContext::restoreGraphicsState_class();
unsafe { content.cacheDisplayInRect_toBitmapImageRep(bounds, &rep) };
png_of_rep(&rep)
}
fn center_on_screen_of(window: &NSWindow, primary: Option<&NSWindow>) {
let Some(screen) = primary.and_then(objc2_app_kit::NSWindow::screen) else {
return;
};
if window.screen().is_some_and(|s| s.frame() == screen.frame()) {
return;
}
let (visible, frame) = (screen.visibleFrame(), window.frame());
window.setFrameOrigin(NSPoint::new(
visible.origin.x + ((visible.size.width - frame.size.width) / 2.0).max(0.0),
visible.origin.y + ((visible.size.height - frame.size.height) * 2.0 / 3.0).max(0.0),
));
}
fn capture_screen(mtm: MainThreadMarker) -> Option<Retained<objc2_app_kit::NSScreen>> {
let wanted: u32 = std::env::var("DAY_WINDOW_SCREEN")
.ok()?
.trim()
.parse()
.ok()?;
let key = NSString::from_str("NSScreenNumber");
objc2_app_kit::NSScreen::screens(mtm)
.into_iter()
.find(|screen| {
let number: Option<Retained<objc2_foundation::NSNumber>> =
unsafe { msg_send![&*screen.deviceDescription(), objectForKey: &*key] };
number.is_some_and(|n| n.unsignedIntValue() == wanted)
})
}
fn mark_tree_needs_display(view: &NSView) {
view.setNeedsDisplay(true);
for sub in view.subviews() {
mark_tree_needs_display(&sub);
}
}
#[must_use]
pub const fn lifecycle_supported(phase: day_spec::Lifecycle) -> bool {
phase.is_universal()
}
fn install_appearance_observer() {
use objc2_foundation::{NSDistributedNotificationCenter, NSNotification, NSString};
let center = unsafe { NSDistributedNotificationCenter::defaultCenter() };
let block = block2::RcBlock::new(move |_: std::ptr::NonNull<NSNotification>| {
dispatch2::DispatchQueue::main().exec_async(|| {
ffi_guard::contain((), || {
day_core::note_appearance_changed();
color::refresh_layers();
});
});
});
let name = NSString::from_str("AppleInterfaceThemeChangedNotification");
let token = unsafe {
center.addObserverForName_object_queue_usingBlock(Some(&name), None, None, &block)
};
std::mem::forget(token);
let center = objc2_foundation::NSNotificationCenter::defaultCenter();
let name = NSString::from_str("NSSystemColorsDidChangeNotification");
let token = unsafe {
center.addObserverForName_object_queue_usingBlock(Some(&name), None, None, &block)
};
std::mem::forget(token);
}
fn install_reduce_motion_observer() {
use objc2_foundation::NSNotification;
let center = objc2_app_kit::NSWorkspace::sharedWorkspace().notificationCenter();
let block = block2::RcBlock::new(move |_: std::ptr::NonNull<NSNotification>| {
dispatch2::DispatchQueue::main()
.exec_async(|| ffi_guard::contain((), day_core::note_motion_changed));
});
let token = unsafe {
center.addObserverForName_object_queue_usingBlock(
Some(objc2_app_kit::NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification),
None,
None,
&block,
)
};
std::mem::forget(token);
}
fn install_lifecycle_observers() {
use objc2_foundation::{NSNotification, NSNotificationCenter, NSNotificationName};
let center = unsafe { NSNotificationCenter::defaultCenter() };
let observe = |name: &NSNotificationName, phase: day_spec::Lifecycle| {
let block = block2::RcBlock::new(move |_: std::ptr::NonNull<NSNotification>| {
ffi_guard::contain((), || emit(day_spec::WINDOW_NODE, Event::Lifecycle(phase)));
});
let token = unsafe {
center.addObserverForName_object_queue_usingBlock(Some(name), None, None, &block)
};
std::mem::forget(token); };
unsafe {
observe(
NSApplicationDidBecomeActiveNotification,
day_spec::Lifecycle::DidBecomeActive,
);
observe(
NSApplicationWillResignActiveNotification,
day_spec::Lifecycle::WillResignActive,
);
observe(
NSApplicationWillTerminateNotification,
day_spec::Lifecycle::WillTerminate,
);
observe(
NSApplicationWillTerminateNotification,
day_spec::Lifecycle::DidExit,
);
}
}
day_localization_ui::locales!();
fn install_application_menu(
mtm: MainThreadMarker,
menubar: &NSMenu,
title: &str,
about: Option<day_spec::MenuItem>,
updates: Option<day_spec::MenuItem>,
prefs: Option<day_spec::MenuItem>,
quit: Option<day_spec::MenuItem>,
) -> Retained<NSMenu> {
use day_spec::{MenuItem as M, MenuRole as R};
let app = NSApplication::sharedApplication(mtm);
let standard = |role, label| M::Action {
id: None,
action: 0,
label,
shortcut: None,
enabled: true,
checked: None,
role: Some(role),
icon: None,
};
let mut about =
about.unwrap_or_else(|| standard(R::About, res::str::about_app(title).format()));
let mut quit = quit.unwrap_or_else(|| standard(R::Quit, res::str::quit_app(title).format()));
for (item, title) in [
(&mut about, res::str::about_app(title).format()),
(&mut quit, res::str::quit_app(title).format()),
] {
if let M::Action { label, .. } = item {
*label = title;
}
}
if let M::Action { action, .. } = &mut about
&& *action == 0
{
*action = day_core::windows::about_action_id();
}
let updates_id = day_core::windows::check_for_updates_action_id();
let updates = updates.or_else(|| {
(updates_id != 0).then(|| {
standard(
R::CheckForUpdates,
day_localization::t(day_localization::key::day_check_for_updates),
)
})
});
let mut head = vec![about];
if let Some(mut updates) = updates {
if let M::Action { action, .. } = &mut updates
&& *action == 0
{
*action = updates_id;
}
head.push(updates);
}
head.push(M::Separator);
let menu = build_ns_menu(mtm, title, &head);
if let Some(mut prefs) = prefs {
if let M::Action { label, .. } = &mut prefs {
*label = res::str::settings().format();
}
let section = build_ns_menu(mtm, title, &[prefs, M::Separator]);
for item in section.itemArray() {
section.removeItem(&item);
menu.addItem(&item);
}
}
let services = app.servicesMenu().unwrap_or_else(|| NSMenu::new(mtm));
if let Some(parent) = unsafe { services.supermenu() } {
let index = parent.indexOfItemWithSubmenu(Some(&services));
if index >= 0 {
parent.itemAtIndex(index).unwrap().setSubmenu(None);
}
}
let services_item = NSMenuItem::new(mtm);
services_item.setTitle(&NSString::from_str(&res::str::services().format()));
services_item.setSubmenu(Some(&services));
menu.addItem(&services_item);
menu.addItem(&NSMenuItem::separatorItem(mtm));
for (label, selector, shortcut) in [
(
res::str::hide_app(title).format(),
sel!(hide:),
Some(day_spec::Shortcut::primary("h")),
),
(
res::str::hide_others().format(),
sel!(hideOtherApplications:),
Some(day_spec::Shortcut::primary("h").alt()),
),
(
res::str::show_all().format(),
sel!(unhideAllApplications:),
None,
),
] {
let item = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
NSMenuItem::alloc(mtm),
&NSString::from_str(&label),
Some(selector),
&NSString::from_str(shortcut.as_ref().map_or("", |s| s.key.as_str())),
)
};
if let Some(shortcut) = shortcut {
item.setKeyEquivalentModifierMask(ns_modifiers(&shortcut));
}
unsafe { item.setTarget(Some(&app)) };
menu.addItem(&item);
}
menu.addItem(&NSMenuItem::separatorItem(mtm));
let section = build_ns_menu(mtm, title, &[quit]);
let quit = section.itemAtIndex(0).unwrap();
section.removeItem(&quit);
menu.addItem(&quit);
let item = NSMenuItem::new(mtm);
item.setSubmenu(Some(&menu));
menubar.addItem(&item);
services
}
fn install_main_menu(mtm: MainThreadMarker, app: &NSApplication, title: &str) {
let menubar = NSMenu::new(mtm);
let prefs_id = day_core::windows::preferences_action_id();
let prefs = (prefs_id != 0).then(|| day_spec::MenuItem::Action {
id: None,
action: prefs_id,
label: res::str::settings().format(),
shortcut: None,
enabled: true,
checked: None,
role: Some(day_spec::MenuRole::Preferences),
icon: None,
});
let services = install_application_menu(mtm, &menubar, title, None, None, prefs, None);
let new_window_id = day_core::windows::new_window_action_id();
if new_window_id != 0 {
let file_item = NSMenuItem::new(mtm);
let file_menu = unsafe {
NSMenu::initWithTitle(
NSMenu::alloc(mtm),
&NSString::from_str(&day_localization::t(day_localization::key::day_file)),
)
};
let new_window = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
NSMenuItem::alloc(mtm),
&NSString::from_str(&day_localization::t(day_localization::key::day_new_window)),
Some(sel!(fire:)),
&NSString::from_str("n"),
)
};
let target = menu_target(mtm);
let tobj: &objc2::runtime::AnyObject = target.as_ref();
unsafe { new_window.setTarget(Some(tobj)) };
new_window.setTag(new_window_id as isize);
file_menu.addItem(&new_window);
file_menu.addItem(&NSMenuItem::separatorItem(mtm));
let close = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
NSMenuItem::alloc(mtm),
&NSString::from_str(&day_localization::t(day_localization::key::day_close)),
Some(sel!(performClose:)),
&NSString::from_str("w"),
)
};
file_menu.addItem(&close);
file_item.setSubmenu(Some(&file_menu));
menubar.addItem(&file_item);
}
let edit_item = NSMenuItem::new(mtm);
let edit_menu = unsafe {
NSMenu::initWithTitle(
NSMenu::alloc(mtm),
&NSString::from_str(&day_localization::t(day_localization::key::day_edit)),
)
};
let add = |key: &str, action: objc2::runtime::Sel, accel: &str| {
let item = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
NSMenuItem::alloc(mtm),
&NSString::from_str(&day_localization::t(key)),
Some(action),
&NSString::from_str(accel),
)
};
edit_menu.addItem(&item);
};
add("day_undo", sel!(undo:), "z");
add("day_redo", sel!(redo:), "Z");
edit_menu.addItem(&NSMenuItem::separatorItem(mtm));
add("day_cut", sel!(cut:), "x");
add("day_copy", sel!(copy:), "c");
add("day_paste", sel!(paste:), "v");
add("day_select_all", sel!(selectAll:), "a");
edit_item.setSubmenu(Some(&edit_menu));
menubar.addItem(&edit_item);
install_windows_menu(mtm, app, &menubar);
app.setMainMenu(Some(&menubar));
unsafe { app.setServicesMenu(Some(&services)) };
}
fn install_windows_menu(mtm: MainThreadMarker, app: &NSApplication, menubar: &NSMenu) {
let menu = unsafe {
NSMenu::initWithTitle(
NSMenu::alloc(mtm),
&NSString::from_str(&day_localization::t(day_localization::key::day_window)),
)
};
let add = |key: &str, action: objc2::runtime::Sel, accel: &str| {
let item = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
NSMenuItem::alloc(mtm),
&NSString::from_str(&day_localization::t(key)),
Some(action),
&NSString::from_str(accel),
)
};
menu.addItem(&item);
};
add("day_minimize", sel!(performMiniaturize:), "m");
add("day_zoom", sel!(performZoom:), "");
menu.addItem(&NSMenuItem::separatorItem(mtm));
add("day_bring_all_front", sel!(arrangeInFront:), "");
let item = NSMenuItem::new(mtm);
item.setTitle(&NSString::from_str(&day_localization::t(
day_localization::key::day_window,
)));
item.setSubmenu(Some(&menu));
menubar.addItem(&item);
unsafe { app.setWindowsMenu(Some(&menu)) };
}
fn extract_app_role(
items: &mut Vec<day_spec::MenuItem>,
role: day_spec::MenuRole,
) -> Option<day_spec::MenuItem> {
use day_spec::MenuItem as M;
let mut found = None;
let mut remaining = Vec::new();
for mut item in items.drain(..) {
match &mut item {
M::Action { role: Some(r), .. } if *r == role => {
found.get_or_insert(item);
continue;
}
M::Submenu { items, .. } => {
let nested = extract_app_role(items, role);
if found.is_none() {
found = nested;
}
if items.is_empty() {
continue;
}
}
M::Separator
if remaining.is_empty() || matches!(remaining.last(), Some(M::Separator)) =>
{
continue;
}
_ => {}
}
remaining.push(item);
}
while matches!(remaining.last(), Some(M::Separator)) {
remaining.pop();
}
*items = remaining;
found
}
fn model_has_role(items: &[day_spec::MenuItem], role: day_spec::MenuRole) -> bool {
items.iter().any(|it| match it {
day_spec::MenuItem::Action { role: r, .. } => *r == Some(role),
day_spec::MenuItem::Submenu { items, .. } => model_has_role(items, role),
day_spec::MenuItem::Separator => false,
})
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "DayMenuTarget"]
#[ivars = ()]
struct DayMenuTarget;
unsafe impl NSObjectProtocol for DayMenuTarget {}
impl DayMenuTarget {
#[unsafe(method(fire:))]
fn fire(&self, sender: &NSMenuItem) {
ffi_guard::contain((), || {
let id = sender.tag() as u64;
if id != 0 {
emit(day_spec::WINDOW_NODE, Event::MenuAction(id));
}
});
}
}
);
pub(crate) fn pin_below_title_bar(window: &NSWindow) -> Option<Size> {
let content = window.contentView()?;
let full = content.frame().size;
let layout = unsafe { window.contentLayoutRect() }.size;
let top = (full.height - layout.height).max(0.0);
if window_chrome::is_overlay(window) {
unsafe { content.setBoundsOrigin(NSPoint::new(0.0, 0.0)) };
window_chrome::report_title_bar_inset(window, top);
return Some(Size::new(full.width, full.height));
}
unsafe { content.setBoundsOrigin(NSPoint::new(0.0, -top)) };
Some(Size::new(full.width, (full.height - top).max(0.0)))
}
fn native_text(h: &NSView) -> Option<String> {
if h.downcast_ref::<NSSecureTextField>().is_some() {
return None;
}
if let Some(tf) = h.downcast_ref::<NSTextField>() {
return Some(tf.stringValue().to_string());
}
if let Some(btn) = h.downcast_ref::<NSButton>() {
if unsafe { btn.imagePosition() } == objc2_app_kit::NSCellImagePosition::ImageOnly {
return None;
}
return Some(btn.title().to_string());
}
let sv = h.downcast_ref::<NSScrollView>()?;
let doc = sv.documentView()?;
let tv = doc.downcast_ref::<NSTextView>()?;
Some(tv.string().to_string())
}
fn native_number(h: &NSView) -> Option<f64> {
if let Some(sl) = h.downcast_ref::<NSSlider>() {
return Some(sl.doubleValue());
}
let pi = h.downcast_ref::<NSProgressIndicator>()?;
(!pi.isIndeterminate()).then(|| pi.doubleValue())
}
fn native_frame(h: &NSView) -> Option<Rect> {
let content = h.window()?.contentView()?;
let r = h.convertRect_toView(h.bounds(), Some(&content));
let y = if content.isFlipped() {
r.origin.y
} else {
let b = content.bounds();
b.origin.y + b.size.height - (r.origin.y + r.size.height)
};
Some(Rect::new(r.origin.x, y, r.size.width, r.size.height))
}
fn title_bar_inset(content: &NSView) -> f64 {
(-content.bounds().origin.y).max(0.0)
}
fn primary_content() -> Option<Retained<NSView>> {
PRIMARY_CONTENT.with(|c| c.borrow().clone())
}
fn menu_target(mtm: MainThreadMarker) -> Retained<DayMenuTarget> {
MENU_TARGET.with(|t| {
t.borrow_mut()
.get_or_insert_with(|| {
let this = DayMenuTarget::alloc(mtm).set_ivars(());
let obj: Retained<DayMenuTarget> = unsafe { msg_send![super(this), init] };
obj
})
.clone()
})
}
fn ns_modifiers(s: &day_spec::Shortcut) -> objc2_app_kit::NSEventModifierFlags {
use objc2_app_kit::NSEventModifierFlags as F;
let mut m = F::empty();
if s.primary {
m |= F::Command;
}
if s.shift {
m |= F::Shift;
}
if s.alt {
m |= F::Option;
}
if s.control {
m |= F::Control;
}
m
}
fn ns_key_equivalent(key: &str) -> String {
match key {
"Return" | "Enter" => "\r".into(),
"Tab" => "\t".into(),
"Delete" | "Backspace" => "\u{8}".into(),
"Escape" => "\u{1b}".into(),
"Space" => " ".into(),
k if k.chars().count() == 1 => k.to_lowercase(),
_ => String::new(), }
}
fn role_spec(
role: day_spec::MenuRole,
) -> (
&'static str,
Option<objc2::runtime::Sel>,
Option<day_spec::Shortcut>,
) {
use day_spec::MenuRole as R;
use day_spec::Shortcut as S;
match role {
R::Cut => ("Cut", Some(sel!(cut:)), Some(S::primary("x"))),
R::Copy => ("Copy", Some(sel!(copy:)), Some(S::primary("c"))),
R::Paste => ("Paste", Some(sel!(paste:)), Some(S::primary("v"))),
R::SelectAll => ("Select All", Some(sel!(selectAll:)), Some(S::primary("a"))),
R::Undo => ("Undo", Some(sel!(undo:)), Some(S::primary("z"))),
R::Redo => ("Redo", Some(sel!(redo:)), Some(S::primary("z").shift())),
R::Delete => ("Delete", Some(sel!(delete:)), None),
R::About => ("About", Some(sel!(orderFrontStandardAboutPanel:)), None),
R::Quit => ("Quit", Some(sel!(terminate:)), Some(S::primary("q"))),
R::Preferences => ("Settings…", None, Some(S::primary(","))),
R::Minimize => (
"Minimize",
Some(sel!(performMiniaturize:)),
Some(S::primary("m")),
),
R::CloseWindow => ("Close", Some(sel!(performClose:)), Some(S::primary("w"))),
R::Fullscreen => (
"Enter Full Screen",
Some(sel!(toggleFullScreen:)),
Some(S::primary("f").control()),
),
R::NewWindow => ("New Window", None, Some(S::primary("n"))),
R::CheckForUpdates => ("Check for Updates…", None, None),
}
}
pub(crate) fn build_ns_menu(
mtm: MainThreadMarker,
title: &str,
items: &[day_spec::MenuItem],
) -> Retained<NSMenu> {
use day_spec::MenuItem as MI;
let menu = unsafe { NSMenu::initWithTitle(NSMenu::alloc(mtm), &NSString::from_str(title)) };
let target = menu_target(mtm);
for item in items {
match item {
MI::Separator => menu.addItem(&NSMenuItem::separatorItem(mtm)),
MI::Submenu { label, items, .. } => {
let sub = build_ns_menu(mtm, label, items);
let it = NSMenuItem::new(mtm);
it.setTitle(&NSString::from_str(label));
it.setSubmenu(Some(&sub));
menu.addItem(&it);
}
MI::Action {
action,
label,
shortcut,
enabled,
checked,
role,
icon,
..
} => {
let (mut lbl, sel, mut sc) = match role {
Some(r) => {
let (dl, ds, dsc) = role_spec(*r);
(dl.to_string(), ds, dsc)
}
None => (String::new(), None, None),
};
if !label.is_empty() {
lbl.clone_from(label);
}
if shortcut.is_some() {
sc.clone_from(shortcut);
}
let responder_role = matches!(
role,
Some(
day_spec::MenuRole::Undo
| day_spec::MenuRole::Redo
| day_spec::MenuRole::Cut
| day_spec::MenuRole::Copy
| day_spec::MenuRole::Paste
| day_spec::MenuRole::SelectAll
)
);
let custom = *action != 0 && !responder_role;
let key = sc.as_ref().map_or_default(|s| ns_key_equivalent(&s.key));
let it = unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(
NSMenuItem::alloc(mtm),
&NSString::from_str(&lbl),
if custom { Some(sel!(fire:)) } else { sel },
&NSString::from_str(&key),
)
};
if let Some(s) = &sc {
it.setKeyEquivalentModifierMask(ns_modifiers(s));
it.setAllowsAutomaticKeyEquivalentLocalization(true);
it.setAllowsAutomaticKeyEquivalentMirroring(true);
}
if custom {
let tobj: &objc2::runtime::AnyObject = target.as_ref();
unsafe { it.setTarget(Some(tobj)) };
it.setTag(*action as isize);
}
it.setEnabled(*enabled);
if let Some(on) = checked {
it.setState(if *on {
NSControlStateValueOn
} else {
NSControlStateValueOff
});
}
if let Some(icon) = icon
&& let Some(img) = crate::toolbar::image_for(icon, &lbl, mtm)
{
img.setSize(NSSize::new(16.0, 16.0));
it.setImage(Some(&img));
}
menu.addItem(&it);
}
}
}
menu
}
fn release_bitmap(id: day_spec::BitmapId) {
let _ = BITMAPS.try_with(|m| m.borrow_mut().remove(&id.0));
}
#[cfg(test)]
mod splice_tests {
use super::{SpliceRows, splice_fits};
use day_spec::props::RowDelta::{Insert, Move, Remove};
#[test]
fn a_splice_fits_only_the_rows_it_was_made_for() {
let deltas = [Insert(0), Move(3, 0), Remove(2)];
assert!(splice_fits(3, &deltas, 3));
assert!(splice_fits(3, &[Insert(3), Insert(4)], 5));
assert!(splice_fits(0, &[], 0));
assert!(!splice_fits(2, &deltas, 2));
assert!(!splice_fits(3, &[Insert(5)], 4));
assert!(!splice_fits(3, &[Remove(3)], 2));
assert!(!splice_fits(3, &[Move(0, 3)], 3));
assert!(!splice_fits(3, &[Insert(0)], 3));
}
#[test]
fn a_splice_in_flight_maps_each_row_to_the_source_row_it_ends_at() {
let deltas = [
Insert(4),
Insert(4),
Insert(4),
Insert(4),
Insert(4),
Insert(2),
Insert(2),
];
let mut rows = SpliceRows::new(4, &deltas);
let shown = |rows: &SpliceRows| -> Vec<Option<usize>> {
(0..rows.entries.len())
.map(|r| rows.source_row(r))
.collect()
};
assert!(shown(&rows).into_iter().eq([0, 1, 4, 5].map(Some)));
rows.apply(deltas[0]);
assert!(shown(&rows).into_iter().eq([0, 1, 4, 5, 10].map(Some)));
for delta in &deltas[1..] {
rows.apply(*delta);
}
assert!(shown(&rows).into_iter().eq((0..11).map(Some)));
let deltas = [Remove(3), Remove(1), Move(1, 0)];
let mut rows = SpliceRows::new(4, &deltas);
assert!(shown(&rows).into_iter().eq([Some(1), None, Some(0), None]));
rows.apply(deltas[0]);
rows.apply(deltas[1]);
assert!(shown(&rows).into_iter().eq([1, 0].map(Some)));
rows.apply(deltas[2]);
assert!(shown(&rows).into_iter().eq([0, 1].map(Some)));
}
}
#[cfg(test)]
mod bitmap_teardown_tests {
use super::*;
#[test]
fn releasing_after_bitmap_registry_teardown_does_not_abort() {
const CHILD: &str = "DAY_APPKIT_BITMAP_TEARDOWN_CHILD";
if std::env::var_os(CHILD).is_some() {
struct LateRelease;
impl Drop for LateRelease {
fn drop(&mut self) {
assert!(BITMAPS.try_with(|_| ()).is_err());
release_bitmap(day_spec::BitmapId(1));
}
}
std::thread_local! {
static LATE: RefCell<Option<LateRelease>> = const { RefCell::new(None) };
}
std::thread::spawn(|| {
LATE.with(|slot| *slot.borrow_mut() = Some(LateRelease));
BITMAPS.with(|_| ()); })
.join()
.unwrap();
return;
}
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"bitmap_teardown_tests::releasing_after_bitmap_registry_teardown_does_not_abort",
"--nocapture",
])
.env(CHILD, "1")
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}
}
#[cfg(test)]
mod canvas_ink_tests {
use super::*;
use day_spec::{Affine, BlendMode, Color, ConicGradient, Shadow, Shape};
const RED: Color = Color::rgb(1.0, 0.0, 0.0);
const GREEN: Color = Color::rgb(0.0, 1.0, 0.0);
const BLUE: Color = Color::rgb(0.0, 0.0, 1.0);
const YELLOW: Color = Color::rgb(1.0, 1.0, 0.0);
fn rect(x: f64, y: f64, w: f64, h: f64) -> Shape {
Shape::Rect(day_spec::Rect::new(x, y, w, h))
}
fn quarters() -> ConicGradient {
let stops = [RED, GREEN, BLUE, YELLOW]
.iter()
.enumerate()
.flat_map(|(i, c)| [(i as f64 * 0.25, *c), (i as f64 * 0.25 + 0.25, *c)])
.collect::<Vec<_>>();
ConicGradient::centered(day_spec::Angle::deg(0.0), stops)
}
fn painted(ops: &[DrawOp], size: Size, scale: f64, at: &[(usize, usize)]) -> Vec<[u8; 4]> {
let image = render_bitmap_cgimage(ops, size, scale).unwrap();
let rep = unsafe {
objc2_app_kit::NSBitmapImageRep::initWithCGImage(
objc2_app_kit::NSBitmapImageRep::alloc(),
&image,
)
};
let (data, stride) = (rep.bitmapData(), rep.bytesPerRow() as usize);
assert_eq!(rep.bitsPerPixel(), 32);
at.iter()
.map(|(x, y)| {
let mut px = [0u8; 4];
unsafe {
data.add(y * stride + x * 4)
.copy_to_nonoverlapping(px.as_mut_ptr(), 4);
};
px
})
.collect()
}
fn near(px: [u8; 4], want: [u8; 4]) -> bool {
px.iter().zip(want).all(|(a, b)| a.abs_diff(b) <= 24)
}
#[test]
fn replay_mirrors_save_and_restore() {
let mut replay = Replay::new(1.0);
replay.save();
replay.ink.shadow = true;
replay.save();
replay.ink.blend = BlendMode::Erase;
assert!(replay.restore());
assert_eq!(
replay.ink,
Ink {
shadow: true,
blend: BlendMode::Normal
}
);
assert!(replay.restore());
assert_eq!(replay.ink, Ink::default());
assert!(!replay.restore());
assert_eq!(replay.ink, Ink::default());
}
#[test]
fn pdf_leaves_out_what_erase_draws() {
let plain = [DrawOp::Fill(rect(0.0, 0.0, 9.0, 9.0), RED.into())];
assert!(matches!(pdf_ops(&plain), std::borrow::Cow::Borrowed(_)));
let ops = [
DrawOp::Fill(rect(0.0, 0.0, 9.0, 9.0), RED.into()),
DrawOp::Save,
DrawOp::Blend(BlendMode::Erase),
DrawOp::Fill(rect(1.0, 1.0, 2.0, 2.0), GREEN.into()),
DrawOp::Restore,
DrawOp::Fill(rect(3.0, 3.0, 2.0, 2.0), BLUE.into()),
];
let kept = pdf_ops(&ops);
assert_eq!(kept.len(), 5);
assert!(!kept.contains(&ops[3]));
assert!(kept.contains(&ops[5]));
}
#[test]
fn conic_pixels_turn_clockwise_from_the_start() {
let px = conic_pixels(
&quarters(),
Point::new(2.0, 2.0),
Point::new(0.0, 0.0),
1.0,
4,
4,
);
let at = |x: usize, y: usize| <[u8; 4]>::try_from(&px[(y * 4 + x) * 4..][..4]).unwrap();
assert_eq!(px.len(), 4 * 4 * 4);
assert_eq!(at(3, 3), [255, 0, 0, 255]);
assert_eq!(at(0, 3), [0, 255, 0, 255]);
assert_eq!(at(0, 0), [0, 0, 255, 255]);
assert_eq!(at(3, 0), [255, 255, 0, 255]);
let half = Color::rgba(1.0, 0.0, 0.0, 0.5);
let faint =
ConicGradient::centered(day_spec::Angle::deg(90.0), vec![(0.0, half), (1.0, half)]);
let px = conic_pixels(
&faint,
Point::new(0.5, 0.5),
Point::new(0.0, 0.0),
1.0,
1,
1,
);
assert_eq!(px, [128, 0, 0, 128]);
let turned = ConicGradient::centered(day_spec::Angle::deg(90.0), quarters().stops);
let px = conic_pixels(
&turned,
Point::new(1.0, 1.0),
Point::new(0.0, 0.0),
1.0,
2,
2,
);
assert_eq!(px[8..12], [255, 0, 0, 255]);
}
#[test]
fn stamp_bounds_join_every_copy() {
let mut stamp = day_spec::Stamp {
shape: Shape::Ellipse(day_spec::Rect::new(-3.0, -3.0, 6.0, 6.0)),
at: vec![Point::new(10.0, 50.0), Point::new(40.0, 20.0)],
paint: RED.into(),
stroke: None,
};
assert_eq!(
stamp_bounds(&stamp),
Some(day_spec::Rect::new(7.0, 17.0, 36.0, 36.0))
);
stamp.at.clear();
assert_eq!(stamp_bounds(&stamp), None);
}
#[test]
fn export_paints_conic_quadrants() {
let size = Size::new(100.0, 100.0);
let ops = [DrawOp::Fill(
rect(0.0, 0.0, 100.0, 100.0),
quarters().into(),
)];
let px = painted(&ops, size, 1.0, &[(75, 75), (25, 75), (25, 25), (75, 25)]);
assert!(near(px[0], [255, 0, 0, 255]), "{px:?}");
assert!(near(px[1], [0, 255, 0, 255]), "{px:?}");
assert!(near(px[2], [0, 0, 255, 255]), "{px:?}");
assert!(near(px[3], [255, 255, 0, 255]), "{px:?}");
}
fn fanned(gradient: &ConicGradient, at: &[(usize, usize)]) -> Vec<[u8; 4]> {
use objc2_core_graphics::{
CGBitmapContextCreate, CGBitmapContextGetBytesPerRow, CGBitmapContextGetData,
CGColorSpace, CGContext, CGImageAlphaInfo,
};
let space =
CGColorSpace::with_name(Some(unsafe { objc2_core_graphics::kCGColorSpaceSRGB }));
let cg = unsafe {
CGBitmapContextCreate(
std::ptr::null_mut(),
100,
100,
8,
0,
space.as_deref(),
CGImageAlphaInfo::PremultipliedLast.0,
)
}
.unwrap();
CGContext::translate_ctm(Some(&cg), 0.0, 100.0);
CGContext::scale_ctm(Some(&cg), 1.0, -1.0);
CGContext::add_ellipse_in_rect(
Some(&cg),
NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(100.0, 100.0)),
);
CGContext::clip(Some(&cg));
draw_conic_pixels(&cg, gradient, Point::new(50.0, 50.0));
let (data, stride) = (
CGBitmapContextGetData(Some(&cg)).cast::<u8>(),
CGBitmapContextGetBytesPerRow(Some(&cg)),
);
at.iter()
.map(|(x, y)| {
let mut px = [0u8; 4];
unsafe {
data.add(y * stride + x * 4)
.copy_to_nonoverlapping(px.as_mut_ptr(), 4);
};
px
})
.collect()
}
#[test]
fn emulated_conic_paints_the_same_quadrants() {
let px = fanned(
&quarters(),
&[(75, 75), (25, 75), (25, 25), (75, 25), (2, 2)],
);
assert_eq!(px[0], [255, 0, 0, 255]);
assert_eq!(px[1], [0, 255, 0, 255]);
assert_eq!(px[2], [0, 0, 255, 255]);
assert_eq!(px[3], [255, 255, 0, 255]);
assert_eq!(px[4], [0, 0, 0, 0]);
}
#[test]
fn emulated_conic_is_one_even_coat() {
let half = Color::rgba(0.0, 0.0, 0.0, 0.5);
let gradient =
ConicGradient::centered(day_spec::Angle::deg(17.0), vec![(0.0, half), (1.0, half)]);
let inside: Vec<(usize, usize)> = (20..80)
.flat_map(|y| (20..80).map(move |x| (x, y)))
.collect();
for (px, at) in fanned(&gradient, &inside).iter().zip(&inside) {
assert!(
px[3].abs_diff(128) <= 1,
"uneven at {at:?}: alpha {}",
px[3]
);
}
}
#[test]
fn export_shadow_is_in_points_and_scoped() {
let ops = [
DrawOp::Fill(rect(0.0, 0.0, 200.0, 100.0), Color::WHITE.into()),
DrawOp::Save,
DrawOp::Concat(Affine::scale(2.0, 2.0)),
DrawOp::Save,
DrawOp::Shadow(Some(
Shadow::new(Color::rgb(0.0, 0.0, 0.0), 2.0).offset(0.0, 20.0),
)),
DrawOp::Fill(rect(15.0, 10.0, 20.0, 15.0), BLUE.into()),
DrawOp::Restore,
DrawOp::Restore,
DrawOp::Fill(rect(130.0, 20.0, 40.0, 30.0), BLUE.into()),
];
let at = [(100, 70), (100, 120), (100, 170), (300, 70), (300, 120)];
let px = painted(&ops, Size::new(200.0, 100.0), 2.0, &at);
assert!(near(px[0], [0, 0, 255, 255]), "{px:?}");
assert!(near(px[1], [0, 0, 0, 255]), "{px:?}");
assert!(near(px[2], [255, 255, 255, 255]), "{px:?}");
assert!(near(px[3], [0, 0, 255, 255]), "{px:?}");
assert!(near(px[4], [255, 255, 255, 255]), "{px:?}");
}
#[test]
fn export_gradient_casts_a_whole_shadow() {
let ops = [
DrawOp::Shadow(Some(Shadow::new(BLUE, 0.0).offset(0.0, 30.0))),
DrawOp::Fill(rect(10.0, 10.0, 40.0, 20.0), quarters().into()),
];
let px = painted(&ops, Size::new(60.0, 80.0), 1.0, &[(30, 50), (30, 70)]);
assert!(near(px[0], [0, 0, 255, 255]), "{px:?}");
assert_eq!(px[1], [0, 0, 0, 0]);
}
#[test]
fn export_blends_path_object_shapes_too() {
let round = |x: f64| Shape::RoundedRect(day_spec::Rect::new(x, 0.0, 30.0, 30.0), 4.0);
let ops = [
DrawOp::Fill(rect(0.0, 0.0, 60.0, 60.0), YELLOW.into()),
DrawOp::Save,
DrawOp::Blend(BlendMode::Multiply),
DrawOp::Fill(round(0.0), Color::rgb(0.0, 1.0, 1.0).into()),
DrawOp::Blend(BlendMode::Erase),
DrawOp::Fill(round(30.0), RED.into()),
DrawOp::Restore,
DrawOp::Shadow(Some(Shadow::new(BLUE, 0.0).offset(0.0, 30.0))),
DrawOp::Fill(round(0.0), RED.into()),
];
let px = painted(
&ops,
Size::new(60.0, 60.0),
1.0,
&[(15, 15), (45, 15), (15, 45)],
);
assert!(near(px[0], [255, 0, 0, 255]), "{px:?}");
assert_eq!(px[1], [0, 0, 0, 0], "{px:?}");
assert!(near(px[2], [0, 0, 255, 255]), "{px:?}");
}
#[test]
fn export_blends_and_erases() {
let ops = [
DrawOp::Fill(rect(0.0, 0.0, 90.0, 30.0), Color::WHITE.into()),
DrawOp::Fill(rect(0.0, 0.0, 30.0, 30.0), YELLOW.into()),
DrawOp::Save,
DrawOp::Blend(BlendMode::Multiply),
DrawOp::Fill(rect(0.0, 0.0, 30.0, 30.0), Color::rgb(0.0, 1.0, 1.0).into()),
DrawOp::Restore,
DrawOp::Fill(rect(30.0, 0.0, 30.0, 30.0), RED.into()),
DrawOp::Save,
DrawOp::Blend(BlendMode::Add),
DrawOp::Fill(rect(30.0, 0.0, 30.0, 30.0), BLUE.into()),
DrawOp::Restore,
DrawOp::Save,
DrawOp::Blend(BlendMode::Erase),
DrawOp::Fill(rect(60.0, 0.0, 15.0, 30.0), GREEN.into()),
DrawOp::Fill(rect(75.0, 0.0, 15.0, 15.0), quarters().into()),
DrawOp::Restore,
DrawOp::Fill(rect(80.0, 20.0, 10.0, 10.0), BLUE.into()),
];
let at = [(15, 15), (45, 15), (67, 15), (82, 7), (78, 25), (85, 25)];
let px = painted(&ops, Size::new(90.0, 30.0), 1.0, &at);
assert!(near(px[0], [0, 255, 0, 255]), "{px:?}");
assert!(near(px[1], [255, 0, 255, 255]), "{px:?}");
assert_eq!(px[2], [0, 0, 0, 0], "{px:?}");
assert!(near(px[3], [0, 0, 0, 0]), "{px:?}");
assert!(near(px[4], [255, 255, 255, 255]), "{px:?}");
assert!(near(px[5], [0, 0, 255, 255]), "{px:?}");
}
}