ferrokit 0.15.1

Native macOS GUI framework for Rust: every view a real AppKit control (NSTableView, NSOutlineView, NSTextView), updated in place by fine-grained signals
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! AppKit's own views for the node tree: text and labels, buttons, the
//! divider and the scroll view. The core (`view`) knows them only as
//! nodes; a second platform would provide its own (see `platform`).

use std::rc::{Rc, Weak};

use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObject};
use objc2::{DefinedClass, MainThreadMarker, MainThreadOnly, define_class, msg_send, sel};
use objc2_app_kit::{NSBox, NSBoxType, NSButton, NSResponder, NSScrollView, NSTextField, NSView};
use objc2_foundation::NSString;

use crate::signal::Effect;
use crate::view::{AnyView, Cx, Kind, LeafHandle, Meta, Node, Size, View, invalidate_layout};

/// A thin separator line across its stack: horizontal in a vertical stack,
/// vertical in a horizontal one.
pub struct Divider;

impl View for Divider {
    fn build(self, cx: &mut Cx) -> Node {
        let line = NSBox::new(cx.mtm);
        line.setBoxType(NSBoxType::Separator);
        let (mut node, _) = Node::leaf(cx, Retained::into_super(line), (false, false), Size { w: 1.0, h: 1.0 });
        if let Kind::Leaf(l) = &mut node.kind {
            l.cross_only = true;
            l.content.set(Some(Size { w: 1.0, h: 1.0 }));
        }
        node
    }
}

/// A separator line across its stack.
pub fn divider() -> Divider {
    Divider
}

define_class!(
    // A scroll view's document view: flipped, so content starts at the top.
    #[unsafe(super(NSView, NSResponder, NSObject))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitFlippedView"]
    struct FlippedView;

    impl FlippedView {
        #[unsafe(method(isFlipped))]
        fn is_flipped(&self) -> bool {
            true
        }
    }
);

/// Vertically scrolling content (`NSScrollView`). The content is laid out at
/// the visible width and at least the visible height.
pub struct Scroll(AnyView);

impl View for Scroll {
    fn build(self, cx: &mut Cx) -> Node {
        let scroll = NSScrollView::new(cx.mtm);
        scroll.setHasVerticalScroller(true);
        scroll.setAutohidesScrollers(true);
        scroll.setDrawsBackground(false);
        // SAFETY: NSView's designated initialiser.
        let doc: Retained<FlippedView> = unsafe { msg_send![FlippedView::alloc(cx.mtm), init] };
        let doc: Retained<NSView> = Retained::into_super(doc);
        scroll.setDocumentView(Some(&doc));
        cx.container.addSubview(&scroll);
        let content = self.0.build_node(&mut Cx::new(doc.clone()));
        invalidate_layout();
        let clip = scroll.contentView();
        let mut node = Node::new(Kind::Scroll { scroll, doc: doc.clone(), content: Box::new(content) });
        // Classic scroll bars appear and go when AppKit tiles the view (also
        // while drawing, after our layout): re-fit the content when the
        // visible width no longer matches it.
        let observer = ClipObserver::alloc(cx.mtm).set_ivars(ClipWatch { meta: Rc::downgrade(&node.meta), doc: objc2::rc::Weak::from_retained(&doc) });
        // SAFETY: NSObject's designated initialiser.
        let observer: Retained<ClipObserver> = unsafe { msg_send![super(observer), init] };
        clip.setPostsFrameChangedNotifications(true);
        // SAFETY: the observer answers clipChanged:; the node keeps it, and
        // the notification centre doesn't retain selector observers.
        unsafe {
            objc2_foundation::NSNotificationCenter::defaultCenter().addObserver_selector_name_object(
                &observer,
                objc2::sel!(clipChanged:),
                Some(objc2_app_kit::NSViewFrameDidChangeNotification),
                Some(&clip),
            )
        };
        node.hold(Rc::new(Observing(observer)));
        node
    }
}

/// Unregisters a notification observer when its node goes (the centre
/// would otherwise keep its record of it).
struct Observing(Retained<ClipObserver>);

impl Drop for Observing {
    fn drop(&mut self) {
        // SAFETY: removing an observer this module registered.
        unsafe { objc2_foundation::NSNotificationCenter::defaultCenter().removeObserver(&self.0) };
    }
}

struct ClipWatch {
    meta: Weak<Meta>,
    doc: objc2::rc::Weak<NSView>,
}

