Skip to main content

tauri_plugin_decor/
builder.rs

1use tauri::plugin::{Builder, TauriPlugin};
2use tauri::{Manager, Runtime};
3
4use crate::commands;
5use crate::decor::Decor;
6
7#[derive(Clone)]
8pub struct DecorPluginBuilder {
9    #[cfg(any(target_os = "windows", target_os = "linux"))]
10    titlebar_height: u32,
11    #[cfg(any(target_os = "windows", target_os = "linux"))]
12    button_width: u32,
13    #[cfg(any(target_os = "windows", target_os = "linux"))]
14    controls_scale: f64,
15    #[cfg(any(target_os = "windows", target_os = "linux"))]
16    auto_titlebar: bool,
17    #[cfg(target_os = "windows")]
18    snap_overlay_delay_ms: u64,
19    #[cfg(any(target_os = "windows", target_os = "linux"))]
20    close_hover_bg: String,
21    #[cfg(any(target_os = "windows", target_os = "linux"))]
22    button_hover_bg: String,
23    #[cfg(target_os = "macos")]
24    traffic_inset_x: f64,
25    #[cfg(target_os = "macos")]
26    traffic_inset_y: f64,
27    #[cfg(target_os = "macos")]
28    traffic_spacing: f64,
29    #[cfg(target_os = "macos")]
30    traffic_scale: f64,
31}
32
33impl Default for DecorPluginBuilder {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39impl DecorPluginBuilder {
40    pub fn new() -> Self {
41        Self {
42            #[cfg(any(target_os = "windows", target_os = "linux"))]
43            titlebar_height: 32,
44            #[cfg(any(target_os = "windows", target_os = "linux"))]
45            button_width: 46,
46            #[cfg(any(target_os = "windows", target_os = "linux"))]
47            controls_scale: 1.0,
48            #[cfg(any(target_os = "windows", target_os = "linux"))]
49            auto_titlebar: false,
50            #[cfg(target_os = "windows")]
51            snap_overlay_delay_ms: 10,
52            #[cfg(any(target_os = "windows", target_os = "linux"))]
53            close_hover_bg: "rgba(196,43,28,1)".into(),
54            #[cfg(any(target_os = "windows", target_os = "linux"))]
55            button_hover_bg: "rgba(0,0,0,0.2)".into(),
56            #[cfg(target_os = "macos")]
57            traffic_inset_x: 12.0,
58            #[cfg(target_os = "macos")]
59            traffic_inset_y: 16.0,
60            #[cfg(target_os = "macos")]
61            traffic_spacing: 20.0,
62            #[cfg(target_os = "macos")]
63            traffic_scale: 1.0,
64        }
65    }
66
67    pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
68        self.apply_to_config();
69
70        Builder::new("decor")
71            .invoke_handler(tauri::generate_handler![
72                commands::show_snap_overlay,
73                commands::update_style
74            ])
75            .setup(|app, _| {
76                app.manage(Decor::new(app.clone()));
77                Ok(())
78            })
79            .on_page_load(|win, _| crate::dispatch::on_page_load(win))
80            .on_window_ready(crate::dispatch::on_window_ready)
81            .build()
82    }
83
84    #[cfg(any(target_os = "windows", target_os = "linux"))]
85    fn apply_to_config(&self) {
86        crate::config::store(
87            self.titlebar_height,
88            self.button_width,
89            self.controls_scale,
90            self.auto_titlebar,
91            self.close_hover_bg.clone(),
92            self.button_hover_bg.clone(),
93        );
94        #[cfg(target_os = "windows")]
95        crate::config::store_snap(self.snap_overlay_delay_ms);
96    }
97
98    #[cfg(target_os = "macos")]
99    fn apply_to_config(&self) {
100        crate::config::store_traffic(
101            self.traffic_inset_x,
102            self.traffic_inset_y,
103            self.traffic_spacing,
104            self.traffic_scale,
105        );
106    }
107
108    #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
109    fn apply_to_config(&self) {}
110}
111
112#[cfg(any(target_os = "windows", target_os = "linux"))]
113impl DecorPluginBuilder {
114    pub fn controls_height(mut self, height: f64) -> Self {
115        self.titlebar_height = height.round().max(0.0) as u32;
116        self
117    }
118
119    pub fn controls_inset_x(self, _inset: f64) -> Self {
120        self
121    }
122
123    pub fn controls_spacing(self, _spacing: f64) -> Self {
124        self
125    }
126
127    pub fn controls_scale(mut self, scale: f64) -> Self {
128        self.controls_scale = scale;
129        self
130    }
131
132    pub fn controls_button_width(mut self, width: u32) -> Self {
133        self.button_width = width;
134        self
135    }
136
137    pub fn controls_close_hover_bg(mut self, color: impl Into<String>) -> Self {
138        self.close_hover_bg = color.into();
139        self
140    }
141
142    pub fn controls_button_hover_bg(mut self, color: impl Into<String>) -> Self {
143        self.button_hover_bg = color.into();
144        self
145    }
146
147    pub fn controls_auto_titlebar(mut self, auto: bool) -> Self {
148        self.auto_titlebar = auto;
149        self
150    }
151}
152
153#[cfg(target_os = "macos")]
154impl DecorPluginBuilder {
155    pub fn controls_height(mut self, height: f64) -> Self {
156        self.traffic_inset_y = height;
157        self
158    }
159
160    pub fn controls_inset_x(mut self, inset: f64) -> Self {
161        self.traffic_inset_x = inset;
162        self
163    }
164
165    pub fn controls_spacing(mut self, spacing: f64) -> Self {
166        self.traffic_spacing = spacing;
167        self
168    }
169
170    pub fn controls_scale(mut self, scale: f64) -> Self {
171        self.traffic_scale = scale;
172        self
173    }
174
175    pub fn controls_button_width(self, _width: u32) -> Self {
176        self
177    }
178
179    pub fn controls_close_hover_bg(self, _color: impl Into<String>) -> Self {
180        self
181    }
182
183    pub fn controls_button_hover_bg(self, _color: impl Into<String>) -> Self {
184        self
185    }
186
187    pub fn controls_auto_titlebar(self, _auto: bool) -> Self {
188        self
189    }
190}
191
192#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
193impl DecorPluginBuilder {
194    pub fn controls_height(self, _height: f64) -> Self {
195        self
196    }
197    pub fn controls_inset_x(self, _inset: f64) -> Self {
198        self
199    }
200    pub fn controls_spacing(self, _spacing: f64) -> Self {
201        self
202    }
203    pub fn controls_scale(self, _scale: f64) -> Self {
204        self
205    }
206    pub fn controls_button_width(self, _width: u32) -> Self {
207        self
208    }
209    pub fn controls_close_hover_bg(self, _color: impl Into<String>) -> Self {
210        self
211    }
212    pub fn controls_button_hover_bg(self, _color: impl Into<String>) -> Self {
213        self
214    }
215    pub fn controls_auto_titlebar(self, _auto: bool) -> Self {
216        self
217    }
218}
219
220#[cfg(target_os = "windows")]
221impl DecorPluginBuilder {
222    pub fn snap_overlay_delay_ms(mut self, delay: u64) -> Self {
223        self.snap_overlay_delay_ms = delay;
224        self
225    }
226}
227
228#[cfg(not(target_os = "windows"))]
229impl DecorPluginBuilder {
230    pub fn snap_overlay_delay_ms(self, _delay: u64) -> Self {
231        self
232    }
233}