@@ -4,6 +4,7 @@ const Settings = imports.ui.settings;
4
4
const UUID = "desaturate-all@hkoosha" ;
5
5
const Clutter = imports . gi . Clutter ;
6
6
const Main = imports . ui . main ;
7
+ const Mainloop = imports . mainloop ;
7
8
8
9
function MyApplet ( ) {
9
10
this . _init . apply ( this , arguments ) ;
@@ -12,7 +13,7 @@ function MyApplet() {
12
13
MyApplet . prototype = {
13
14
__proto__ : Applet . IconApplet . prototype ,
14
15
15
- _init : function ( aMetadata , aOrientation , aPanelHeight , aInstanceId ) {
16
+ _init ( aMetadata , aOrientation , aPanelHeight , aInstanceId ) {
16
17
Applet . IconApplet . prototype . _init . call ( this , aOrientation , aPanelHeight , aInstanceId ) ;
17
18
18
19
if ( Applet . hasOwnProperty ( "AllowedLayout" ) ) {
@@ -23,25 +24,104 @@ MyApplet.prototype = {
23
24
this . set_applet_icon_symbolic_name ( "applications-graphics" ) ;
24
25
25
26
this . settings = new Settings . AppletSettings ( this , UUID , this . instance_id ) ;
27
+
26
28
this . settings . bind ( "keybinding" , "keybinding" , this . on_keybinding_changed ) ;
29
+ this . settings . bind ( "saturation" , "saturation" , this . on_saturation_changed ) ;
30
+ this . settings . bind ( "resume-on-startup" , "resumeOnStartup" ) ;
31
+ this . settings . bind ( "state" , "state" ) ;
32
+
33
+ this . settings . connect ( "changed::automatic" , Lang . bind ( this , this . on_automatic_changed ) ) ;
34
+ this . settings . connect ( "changed::start-timechooser" , Lang . bind ( this , this . on_time_changed ) ) ;
35
+ this . settings . connect ( "changed::end-timechooser" , Lang . bind ( this , this . on_time_changed ) ) ;
27
36
28
37
this . on_keybinding_changed ( ) ;
38
+ this . on_saturation_changed ( ) ;
39
+ if ( ! this . settings . getValue ( "automatic" ) ) {
40
+ if ( this . resumeOnStartup && this . state )
41
+ this . _toggleEffect ( ) ;
42
+ } else {
43
+ this . _toggleEffect_based_on_time ( "init" ) ;
44
+ }
29
45
} ,
30
46
31
- _toggleEffect : function ( ) {
32
- if ( Main . uiGroup . has_effects ( ) && Main . uiGroup . get_effects ( ) . indexOf ( this . effect ) > - 1 ) {
47
+ _toggleEffect ( enable = null ) {
48
+ let effectEnabled = Main . uiGroup . has_effects ( ) && Main . uiGroup . get_effects ( ) . indexOf ( this . effect ) > - 1 ;
49
+ if ( enable != true && effectEnabled ) {
33
50
Main . uiGroup . remove_effect ( this . effect ) ;
34
- } else {
51
+ this . settings . setValue ( "state" , false ) ;
52
+ } else if ( enable != false && ! effectEnabled ) {
35
53
Main . uiGroup . add_effect ( this . effect ) ;
54
+ this . settings . setValue ( "state" , true ) ;
55
+ }
56
+ } ,
57
+
58
+ _toggleEffect_based_on_time ( ) {
59
+ if ( this . toggleDelay ) {
60
+ Mainloop . source_remove ( this . toggleDelay ) ;
61
+ this . toggleDelay = null ;
62
+ }
63
+
64
+ if ( ! this . settings . getValue ( "automatic" ) )
65
+ return ;
66
+
67
+ const date = new Date ( ) ;
68
+ const enableAt = new Date ( ) ;
69
+ const disableAt = new Date ( ) ;
70
+
71
+ let enableTime = this . settings . getValue ( "start-timechooser" ) ;
72
+ enableAt . setHours ( enableTime . h ) ;
73
+ enableAt . setMinutes ( enableTime . m ) ;
74
+ enableAt . setSeconds ( 0 ) ;
75
+
76
+ let disableTime = this . settings . getValue ( "end-timechooser" ) ;
77
+ disableAt . setHours ( disableTime . h ) ;
78
+ disableAt . setMinutes ( disableTime . m ) ;
79
+ disableAt . setSeconds ( 0 ) ;
80
+
81
+ if ( disableAt < enableAt )
82
+ disableAt . setDate ( disableAt . getDate ( ) + 1 ) ;
83
+
84
+ let enable = ( date < disableAt && date >= enableAt ) ;
85
+ this . _toggleEffect ( enable ) ;
86
+
87
+ let diffTime ;
88
+ if ( enable ) {
89
+ diffTime = Math . abs ( disableAt - date ) / 1000 ;
90
+ //log( `Disabling in ${diffTime} seconds` );
91
+ } else {
92
+ diffTime = Math . abs ( enableAt - date ) / 1000 ;
93
+ //log( `Enabling in ${diffTime} seconds` );
94
+ }
95
+ this . toggleDelay = Mainloop . timeout_add_seconds ( diffTime , ( ) => { this . toggleDelay = null ; this . _toggleEffect_based_on_time ( ) ; } ) ;
96
+ } ,
97
+
98
+ on_automatic_changed ( signal , key , oldValue , value ) {
99
+ if ( oldValue != value ) {
100
+ this . _toggleEffect_based_on_time ( ) ;
36
101
}
37
102
} ,
38
103
39
- on_applet_clicked : function ( ) {
104
+ on_time_changed ( signal , key , oldValue , value ) {
105
+ if ( oldValue . h !== value . h || oldValue . m !== value . m ) {
106
+ this . _toggleEffect_based_on_time ( ) ;
107
+ }
108
+ } ,
109
+
110
+ on_applet_clicked ( ) {
40
111
this . _toggleEffect ( ) ;
41
112
} ,
42
113
43
114
on_keybinding_changed ( ) {
44
115
Main . keybindingManager . addHotKey ( UUID , this . keybinding , Lang . bind ( this , this . _toggleEffect ) ) ;
116
+ } ,
117
+
118
+ on_saturation_changed ( ) {
119
+ if ( ! this . satDelay ) {
120
+ this . effect . set_factor ( ( 100 - this . saturation ) / 100 ) ;
121
+ } else {
122
+ Mainloop . source_remove ( this . satDelay ) ;
123
+ }
124
+ this . satDelay = Mainloop . timeout_add ( 200 , ( ) => this . satDelay = null ) ;
45
125
}
46
126
} ;
47
127
0 commit comments