ferrokit-core 0.10.0

The AppKit layer under ferrokit (use that): places real AppKit views in a window and keeps layout, focus and shortcuts in step
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Helpers for unattended tests: synthetic input posted into the app's own
//! event queue, and probes of AppKit state. Not for production use.

use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType};
use objc2_foundation::NSPoint;

use std::sync::atomic::{AtomicUsize, Ordering};

use crate::ViewId;

/// Table and text models alive (Rust side).
pub(crate) static LIVE_MODELS: AtomicUsize = AtomicUsize::new(0);
/// Library-defined Objective-C objects alive (data sources, delegates, text
/// views): decremented when AppKit actually deallocates them.
pub(crate) static LIVE_OBJC: AtomicUsize = AtomicUsize::new(0);

/// (models alive, library Objective-C objects alive), for leak checks.
pub fn live_objects() -> (usize, usize) {
    (LIVE_MODELS.load(Ordering::Relaxed), LIVE_OBJC.load(Ordering::Relaxed))
}
use crate::host::{mtm, with_host};

/// Posts a key-down (optionally with ⌘ / ⇧).
pub fn post_key(chars: &str, key_code: u16, command: bool, shift: bool) {
    crate::keys::post_key_down(chars, key_code, command, shift);
}

/// Posts Tab (or Shift-Tab) to the window.
pub fn post_tab(shift: bool) {
    post_key("\t", 48, false, shift);
}

/// Posts a left click at a point in host coordinates (top-left origin, points).
pub fn post_click(x: f64, y: f64) {
    let Some((window_number, location)) = with_host(|h| {
        let window = h.content.window()?;
        let p = if h.content.isFlipped() { NSPoint::new(x, y) } else { NSPoint::new(x, h.content.bounds().size.height - y) };
        Some((window.windowNumber(), h.content.convertPoint_toView(p, None)))
    })
    .flatten() else {
        return;
    };
    for kind in [NSEventType::LeftMouseDown, NSEventType::LeftMouseUp] {
        let event = NSEvent::mouseEventWithType_location_modifierFlags_timestamp_windowNumber_context_eventNumber_clickCount_pressure(
            kind, location, NSEventModifierFlags::empty(), 0.0, window_number, None, 0, 1, 1.0,
        );
        if let Some(event) = event {
            NSApplication::sharedApplication(mtm()).postEvent_atStart(&event, false);
        }
    }
}

/// Posts a mouse move to a point in host coordinates (hover).
pub fn post_mouse_move(x: f64, y: f64) {
    let Some((window_number, location)) = with_host(|h| {
        let window = h.content.window()?;
        let p = if h.content.isFlipped() { NSPoint::new(x, y) } else { NSPoint::new(x, h.content.bounds().size.height - y) };
        Some((window.windowNumber(), h.content.convertPoint_toView(p, None)))
    })
    .flatten() else {
        return;
    };
    let event = NSEvent::mouseEventWithType_location_modifierFlags_timestamp_windowNumber_context_eventNumber_clickCount_pressure(
        NSEventType::MouseMoved, location, NSEventModifierFlags::empty(), 0.0, window_number, None, 0, 0, 0.0,
    );
    if let Some(event) = event {
        NSApplication::sharedApplication(mtm()).postEvent_atStart(&event, false);
    }
}

/// Would a click at the centre of the view's visible part reach the view?
/// (AppKit's own hit test, so it reflects what a real click would do.)
pub fn click_reaches(id: ViewId) -> Option<bool> {
    with_host(|h| {
        let m = h.views.get(&id)?;
        let v = m.visible?;
        let (x, y) = (v.x + v.width / 2.0, v.y + v.height / 2.0);
        let p = if h.content.isFlipped() { NSPoint::new(x, y) } else { NSPoint::new(x, h.content.bounds().size.height - y) };
        // SAFETY: the content view is in a window, so its superview is valid.
        let p = unsafe { h.content.superview() }.map_or(p, |s| h.content.convertPoint_toView(p, Some(&s)));
        let hit = h.content.hitTest(p)?;
        Some(hit.isDescendantOf(&m.view))
    })
    .flatten()
}

