11use std:: collections:: HashMap ;
22use std:: time;
33
4- use crate :: config:: Config ;
4+ use crate :: config:: { self , Config } ;
55use crate :: monitor:: { DisplayId , EventToSub , Monitor } ;
66use crate :: { fl, monitor} ;
77use cosmic:: app:: { Core , Task } ;
@@ -15,7 +15,8 @@ use cosmic::iced::{Alignment, Length, Limits, Subscription, stream};
1515use cosmic:: iced_runtime:: core:: window;
1616use cosmic:: iced_winit:: commands:: popup:: { destroy_popup, get_popup} ;
1717use cosmic:: widget:: {
18- Row , button, column, divider, horizontal_space, icon, mouse_area, row, slider, text, toggler,
18+ Button , Row , button, column, divider, horizontal_space, icon, mouse_area, row, slider, text,
19+ toggler,
1920} ;
2021use cosmic:: { Element , iced_runtime} ;
2122use tokio:: sync:: watch:: Sender ;
@@ -52,6 +53,7 @@ pub enum Message {
5253 ShowSettings ( bool ) ,
5354 Ready ( ( HashMap < DisplayId , Monitor > , Sender < EventToSub > ) ) ,
5455 BrightnessWasUpdated ( DisplayId , u16 ) ,
56+ UpdateMonitors ( HashMap < DisplayId , Monitor > ) ,
5557 Refresh ,
5658}
5759
@@ -168,6 +170,18 @@ impl Window {
168170 vec
169171 }
170172
173+ fn refresh_view ( & self ) -> Vec < Element < Message > > {
174+ let mut vec = Vec :: with_capacity ( 2 ) ;
175+
176+ let text = text:: body ( fl ! ( "refresh" ) )
177+ . width ( Length :: Fill )
178+ . height ( Length :: Fixed ( 24.0 ) )
179+ . align_y ( Alignment :: Center ) ;
180+ vec. push ( menu_button ( text) . on_press ( Message :: Refresh ) . into ( ) ) ;
181+
182+ vec
183+ }
184+
171185 fn dark_mode_view ( & self ) -> Vec < Element < Message > > {
172186 let mut vec = Vec :: with_capacity ( 2 ) ;
173187 if !self . monitors . is_empty ( ) {
@@ -192,6 +206,51 @@ impl Window {
192206
193207 vec
194208 }
209+
210+ fn apply_gamma_curves ( & mut self ) {
211+ for ( monitor_id, monitor) in self . monitors . iter_mut ( ) {
212+ if let Some ( ( _id, gamma) ) = self
213+ . config
214+ . gamma_curves
215+ . iter ( )
216+ . find ( |( id, _) | id == monitor_id)
217+ {
218+ monitor. gamma_curve = * gamma;
219+ }
220+ }
221+ }
222+
223+ fn refresh ( & mut self ) {
224+ self . send ( EventToSub :: Refresh { all : true } ) ;
225+ if let Some ( config_handler) = & self . config_handler {
226+ if let Ok ( c) = config:: Config :: get_entry ( config_handler) {
227+ self . config = c;
228+ self . apply_gamma_curves ( ) ;
229+ }
230+ }
231+ if let Some ( last_dirty) = self . last_config_dirty {
232+ if time:: Instant :: now ( ) . duration_since ( last_dirty) > time:: Duration :: from_secs ( 5 ) {
233+ for ( id, mon) in self . monitors . iter ( ) {
234+ if let Some ( ( _id, gamma) ) = self
235+ . config
236+ . gamma_curves
237+ . iter_mut ( )
238+ . find ( |( mon_id, _gamma) | mon_id == id)
239+ {
240+ * gamma = mon. gamma_curve
241+ } else {
242+ self . config . gamma_curves . push ( ( id. clone ( ) , mon. gamma_curve ) )
243+ }
244+ }
245+ if let Some ( config_handler) = & self . config_handler {
246+ self . config
247+ . write_entry ( config_handler)
248+ . unwrap_or_else ( |e| error ! ( "{e:?}" ) ) ;
249+ }
250+ self . last_config_dirty = None ;
251+ }
252+ }
253+ }
195254}
196255
197256impl cosmic:: Application for Window {
@@ -228,37 +287,19 @@ impl cosmic::Application for Window {
228287
229288 match message {
230289 Message :: Refresh => {
231- if let Some ( last_dirty) = self . last_config_dirty {
232- if time:: Instant :: now ( ) . duration_since ( last_dirty)
233- > time:: Duration :: from_secs ( 5 )
234- {
235- for ( id, mon) in self . monitors . iter ( ) {
236- if let Some ( ( _id, gamma) ) = self
237- . config
238- . gamma_curves
239- . iter_mut ( )
240- . find ( |( mon_id, _gamma) | mon_id == id)
241- {
242- * gamma = mon. gamma_curve
243- } else {
244- self . config . gamma_curves . push ( ( id. clone ( ) , mon. gamma_curve ) )
245- }
246- }
247- if let Some ( config_handler) = & self . config_handler {
248- self . config
249- . write_entry ( config_handler)
250- . unwrap_or_else ( |e| error ! ( "{e:?}" ) ) ;
251- }
252- self . last_config_dirty = None ;
253- }
254- }
290+ self . refresh ( ) ;
291+ }
292+ Message :: UpdateMonitors ( mon) => {
293+ self . monitors = mon;
294+ self . apply_gamma_curves ( ) ;
255295 }
256296 Message :: TogglePopup => {
257297 self . show_settings = false ;
258298 return if let Some ( p) = self . popup . take ( ) {
259299 destroy_popup ( p)
260300 } else {
261- self . send ( EventToSub :: Refresh ) ;
301+ self . send ( EventToSub :: Refresh { all : false } ) ;
302+ // self.refresh();
262303
263304 let new_id = Id :: unique ( ) ;
264305 self . popup . replace ( new_id) ;
@@ -323,16 +364,7 @@ impl cosmic::Application for Window {
323364 }
324365 Message :: Ready ( ( mon, sender) ) => {
325366 self . monitors = mon;
326- for ( monitor_id, monitor) in self . monitors . iter_mut ( ) {
327- if let Some ( ( _id, gamma) ) = self
328- . config
329- . gamma_curves
330- . iter ( )
331- . find ( |( id, _) | id == monitor_id)
332- {
333- monitor. gamma_curve = * gamma;
334- }
335- }
367+ self . apply_gamma_curves ( ) ;
336368 self . sender . replace ( sender) ;
337369 }
338370 Message :: BrightnessWasUpdated ( id, value) => {
@@ -376,15 +408,13 @@ impl cosmic::Application for Window {
376408 }
377409
378410 fn view_window ( & self , _id : Id ) -> Element < Self :: Message > {
379- let mut col = column ( )
411+ let col = column ( )
380412 . padding ( [ 8 , 0 ] )
381413 . extend ( self . sliders_view ( ) )
414+ . extend ( self . refresh_view ( ) )
382415 . extend ( self . dark_mode_view ( ) )
383416 . extend ( self . settings_collapsible_view ( ) )
384417 . extend ( self . settings_view ( ) ) ;
385- // if self.show_settings {
386- // col = col.extend(self.settings_view())
387- // }
388418 self . core . applet . popup_container ( col) . into ( )
389419 }
390420
@@ -398,20 +428,11 @@ impl cosmic::Application for Window {
398428 . watch_config( THEME_MODE_ID )
399429 . map( |u| Message :: ThemeModeConfigChanged ( u. config) ) ,
400430 Subscription :: run( monitor:: sub) ,
401- Subscription :: run( refresh_sub) ,
431+ // Subscription::run(refresh_sub),
402432 ] )
403433 }
404434}
405435
406- fn refresh_sub ( ) -> impl Stream < Item = Message > {
407- stream:: channel ( 10 , |mut output| async move {
408- loop {
409- tokio:: time:: sleep ( time:: Duration :: from_secs ( 10 ) ) . await ;
410- output. send ( Message :: Refresh ) . await . unwrap ( ) ;
411- }
412- } )
413- }
414-
415436fn brightness_icon ( brightness : f32 ) -> & ' static str {
416437 if brightness > 0.66 {
417438 ICON_HIGH
0 commit comments