use crate::color::Color;
use crate::core_text::SystemText;
use crate::event::{Button, Cursor, Event, Key, Mods};
use crate::geom::{Point, Rect, Size, pt};
use crate::id::Id;
use crate::macos_sys::*;
use crate::platform::{Clipboard, PicturesAndFiles, Window, WindowPlacement};
use crate::preferences::Preferences;
use crate::screens::{OpenAt, Screen, window_origin};
use crate::second_window::{WindowKind, WindowOpening};
use crate::surface::Surface;
use crate::theme::ContrastColors;
use crate::ui::Ui;
use std::cell::{Cell, RefCell};
use std::ffi::{CStr, c_void};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
use std::time::{Duration, Instant};
const TITLED: usize = 1;
const CLOSABLE: usize = 2;
const MINIATURIZABLE: usize = 4;
const RESIZABLE: usize = 8;
const FULL_SIZE_CONTENT: usize = 1 << 15;
const NORMAL_LEVEL: isize = 0;
const FLOATING_LEVEL: isize = 3;
const LEFT_DOWN: usize = 1;
const LEFT_UP: usize = 2;
const RIGHT_DOWN: usize = 3;
const RIGHT_UP: usize = 4;
const EXITED: usize = 9;
const APPLICATION: usize = 15;
const OTHER_DOWN: usize = 25;
const OTHER_UP: usize = 26;
const SHIFT: usize = 1 << 17;
const CONTROL: usize = 1 << 18;
const OPTION: usize = 1 << 19;
const COMMAND: usize = 1 << 20;
const WAKE: i16 = 0x7461;
const NOTCH: f32 = 48.0;
pub(crate) enum Heard {
Event(Obj, Event),
Close(Obj),
Resized(Obj),
Moved(Obj),
Minimized(Obj, bool),
Focused(Obj, bool),
Looks,
Quit,
Desktop,
Other(Obj, Box<dyn FnOnce(&mut Ui) -> bool>),
}
struct ViewState {
view: Obj,
cursor: Cursor,
ime: Option<Rect>,
marked: usize,
picked: NSRange,
typed: Option<String>,
held: Mods,
down: Obj,
}
thread_local! {
static HEARD: RefCell<Vec<Heard>> = const { RefCell::new(Vec::new()) };
static VIEWS: RefCell<Vec<ViewState>> = const { RefCell::new(Vec::new()) };
static LIVE: Cell<Option<*mut dyn Turns>> = const { Cell::new(None) };
static TURNING: Cell<bool> = const { Cell::new(false) };
}
impl Heard {
fn view(&self) -> Obj {
match self {
Heard::Event(view, _) | Heard::Close(view) | Heard::Resized(view) | Heard::Moved(view) | Heard::Minimized(view, _) | Heard::Focused(view, _) | Heard::Other(view, _) => *view,
Heard::Looks | Heard::Quit | Heard::Desktop => std::ptr::null_mut(),
}
}
}
pub(crate) fn heard(what: Heard) {
HEARD.with_borrow_mut(|heard| heard.push(what));
}
pub(crate) fn each_view(work: fn(Obj)) {
let views: Vec<Obj> = VIEWS.with_borrow(|views| views.iter().map(|state| state.view).collect());
views.into_iter().for_each(work);
}
fn with_view<T>(view: Obj, work: impl FnOnce(&mut ViewState) -> T) -> Option<T> {
VIEWS.with_borrow_mut(|views| views.iter_mut().find(|state| state.view == view).map(work))
}
static APP: AtomicPtr<c_void> = AtomicPtr::new(std::ptr::null_mut());
static WOKEN: AtomicBool = AtomicBool::new(false);
#[derive(Clone, Copy, Debug, Default)]
pub struct Waker;
impl Waker {
pub fn wake(&self) {
let app = APP.load(Ordering::SeqCst);
if app.is_null() || WOKEN.swap(true, Ordering::SeqCst) {
return;
}
let _pool = Pool::new();
unsafe {
let event: Obj = send(
class(c"NSEvent"),
c"otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:",
(APPLICATION, NSPoint::default(), 0usize, 0.0f64, 0isize, std::ptr::null_mut::<c_void>(), WAKE, 0isize, 0isize),
);
send::<()>(app, c"postEvent:atStart:", (event, 0 as Bool));
}
}
}
fn application() -> Obj {
let app: Obj = unsafe { send(class(c"NSApplication"), c"sharedApplication", ()) };
if APP.swap(app, Ordering::SeqCst).is_null() {
prepare(app);
}
app
}
fn prepare(app: Obj) {
let methods: [(&CStr, *const c_void, &str); 2] = [(c"applicationShouldTerminate:", should_terminate as *const c_void, "Q@:@"), (c"looksChanged:", looks_changed as *const c_void, "v@:@")];
made_class(c"TautApplication", c"NSObject", &methods, &[c"NSApplicationDelegate"]);
let delegate = made(c"TautApplication");
unsafe {
send::<Bool>(app, c"setActivationPolicy:", (0isize,));
send::<()>(app, c"setDelegate:", (delegate,));
send::<()>(app, c"setMainMenu:", (menu(),));
send::<()>(app, c"finishLaunching", ());
let centre: Obj = send(class(c"NSNotificationCenter"), c"defaultCenter", ());
let workspace: Obj = send(send::<Obj>(class(c"NSWorkspace"), c"sharedWorkspace", ()), c"notificationCenter", ());
let notices = [
(centre, c"NSApplicationDidChangeScreenParametersNotification"),
(centre, c"NSSystemColorsDidChangeNotification"),
(workspace, c"NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification"),
];
for (centre, name) in notices {
let name = ns_string(name.to_str().unwrap_or_default());
send::<()>(centre, c"addObserver:selector:name:object:", (delegate, sel(c"looksChanged:"), name, std::ptr::null_mut::<c_void>()));
}
}
}
fn menu() -> Obj {
let name = std::env::current_exe().ok().and_then(|path| path.file_stem().map(|stem| stem.to_string_lossy().into_owned())).unwrap_or_default();
unsafe {
let bar: Obj = send(made(c"NSMenu"), c"autorelease", ());
let own: Obj = send(made(c"NSMenu"), c"autorelease", ());
let holder: Obj = send(made(c"NSMenuItem"), c"autorelease", ());
for (title, action, key) in [(format!("Hide {name}"), c"hide:", "h"), (format!("Quit {name}"), c"terminate:", "q")] {
let item: Obj = send(class(c"NSMenuItem"), c"alloc", ());
let item: Obj = send(item, c"initWithTitle:action:keyEquivalent:", (ns_string(&title), sel(action), ns_string(key)));
send::<()>(own, c"addItem:", (item,));
send::<()>(item, c"release", ());
}
send::<()>(holder, c"setSubmenu:", (own,));
send::<()>(bar, c"addItem:", (holder,));
bar
}
}
extern "C" fn should_terminate(_: Obj, _: Sel, _: Obj) -> usize {
heard(Heard::Quit);
0
}
extern "C" fn looks_changed(_: Obj, _: Sel, _: Obj) {
heard(Heard::Looks);
}
fn preferences() -> Preferences {
unsafe {
let app: Obj = send(class(c"NSApplication"), c"sharedApplication", ());
let looks: Obj = send(app, c"effectiveAppearance", ());
let dark = rust_string(send(looks, c"name", ())).is_some_and(|name| name.contains("Dark"));
let srgb: Obj = send(class(c"NSColorSpace"), c"sRGBColorSpace", ());
let accent: Obj = send(send::<Obj>(class(c"NSColor"), c"controlAccentColor", ()), c"colorUsingColorSpace:", (srgb,));
let channel = |name: &CStr| (send::<f64>(accent, name, ()).clamp(0.0, 1.0) * 255.0).round() as u32;
let accent = (!accent.is_null()).then(|| Color::rgb(channel(c"redComponent") << 16 | channel(c"greenComponent") << 8 | channel(c"blueComponent")));
let workspace: Obj = send(class(c"NSWorkspace"), c"sharedWorkspace", ());
let contrast = send::<Bool>(workspace, c"accessibilityDisplayShouldIncreaseContrast", ()) != 0;
let still = send::<Bool>(workspace, c"accessibilityDisplayShouldReduceMotion", ()) != 0;
let high_contrast = contrast.then_some(if dark { ContrastColors::DARK } else { ContrastColors::LIGHT });
Preferences { dark, accent, high_contrast, reduced_motion: still, ..Preferences::default() }
}
}
fn desk_height() -> f64 {
unsafe {
let screens: Obj = send(class(c"NSScreen"), c"screens", ());
send_rect(array_items(screens).first().copied().unwrap_or(std::ptr::null_mut()), c"frame", ()).size.height
}
}
fn on_desk(frame: NSRect, height: f64) -> Rect {
let top = height - frame.origin.y - frame.size.height;
Rect { x: frame.origin.x as f32, y: top as f32, w: frame.size.width as f32, h: frame.size.height as f32 }
}
fn screens() -> Vec<Screen> {
let height = desk_height();
unsafe {
let screens: Obj = send(class(c"NSScreen"), c"screens", ());
let screen = |of: Obj| Screen {
rect: on_desk(send_rect(of, c"frame", ()), height),
work_area: on_desk(send_rect(of, c"visibleFrame", ()), height),
scale: send::<f64>(of, c"backingScaleFactor", ()) as f32,
};
array_items(screens).into_iter().map(screen).collect()
}
}
pub(crate) fn view_class() -> Obj {
let (event, plain, yes) = ("v@:@".to_string(), "v@:".to_string(), format!("{BOOL}@:"));
let pointer = [
c"mouseDown:",
c"mouseUp:",
c"rightMouseDown:",
c"rightMouseUp:",
c"otherMouseDown:",
c"otherMouseUp:",
c"mouseMoved:",
c"mouseDragged:",
c"rightMouseDragged:",
c"otherMouseDragged:",
c"mouseExited:",
c"mouseEntered:",
];
let mut methods: Vec<(&CStr, *const c_void, String)> = pointer.into_iter().map(|name| (name, pointed as *const c_void, event.clone())).collect();
let heard_of: [(&CStr, *const c_void); 18] = [
(c"scrollWheel:", scrolled as *const c_void),
(c"magnifyWithEvent:", magnified as *const c_void),
(c"keyDown:", key_down as *const c_void),
(c"keyUp:", key_up as *const c_void),
(c"flagsChanged:", flags_changed as *const c_void),
(c"cursorUpdate:", cursor_update as *const c_void),
(c"windowDidResize:", window_resized as *const c_void),
(c"windowDidChangeBackingProperties:", window_rescaled as *const c_void),
(c"windowDidMove:", window_moved as *const c_void),
(c"windowDidEnterFullScreen:", window_moved as *const c_void),
(c"windowDidExitFullScreen:", window_moved as *const c_void),
(c"windowDidBecomeKey:", window_focused as *const c_void),
(c"windowDidResignKey:", window_unfocused as *const c_void),
(c"windowDidMiniaturize:", window_minimized as *const c_void),
(c"windowDidDeminiaturize:", window_restored as *const c_void),
(c"insertText:", inserted_alone as *const c_void),
(c"viewDidChangeBackingProperties", view_rescaled as *const c_void),
(c"viewDidChangeEffectiveAppearance", view_looks as *const c_void),
];
methods.extend(heard_of.into_iter().map(|(name, imp)| (name, imp, if name.to_bytes().ends_with(b":") { event.clone() } else { plain.clone() })));
methods.extend([
(c"isFlipped", yes_please as *const c_void, yes.clone()),
(c"acceptsFirstResponder", yes_please as *const c_void, yes.clone()),
(c"canBecomeKeyView", yes_please as *const c_void, yes.clone()),
(c"acceptsFirstMouse:", accepts_first_mouse as *const c_void, format!("{BOOL}@:@")),
(c"windowShouldClose:", should_close as *const c_void, format!("{BOOL}@:@")),
(c"unmarkText", unmarked as *const c_void, plain.clone()),
(c"doCommandBySelector:", commanded as *const c_void, "v@::".to_string()),
(c"insertText:replacementRange:", inserted as *const c_void, format!("v@:@{RANGE}")),
(c"setMarkedText:selectedRange:replacementRange:", marked as *const c_void, format!("v@:@{RANGE}{RANGE}")),
(c"hasMarkedText", has_marked as *const c_void, yes.clone()),
(c"markedRange", marked_range as *const c_void, format!("{RANGE}@:")),
(c"selectedRange", selected_range as *const c_void, format!("{RANGE}@:")),
(c"validAttributesForMarkedText", no_attributes as *const c_void, "@@:".to_string()),
(c"attributedSubstringForProposedRange:actualRange:", no_substring as *const c_void, format!("@@:{RANGE}^{RANGE}")),
(c"characterIndexForPoint:", no_index as *const c_void, format!("Q@:{POINT}")),
(c"firstRectForCharacterRange:actualRange:", first_rect as *const c_void, format!("{RECT}@:{RANGE}^{RANGE}")),
]);
methods.extend(crate::ns_accessibility::view_methods());
let listed: Vec<(&CStr, *const c_void, &str)> = methods.iter().map(|(name, imp, types)| (*name, *imp, types.as_str())).collect();
made_class(c"TautView", c"NSView", &listed, &[c"NSTextInputClient", c"NSWindowDelegate"])
}
fn window_class() -> Obj {
let yes = yes_please as extern "C" fn(Obj, Sel) -> Bool as *const c_void;
let types = format!("{BOOL}@:");
made_class(c"TautWindow", c"NSWindow", &[(c"canBecomeKeyWindow", yes, &types), (c"canBecomeMainWindow", yes, &types)], &[])
}
extern "C" fn yes_please(_: Obj, _: Sel) -> Bool {
1
}
extern "C" fn accepts_first_mouse(_: Obj, _: Sel, _: Obj) -> Bool {
1
}
fn mods_of(flags: usize) -> Mods {
Mods { ctrl: flags & (CONTROL | COMMAND) != 0, shift: flags & SHIFT != 0, alt: flags & OPTION != 0 }
}
fn mods(event: Obj) -> Mods {
mods_of(unsafe { send::<usize>(event, c"modifierFlags", ()) })
}
fn place_in(view: Obj, event: Obj) -> Point {
let at: NSPoint = unsafe { send(view, c"convertPoint:fromView:", (send::<NSPoint>(event, c"locationInWindow", ()), std::ptr::null_mut::<c_void>())) };
pt(at.x as f32, at.y as f32)
}
extern "C" fn pointed(view: Obj, _: Sel, event: Obj) {
let (kind, button) = unsafe { (send::<usize>(event, c"type", ()), send::<isize>(event, c"buttonNumber", ())) };
if kind == EXITED {
return heard(Heard::Event(view, Event::Leave));
}
heard(Heard::Event(view, Event::Move(place_in(view, event))));
let which = match (kind, button) {
(LEFT_DOWN | LEFT_UP, _) => Button::Left,
(RIGHT_DOWN | RIGHT_UP, _) => Button::Right,
(OTHER_DOWN | OTHER_UP, 2) => Button::Middle,
_ => return,
};
if matches!(kind, LEFT_DOWN | RIGHT_DOWN | OTHER_DOWN) {
keep_press(view, event);
heard(Heard::Event(view, Event::Press(which, mods(event))));
} else {
heard(Heard::Event(view, Event::Release(which)));
}
}
fn keep_press(view: Obj, event: Obj) {
let kept: Obj = unsafe { send(event, c"retain", ()) };
let before = with_view(view, |state| std::mem::replace(&mut state.down, kept)).unwrap_or(kept);
unsafe { send::<()>(before, c"release", ()) };
}
extern "C" fn scrolled(view: Obj, _: Sel, event: Obj) {
let (precise, x, y) = unsafe { (send::<Bool>(event, c"hasPreciseScrollingDeltas", ()) != 0, send::<f64>(event, c"scrollingDeltaX", ()), send::<f64>(event, c"scrollingDeltaY", ())) };
let per = if precise { 1.0 } else { NOTCH };
let (right, down) = (-x as f32 * per, -y as f32 * per);
if (right, down) == (0.0, 0.0) {
return;
}
heard(Heard::Event(view, Event::Move(place_in(view, event))));
let zoom = mods(event).ctrl && !precise && down != 0.0;
heard(Heard::Event(view, if zoom { Event::Zoom(crate::touch::wheel_zoom(-down / NOTCH)) } else { Event::Wheel(right, down) }));
}
extern "C" fn magnified(view: Obj, _: Sel, event: Obj) {
let by = unsafe { send::<f64>(event, c"magnification", ()) } as f32;
heard(Heard::Event(view, Event::Move(place_in(view, event))));
heard(Heard::Event(view, Event::Zoom((1.0 + by).max(0.1))));
}
fn key_of(event: Obj) -> Option<Key> {
let code = unsafe { send::<u16>(event, c"keyCode", ()) };
let named = match code {
0x24 | 0x4c => Key::Enter,
0x35 => Key::Escape,
0x30 => Key::Tab,
0x33 => Key::Backspace,
0x75 => Key::Delete,
0x7b => Key::Left,
0x7c => Key::Right,
0x7e => Key::Up,
0x7d => Key::Down,
0x73 => Key::Home,
0x77 => Key::End,
0x74 => Key::PageUp,
0x79 => Key::PageDown,
0x31 => Key::Space,
0x6e => Key::Menu,
_ => return function_key(code).or_else(|| letter_of(event)),
};
Some(named)
}
fn function_key(code: u16) -> Option<Key> {
const CODES: [u16; 12] = [0x7a, 0x78, 0x63, 0x76, 0x60, 0x61, 0x62, 0x64, 0x65, 0x6d, 0x67, 0x6f];
CODES.iter().position(|of| *of == code).map(|at| Key::F(at as u8 + 1))
}
fn letter_of(event: Obj) -> Option<Key> {
let typed = rust_string(unsafe { send(event, c"charactersIgnoringModifiers", ()) })?;
let letter = typed.chars().next()?.to_lowercase().next()?;
(!letter.is_control() && !('\u{f700}'..='\u{f8ff}').contains(&letter)).then_some(Key::Char(letter))
}
extern "C" fn key_down(view: Obj, _: Sel, event: Obj) {
let (held, key) = (mods(event), key_of(event));
let command = unsafe { send::<usize>(event, c"modifierFlags", ()) } & (COMMAND | CONTROL) != 0;
let was_marked = with_view(view, |state| {
state.typed = Some(String::new());
state.marked
});
if !command {
unsafe { send::<()>(view, c"interpretKeyEvents:", (ns_array(&[event]),)) };
}
let (typed, marked) = with_view(view, |state| (state.typed.take().unwrap_or_default(), state.marked)).unwrap_or_default();
if let Some(key) = key.filter(|_| was_marked == Some(0) && marked == 0) {
heard(Heard::Event(view, Event::Key(key, held)));
}
if !typed.is_empty() {
heard(Heard::Event(view, Event::Text(typed)));
}
}
extern "C" fn key_up(view: Obj, _: Sel, event: Obj) {
if let Some(key) = key_of(event) {
heard(Heard::Event(view, Event::KeyUp(key)));
}
}
extern "C" fn flags_changed(view: Obj, _: Sel, event: Obj) {
let now = mods(event);
if with_view(view, |state| std::mem::replace(&mut state.held, now) != now) == Some(true) {
heard(Heard::Event(view, Event::ModifiersChanged(now)));
}
}
extern "C" fn cursor_update(view: Obj, _: Sel, _: Obj) {
if let Some(cursor) = with_view(view, |state| state.cursor) {
show_cursor(cursor);
}
}
fn show_cursor(cursor: Cursor) {
let name = match cursor {
Cursor::Arrow => c"arrowCursor",
Cursor::Hand => c"pointingHandCursor",
Cursor::Text => c"IBeamCursor",
Cursor::ResizeX => c"resizeLeftRightCursor",
Cursor::ResizeY => c"resizeUpDownCursor",
Cursor::ResizeDown => c"_windowResizeNorthWestSouthEastCursor",
Cursor::ResizeUp => c"_windowResizeNorthEastSouthWestCursor",
Cursor::Move => c"openHandCursor",
Cursor::No => c"operationNotAllowedCursor",
};
unsafe {
let has = send::<Bool>(class(c"NSCursor"), c"respondsToSelector:", (sel(name),)) != 0;
let shape: Obj = send(class(c"NSCursor"), if has { name } else { c"crosshairCursor" }, ());
send::<()>(shape, c"set", ());
}
}
extern "C" fn view_rescaled(view: Obj, _: Sel) {
heard(Heard::Resized(view));
}
extern "C" fn view_looks(_: Obj, _: Sel) {
heard(Heard::Looks);
}
extern "C" fn window_resized(view: Obj, _: Sel, _: Obj) {
heard(Heard::Resized(view));
if unsafe { send::<Bool>(view, c"inLiveResize", ()) } != 0 {
live_turn();
}
}
extern "C" fn window_rescaled(view: Obj, _: Sel, _: Obj) {
heard(Heard::Resized(view));
}
extern "C" fn window_moved(view: Obj, _: Sel, _: Obj) {
heard(Heard::Moved(view));
}
extern "C" fn window_focused(view: Obj, _: Sel, _: Obj) {
heard(Heard::Focused(view, true));
}
extern "C" fn window_unfocused(view: Obj, _: Sel, _: Obj) {
heard(Heard::Focused(view, false));
}
extern "C" fn window_minimized(view: Obj, _: Sel, _: Obj) {
heard(Heard::Minimized(view, true));
}
extern "C" fn window_restored(view: Obj, _: Sel, _: Obj) {
heard(Heard::Minimized(view, false));
}
extern "C" fn should_close(view: Obj, _: Sel, _: Obj) -> Bool {
heard(Heard::Close(view));
0
}
fn text_of(string: Obj) -> String {
let attributed = unsafe { send::<Bool>(string, c"isKindOfClass:", (class(c"NSAttributedString"),)) } != 0;
let plain = if attributed { unsafe { send(string, c"string", ()) } } else { string };
rust_string(plain).unwrap_or_default()
}
fn typed(view: Obj, text: String) {
crate::macos_composing::hide(view);
let kept = with_view(view, |state| {
state.marked = 0;
state.typed.as_mut().map(|typed| typed.push_str(&text)).is_some()
});
if kept == Some(false) && !text.is_empty() {
heard(Heard::Event(view, Event::Text(text)));
}
}
extern "C" fn inserted(view: Obj, _: Sel, string: Obj, _: NSRange) {
typed(view, text_of(string));
}
extern "C" fn inserted_alone(view: Obj, _: Sel, string: Obj) {
typed(view, text_of(string));
}
extern "C" fn marked(view: Obj, _: Sel, string: Obj, picked: NSRange, _: NSRange) {
let text = text_of(string);
let at = with_view(view, |state| {
(state.marked, state.picked) = (text.encode_utf16().count(), picked);
state.ime
});
crate::macos_composing::show(view, &text, picked, at.flatten());
}
extern "C" fn unmarked(view: Obj, _: Sel) {
with_view(view, |state| state.marked = 0);
crate::macos_composing::hide(view);
}
extern "C" fn has_marked(view: Obj, _: Sel) -> Bool {
(with_view(view, |state| state.marked).unwrap_or(0) > 0) as Bool
}
extern "C" fn marked_range(view: Obj, _: Sel) -> NSRange {
match with_view(view, |state| state.marked) {
Some(length) if length > 0 => NSRange { location: 0, length },
_ => NSRange { location: NOT_FOUND, length: 0 },
}
}
extern "C" fn selected_range(view: Obj, _: Sel) -> NSRange {
with_view(view, |state| if state.marked > 0 { state.picked } else { NSRange::default() }).unwrap_or_default()
}
extern "C" fn no_attributes(_: Obj, _: Sel) -> Obj {
ns_array(&[])
}
extern "C" fn no_substring(_: Obj, _: Sel, _: NSRange, _: *mut NSRange) -> Obj {
std::ptr::null_mut()
}
extern "C" fn no_index(_: Obj, _: Sel, _: NSPoint) -> usize {
NOT_FOUND
}
extern "C" fn commanded(_: Obj, _: Sel, _: Sel) {}
extern "C" fn first_rect(view: Obj, _: Sel, _: NSRange, _: *mut NSRange) -> NSRect {
let place = with_view(view, |state| state.ime).flatten().unwrap_or_default();
let rect = NSRect::new(f64::from(place.x), f64::from(place.y), f64::from(place.w.max(1.0)), f64::from(place.h));
unsafe {
let window: Obj = send(view, c"window", ());
let in_window = send_rect(view, c"convertRect:toView:", (rect, std::ptr::null_mut::<c_void>()));
send_rect(window, c"convertRectToScreen:", (in_window,))
}
}
trait Turns {
fn turn(&mut self);
}
fn live_turn() {
let Some(host) = LIVE.get() else { return };
if TURNING.get() {
return;
}
unsafe { (*host).turn() }
}
struct Shown {
known: Option<Id>,
window: Obj,
view: Obj,
title: String,
size: Size,
scale: f32,
surface: Surface,
see_through: bool,
frameless: bool,
kind: WindowKind,
minimized: bool,
#[cfg(feature = "gpu")]
way: crate::gpu_metal::Way,
}
struct Opening<'a> {
known: Option<Id>,
title: &'a str,
size: Size,
min_size: Size,
open_at: OpenAt,
frameless: bool,
transparent: bool,
kind: WindowKind,
}
impl Shown {
fn open(opening: Opening) -> Shown {
let (view_class, window_class) = (view_class(), window_class());
let frame = NSRect::new(0.0, 0.0, f64::from(opening.size.w.max(1.0)), f64::from(opening.size.h.max(1.0)));
let style = match (opening.transparent, opening.frameless) {
(true, true) => RESIZABLE,
(true, false) => 0,
(false, true) => TITLED | CLOSABLE | MINIATURIZABLE | RESIZABLE | FULL_SIZE_CONTENT,
(false, false) => TITLED | CLOSABLE | MINIATURIZABLE | RESIZABLE,
};
unsafe {
let window: Obj = send(class_alloc(window_class), c"initWithContentRect:styleMask:backing:defer:", (frame, style, 2usize, 0 as Bool));
let view: Obj = send(class_alloc(view_class), c"initWithFrame:", (frame,));
send::<()>(window, c"setReleasedWhenClosed:", (0 as Bool,));
send::<()>(window, c"setTitle:", (ns_string(opening.title),));
send::<()>(window, c"setContentMinSize:", (NSSize { width: f64::from(opening.min_size.w), height: f64::from(opening.min_size.h) },));
send::<()>(window, c"setContentView:", (view,));
send::<()>(window, c"setDelegate:", (view,));
send::<()>(window, c"setAcceptsMouseMovedEvents:", (1 as Bool,));
send::<Bool>(window, c"makeFirstResponder:", (view,));
send::<()>(view, c"release", ());
prepare_view(view, opening.transparent);
Shown::dressed(window, opening.frameless, opening.transparent, opening.kind);
Shown::placed(window, opening.open_at);
let scale = send::<f64>(window, c"backingScaleFactor", ()) as f32;
let size = Shown::content_size(view);
let state = ViewState { view, cursor: Cursor::Arrow, ime: None, marked: 0, picked: NSRange::default(), typed: None, held: Mods::NONE, down: std::ptr::null_mut() };
VIEWS.with_borrow_mut(|views| views.push(state));
if let Some(drops) = crate::macos_files::DROPS.get() {
(drops.take)(view);
}
let (frameless, kind) = (opening.frameless, opening.kind);
Shown {
known: opening.known,
window,
view,
title: opening.title.into(),
size,
scale,
surface: Surface::default(),
see_through: opening.transparent,
frameless,
kind,
minimized: false,
#[cfg(feature = "gpu")]
way: crate::gpu_metal::Way::Unasked,
}
}
}
fn dressed(window: Obj, frameless: bool, transparent: bool, kind: WindowKind) {
unsafe {
if frameless && !transparent {
send::<()>(window, c"setTitlebarAppearsTransparent:", (1 as Bool,));
send::<()>(window, c"setTitleVisibility:", (1isize,));
(0..3usize).for_each(|button| send::<()>(send::<Obj>(window, c"standardWindowButton:", (button,)), c"setHidden:", (1 as Bool,)));
}
if transparent {
send::<()>(window, c"setOpaque:", (0 as Bool,));
send::<()>(window, c"setBackgroundColor:", (send::<Obj>(class(c"NSColor"), c"clearColor", ()),));
send::<()>(window, c"setHasShadow:", (0 as Bool,));
}
let above = matches!(kind, WindowKind::Tool | WindowKind::Dialog | WindowKind::OnTop);
send::<()>(window, c"setLevel:", (if above { FLOATING_LEVEL } else { NORMAL_LEVEL },));
}
}
fn placed(window: Obj, asked: OpenAt) {
unsafe {
let frame = send_rect(window, c"frame", ());
let size = [frame.size.width as f32, frame.size.height as f32];
let Some((_, at)) = window_origin(&screens(), asked, |_| size) else { return send::<()>(window, c"center", ()) };
send::<()>(window, c"setFrameTopLeftPoint:", (NSPoint { x: f64::from(at.x), y: desk_height() - f64::from(at.y) },));
}
}
fn content_size(view: Obj) -> Size {
let bounds = unsafe { send_rect(view, c"bounds", ()) };
Size { w: bounds.size.width as f32, h: bounds.size.height as f32 }
}
fn measure(&mut self) -> bool {
let (size, scale) = (Shown::content_size(self.view), unsafe { send::<f64>(self.window, c"backingScaleFactor", ()) } as f32);
let changed = (size, scale) != (self.size, self.scale);
(self.size, self.scale) = (size, scale);
changed
}
fn placement(&self) -> (WindowPlacement, bool) {
unsafe {
let frame = send_rect(self.window, c"frame", ());
let at = on_desk(frame, desk_height());
let whole = send::<usize>(self.window, c"styleMask", ()) & (1 << 14) != 0;
let zoomed = send::<Bool>(self.window, c"isZoomed", ()) != 0 && !whole;
(WindowPlacement { position: (at.x.round() as i32, at.y.round() as i32), width: self.size.w, height: self.size.h, maximized: zoomed }, whole)
}
}
fn present(&self) {
let (width, height) = (self.surface.width, self.surface.height);
if width == 0 || height == 0 {
return;
}
unsafe {
let bytes = CFDataCreate(std::ptr::null(), self.surface.pixels.as_ptr().cast(), (width * height * 4) as isize);
let provider = CGDataProviderCreateWithCFData(bytes);
let space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
let info = if self.see_through { PREMULTIPLIED } else { SOLID };
let picture = CGImageCreate(width, height, 8, 32, width * 4, space, info, provider, std::ptr::null(), false, 0);
let layer: Obj = send(self.view, c"layer", ());
send::<()>(class(c"CATransaction"), c"begin", ());
send::<()>(class(c"CATransaction"), c"setDisableActions:", (1 as Bool,));
send::<()>(layer, c"setContentsScale:", (f64::from(self.scale),));
send::<()>(layer, c"setContents:", (picture,));
send::<()>(class(c"CATransaction"), c"commit", ());
CGImageRelease(picture);
CGColorSpaceRelease(space);
CGDataProviderRelease(provider);
CFRelease(bytes);
}
}
fn show(&self, front: bool) {
unsafe { send::<()>(self.window, if front { c"makeKeyAndOrderFront:" } else { c"orderFront:" }, (std::ptr::null_mut::<c_void>(),)) };
}
fn close(self) {
VIEWS.with_borrow_mut(|views| views.retain(|state| state.view != self.view));
crate::ns_accessibility::forget(self.view);
crate::macos_composing::forget(self.view);
unsafe {
send::<()>(self.window, c"setDelegate:", (std::ptr::null_mut::<c_void>(),));
send::<()>(self.window, c"orderOut:", (std::ptr::null_mut::<c_void>(),));
send::<()>(self.window, c"close", ());
send::<()>(self.window, c"release", ());
}
}
}
unsafe fn class_alloc(class: Obj) -> Obj {
unsafe { send(class, c"alloc", ()) }
}
fn prepare_view(view: Obj, transparent: bool) {
unsafe {
send::<()>(view, c"setWantsLayer:", (1 as Bool,));
send::<()>(view, c"setLayerContentsRedrawPolicy:", (0isize,));
let layer: Obj = send(view, c"layer", ());
send::<()>(layer, c"setContentsGravity:", (kCAGravityTopLeft,));
send::<()>(layer, c"setOpaque:", (Bool::from(!transparent),));
let options = 0x01usize | 0x02 | 0x04 | 0x80 | 0x200;
let area: Obj = send(class_alloc(class(c"NSTrackingArea")), c"initWithRect:options:owner:userInfo:", (NSRect::default(), options, view, std::ptr::null_mut::<c_void>()));
send::<()>(view, c"addTrackingArea:", (area,));
send::<()>(area, c"release", ());
}
}
fn within<T>(first: &mut Ui, known: Option<Id>, work: impl FnOnce(&mut Ui) -> T) -> Option<T> {
match known {
Some(known) => first.in_window(known, work),
None => Some(work(first)),
}
}
struct Host<App> {
app: App,
ui: Ui,
windows: Vec<Shown>,
rerun: bool,
done: bool,
start: Instant,
over: Option<Obj>,
#[cfg(feature = "gpu")]
metal: crate::gpu_metal::Metal,
}
impl<App: FnMut(&mut Ui)> Host<App> {
fn open(text: SystemText, window: Window, app: App) -> Host<App> {
let application = application();
let opening = Opening {
known: None,
title: window.title,
size: Size { w: window.width, h: window.height },
min_size: Size { w: window.min_width, h: window.min_height },
open_at: window.position.map(|(x, y)| OpenAt::At(x, y)).or(window.screen.map(OpenAt::Middle)).unwrap_or_default(),
frameless: window.frameless,
transparent: window.transparent,
kind: WindowKind::Plain,
};
let first = Shown::open(opening);
set_icon(application, window.icons);
let mut ui = Ui::new(Box::new(text), Box::new(SystemClipboard));
if window.transparent {
ui.theme.bg = Color::NONE;
}
ui.set_preferences(preferences());
ui.set_screens(screens());
ui.set_native_file_dialog(true);
#[cfg(feature = "gpu")]
let metal = crate::gpu_metal::Metal::new(window.renderer);
let mut host = Host {
app,
ui,
windows: vec![first],
rerun: true,
done: false,
start: Instant::now(),
over: None,
#[cfg(feature = "gpu")]
metal,
};
host.turn();
let shown = &host.windows[0];
unsafe {
if window.maximized {
send::<()>(shown.window, c"zoom:", (std::ptr::null_mut::<c_void>(),));
}
shown.show(!window.minimized);
if window.minimized {
send::<()>(shown.window, c"miniaturize:", (std::ptr::null_mut::<c_void>(),));
} else {
send::<()>(application, c"activateIgnoringOtherApps:", (1 as Bool,));
}
}
host
}
fn window_of(&self, view: Obj) -> Option<usize> {
self.windows.iter().position(|shown| shown.view == view)
}
fn take_heard(&mut self) {
for what in HEARD.with_borrow_mut(std::mem::take) {
self.take(what);
}
}
fn take(&mut self, what: Heard) {
let at = self.window_of(what.view());
match (what, at) {
(Heard::Looks, _) => self.rerun |= self.ui.set_preferences(preferences()) | self.ui.set_screens(screens()),
(Heard::Quit, _) => self.closed(0),
(Heard::Desktop, _) => self.rerun = true,
(_, None) => {}
(Heard::Event(_, event), Some(at)) => self.hear(at, event),
(Heard::Close(_), Some(at)) => self.closed(at),
(Heard::Resized(_), Some(at)) => self.rerun |= self.windows[at].measure(),
(Heard::Moved(_), Some(at)) => self.place(at),
(Heard::Minimized(_, down), Some(at)) => self.minimized(at, down),
(Heard::Focused(_, on), Some(at)) => self.hear(at, Event::Focused(on)),
(Heard::Other(_, work), Some(at)) => self.rerun |= within(&mut self.ui, self.windows[at].known, work) == Some(true),
}
}
fn hear(&mut self, at: usize, event: Event) {
let blocked = self.ui.window_blocked(self.windows[at].known.unwrap_or(0));
let input = !matches!(event, Event::Focused(_) | Event::Minimized(_) | Event::Leave);
if blocked && input {
let last = self.windows.iter().rposition(|shown| shown.kind == WindowKind::Dialog);
return last.into_iter().for_each(|dialog| self.windows[dialog].show(matches!(event, Event::Press(..))));
}
match event {
Event::Move(_) => self.over = Some(self.windows[at].view),
Event::Leave => self.over = None,
Event::Press(Button::Left, _) if self.grip(at) => return,
_ => {}
}
self.rerun |= within(&mut self.ui, self.windows[at].known, |ui| ui.event(event)) == Some(true);
}
fn grip(&mut self, at: usize) -> bool {
let shown = &self.windows[at];
if !shown.frameless {
return false;
}
let (view, window) = (shown.view, shown.window);
let Some(down) = with_view(view, |state| state.down).filter(|down| !down.is_null()) else { return false };
let place = place_in(view, down);
let hit = within(&mut self.ui, shown.known, |ui| ui.hit_test(place, false));
if hit != Some(crate::frameless::HitTest::TitleBar) {
return false;
}
unsafe { send::<()>(window, c"performWindowDragWithEvent:", (down,)) };
true
}
fn closed(&mut self, at: usize) {
let Some(known) = self.windows[at].known else {
self.ui.request_close();
return self.rerun = true;
};
self.rerun |= self.ui.in_window(known, Ui::request_close).is_some();
}
fn minimized(&mut self, at: usize, down: bool) {
self.windows[at].minimized = down;
self.hear(at, Event::Minimized(down));
self.place(at);
}
fn place(&mut self, at: usize) {
let (now, whole) = self.windows[at].placement();
self.rerun |= within(&mut self.ui, self.windows[at].known, |ui| ui.set_window_placement(now) | ui.full_screen_now(whole)) == Some(true);
}
fn describe(&mut self) {
for shown in &self.windows {
within(&mut self.ui, shown.known, |ui| ui.resize(shown.size, shown.scale));
}
self.ui.run(&mut self.app);
let closes = self.ui.take_closing() == Some(Ok(()));
(self.rerun, self.done) = (false, self.done || closes || self.ui.quitting());
if let Some(stood) = crate::macos_shell::DESKTOP.get() {
self.rerun |= stood(&mut self.ui, self.windows[0].window, self.windows[0].view);
}
self.keep_up();
(0..self.windows.len()).for_each(|at| self.follow(at));
if self.ui.listening {
self.tell_readers();
}
}
fn listen(&mut self) {
if self.ui.listening || !crate::ns_accessibility::listening() {
return;
}
for shown in &self.windows {
within(&mut self.ui, shown.known, |ui| ui.set_screen_reader(true));
}
self.rerun = true;
}
fn tell_readers(&mut self) {
for shown in &self.windows {
within(&mut self.ui, shown.known, |ui| crate::ns_accessibility::keep(shown.view, ui));
}
}
fn keep_up(&mut self) {
for at in (1..self.windows.len()).rev() {
if !self.ui.windows().any(|(known, ..)| self.windows[at].known == Some(known)) {
#[cfg(feature = "gpu")]
self.metal.unshow(&mut self.windows[at].way);
self.windows.remove(at).close();
}
}
let new: Vec<(Id, String, Size)> =
self.ui.windows().filter(|(known, ..)| self.windows.iter().all(|shown| shown.known != Some(*known))).map(|(known, title, size)| (known, title.into(), size)).collect();
for (known, title, size) in new {
let opens: WindowOpening = self.ui.window_opening(known).unwrap_or_default();
let over = || on_desk(unsafe { send_rect(self.windows[0].window, c"frame", ()) }, desk_height());
let open_at = match (opens.position, opens.screen, opens.kind) {
(Some((x, y)), ..) => OpenAt::At(x, y),
(None, Some(nth), _) => OpenAt::Middle(nth),
(None, None, WindowKind::Dialog) => OpenAt::Over(over()),
_ => OpenAt::Any,
};
let opening = Opening { known: Some(known), title: &title, size, min_size: opens.min_size, open_at, frameless: opens.frameless, transparent: false, kind: opens.kind };
let shown = Shown::open(opening);
if self.ui.listening {
self.ui.in_window(known, |ui| ui.set_screen_reader(true));
}
shown.show(true);
self.windows.push(shown);
self.rerun = true;
}
}
fn follow(&mut self, at: usize) {
let (view, drops) = (self.windows[at].view, crate::macos_files::DROPS.get());
let asks = |ui: &mut Ui| {
drops.inspect(|drops| (drops.note)(view, ui));
(ui.take_title(), ui.cursor(), ui.ime_area(), ui.take_minimize(), ui.take_maximized(), ui.take_full_screen(), ui.take_on_top(), ui.take_bring_to_front(), ui.take_file_dialog_request())
};
let Some((title, cursor, ime, minimize, fill, whole, on_top, forward, files)) = within(&mut self.ui, self.windows[at].known, asks) else { return };
if let Some(outward) = crate::macos_drag_out::OUTWARD.get() {
let down = with_view(view, |state| state.down).unwrap_or(std::ptr::null_mut());
self.rerun |= within(&mut self.ui, self.windows[at].known, |ui| outward(ui, view, down)) == Some(true);
}
if let Some((ask, files)) = crate::macos_files::DIALOG.get().zip(files) {
ask(self.windows[at].window, view, files);
}
let (shown, over) = (&mut self.windows[at], self.over);
let moved = with_view(shown.view, |state| {
let moved = state.ime != ime && state.marked > 0;
(state.cursor, state.ime) = (cursor, ime);
moved
});
if moved == Some(true) {
crate::macos_composing::follow(shown.view, ime);
}
if over == Some(shown.view) {
show_cursor(cursor);
}
let window = shown.window;
unsafe {
if let Some(title) = title {
send::<()>(window, c"setTitle:", (ns_string(&title),));
shown.title = title;
}
if minimize {
send::<()>(window, c"miniaturize:", (std::ptr::null_mut::<c_void>(),));
}
let (zoomed, full) = (send::<Bool>(window, c"isZoomed", ()) != 0, send::<usize>(window, c"styleMask", ()) & (1 << 14) != 0);
if fill.is_some_and(|fill| fill != zoomed) {
send::<()>(window, c"zoom:", (std::ptr::null_mut::<c_void>(),));
}
if whole.is_some_and(|whole| whole != full) {
send::<()>(window, c"toggleFullScreen:", (std::ptr::null_mut::<c_void>(),));
}
if let Some(on_top) = on_top {
send::<()>(window, c"setLevel:", (if on_top { FLOATING_LEVEL } else { NORMAL_LEVEL },));
}
if forward {
send::<()>(APP.load(Ordering::SeqCst), c"activateIgnoringOtherApps:", (1 as Bool,));
shown.show(true);
}
}
}
fn draw(&mut self, at: usize) {
#[cfg(feature = "gpu")]
if self.through_gpu(at) {
return;
}
let shown = &mut self.windows[at];
let surface = &mut shown.surface;
let changed = within(&mut self.ui, shown.known, |ui| {
let changed = surface.update(ui);
ui.changed();
changed
});
if changed.is_some_and(|changed| !changed.is_empty()) {
shown.present();
}
}
fn patience(&self) -> Option<Duration> {
if self.rerun || !HEARD.with_borrow(Vec::is_empty) { Some(Duration::ZERO) } else { self.ui.wake_in() }
}
}
#[cfg(feature = "gpu")]
impl Shown {
fn pixels(&self) -> [usize; 2] {
[(self.size.w * self.scale).round() as usize, (self.size.h * self.scale).round() as usize]
}
fn bounds(&self) -> NSRect {
unsafe { send_rect(self.view, c"bounds", ()) }
}
fn live(&self) -> bool {
unsafe { send::<Bool>(self.view, c"inLiveResize", ()) != 0 }
}
}
#[cfg(feature = "gpu")]
impl<App: FnMut(&mut Ui)> Host<App> {
fn mend(&mut self) -> bool {
let moved = self.moved();
let lost = moved || self.metal.gpu.as_ref().is_some_and(|gpu| gpu.lost);
if lost {
self.let_go();
}
self.metal.keep_opening();
(0..self.windows.len()).for_each(|at| self.ask_gpu(at));
self.ui.renderer = self.metal.gpu.as_ref().map_or(crate::platform::Renderer::Cpu, crate::gpu_metal::Gpu::renderer);
lost
}
fn moved(&mut self) -> bool {
let Some(gpu) = &self.metal.gpu else { return false };
let shows = self.windows.iter().filter_map(|shown| if let crate::gpu_metal::Way::Gpu(shows) = &shown.way { Some(shows) } else { None });
let Some(device) = shows.into_iter().find_map(|shows| gpu.moved(shows)) else { return false };
self.metal.prefer(device);
true
}
fn let_go(&mut self) {
for shown in &mut self.windows {
self.metal.unshow(&mut shown.way);
(shown.way, shown.surface) = (crate::gpu_metal::Way::Unasked, Surface::default());
}
self.metal.let_go();
self.ui.renderer = crate::platform::Renderer::Cpu;
}
fn ask_gpu(&mut self, at: usize) {
let shown = &mut self.windows[at];
let Some(gpu) = self.metal.gpu.as_mut().filter(|_| matches!(shown.way, crate::gpu_metal::Way::Unasked)) else { return };
if shown.see_through {
return shown.way = crate::gpu_metal::Way::Cpu;
}
let under: Obj = unsafe { send(shown.view, c"layer", ()) };
let made = gpu.window(under, shown.bounds(), shown.pixels(), shown.scale);
shown.way = made.map_or(crate::gpu_metal::Way::Cpu, crate::gpu_metal::Way::Gpu);
}
fn through_gpu(&mut self, at: usize) -> bool {
let Host { ui, windows, metal, .. } = self;
let (Some(gpu), shown) = (metal.gpu.as_mut(), &mut windows[at]) else { return false };
let (size, scale, bounds, live, units) = (shown.pixels(), shown.scale, shown.bounds(), shown.live(), shown.size);
let Shown { way, surface, known, .. } = shown;
let crate::gpu_metal::Way::Gpu(shows) = way else { return false };
if shows.size() != (size, scale) && !gpu.fit(shows, bounds, size, scale) {
if let crate::gpu_metal::Way::Gpu(shows) = std::mem::replace(way, crate::gpu_metal::Way::Cpu) {
gpu.forget(shows);
}
*surface = Surface::default();
return false;
}
let (whole, everything) = (shows.take_whole(), crate::geom::rect(0.0, 0.0, units.w, units.h));
let changed = within(ui, *known, |ui| {
let places: Vec<Rect> = ui.changes().collect();
let places = if whole { vec![everything] } else { places };
places.iter().for_each(|place| draw_place(gpu, shows, surface, ui, *place, (size, scale)));
ui.changed();
!places.is_empty()
});
if changed == Some(true) {
gpu.show(shows, live);
}
true
}
}
#[cfg(feature = "gpu")]
fn draw_place(gpu: &mut crate::gpu_metal::Gpu, shows: &mut crate::gpu_metal::Shows, surface: &mut Surface, ui: &mut Ui, place: Rect, (size, scale): ([usize; 2], f32)) {
let snapped = place.snap_to_pixels(scale);
let edge = |at: f32, most: usize| ((at * scale).round().max(0.0) as usize).min(most);
let pixels = [edge(snapped.x, size[0]), edge(snapped.y, size[1]), edge(snapped.right(), size[0]), edge(snapped.bottom(), size[1])];
if pixels[0] >= pixels[2] || pixels[1] >= pixels[3] {
return;
}
#[cfg(feature = "gpu-draw")]
if drawn_by_gpu(gpu, shows, ui, snapped, pixels.map(|side| side as i32), (size, scale)) {
return;
}
surface.redraw(ui, place);
gpu.put(shows, surface, pixels);
}
#[cfg(feature = "gpu-draw")]
fn drawn_by_gpu(gpu: &mut crate::gpu_metal::Gpu, shows: &mut crate::gpu_metal::Shows, ui: &mut Ui, area: Rect, place: [i32; 4], (size, scale): ([usize; 2], f32)) -> bool {
let Some(list) = gpu.drawing(place) else { return false };
let mut list = std::mem::take(list);
list.begin(size.map(|side| side as i32));
let mut canvas = crate::canvas::Canvas::recording(&mut list, size[0], size[1], (0, 0), scale);
canvas.clip(area);
ui.paint(&mut canvas);
drop(canvas);
gpu.draw(shows, list, place)
}
impl<App: FnMut(&mut Ui)> Turns for Host<App> {
fn turn(&mut self) {
TURNING.set(true);
self.ui.set_time(self.start.elapsed());
self.rerun |= self.ui.wake_in() == Some(Duration::ZERO);
self.take_heard();
self.listen();
if self.rerun {
self.describe();
}
#[cfg(feature = "gpu")]
self.mend();
(0..self.windows.len()).for_each(|at| self.draw(at));
#[cfg(feature = "gpu")]
if self.mend() {
(0..self.windows.len()).for_each(|at| self.draw(at));
}
TURNING.set(false);
}
}
impl<App> Drop for Host<App> {
fn drop(&mut self) {
#[cfg(feature = "gpu")]
self.windows.iter_mut().for_each(|shown| self.metal.unshow(&mut shown.way));
self.windows.drain(..).for_each(Shown::close);
HEARD.with_borrow_mut(Vec::clear);
}
}
fn wait(app: Obj, patience: Option<Duration>, host: *mut dyn Turns) -> bool {
LIVE.set(Some(host));
let mut woken = false;
unsafe {
let until: Obj = match patience {
Some(wait) => send(class(c"NSDate"), c"dateWithTimeIntervalSinceNow:", (wait.as_secs_f64(),)),
None => send(class(c"NSDate"), c"distantFuture", ()),
};
let mut next: Obj = send(app, c"nextEventMatchingMask:untilDate:inMode:dequeue:", (usize::MAX, until, NSDefaultRunLoopMode, 1 as Bool));
while !next.is_null() {
woken |= is_wake(next);
send::<()>(app, c"sendEvent:", (next,));
let now: Obj = send(class(c"NSDate"), c"distantPast", ());
next = send(app, c"nextEventMatchingMask:untilDate:inMode:dequeue:", (usize::MAX, now, NSDefaultRunLoopMode, 1 as Bool));
}
}
LIVE.set(None);
woken
}
fn is_wake(event: Obj) -> bool {
let wake = unsafe { send::<usize>(event, c"type", ()) == APPLICATION && send::<i16>(event, c"subtype", ()) == WAKE };
if wake {
WOKEN.store(false, Ordering::SeqCst);
}
wake
}
fn set_icon(app: Obj, icons: &[(usize, &[u32])]) {
let Some((width, pixels)) = icons.iter().max_by_key(|(width, _)| *width) else { return };
let height = pixels.len() / (*width).max(1);
let Some(png) = crate::png::encode_rgba(*width, &pixels[..width * height]) else { return };
unsafe {
let image: Obj = send(class_alloc(class(c"NSImage")), c"initWithData:", (ns_data(&png),));
send::<()>(app, c"setApplicationIconImage:", (image,));
send::<()>(image, c"release", ());
}
}
pub fn run(window: Window, app: impl FnMut(&mut Ui) + 'static) {
let _pool = Pool::new();
if unsafe { send::<Bool>(class(c"NSThread"), c"isMainThread", ()) } == 0 {
return eprintln!("taut: macOS shows windows only from the program's main thread.");
}
let Some(text) = SystemText::new() else {
return eprintln!("taut: CoreText has no system font.");
};
let mut host = Host::open(text, window, app);
let application = application();
while !host.done {
let _pool = Pool::new();
let patience = host.patience();
let lent: *mut dyn Turns = &mut host;
host.rerun |= wait(application, patience, lent);
host.turn();
}
}
struct SystemClipboard;
fn pasteboard() -> Obj {
unsafe { send(class(c"NSPasteboard"), c"generalPasteboard", ()) }
}
impl Clipboard for SystemClipboard {
fn get(&mut self) -> Option<String> {
let _pool = Pool::new();
rust_string(unsafe { send(pasteboard(), c"stringForType:", (NSPasteboardTypeString,)) })
}
fn set(&mut self, text: &str) {
let _pool = Pool::new();
unsafe {
send::<isize>(pasteboard(), c"clearContents", ());
send::<Bool>(pasteboard(), c"setString:forType:", (ns_string(text), NSPasteboardTypeString));
}
}
fn picture(&mut self) -> Option<(usize, Vec<u32>)> {
(PICTURES_AND_FILES.get()?.picture)(self)
}
fn set_picture(&mut self, width: usize, pixels: &[u32]) {
PICTURES_AND_FILES.get().map_or((), |its| (its.set_picture)(self, width, pixels))
}
fn files(&mut self) -> Vec<PathBuf> {
PICTURES_AND_FILES.get().map_or(Vec::new(), |its| (its.files)(self))
}
fn set_files(&mut self, files: &[PathBuf]) {
PICTURES_AND_FILES.get().map_or((), |its| (its.set_files)(self, files))
}
}
thread_local! {
static PICTURES_AND_FILES: Cell<Option<PicturesAndFiles<SystemClipboard>>> = const { Cell::new(None) };
}
pub(crate) fn hand_pictures_and_files() {
let (picture, set_picture) = (SystemClipboard::picture_on, SystemClipboard::put_picture);
PICTURES_AND_FILES.set(Some(PicturesAndFiles { picture, set_picture, files: SystemClipboard::files_on, set_files: SystemClipboard::put_files }));
}
impl SystemClipboard {
fn picture_on(&mut self) -> Option<(usize, Vec<u32>)> {
let _pool = Pool::new();
let board = pasteboard();
unsafe {
let png: Obj = send(board, c"dataForType:", (NSPasteboardTypePNG,));
if !png.is_null() {
return crate::png::decode(&bytes_of(png));
}
let tiff: Obj = send(board, c"dataForType:", (NSPasteboardTypeTIFF,));
let rep: Obj = send(class(c"NSBitmapImageRep"), c"imageRepWithData:", (tiff,));
let properties: Obj = send(class(c"NSDictionary"), c"dictionary", ());
let png: Obj = send(rep, c"representationUsingType:properties:", (4usize, properties));
crate::png::decode(&bytes_of(png))
}
}
fn put_picture(&mut self, width: usize, pixels: &[u32]) {
let Some(png) = crate::png::encode_rgba(width, pixels) else { return };
let _pool = Pool::new();
unsafe {
send::<isize>(pasteboard(), c"clearContents", ());
send::<Bool>(pasteboard(), c"setData:forType:", (ns_data(&png), NSPasteboardTypePNG));
}
}
fn files_on(&mut self) -> Vec<PathBuf> {
let _pool = Pool::new();
let items = array_items(unsafe { send(pasteboard(), c"pasteboardItems", ()) });
items.into_iter().filter_map(|item| file_of(unsafe { send(item, c"stringForType:", (NSPasteboardTypeFileURL,)) })).collect()
}
fn put_files(&mut self, files: &[PathBuf]) {
let _pool = Pool::new();
unsafe {
let urls: Vec<Obj> = files.iter().map(|file| send(class(c"NSURL"), c"fileURLWithPath:", (ns_string(&file.to_string_lossy()),))).collect();
send::<isize>(pasteboard(), c"clearContents", ());
send::<Bool>(pasteboard(), c"writeObjects:", (ns_array(&urls),));
}
}
}
pub(crate) fn file_of(address: Obj) -> Option<PathBuf> {
let url: Obj = unsafe { send(class(c"NSURL"), c"URLWithString:", (address,)) };
rust_string(unsafe { send(url, c"path", ()) }).map(PathBuf::from)
}