1use 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
18pub 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
43impl 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
60impl Tweakable for Group {
62 type Native = NSBox;
63}
64
65impl 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
86impl 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
103impl 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
117impl Tweakable for Table {
119 type Native = NSTableView;
120}
121
122impl<T> Tweakable for Sidebar<T> {
125 type Native = NSTableView;
126}
127
128impl<T> Tweakable for DocumentTabs<T> {
130 type Native = NSView;
131}
132
133impl Tweakable for Text {
134 type Native = NSTextField;
135}
136
137pub(crate) type TweakFn = Rc<dyn Fn(&NSView)>;
139
140pub fn tweak<W: Tweakable>(apply: impl Fn(&W::Native) + 'static) -> Tweak<W> {
144 tweak_with(Value::Static(()), move |native, ()| apply(native))
145}
146
147pub 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}