Skip to content

Commit be7e5b0

Browse files
authored
Update Dimmer.vala
1 parent 237b53b commit be7e5b0

File tree

1 file changed

+129
-78
lines changed

1 file changed

+129
-78
lines changed

src/Dimmer.vala

Lines changed: 129 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public class Dimmer : Gtk.Application {
2828
public static Gtk.Scale slider3;
2929
public static Gtk.Scale slider4;
3030

31+
public static Label label1;
32+
public static Label label2;
33+
public static Label label3;
34+
public static Label label4;
35+
36+
public static Switch switcher;
37+
38+
public static string[] lines;
39+
public static string[] monitors;
40+
3141
public Dimmer () {
3242
Object (
3343
application_id: "com.github.panosx2.brightness",
@@ -67,7 +77,7 @@ public class Dimmer : Gtk.Application {
6777
GLib.Process.spawn_command_line_sync("sh -c \"xrandr | grep ' connected ' | awk '{ print$1 }'\"", out names);
6878
} catch (SpawnError se) { names = ""; }
6979

70-
string[] lines = names.split("\n");
80+
lines = names.split("\n");
7181

7282
if (lines.length > 1) {
7383
var hboxAll = new Box (Orientation.HORIZONTAL, 0);
@@ -77,21 +87,42 @@ public class Dimmer : Gtk.Application {
7787
label.halign = Align.START;
7888
label.set_margin_bottom(30);
7989

80-
var switcher = new Switch();
90+
switcher = new Switch();
8191
switcher.active = true;
8292
switcher.state_changed.connect(() => {
8393
if (switcher.active == true) {
8494
slider.visible = true;
8595
vboxInd.visible = false;
86-
slider.adjustment.value = slider.get_value();
96+
97+
string prevValue;
98+
99+
if (FileUtils.test(".dimmer_all_monitors.txt", GLib.FileTest.EXISTS) == true) {
100+
try {
101+
FileUtils.get_contents(".dimmer_all_monitors.txt", out prevValue);
102+
}
103+
catch(Error e) {
104+
stderr.printf ("Error: %s\n", e.message);
105+
}
106+
}
107+
else {
108+
prevValue = "1.00"; //default
109+
}
110+
111+
slider.adjustment.value = double.parse(prevValue) * 100;
112+
113+
string edited = prevValue;
114+
115+
try {
116+
for (int i = 0; i < lines.length - 1; i++) {
117+
GLib.Process.spawn_command_line_async("xrandr --output " + lines[i] + " --brightness " + edited);
118+
}
119+
} catch (SpawnError se) {}
87120
}
88121
else {
89122
slider.visible = false;
90123
vboxInd.visible = true;
91-
slider1.set_value(slider1.get_value());
92-
slider2.set_value(slider2.get_value());
93-
slider3.set_value(slider3.get_value());
94-
slider4.set_value(slider4.get_value());
124+
125+
setValues();
95126
}
96127
});
97128
switcher.set_margin_bottom(30);
@@ -102,32 +133,30 @@ public class Dimmer : Gtk.Application {
102133
vboxMain.add(hboxAll);
103134
}
104135

105-
string[] monitors = {"", "", "", ""};
136+
monitors = {"", "", "", ""};
106137

107138
for (int i = 0; i < lines.length - 1; i++) {
108139
monitors[i] = lines[i];
109140
}
110141

111-
if (lines.length > 1) {
142+
if (lines.length-1 > 1) {
112143
//slider1
113-
Label label1 = new Label(monitors[0]);
144+
label1 = new Label(monitors[0]);
114145
label1.halign = Align.START;
115146

116-
try {
117-
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '\"", out currentBr);
118-
} catch (SpawnError se) { currentBr = "100"; }
119-
120147
slider1 = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
121148
slider1.set_size_request(380, 30);
122-
slider1.adjustment.value = double.parse(currentBr) * 100;
123149
slider1.adjustment.value_changed.connect (() => {
124150
try {
125151
string edited = (slider1.adjustment.value / 100).to_string();
126152

127-
if ((slider1.adjustment.value / 100) >= 1.0) {
128-
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[0] + " --gamma " + edited + ":" + edited + ":" + edited);
129-
}
130-
else GLib.Process.spawn_command_line_async("xrandr --output " + monitors[0] + " --brightness " + edited);
153+
//if ((slider1.adjustment.value / 100) >= 1.0) {
154+
//GLib.Process.spawn_command_line_async("xrandr --output " + monitors[0] + " --gamma " + edited + ":" + edited + ":" + edited);
155+
//}
156+
//else
157+
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[0] + " --brightness " + edited);
158+
159+
saveValue(".dimmer_" + monitors[0] + ".txt", edited);
131160

132161
if ((slider1.adjustment.value / 100) > 1.2) slider1.tooltip_text = "Too Bright";
133162
else if ((slider1.adjustment.value / 100) < 0.6) slider1.tooltip_text = "Too Dark";
@@ -137,24 +166,18 @@ public class Dimmer : Gtk.Application {
137166
slider1.set_margin_bottom(30);
138167

139168
//slider2
140-
Label label2 = new Label(monitors[1]);
169+
label2 = new Label(monitors[1]);
141170
label2.halign = Align.START;
142171

143-
try {
144-
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '\"", out currentBr);
145-
} catch (SpawnError se) { currentBr = "100"; }
146-
147172
slider2 = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
148173
slider2.set_size_request(380, 30);
149-
slider2.adjustment.value = double.parse(currentBr) * 100;
150174
slider2.adjustment.value_changed.connect (() => {
151175
try {
152176
string edited = (slider2.adjustment.value / 100).to_string();
153177

154-
if ((slider2.adjustment.value / 100) >= 1.0) {
155-
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[1] + " --gamma " + edited + ":" + edited + ":" + edited);
156-
}
157-
else GLib.Process.spawn_command_line_async("xrandr --output " + monitors[1] + " --brightness " + edited);
178+
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[1] + " --brightness " + edited);
179+
180+
saveValue(".dimmer_" + monitors[1] + ".txt", edited);
158181

159182
if ((slider2.adjustment.value / 100) > 1.2) slider2.tooltip_text = "Too Bright";
160183
else if ((slider2.adjustment.value / 100) < 0.6) slider2.tooltip_text = "Too Dark";
@@ -164,24 +187,18 @@ public class Dimmer : Gtk.Application {
164187
slider2.set_margin_bottom(30);
165188

166189
//slider3
167-
Label label3 = new Label(monitors[2]);
190+
label3 = new Label(monitors[2]);
168191
label3.halign = Align.START;
169192

170-
try {
171-
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '\"", out currentBr);
172-
} catch (SpawnError se) { currentBr = "100"; }
173-
174193
slider3 = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
175194
slider3.set_size_request(380, 30);
176-
slider3.adjustment.value = double.parse(currentBr) * 100;
177195
slider3.adjustment.value_changed.connect (() => {
178196
try {
179197
string edited = (slider3.adjustment.value / 100).to_string();
180198

181-
if ((slider3.adjustment.value / 100) >= 1.0) {
182-
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[2] + " --gamma " + edited + ":" + edited + ":" + edited);
183-
}
184-
else GLib.Process.spawn_command_line_async("xrandr --output " + monitors[2] + " --brightness " + edited);
199+
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[2] + " --brightness " + edited);
200+
201+
saveValue(".dimmer_" + monitors[2] + ".txt", edited);
185202

186203
if ((slider3.adjustment.value / 100) > 1.2) slider3.tooltip_text = "Too Bright";
187204
else if ((slider3.adjustment.value / 100) < 0.6) slider3.tooltip_text = "Too Dark";
@@ -191,63 +208,57 @@ public class Dimmer : Gtk.Application {
191208
slider3.set_margin_bottom(30);
192209

193210
//slider4
194-
Label label4 = new Label(monitors[3]);
211+
label4 = new Label(monitors[3]);
195212
label4.halign = Align.START;
196213

197-
try {
198-
GLib.Process.spawn_command_line_sync("sh -c \"xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '\"", out currentBr);
199-
} catch (SpawnError se) { currentBr = "100"; }
200-
201214
slider4 = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
202215
slider4.set_size_request(380, 30);
203-
slider4.adjustment.value = double.parse(currentBr) * 100;
204216
slider4.adjustment.value_changed.connect (() => {
205217
try {
206218
string edited = (slider4.adjustment.value / 100).to_string();
207219

208-
if ((slider4.adjustment.value / 100) >= 1.0) {
209-
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[3] + " --gamma " + edited + ":" + edited + ":" + edited);
210-
}
211-
else GLib.Process.spawn_command_line_async("xrandr --output " + monitors[3] + " --brightness " + edited);
220+
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[3] + " --brightness " + edited);
221+
222+
saveValue(".dimmer_" + monitors[3] + ".txt", edited);
212223

213224
if ((slider4.adjustment.value / 100) > 1.2) slider4.tooltip_text = "Too Bright";
214225
else if ((slider4.adjustment.value / 100) < 0.6) slider4.tooltip_text = "Too Dark";
215226
else slider4.tooltip_text = "";
216227
} catch (SpawnError se) {}
217228
});
229+
}
230+
231+
//set the values at start
232+
if (lines.length > 1) {
233+
setValues();
234+
235+
if (slider1.get_value() != slider2.get_value()) {
236+
switcher.set_active(false);
237+
}
238+
}
218239

219-
//slider for all monitors
220-
slider = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
221-
slider.set_size_request(380, 50);
222-
slider.adjustment.value = double.parse(currentBr) * 100;
223-
slider.adjustment.value_changed.connect (() => {
224-
try {
225-
string edited = (slider.adjustment.value / 100).to_string();
240+
//slider for all monitors
241+
slider = new Scale.with_range(Orientation.HORIZONTAL, 40, 150, 1);
242+
slider.set_size_request(380, 50);
243+
slider.adjustment.value_changed.connect (() => {
244+
try {
245+
string edited = (slider.adjustment.value / 100).to_string();
226246

227-
for (int i = 0; i < lines.length - 1; i++) {
228-
if ((slider.adjustment.value / 100) >= 1.0) {
229-
GLib.Process.spawn_command_line_async("xrandr --output " + lines[i] + " --gamma " + edited + ":" + edited + ":" + edited);
230-
}
231-
else GLib.Process.spawn_command_line_async("xrandr --output " + lines[i] + " --brightness " + edited);
232-
}
233-
234-
if (lines.length > 1) {
235-
for (int j = 0; j < lines.length - 1; j++) {
236-
if (j == 0) slider1.adjustment.value = slider.adjustment.value;
237-
else if (j == 1) slider2.adjustment.value = slider.adjustment.value;
238-
else if (j == 2) slider3.adjustment.value = slider.adjustment.value;
239-
else if (j == 3) slider4.adjustment.value = slider.adjustment.value;
240-
}
241-
}
247+
for (int i = 0; i < lines.length - 1; i++) {
248+
GLib.Process.spawn_command_line_async("xrandr --output " + lines[i] + " --brightness " + edited);
249+
}
242250

243-
if ((slider.adjustment.value / 100) > 1.2) slider.tooltip_text = "Too Bright";
244-
else if ((slider.adjustment.value / 100) < 0.6) slider.tooltip_text = "Too Dark";
245-
else slider.tooltip_text = "";
246-
} catch (SpawnError se) {}
247-
});
248-
249-
vboxMain.add(slider);
251+
saveValue(".dimmer_all_monitors.txt", edited);
252+
253+
if ((slider.adjustment.value / 100) > 1.2) slider.tooltip_text = "Too Bright";
254+
else if ((slider.adjustment.value / 100) < 0.6) slider.tooltip_text = "Too Dark";
255+
else slider.tooltip_text = "";
256+
} catch (SpawnError se) {}
257+
});
250258

259+
vboxMain.add(slider);
260+
261+
if (lines.length > 1) {
251262
for (int i = 0; i < lines.length - 1; i++) {
252263
if (i == 0) {
253264
vboxInd.add(label1);
@@ -275,6 +286,46 @@ public class Dimmer : Gtk.Application {
275286
window.show_all();
276287
}
277288

289+
private static void setValues() {
290+
for (int i = 0; i < lines.length - 1; i++) {
291+
string prevValue;
292+
293+
if (monitors[i] != "") {
294+
if (FileUtils.test(".dimmer_"+monitors[i]+".txt", GLib.FileTest.EXISTS) == true) {
295+
try {
296+
FileUtils.get_contents(".dimmer_"+monitors[i]+".txt", out prevValue);
297+
}
298+
catch(Error e) {
299+
stderr.printf ("Error: %s\n", e.message);
300+
}
301+
}
302+
else {
303+
prevValue = "1.00"; //default
304+
}
305+
306+
string edited = prevValue;
307+
308+
if (i==0) slider1.adjustment.value = double.parse(prevValue) * 100;
309+
else if (i==1) slider2.adjustment.value = double.parse(prevValue) * 100;
310+
else if (i==2) slider3.adjustment.value = double.parse(prevValue) * 100;
311+
else if (i==3) slider4.adjustment.value = double.parse(prevValue) * 100;
312+
313+
try {
314+
GLib.Process.spawn_command_line_async("xrandr --output " + monitors[i] + " --brightness " + edited);
315+
} catch (SpawnError se) {}
316+
}
317+
}
318+
}
319+
320+
private static void saveValue(string filename, string valueToSave) {
321+
try {
322+
FileUtils.set_contents(filename, valueToSave);
323+
}
324+
catch(Error e) {
325+
stderr.printf ("Error: %s\n", e.message);
326+
}
327+
}
328+
278329
public static int main(string[] args) {
279330
return new Dimmer().run (args);
280331
}

0 commit comments

Comments
 (0)