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