Skip to main content

iced_window_chrome/
settings.rs

1use iced::Color;
2use std::hash::{Hash, Hasher};
3
4/// Cross-platform chrome settings. Each platform reads only its own section.
5#[derive(Debug, Clone, PartialEq, Default)]
6pub struct ChromeSettings {
7    pub windows: WindowsChromeSettings,
8    pub macos: MacosChromeSettings,
9    pub linux: LinuxChromeSettings,
10}
11
12impl Hash for ChromeSettings {
13    fn hash<H: Hasher>(&self, state: &mut H) {
14        self.windows.hash(state);
15        self.macos.hash(state);
16        self.linux.hash(state);
17    }
18}
19
20/// Windows caption button visibility.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub struct CaptionButtons {
23    pub close: bool,
24    pub minimize: bool,
25    pub maximize: bool,
26}
27
28impl Default for CaptionButtons {
29    fn default() -> Self {
30        Self {
31            close: true,
32            minimize: true,
33            maximize: true,
34        }
35    }
36}
37
38/// Windows 11 corner preference.
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
40pub enum WindowCornerPreference {
41    Default,
42    DoNotRound,
43    Round,
44    RoundSmall,
45}
46
47/// System-managed Windows backdrop material.
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
49pub enum WindowsBackdrop {
50    None,
51    Mica,
52    Acrylic,
53    MicaAlt,
54}
55
56/// Native macOS titlebar separator style.
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
58pub enum MacosTitlebarSeparatorStyle {
59    Automatic,
60    None,
61    Line,
62    Shadow,
63}
64
65/// Native Linux/X11 window manager chrome settings.
66#[derive(Debug, Clone, PartialEq)]
67pub struct LinuxChromeSettings {
68    pub decorations: bool,
69    pub buttons: CaptionButtons,
70}
71
72impl Default for LinuxChromeSettings {
73    fn default() -> Self {
74        Self {
75            decorations: true,
76            buttons: CaptionButtons::default(),
77        }
78    }
79}
80
81impl Hash for LinuxChromeSettings {
82    fn hash<H: Hasher>(&self, state: &mut H) {
83        self.decorations.hash(state);
84        self.buttons.hash(state);
85    }
86}
87
88/// Native Windows window chrome settings.
89#[derive(Debug, Clone, PartialEq)]
90pub struct WindowsChromeSettings {
91    pub caption: bool,
92    pub border: bool,
93    pub buttons: CaptionButtons,
94    pub corner_preference: Option<WindowCornerPreference>,
95    pub border_color: Option<Color>,
96    pub title_background_color: Option<Color>,
97    pub title_text_color: Option<Color>,
98    pub backdrop: Option<WindowsBackdrop>,
99}
100
101impl Default for WindowsChromeSettings {
102    fn default() -> Self {
103        Self {
104            caption: true,
105            border: true,
106            buttons: CaptionButtons::default(),
107            corner_preference: None,
108            border_color: None,
109            title_background_color: None,
110            title_text_color: None,
111            backdrop: None,
112        }
113    }
114}
115
116impl Hash for WindowsChromeSettings {
117    fn hash<H: Hasher>(&self, state: &mut H) {
118        self.caption.hash(state);
119        self.border.hash(state);
120        self.buttons.hash(state);
121        self.corner_preference.hash(state);
122        hash_color(self.border_color, state);
123        hash_color(self.title_background_color, state);
124        hash_color(self.title_text_color, state);
125        self.backdrop.hash(state);
126    }
127}
128
129/// Native macOS titlebar settings.
130#[derive(Debug, Clone, PartialEq)]
131pub struct MacosChromeSettings {
132    pub titlebar: bool,
133    pub title: bool,
134    pub traffic_lights: bool,
135    pub titlebar_transparent: bool,
136    pub fullsize_content_view: bool,
137    pub titlebar_height: Option<f64>,
138    pub traffic_light_offset_y: Option<f64>,
139    pub titlebar_separator_style: Option<MacosTitlebarSeparatorStyle>,
140}
141
142impl Default for MacosChromeSettings {
143    fn default() -> Self {
144        Self {
145            titlebar: true,
146            title: true,
147            traffic_lights: true,
148            titlebar_transparent: false,
149            fullsize_content_view: false,
150            titlebar_height: None,
151            traffic_light_offset_y: None,
152            titlebar_separator_style: None,
153        }
154    }
155}
156
157impl Hash for MacosChromeSettings {
158    fn hash<H: Hasher>(&self, state: &mut H) {
159        self.titlebar.hash(state);
160        self.title.hash(state);
161        self.traffic_lights.hash(state);
162        self.titlebar_transparent.hash(state);
163        self.fullsize_content_view.hash(state);
164        hash_f64(self.titlebar_height, state);
165        hash_f64(self.traffic_light_offset_y, state);
166        self.titlebar_separator_style.hash(state);
167    }
168}
169
170fn hash_color<H: Hasher>(color: Option<Color>, state: &mut H) {
171    match color {
172        Some(color) => {
173            true.hash(state);
174            color.r.to_bits().hash(state);
175            color.g.to_bits().hash(state);
176            color.b.to_bits().hash(state);
177            color.a.to_bits().hash(state);
178        }
179        None => false.hash(state),
180    }
181}
182
183fn hash_f64<H: Hasher>(value: Option<f64>, state: &mut H) {
184    match value {
185        Some(value) => {
186            true.hash(state);
187            value.to_bits().hash(state);
188        }
189        None => false.hash(state),
190    }
191}