1
1
use cosmic:: {
2
+ app,
3
+ cosmic_theme:: { CosmicPalette , ThemeBuilder } ,
2
4
iced_core:: text:: Wrapping ,
3
- theme,
5
+ theme:: { self , CosmicTheme } ,
4
6
widget:: { button, container, horizontal_space, icon, settings, text} ,
5
- Apply ,
7
+ Apply , Task ,
6
8
} ;
7
9
pub use cosmic_comp_config:: ZoomMovement ;
10
+ use cosmic_config:: CosmicConfigEntry ;
8
11
use cosmic_settings_page:: {
9
12
self as page,
10
13
section:: { self , Section } ,
@@ -14,6 +17,7 @@ use slotmap::SlotMap;
14
17
15
18
pub mod magnifier;
16
19
mod wayland;
20
+ use tokio:: task:: spawn_blocking;
17
21
pub use wayland:: { AccessibilityEvent , AccessibilityRequest } ;
18
22
19
23
#[ derive( Debug , Default ) ]
@@ -23,13 +27,17 @@ pub struct Page {
23
27
24
28
wayland_available : bool ,
25
29
wayland_thread : Option < wayland:: Sender > ,
30
+ theme : Box < cosmic:: cosmic_theme:: Theme > ,
31
+ high_contrast : Option < bool > ,
26
32
}
27
33
28
34
#[ derive( Debug , Clone ) ]
29
35
pub enum Message {
30
36
Event ( wayland:: AccessibilityEvent ) ,
31
37
ProtocolUnavailable ,
32
38
Return ,
39
+ HighContrast ( bool ) ,
40
+ SystemTheme ( Box < cosmic:: cosmic_theme:: Theme > ) ,
33
41
}
34
42
35
43
impl page:: Page < crate :: pages:: Message > for Page {
@@ -110,6 +118,7 @@ pub fn vision() -> section::Section<crate::pages::Message> {
110
118
on = fl!( "accessibility" , "on" ) ;
111
119
off = fl!( "accessibility" , "off" ) ;
112
120
unavailable = fl!( "accessibility" , "unavailable" ) ;
121
+ high_contrast = fl!( "accessibility" , "high-contrast" ) ;
113
122
} ) ;
114
123
115
124
Section :: default ( )
@@ -154,6 +163,13 @@ pub fn vision() -> section::Section<crate::pages::Message> {
154
163
. then_some ( crate :: pages:: Message :: Page ( magnifier_entity) ) ,
155
164
)
156
165
} )
166
+ . add (
167
+ cosmic:: Element :: from (
168
+ settings:: item:: builder ( & descriptions[ high_contrast] )
169
+ . toggler ( page. theme . is_high_contrast , Message :: HighContrast ) ,
170
+ )
171
+ . map ( crate :: pages:: Message :: Accessibility ) ,
172
+ )
157
173
. into ( )
158
174
} )
159
175
}
@@ -170,6 +186,65 @@ impl Page {
170
186
Message :: Return => {
171
187
return cosmic:: iced:: Task :: done ( crate :: app:: Message :: Page ( self . entity ) )
172
188
}
189
+ Message :: SystemTheme ( theme) => {
190
+ self . theme = theme;
191
+ }
192
+ Message :: HighContrast ( enabled) => {
193
+ if self . theme . is_high_contrast == enabled
194
+ || self . high_contrast . is_some_and ( |hc| hc == enabled)
195
+ {
196
+ return Task :: none ( ) ;
197
+ }
198
+ self . high_contrast = Some ( enabled) ;
199
+
200
+ _ = std:: thread:: spawn ( move || {
201
+ let set_hc = |is_dark : bool | {
202
+ let builder_config = if is_dark {
203
+ ThemeBuilder :: dark_config ( ) ?
204
+ } else {
205
+ ThemeBuilder :: light_config ( ) ?
206
+ } ;
207
+ let mut builder = match ThemeBuilder :: get_entry ( & builder_config) {
208
+ Ok ( b) => b,
209
+ Err ( ( errs, b) ) => {
210
+ tracing:: warn!( "{errs:?}" ) ;
211
+ b
212
+ }
213
+ } ;
214
+
215
+ builder. palette = if is_dark {
216
+ if enabled {
217
+ CosmicPalette :: HighContrastDark ( builder. palette . inner ( ) )
218
+ } else {
219
+ CosmicPalette :: Dark ( builder. palette . inner ( ) )
220
+ }
221
+ } else if enabled {
222
+ CosmicPalette :: HighContrastLight ( builder. palette . inner ( ) )
223
+ } else {
224
+ CosmicPalette :: Light ( builder. palette . inner ( ) )
225
+ } ;
226
+ builder. write_entry ( & builder_config) ?;
227
+
228
+ let new_theme = builder. build ( ) ;
229
+
230
+ let theme_config = if is_dark {
231
+ CosmicTheme :: dark_config ( ) ?
232
+ } else {
233
+ CosmicTheme :: light_config ( ) ?
234
+ } ;
235
+
236
+ new_theme. write_entry ( & theme_config) ?;
237
+
238
+ Result :: < ( ) , cosmic_config:: Error > :: Ok ( ( ) )
239
+ } ;
240
+ if let Err ( err) = set_hc ( true ) {
241
+ tracing:: warn!( "{err:?}" ) ;
242
+ }
243
+ if let Err ( err) = set_hc ( false ) {
244
+ tracing:: warn!( "{err:?}" ) ;
245
+ }
246
+ } ) ;
247
+ }
173
248
}
174
249
175
250
cosmic:: iced:: Task :: none ( )
0 commit comments