/// Class name of the window's first responder ("host" for the host's view).
pub fn first_responder() -> String {
    with_host(|h| {
        let Some(r) = h.content.window().and_then(|w| w.firstResponder()) else { return "none".into() };
        if std::ptr::eq(&*r as *const _ as *const objc2_app_kit::NSView, &*h.content) {
            return "host".into();
        }
        r.class().name().to_string_lossy().into_owned()
    })
    .unwrap_or_else(|| "detached".into())
}

/// Whether the mounted view's clip container is currently shown.
pub fn is_visible(id: ViewId) -> bool {
    with_host(|h| h.views.get(&id).is_some_and(|m| m.shown()))
        .unwrap_or(false)
}

/// Forces this app's appearance (`None`: follow the system). The system
/// setting is untouched; windows' `effectiveAppearance` changes exactly as on
/// a system switch.
pub fn set_app_appearance(dark: Option<bool>) {
    let app = NSApplication::sharedApplication(mtm());
    let appearance = dark.and_then(|dark| {
        // SAFETY: framework string constants.
        let name = unsafe { if dark { objc2_app_kit::NSAppearanceNameDarkAqua } else { objc2_app_kit::NSAppearanceNameAqua } };
        objc2_app_kit::NSAppearance::appearanceNamed(name)
    });
    app.setAppearance(appearance.as_deref());
}

/// Whether a mounted view resolves to a dark appearance.
pub fn view_is_dark(id: ViewId) -> Option<bool> {
    with_host(|h| {
        let view = &h.views.get(&id)?.view;
        Some(objc2_app_kit::NSAppearanceCustomization::effectiveAppearance(&**view).name().to_string().contains("Dark"))
    })
    .flatten()
}

/// Shown views whose clip frame isn't on the device-pixel grid.
pub fn off_grid() -> Vec<ViewId> {
    with_host(|h| {
        let scale = h.content.window().map_or(1.0, |w| w.backingScaleFactor());
        let on = |v: f64| ((v * scale) - (v * scale).round()).abs() < 1e-6;
        h.views
            .iter()
            .filter(|(_, m)| m.shown())
            .filter(|(_, m)| {
                let f = m.clip_frame();
                !(on(f.origin.x) && on(f.origin.y) && on(f.size.width) && on(f.size.height))
            })
            .map(|(id, _)| *id)
            .collect()
    })
    .unwrap_or_default()
}

/// The visible part of a view as last placed (host coordinates).
pub fn visible_rect(id: ViewId) -> Option<crate::Rect> {
    with_host(|h| h.views.get(&id).and_then(|m| m.visible)).flatten()
}

/// Number of subviews of the host's content view (mounted clip containers).
pub fn subview_count() -> usize {
    with_host(|h| h.content.subviews().len()).unwrap_or(0)
}

/// (window is key, app is active), for diagnosing posted clicks.
pub fn window_state() -> (bool, bool) {
    let key = with_host(|h| h.content.window().is_some_and(|w| w.isKeyWindow())).unwrap_or(false);
    (key, NSApplication::sharedApplication(mtm()).isActive())
}

/// Makes a mounted view first responder directly through AppKit, bypassing
/// [`crate::focus`] (as when AppKit moves focus itself, e.g. on a click).
pub fn make_first_responder_raw(id: ViewId) -> bool {
    with_host(|h| {
        let (Some(m), Some(window)) = (h.views.get(&id), h.content.window()) else { return false };
        window.makeFirstResponder(Some(&crate::host::focus_target(&m.view)))
    })
    .unwrap_or(false)
}