define_class!(
    // Watches a scroll view's visible area for size changes.
    #[unsafe(super(NSObject))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitClipObserver"]
    #[ivars = ClipWatch]
    struct ClipObserver;

    impl ClipObserver {
        #[unsafe(method(clipChanged:))]
        fn clip_changed(&self, note: &objc2_foundation::NSNotification) {
            crate::failure::guard("a view (clip_changed)", (), || {
                let (Some(clip), Some(doc)) = (note.object().and_then(|o| o.downcast::<NSView>().ok()), self.ivars().doc.load()) else { return };
                if (clip.frame().size.width - doc.frame().size.width).abs() < 0.25 || crate::in_layout() {
                    return;
                }
                if let Some(meta) = self.ivars().meta.upgrade() {
                    meta.mark();
                    crate::later(crate::relayout_now);
                }
            })
        }
    }
);

/// Vertically scrolling `content`.
pub fn scroll(content: impl View) -> Scroll {
    Scroll(AnyView::new(content))
}

/// After a content change: re-measure, and mark the leaf (and its
/// ancestors) for layout only if its size changed.
pub(crate) fn remeasure(view: &NSView, leaf: &LeafHandle) {
    let s = view.fittingSize();
    let new = Size { w: s.width, h: s.height };
    if leaf.content.get() != Some(new) {
        leaf.content.set(Some(new));
        leaf.meta.mark();
    }
}

/// How a label's text looks: its font.
#[derive(Clone, Default)]
struct TextLook {
    font: Option<crate::Font>,
}

impl TextLook {
    fn apply(&self, field: &NSTextField) {
        if let Some(font) = &self.font {
            field.setFont(Some(&font.ns()));
        }
    }
}

/// Text (see [`text`]).
pub struct Text {
    content: crate::Value<String>,
    look: TextLook,
    color: Option<crate::Value<crate::Color>>,
    truncates: bool,
}

impl Text {
    /// Its font: a text style (`Font::title()`, `Font::caption()`) or a
    /// custom one.
    pub fn font(mut self, font: crate::Font) -> Self {
        self.look.font = Some(font);
        self
    }

    /// Its colour (`Color::Secondary` for less prominent text), or a closure
    /// computing it from signals (a balance red when it's negative).
    pub fn color(mut self, color: impl crate::IntoValue<crate::Color>) -> Self {
        self.color = Some(color.into_value());
        self
    }

    /// On one line, shortened with "…" when there isn't room for it all,
    /// instead of pushing past its row (a file's path, a long value). With
    /// `.min_width(w)` it shrinks to `w` and no further, so a status line
    /// beside a fixed-width row stays readable.
    pub fn truncate(mut self) -> Self {
        self.truncates = true;
        self
    }
}

define_class!(
    // A label that prints its text once: AppKit's labels write their text
    // into a printed PDF twice, a few points apart, which breaks searching
    // and copying the PDF (seen on macOS 27).
    #[unsafe(super(NSTextField, objc2_app_kit::NSControl, NSView, NSResponder, NSObject))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitLabel"]
    struct PrintableLabel;

    impl PrintableLabel {
        #[unsafe(method(drawRect:))]
        fn draw_rect(&self, dirty: objc2_foundation::NSRect) {
            crate::failure::guard("a view (draw_rect)", (), || {
                if objc2_app_kit::NSGraphicsContext::currentContextDrawingToScreen() {
                    // SAFETY: NSTextField's own drawing.
                    let _: () = unsafe { msg_send![super(self), drawRect: dirty] };
                    return;
                }
                let Some(cell) = self.cell() else { return };
                let mut r = cell.drawingRectForBounds(self.bounds());
                // The cell's text container pads each side by 2 points.
                r.origin.x += 2.0;
                r.size.width -= 4.0;
                let options = objc2_app_kit::NSStringDrawingOptions::UsesLineFragmentOrigin | objc2_app_kit::NSStringDrawingOptions::TruncatesLastVisibleLine;
                objc2_app_kit::NSAttributedStringNSExtendedStringDrawing::drawWithRect_options_context(&*self.attributedStringValue(), r, options, None);
            })
        }
    }
);

/// A label (`NSTextField::labelWithString`) that prints its text once.
pub(crate) fn printable_label(text: &str, mtm: MainThreadMarker) -> Retained<NSTextField> {
    let _ = mtm;
    // SAFETY: NSTextField's class constructor, called on the subclass.
    let field: Retained<PrintableLabel> = unsafe { msg_send![<PrintableLabel as objc2::ClassType>::class(), labelWithString: &*NSString::from_str(text)] };
    Retained::into_super(field)
}

