1use 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
19pub 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
44impl 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
65impl Tweakable for Group {
67 type Native = NSBox;
68}
69
70impl Tweakable for Split {
71 type Native = NSSplitView;
72}
73
74impl Tweakable for MenuButton {
76 type Native = NSPopUpButton;
77}
78
79impl 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
100impl 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
117impl 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
131impl Tweakable for Table {
133 type Native = NSTableView;
134}
135
136impl<T> Tweakable for Sidebar<T> {
139 type Native = NSTableView;
140}
141
142impl<T> Tweakable for DocumentTabs<T> {
144 type Native = NSView;
145}
146
147impl Tweakable for Text {
148 type Native = NSTextField;
149}
150
151pub(crate) type TweakFn = Rc<dyn Fn(&NSView)>;
153
154pub fn tweak<W: Tweakable>(apply: impl Fn(&W::Native) + 'static) -> Tweak<W> {
158 tweak_with(Value::Static(()), move |native, ()| apply(native))
159}
160
161pub 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}