/// Posts a key-down with explicit modifier flags (e.g. Control for Ctrl-Tab).
pub fn post_key_with(chars: &str, key_code: u16, flags: NSEventModifierFlags) {
    let Some(window_number) = with_host(|h| h.content.window().map(|w| w.windowNumber())).flatten() else { return };
    let chars = objc2_foundation::NSString::from_str(chars);
    let event = NSEvent::keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode(
        NSEventType::KeyDown, NSPoint::ZERO, flags, 0.0, window_number, None, &chars, &chars, false, key_code,
    );
    if let Some(event) = event {
        NSApplication::sharedApplication(mtm()).postEvent_atStart(&event, false);
    }
}

// ---- drag and drop, without a mouse ----

use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObject, NSObjectProtocol, ProtocolObject};
use objc2::{DefinedClass, MainThreadOnly, Message, define_class, msg_send};
use objc2_app_kit::{
    NSDragOperation, NSDraggingInfo, NSPasteboard, NSPasteboardItem, NSPasteboardTypeFileURL, NSPasteboardTypeString,
    NSPasteboardWriting, NSTableView, NSTableViewDropOperation, NSView,
};
use objc2_foundation::{NSArray, NSInteger, NSString, NSURL};

struct DragIvars {
    pasteboard: Retained<NSPasteboard>,
    source: Option<Retained<AnyObject>>,
}

define_class!(
    // The parts of a drag a table's data source reads: its pasteboard and
    // its source.
    #[unsafe(super(NSObject))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitCoreTestDrag"]
    #[ivars = DragIvars]
    struct TestDrag;

    unsafe impl NSObjectProtocol for TestDrag {}

    unsafe impl NSDraggingInfo for TestDrag {
        #[unsafe(method_id(draggingPasteboard))]
        fn dragging_pasteboard(&self) -> Retained<NSPasteboard> {
            self.ivars().pasteboard.clone()
        }

        #[unsafe(method_id(draggingSource))]
        fn dragging_source(&self) -> Option<Retained<AnyObject>> {
            self.ivars().source.clone()
        }

        // The rest of the protocol's required methods (objc2 checks for
        // them in debug builds): a drag nowhere in particular.
        #[unsafe(method_id(draggingDestinationWindow))]
        fn destination_window(&self) -> Option<Retained<objc2_app_kit::NSWindow>> {
            None
        }

        #[unsafe(method(draggingSourceOperationMask))]
        fn source_operations(&self) -> NSDragOperation {
            NSDragOperation::Copy | NSDragOperation::Move
        }

        #[unsafe(method(draggingLocation))]
        fn location(&self) -> objc2_foundation::NSPoint {
            objc2_foundation::NSPoint::ZERO
        }

        #[unsafe(method(draggedImageLocation))]
        fn image_location(&self) -> objc2_foundation::NSPoint {
            objc2_foundation::NSPoint::ZERO
        }

        #[unsafe(method_id(draggedImage))]
        fn image(&self) -> Option<Retained<objc2_app_kit::NSImage>> {
            None
        }

        #[unsafe(method(draggingSequenceNumber))]
        fn sequence(&self) -> NSInteger {
            1
        }

        #[unsafe(method(slideDraggedImageTo:))]
        fn slide(&self, _to: objc2_foundation::NSPoint) {}

        #[unsafe(method_id(namesOfPromisedFilesDroppedAtDestination:))]
        fn promised(&self, _at: &objc2_foundation::NSURL) -> Option<Retained<objc2_foundation::NSArray<objc2_foundation::NSString>>> {
            None
        }

        #[unsafe(method(draggingFormation))]
        fn formation(&self) -> objc2_app_kit::NSDraggingFormation {
            objc2_app_kit::NSDraggingFormation::Default
        }

        #[unsafe(method(setDraggingFormation:))]
        fn set_formation(&self, _f: objc2_app_kit::NSDraggingFormation) {}

        #[unsafe(method(animatesToDestination))]
        fn animates(&self) -> bool {
            false
        }

        #[unsafe(method(setAnimatesToDestination:))]
        fn set_animates(&self, _a: bool) {}

        #[unsafe(method(numberOfValidItemsForDrop))]
        fn valid_items(&self) -> NSInteger {
            1
        }

        #[unsafe(method(setNumberOfValidItemsForDrop:))]
        fn set_valid_items(&self, _n: NSInteger) {}

        #[unsafe(method(enumerateDraggingItemsWithOptions:forView:classes:searchOptions:usingBlock:))]
        fn enumerate(
            &self,
            _options: objc2_app_kit::NSDraggingItemEnumerationOptions,
            _view: Option<&NSView>,
            _classes: &objc2_foundation::NSArray<objc2::runtime::AnyClass>,
            _search: &objc2_foundation::NSDictionary<objc2_app_kit::NSPasteboardReadingOptionKey, AnyObject>,
            _block: &block2::DynBlock<dyn Fn(std::ptr::NonNull<objc2_app_kit::NSDraggingItem>, NSInteger, std::ptr::NonNull<objc2::runtime::Bool>)>,
        ) {
        }

        #[unsafe(method(springLoadingHighlight))]
        fn spring_highlight(&self) -> objc2_app_kit::NSSpringLoadingHighlight {
            objc2_app_kit::NSSpringLoadingHighlight::None
        }

        #[unsafe(method(resetSpringLoading))]
        fn reset_spring(&self) {}
    }
);