impl View for Text {
    fn build(self, cx: &mut Cx) -> Node {
        let field = printable_label("", cx.mtm);
        self.look.apply(&field);
        if self.truncates {
            field.setLineBreakMode(objc2_app_kit::NSLineBreakMode::ByTruncatingTail);
            field.setMaximumNumberOfLines(1);
        }
        let view: Retained<NSView> = Retained::into_super(Retained::into_super(field.clone()));
        let (mut node, handle) = Node::leaf(cx, view.clone(), (false, false), Size::default());
        if self.truncates {
            node.shrinks = true;
        }
        match self.content {
            crate::Value::Fixed(s) => field.setStringValue(&NSString::from_str(&s)),
            content => {
                let fld = field.clone();
                node.own(Effect::new(move || {
                    fld.setStringValue(&NSString::from_str(&content.get()));
                    remeasure(&view, &handle);
                }));
            }
        }
        match self.color {
            Some(crate::Value::Fixed(c)) => field.setTextColor(Some(&c.ns())),
            Some(color) => node.own(Effect::new(move || field.setTextColor(Some(&color.get().ns())))),
            None => {}
        }
        node
    }
}

/// Text: fixed (`text("Packing list")`), or computed from signals and
/// updated in place (`text(move || format!("{} left", left.get()))`).
pub fn text(content: impl crate::IntoValue<String>) -> Text {
    Text { content: content.into_value(), look: TextLook::default(), color: None, truncates: false }
}

define_class!(
    #[unsafe(super(NSObject))]
    #[thread_kind = MainThreadOnly]
    #[name = "FerrokitAction"]
    #[ivars = Box<dyn Fn()>]
    pub(crate) struct ActionTarget;

    impl ActionTarget {
        #[unsafe(method(fire:))]
        fn fire(&self, _sender: Option<&AnyObject>) {
            crate::models::flush();
            // Kept for the call: an action that closes its own window
            // drops the node that holds this target, and with it the
            // closure that is running.
            let _keep = objc2::Message::retain(self);
            crate::failure::entry("a control's action", (), || (self.ivars())());
        }
    }
);

impl ActionTarget {
    pub(crate) fn new(mtm: MainThreadMarker, f: impl Fn() + 'static) -> Retained<Self> {
        let this = Self::alloc(mtm).set_ivars(Box::new(crate::signal::with_current_owner(f)) as Box<dyn Fn()>);
        // SAFETY: NSObject's designated initialiser.
        unsafe { msg_send![super(this), init] }
    }
}

/// How a button is drawn ([`Button::style`], [`MenuButton::style`]).
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub enum ButtonStyle {
    /// The standard rounded push button.
    #[default]
    Standard,
    /// No bezel: the title or symbol alone, highlighted when pressed.
    Borderless,
    /// A small capsule, for buttons inside text or a list row.
    Inline,
    /// A bezel that shows only on hover and press, for filter bars
    /// (Mail's "All" and "Unread" above a list).
    Recessed,
    /// A round button, for a symbol alone.
    Circular,
    /// The toolbar's look, for buttons in a bar of the content.
    Toolbar,
    /// Liquid Glass (macOS 26); the standard style before it.
    Glass,
}

impl ButtonStyle {
    fn apply(self, b: &NSButton) {
        use objc2_app_kit::NSBezelStyle;
        match self {
            ButtonStyle::Standard => b.setBezelStyle(NSBezelStyle::Push),
            ButtonStyle::Borderless => b.setBordered(false),
            ButtonStyle::Inline => b.setBezelStyle(NSBezelStyle::Badge),
            ButtonStyle::Recessed => b.setBezelStyle(NSBezelStyle::AccessoryBar),
            ButtonStyle::Circular => b.setBezelStyle(NSBezelStyle::Circular),
            ButtonStyle::Toolbar => b.setBezelStyle(NSBezelStyle::Toolbar),
            ButtonStyle::Glass => b.setBezelStyle(if crate::has_liquid_glass() { NSBezelStyle::Glass } else { NSBezelStyle::Push }),
        }
    }
}

/// A control's size, as in Apple's apps: small controls in inspectors and
/// dense panels, large ones for a welcome window's main actions.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub enum ControlSize {
    /// The smallest, for tight spaces.
    Mini,
    /// Inspectors and panels.
    Small,
    /// The usual size.
    #[default]
    Regular,
    /// Prominent actions.
    Large,
}

impl ControlSize {
    pub(crate) fn ns(self) -> objc2_app_kit::NSControlSize {
        use objc2_app_kit::NSControlSize;
        match self {
            ControlSize::Mini => NSControlSize::Mini,
            ControlSize::Small => NSControlSize::Small,
            ControlSize::Regular => NSControlSize::Regular,
            ControlSize::Large => NSControlSize::Large,
        }
    }
}

