Skip to main content

mitsuami_appkit/
tweak.rs

1//! Raw AppKit settings for built-in widgets, past their semantic props.
2
3use std::rc::Rc;
4
5use mitsuami_core::reactive::{IntoValue, Value};
6use mitsuami_core::{List, Opaque, Table, Tweak};
7use mitsuami_widgets::{
8    Button, Checkbox, FileIcon, Group, Icon, Image, MenuButton, NumberInput, PasswordInput, Progress, RadioGroup,
9    ScrollView, SearchInput, Select, Separator, Slider, Spinner, Switch, Text, TextArea, TextInput, ToggleButton,
10};
11use mitsuami_widgets::{DocumentTabs, Sidebar};
12use objc2::DowncastTarget;
13use objc2_app_kit::{
14    NSBox, NSButton, NSImageView, NSPopUpButton, NSProgressIndicator, NSScrollView, NSSearchField, NSSecureTextField,
15    NSSlider, NSStackView, NSSwitch, NSTableView, NSTextField, NSTextView, NSView,
16};
17
18/// A built-in widget, and the AppKit control that shows it.
19pub trait Tweakable {
20    type Native: DowncastTarget;
21}
22
23impl Tweakable for Button {
24    type Native = NSButton;
25}
26
27impl Tweakable for ToggleButton {
28    type Native = NSButton;
29}
30
31impl Tweakable for Checkbox {
32    type Native = NSButton;
33}
34
35impl Tweakable for Switch {
36    type Native = NSSwitch;
37}
38
39impl Tweakable for Select {
40    type Native = NSPopUpButton;
41}
42
43/// The stack view its radio buttons are in.
44impl Tweakable for RadioGroup {
45    type Native = NSStackView;
46}
47
48impl Tweakable for Slider {
49    type Native = NSSlider;
50}
51
52impl Tweakable for NumberInput {
53    type Native = crate::NumberField;
54}
55
56impl Tweakable for Image {
57    type Native = NSImageView;
58}
59
60/// The box behind its children (the node's own view is a layout host).
61impl Tweakable for Group {
62    type Native = NSBox;
63}
64
65/// The pull-down pop-up button.
66impl Tweakable for MenuButton {
67    type Native = NSPopUpButton;
68}
69
70impl Tweakable for Icon {
71    type Native = NSImageView;
72}
73
74impl Tweakable for FileIcon {
75    type Native = NSImageView;
76}
77
78impl Tweakable for Progress {
79    type Native = NSProgressIndicator;
80}
81
82impl Tweakable for Spinner {
83    type Native = NSProgressIndicator;
84}
85
86/// A separator `NSBox`.
87impl Tweakable for Separator {
88    type Native = NSBox;
89}
90
91impl Tweakable for TextInput {
92    type Native = NSTextField;
93}
94
95impl Tweakable for PasswordInput {
96    type Native = NSSecureTextField;
97}
98
99impl Tweakable for SearchInput {
100    type Native = NSSearchField;
101}
102
103/// The text view, not the scroll view around it: reach that with
104/// `enclosingScrollView`.
105impl Tweakable for TextArea {
106    type Native = NSTextView;
107}
108
109impl Tweakable for ScrollView {
110    type Native = NSScrollView;
111}
112
113impl Tweakable for List {
114    type Native = NSTableView;
115}
116
117/// The table view, not the scroll view around it.
118impl Tweakable for Table {
119    type Native = NSTableView;
120}
121
122/// The source list's table view, not the scroll view around it: the split
123/// view that sizes it is the window's.
124impl<T> Tweakable for Sidebar<T> {
125    type Native = NSTableView;
126}
127
128/// The strip's view.
129impl<T> Tweakable for DocumentTabs<T> {
130    type Native = NSView;
131}
132
133impl Tweakable for Text {
134    type Native = NSTextField;
135}
136
137/// Settings as the backend runs them, on the node's view.
138pub(crate) type TweakFn = Rc<dyn Fn(&NSView)>;
139
140/// Raw settings for a built-in widget's AppKit control:
141/// `appkit::tweak(|b: &NSButton| b.setControlSize(NSControlSize::Large))`.
142/// See [`Tweak`].
143pub fn tweak<W: Tweakable>(apply: impl Fn(&W::Native) + 'static) -> Tweak<W> {
144    tweak_with(Value::Static(()), move |native, ()| apply(native))
145}
146
147/// Raw settings made from a value, applied again whenever it changes:
148/// `appkit::tweak_with(tint, |b: &NSButton, tint| ..)`.
149pub fn tweak_with<W: Tweakable, T: Clone + 'static>(
150    value: impl IntoValue<T>,
151    apply: impl Fn(&W::Native, &T) + 'static,
152) -> Tweak<W> {
153    let apply = Rc::new(apply);
154    Tweak::new(value.into_value(), move |value| {
155        let apply = apply.clone();
156        let run: TweakFn = Rc::new(move |view: &NSView| {
157            if let Some(native) = view.downcast_ref::<W::Native>() {
158                apply(native, &value);
159            }
160        });
161        Opaque::new("appkit tweak", run)
162    })
163}