/// A fresh private pasteboard (never the general one); release it with
/// [`release_pasteboard`].
pub fn private_pasteboard() -> Retained<NSPasteboard> {
    NSPasteboard::pasteboardWithUniqueName()
}

/// Releases a pasteboard made by [`private_pasteboard`].
pub fn release_pasteboard(pasteboard: &NSPasteboard) {
    // SAFETY: frees a uniquely named pasteboard's resources.
    let () = unsafe { msg_send![pasteboard, releaseGlobally] };
}

/// Drops whatever is on `pasteboard` onto `table` above view row `row`, as
/// AppKit does at the end of a drag: the data source validates the drop,
/// then accepts it. `from_table` makes the table itself the drag's source.
/// Returns whether the drop was accepted.
pub fn drop_on_table(table: &NSTableView, pasteboard: &NSPasteboard, row: usize, from_table: bool) -> bool {
    let source: Option<Retained<AnyObject>> = from_table.then(|| {
        let any: &AnyObject = table.as_ref();
        any.retain()
    });
    let drag = TestDrag::alloc(mtm()).set_ivars(DragIvars { pasteboard: pasteboard.retain(), source });
    // SAFETY: NSObject's designated initialiser.
    let drag: Retained<TestDrag> = unsafe { msg_send![super(drag), init] };
    let info: &ProtocolObject<dyn NSDraggingInfo> = ProtocolObject::from_ref(&*drag);
    let Some(ds) = (unsafe { table.dataSource() }) else { return false };
    let row = row as NSInteger;
    // SAFETY: the table's own data-source methods, with a drag stand-in that
    // answers what they read.
    unsafe {
        let op: NSDragOperation = msg_send![&*ds, tableView: table, validateDrop: info, proposedRow: row, proposedDropOperation: NSTableViewDropOperation::Above];
        if op == NSDragOperation::None {
            return false;
        }
        msg_send![&*ds, tableView: table, acceptDrop: info, row: row, dropOperation: NSTableViewDropOperation::Above]
    }
}

/// Drops whatever is on `pasteboard` onto `view` (a drag destination) as
/// AppKit does: `draggingEntered:`, then `performDragOperation:` if it
/// offered an operation. Whether the drop was taken.
pub fn drop_on_view(view: &NSView, pasteboard: &NSPasteboard) -> bool {
    let drag = TestDrag::alloc(mtm()).set_ivars(DragIvars { pasteboard: pasteboard.retain(), source: None });
    // SAFETY: NSObject's designated initialiser.
    let drag: Retained<TestDrag> = unsafe { msg_send![super(drag), init] };
    let info: &ProtocolObject<dyn NSDraggingInfo> = ProtocolObject::from_ref(&*drag);
    // SAFETY: the view's NSDraggingDestination methods, with a drag
    // stand-in that answers what they read.
    unsafe {
        let op: NSDragOperation = msg_send![view, draggingEntered: info];
        if op == NSDragOperation::None {
            return false;
        }
        msg_send![view, performDragOperation: info]
    }
}

