@@ -34,7 +34,7 @@ impl NightlightState {
3434 reader. read_marshaled_header ( ) ?;
3535
3636 let mut is_enabled = false ;
37- let mut initialized: i32 = 0 ;
37+ let mut initialized: i32 = 1 ;
3838 let mut last_transition_filetime: u64 = 0 ;
3939
4040 loop {
@@ -105,26 +105,30 @@ impl NightlightState {
105105 + u64:: from ( now. timestamp_subsec_nanos ( ) / 100 ) ;
106106 }
107107
108- /// Enables the nightlight and updates the timestamp.
109- /// Returns true if a change was made (i.e. the nightlight was previously disabled).
110- pub fn enable ( & mut self ) -> bool {
111- if self . is_enabled {
108+ fn set_enabled ( & mut self , is_enabled : bool ) -> bool {
109+ let state_changed = self . is_enabled != is_enabled;
110+ let marker_changed = self . initialized != 1 ;
111+
112+ if !state_changed && !marker_changed {
112113 return false ;
113114 }
114- self . is_enabled = true ;
115+
116+ self . is_enabled = is_enabled;
117+ self . initialized = 1 ;
115118 self . update_transition_timestamps ( ) ;
116119 true
117120 }
118121
122+ /// Enables the nightlight and updates the timestamp.
123+ /// Returns true if a change was made.
124+ pub fn enable ( & mut self ) -> bool {
125+ self . set_enabled ( true )
126+ }
127+
119128 /// Disables the nightlight and updates the timestamp.
120- /// Returns true if a change was made (i.e. the nightlight was previously enabled) .
129+ /// Returns true if a change was made.
121130 pub fn disable ( & mut self ) -> bool {
122- if !self . is_enabled {
123- return false ;
124- }
125- self . is_enabled = false ;
126- self . update_transition_timestamps ( ) ;
127- true
131+ self . set_enabled ( false )
128132 }
129133}
130134
@@ -194,4 +198,25 @@ mod tests {
194198 let state_deserialized = NightlightState :: deserialize_from_bytes ( & bytes) . unwrap ( ) ;
195199 assert_eq ! ( state_deserialized, state_enabled) ;
196200 }
201+
202+ #[ test]
203+ fn state_changes_normalize_initialized_marker ( ) {
204+ let mut state = NightlightState {
205+ timestamp : 0 ,
206+ is_enabled : false ,
207+ initialized : 0 ,
208+ last_transition_filetime : 0 ,
209+ } ;
210+
211+ assert ! ( state. enable( ) ) ;
212+ assert_eq ! ( state. initialized, 1 ) ;
213+
214+ state. initialized = 0 ;
215+ assert ! ( state. disable( ) ) ;
216+ assert_eq ! ( state. initialized, 1 ) ;
217+
218+ state. initialized = 0 ;
219+ assert ! ( state. disable( ) ) ;
220+ assert_eq ! ( state. initialized, 1 ) ;
221+ }
197222}
0 commit comments