/// A push button.
pub struct Button {
    title: crate::Value<String>,
    action: Box<dyn Fn()>,
    disabled: Option<crate::Value<bool>>,
    default_action: bool,
    cancel_action: bool,
    symbol: Option<crate::Value<String>>,
    style: ButtonStyle,
    size: ControlSize,
    destructive: bool,
}

impl Button {
    /// Disables the button: `true`, or a closure reading signals (disabled
    /// while it returns true).
    pub fn disabled(mut self, when: impl crate::IntoValue<bool>) -> Self {
        self.disabled = Some(when.into_value());
        self
    }

    /// An SF Symbol on the button, before its title, or alone when the
    /// title is empty (the title is then what VoiceOver reads; give it one
    /// with `accessibility_label`). A closure changes it with signals: Play
    /// and Pause, `move || if playing.get() { "pause.fill" } else { "play.fill" }`.
    pub fn symbol(mut self, name: impl crate::IntoValue<String>) -> Self {
        self.symbol = Some(name.into_value());
        self
    }

    /// Makes this the window's default button: blue, and Return presses it.
    pub fn default_action(mut self) -> Self {
        self.default_action = true;
        self
    }

    /// Makes this the window's cancel button: Escape presses it, as in a
    /// sheet's Cancel.
    pub fn cancel_action(mut self) -> Self {
        self.cancel_action = true;
        self
    }

    /// How it's drawn: borderless, inline, recessed, circular, toolbar,
    /// glass.
    pub fn style(mut self, style: ButtonStyle) -> Self {
        self.style = style;
        self
    }

    /// Mini, small, regular or large.
    pub fn control_size(mut self, size: ControlSize) -> Self {
        self.size = size;
        self
    }

    /// Marks the action as destructive (Delete, Discard): AppKit draws it in
    /// red where the style allows, and it's never pressed by Return.
    pub fn destructive(mut self) -> Self {
        self.destructive = true;
        self
    }
}

impl View for Button {
    fn build(self, cx: &mut Cx) -> Node {
        let target = ActionTarget::new(cx.mtm, self.action);
        // SAFETY: the target responds to `fire:` and the node keeps it alive.
        let button = unsafe {
            NSButton::buttonWithTitle_target_action(&NSString::from_str(&self.title.get()), Some(&target), Some(sel!(fire:)), cx.mtm)
        };
        if self.default_action {
            button.setKeyEquivalent(&NSString::from_str("\r"));
        } else if self.cancel_action {
            button.setKeyEquivalent(&NSString::from_str("\u{1b}"));
        }
        self.style.apply(&button);
        button.setControlSize(self.size.ns());
        if self.destructive {
            button.setHasDestructiveAction(true);
        }
        let view: Retained<NSView> = Retained::into_super(Retained::into_super(button.clone()));
        let (mut node, handle) = Node::leaf(cx, view.clone(), (false, false), Size::default());
        // Title and symbol, set again (and the button measured again) when
        // their signals change.
        let (title, symbol, b) = (self.title, self.symbol, button.clone());
        if !title.is_fixed() || symbol.is_some() {
            node.own(Effect::new(move || {
                let t = title.get();
                b.setTitle(&NSString::from_str(&t));
                if let Some(name) = symbol.as_ref().map(|s| s.get()) {
                    let image = objc2_app_kit::NSImage::imageWithSystemSymbolName_accessibilityDescription(&NSString::from_str(&name), Some(&NSString::from_str(&t)));
                    b.setImage(image.as_deref());
                    b.setImagePosition(if t.is_empty() { objc2_app_kit::NSCellImagePosition::ImageOnly } else { objc2_app_kit::NSCellImagePosition::ImageLeading });
                }
                remeasure(&view, &handle);
            }));
        }
        // Its own state, under that of the views above it.
        if let Some(disabled) = self.disabled {
            let (meta, b) = (node.meta.clone(), button.clone());
            node.own(Effect::new(move || crate::view::set_control_disabled(&meta, &b, disabled.get())));
        }
        node.keep(Retained::into_super(target));
        node
    }
}

/// A push button; `action` runs inside a batch. The title is fixed, or a
/// closure computing it from signals ("Show Sidebar" or "Hide Sidebar").
pub fn button(title: impl crate::IntoValue<String>, action: impl Fn() + 'static) -> Button {
    Button {
        title: title.into_value(),
        action: Box::new(action),
        disabled: None,
        default_action: false,
        cancel_action: false,
        symbol: None,
        style: ButtonStyle::Standard,
        size: ControlSize::Regular,
        destructive: false,
    }
}