/// Starts a drag of `table`'s view rows `rows` the way AppKit does (asking
/// the data source for each row's pasteboard writer) and puts them on a
/// private pasteboard. `None` if the table doesn't let rows be dragged.
pub fn drag_rows(table: &NSTableView, rows: &[usize]) -> Option<Retained<NSPasteboard>> {
    let ds = unsafe { table.dataSource() }?;
    let mut writers: Vec<Retained<ProtocolObject<dyn NSPasteboardWriting>>> = Vec::new();
    for &r in rows {
        // SAFETY: the data source's tableView:pasteboardWriterForRow:.
        let w: Option<Retained<ProtocolObject<dyn NSPasteboardWriting>>> =
            unsafe { msg_send![&*ds, tableView: table, pasteboardWriterForRow: r as NSInteger] };
        writers.push(w?);
    }
    let pasteboard = private_pasteboard();
    pasteboard.clearContents();
    pasteboard.writeObjects(&NSArray::from_retained_slice(&writers));
    Some(pasteboard)
}

/// A private pasteboard holding file URLs, as a Finder drag carries them.
pub fn files_pasteboard(paths: &[&std::path::Path]) -> Retained<NSPasteboard> {
    let pasteboard = private_pasteboard();
    pasteboard.clearContents();
    let items: Vec<Retained<ProtocolObject<dyn NSPasteboardWriting>>> = paths
        .iter()
        .map(|p| {
            let item = NSPasteboardItem::new();
            let url = NSURL::fileURLWithPath(&NSString::from_str(&p.to_string_lossy()));
            if let Some(s) = url.absoluteString() {
                // SAFETY: AppKit's pasteboard type constant.
                item.setString_forType(&s, unsafe { NSPasteboardTypeFileURL });
            }
            ProtocolObject::from_retained(item)
        })
        .collect();
    pasteboard.writeObjects(&NSArray::from_retained_slice(&items));
    pasteboard
}

/// A private pasteboard holding web addresses, as a link dragged from a
/// browser carries them (the address, and its text).
pub fn urls_pasteboard(urls: &[&str]) -> Retained<NSPasteboard> {
    let pasteboard = private_pasteboard();
    pasteboard.clearContents();
    let items: Vec<Retained<ProtocolObject<dyn NSPasteboardWriting>>> = urls
        .iter()
        .map(|u| {
            let item = NSPasteboardItem::new();
            // SAFETY: AppKit's pasteboard type constants.
            item.setString_forType(&NSString::from_str(u), unsafe { objc2_app_kit::NSPasteboardTypeURL });
            item.setString_forType(&NSString::from_str(u), unsafe { NSPasteboardTypeString });
            ProtocolObject::from_retained(item)
        })
        .collect();
    pasteboard.writeObjects(&NSArray::from_retained_slice(&items));
    pasteboard
}

/// The plain text on `pasteboard`.
pub fn pasteboard_text(pasteboard: &NSPasteboard) -> Option<String> {
    // SAFETY: AppKit's pasteboard type constant.
    pasteboard.stringForType(unsafe { NSPasteboardTypeString }).map(|s| s.to_string())
}

/// Puts `text` on `pasteboard`.
pub fn set_pasteboard_text(pasteboard: &NSPasteboard, text: &str) {
    pasteboard.clearContents();
    // SAFETY: AppKit's pasteboard type constant.
    pasteboard.setString_forType(&NSString::from_str(text), unsafe { NSPasteboardTypeString });
}