/// The round "?" button that opens help for the window or panel it's in
/// (put it in the bottom corner of a settings pane or sheet). VoiceOver
/// reads it as "Help".
pub fn help_button(action: impl Fn() + 'static) -> HelpButton {
    HelpButton(Box::new(action))
}

/// See [`help_button`].
pub struct HelpButton(Box<dyn Fn()>);

impl View for HelpButton {
    fn build(self, cx: &mut Cx) -> Node {
        let target = ActionTarget::new(cx.mtm, self.0);
        // SAFETY: the target responds to `fire:` and the node keeps it alive.
        let button = unsafe { NSButton::buttonWithTitle_target_action(&NSString::from_str(""), Some(&target), Some(sel!(fire:)), cx.mtm) };
        button.setBezelStyle(objc2_app_kit::NSBezelStyle::HelpButton);
        objc2_app_kit::NSAccessibility::setAccessibilityLabel(&*button, Some(&NSString::from_str(&crate::t("Help"))));
        let (mut node, _) = Node::leaf(cx, Retained::into_super(Retained::into_super(button)), (false, false), Size::default());
        node.keep(Retained::into_super(target));
        node
    }
}

/// A button showing a menu of actions when clicked (a pull-down button:
/// "Export" with "CSV…", "JSON…", "PDF…"). The items are menu items, as in
/// the menu bar: enabled, checked and with key equivalents.
pub fn menu_button(title: &str, items: Vec<crate::MenuItem>) -> MenuButton {
    MenuButton { title: title.into(), items, symbol: None, style: ButtonStyle::Standard, size: ControlSize::Regular, disabled: None }
}

/// See [`menu_button`].
pub struct MenuButton {
    title: String,
    items: Vec<crate::MenuItem>,
    symbol: Option<String>,
    style: ButtonStyle,
    size: ControlSize,
    disabled: Option<crate::Value<bool>>,
}

impl MenuButton {
    /// An SF Symbol before the title, or alone when the title is empty
    /// (give it an `accessibility_label` then): `"ellipsis.circle"` for a
    /// "More" menu.
    pub fn symbol(mut self, name: &str) -> Self {
        self.symbol = Some(name.into());
        self
    }

    /// How it's drawn ([`ButtonStyle`]); borderless suits a symbol alone.
    pub fn style(mut self, style: ButtonStyle) -> Self {
        self.style = style;
        self
    }

    /// Mini, small, regular or large.
    pub fn control_size(mut self, size: ControlSize) -> Self {
        self.size = size;
        self
    }

    /// Disables it: `true`, or a closure reading signals.
    pub fn disabled(mut self, when: impl crate::IntoValue<bool>) -> Self {
        self.disabled = Some(when.into_value());
        self
    }
}

impl View for MenuButton {
    fn build(self, cx: &mut Cx) -> Node {
        let mtm = cx.mtm;
        let button = objc2_app_kit::NSPopUpButton::initWithFrame_pullsDown(objc2_app_kit::NSPopUpButton::alloc(mtm), objc2_foundation::NSRect::ZERO, true);
        let (menu, targets) = crate::chrome::build_menu(mtm, self.items);
        // A pull-down button shows its first item as its title.
        let head = objc2_app_kit::NSMenuItem::new(mtm);
        head.setTitle(&NSString::from_str(&self.title));
        if let Some(name) = &self.symbol {
            head.setImage(objc2_app_kit::NSImage::imageWithSystemSymbolName_accessibilityDescription(&NSString::from_str(name), Some(&NSString::from_str(&self.title))).as_deref());
        }
        menu.insertItem_atIndex(&head, 0);
        button.setMenu(Some(&menu));
        if self.symbol.is_some() && self.title.is_empty() {
            button.setImagePosition(objc2_app_kit::NSCellImagePosition::ImageOnly);
        }
        self.style.apply(&button);
        button.setControlSize(self.size.ns());
        let view: Retained<NSView> = Retained::into_super(Retained::into_super(Retained::into_super(button.clone())));
        let (mut node, _) = Node::leaf(cx, view, (false, false), Size::default());
        // Its own state, under that of the views above it.
        if let Some(disabled) = self.disabled {
            let (meta, b) = (node.meta.clone(), button.clone());
            node.own(Effect::new(move || crate::view::set_control_disabled(&meta, &b, disabled.get())));
        }
        for t in targets {
            node.keep(t);
        }
        node
    }
}