-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathshuffler_control.vala
More file actions
1996 lines (1920 loc) · 87 KB
/
shuffler_control.vala
File metadata and controls
1996 lines (1920 loc) · 87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Gtk;
using Gdk;
using Wnck;
using Gdk.X11;
// valac --pkg gtk+-3.0 --pkg gdk-x11-3.0 --pkg gdk-3.0 --pkg gio-2.0 --pkg libwnck-3.0 -X "-D WNCK_I_KNOW_THIS_IS_UNSTABLE"
/*
Budgie Window Shuffler III
Author: Jacob Vlijm
Copyright © 2017 Ubuntu Budgie Developers
Website=https://ubuntubudgie.org
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or any later version. This
program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details. You
should have received a copy of the GNU General Public License along with this
program. If not, see <https://www.gnu.org/licenses/>.
*/
//todo: translations, paths
namespace ShufflerControl2 {
GLib.Settings shufflersettings;
GLib.Settings shufflerappletsettings;
Button applytask_button;
Gtk.Stack appletgallery_stack;
string[] gallery_options;
ListBox listbox;
private void set_widgetstyle(
Widget w, string css_style, bool remove = false
) {
var widgets_stylecontext = w.get_style_context();
if (!remove) {
widgets_stylecontext.add_class(css_style);
}
else {
widgets_stylecontext.remove_class(css_style);
}
}
private void set_margins(
Gtk.Widget widget, int left, int right, int top, int bottom
) {
// lazy margins on a grid
widget.set_margin_start(left);
widget.set_margin_end(right);
widget.set_margin_top(top);
widget.set_margin_bottom(bottom);
}
public void makegallery() {
gallery_options = {
"2x1|0,0,1,1|1,0,1,1",
"2x2|0,0,1,1|1,0,1,1|0,1,1,1|1,1,1,1",
"5x1|0,0,3,1|3,0,2,1",
"5x2|0,0,3,1|0,1,3,1|3,0,2,1|3,1,2,1",
"5x2|0,0,3,2|3,0,2,1|3,1,2,1",
"3x1|0,0,1,1|1,0,1,1|2,0,1,1",
"3x2|0,0,1,1|1,0,1,1|2,0,1,1|0,1,1,1|1,1,1,1|2,1,1,1",
"4x2|0,0,1,2|1,0,2,2|3,0,1,2",
"4x2|0,0,1,1|0,1,1,1|1,0,2,2|3,0,1,1|3,1,1,1",
"4x2|0,0,1,1|1,0,1,1|2,0,1,1|3,0,1,1|0,1,1,1|1,1,1,1|2,1,1,1|3,1,1,1",
};
appletgallery_stack = new Gtk.Stack();
appletgallery_stack.set_transition_type(
StackTransitionType.NONE
);
int previewsize = 120;
int area_ysize = (int)(previewsize*0.67);
int currindex = 0;
foreach (string s in gallery_options) {
Grid showcurrgrid = new Grid();
string[] layout_def = s.split("|");
string grid_string = layout_def[0];
string[] gridcolsrows = grid_string.split("x");
int gridcols = int.parse(gridcolsrows[0]);
int gridrows = int.parse(gridcolsrows[1]);
layout_def = layout_def[1:(layout_def.length)];
foreach (string data in layout_def) {
int[] coords = {};
foreach (string c in data.split(",")) {
coords += int.parse(c);
}
Button sectionbutton = new Button();
sectionbutton.get_style_context().add_class("windowbutton");
sectionbutton.set_sensitive(false);
int xpos = coords[0];
int ypos = coords[1];
int xspan = coords[2];
int yspan = coords[3];
sectionbutton.set_relief(Gtk.ReliefStyle.NONE);
sectionbutton.set_size_request((int)(
xspan * (previewsize/gridcols)),
(int)(yspan * (area_ysize/gridrows))
);
showcurrgrid.attach(
sectionbutton, xpos, ypos, xspan, yspan
);
}
appletgallery_stack.add_named(showcurrgrid, @"$currindex");
currindex += 1;
}
}
class OwnSpinButton : Gtk.Grid{
public Gtk.Entry spinvalue;
Gtk.Button up;
Gtk.Button down;
bool act_onchange;
// css stuff
string spin_stylecss = """
.arrowbutton {
padding: 0px;
border-width: 0px;
}
.red_text {
color: white;
background-color: red;
}
""";
public void set_warning_color(bool warning = false) {
if (warning) {
set_widgetstyle(spinvalue, "red_text");
}
else {
set_widgetstyle(spinvalue, "red_text", true);
}
}
public OwnSpinButton(
string orientation, string key, int min = 0, int max = 10
) {
// css stuff
act_onchange = true;
Gdk.Screen gdk_scr = this.get_screen();
Gtk.CssProvider css_provider = new Gtk.CssProvider();
try {
css_provider.load_from_data(spin_stylecss);
Gtk.StyleContext.add_provider_for_screen(
gdk_scr, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER
);
}
catch (Error e) {
stderr.printf ("%s\n", e.message);
}
this.set_column_spacing(0);
spinvalue = new Gtk.Entry();
spinvalue.set_editable(false);
spinvalue.xalign = (float)0.50;
spinvalue.set_text("0");
spinvalue.set_width_chars(2);
spinvalue.set_max_width_chars(2);
spinvalue.changed.connect(()=>{
if (key != "") {
if (act_onchange) {
int set_spinvalue = get_value();
shufflersettings.set_int(key, set_spinvalue);
}
}
});
if (key != "") {
shufflersettings.changed[key].connect(()=> {
act_onchange = false;
update_value(key);
act_onchange = true;
});
}
up = new Gtk.Button();
set_widgetstyle(up, "arrowbutton");
up.set_size_request(1,1);
up.set_relief(Gtk.ReliefStyle.NONE);
down = new Gtk.Button();
set_widgetstyle(down, "arrowbutton");
down.set_size_request(1,1);
down.set_relief(Gtk.ReliefStyle.NONE);
if (orientation == "hor") {
up.label = "▶";
down.label = "◀";
this.attach(spinvalue, 0, 1, 1, 1);
this.attach(up, 2, 1, 1, 1);
this.attach(down, 1, 1, 1, 1);
}
else if (orientation == "vert") {
up.label = "▲";
down.label = "▼";
this.attach(spinvalue, 0, 1, 1, 1);
this.attach(up, 2, 1, 1, 1);
this.attach(down, 1, 1, 1, 1);
}
up.clicked.connect(()=> {
add_one(up, min, max);
});
down.clicked.connect(()=> {
add_one(down, min, max);
});
if (key != "") {
update_value(key);
}
}
private void update_value (string key) {
set_value(shufflersettings.get_int(key));
}
public int get_value() {
return int.parse(spinvalue.get_text());
}
public void set_value(int newvalue) {
spinvalue.set_text(@"$newvalue");
}
private void add_one(Button b, int min, int max) {
int curr = int.parse(spinvalue.get_text());
if (b == up && curr < max) {
curr += 1;
}
else if (b == down && curr > min) {
curr -= 1;
}
spinvalue.set_text(@"$curr");
}
}
private Gtk.Label makelabel (
string labeltext, float halign, string? cssclass = null
){
Gtk.Label newlabel = new Gtk.Label(labeltext);
newlabel.xalign = 0;
if (cssclass != null) {
newlabel.get_style_context().add_class(cssclass);
}
return newlabel;
}
class ShufflerControlWindow : Gtk.Window {
ExtrasDaemon bd_client;
[DBus (name = "org.UbuntuBudgie.ExtrasDaemon")]
interface ExtrasDaemon : Object {
public abstract bool ReloadShortcuts () throws Error;
}
private void setup_client () {
try {
bd_client = Bus.get_proxy_sync (
BusType.SESSION, "org.UbuntuBudgie.ExtrasDaemon",
("/org/ubuntubudgie/extrasdaemon")
);
}
catch (Error e) {
stderr.printf ("%s\n", e.message);
}
}
Stack allsettings_stack;
Gdk.X11.Window timestamp_window;
ShufflerInfoClient? client;
[DBus (name = "org.UbuntuBudgie.ShufflerInfoDaemon")]
interface ShufflerInfoClient : Object {
public abstract GLib.HashTable<string,
Variant> get_rules () throws Error;
public abstract int get_numberof_workspaces() throws Error;
}
GLib.HashTable<string, Variant> foundrules;
FileMonitor monitor_ruleschange;
// This is about target monitor and - workspace for new windows
// string length is not particularly critical
string default_set = _("Not set");
string control_css = """
.somebox {
border-left: 0px;
border-bottom: 0px;
border-top: 0px;
}
.justbold {
font-weight: bold;
}
.justitalic {
font-style: italic;
}
""";
Wnck.Screen wnck_scr;
Grid tilinggrid;
Grid layoutsgrid;
Grid rulesgrid;
Grid general_settingsgrid;
Grid applet_settingsgrid;
Grid newrulesgrid;
Dialog? get_task;
Gtk.Switch[] switches;
string[] read_switchsettings;
Gtk.CheckButton[] checkbuttons;
CheckButton toggle_guigrid;
string[] read_checkbutton;
string windowrule_location;
Label useanimationlabel;
Gtk.Switch enable_animationswich;
private string[] get_monitornames() {
Gdk.Display gdk_dsp = Gdk.Display.get_default();
int n_monitors = gdk_dsp.get_n_monitors();
string[] monitors = {};
for (int i=0; i < n_monitors; i++) {
monitors += gdk_dsp.get_monitor(i).get_model();
}
return monitors;
}
private int string_inlist (string s, string[] arr) {
// check if in in array
for (int i=0; i<arr.length; i++) {
if (s == arr[i]) {
return i;
}
}
return -1;
}
private bool ask_confirm(string action) {
bool confirm = false;
Dialog ask_confirmdialog = new Dialog();
ask_confirmdialog.decorated = false;
ask_confirmdialog.set_transient_for(get_task);
ask_confirmdialog.set_modal(true);
Gtk.Box contentarea = ask_confirmdialog.get_content_area();
var askgrid = new Gtk.Grid();
contentarea.pack_start(askgrid, false, false, 0);
set_margins(askgrid, 20, 20, 20, 20);
askgrid.attach(new Label(action + "\t\t"), 0, 0, 2, 1);
askgrid.attach(new Label("\n"), 0, 1, 1, 1);
contentarea.orientation = Gtk.Orientation.VERTICAL;
Box buttonbox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
Button cancel = new Gtk.Button();
cancel.label = _("No");
cancel.get_style_context().add_class("suggested-action");
askgrid.get_style_context().remove_class("horizontal");
Button go_on = new Gtk.Button();
go_on.label = _("Yes");
buttonbox.pack_end(go_on, false, false, 2);
buttonbox.pack_end(cancel, false, false, 2);
askgrid.attach(buttonbox, 1, 10, 1, 1);
go_on.set_size_request(70, 10);
cancel.set_size_request(70, 10);
go_on.clicked.connect(()=> {
confirm = true;
ask_confirmdialog.destroy();
ask_confirmdialog = null;
});
cancel.clicked.connect(()=> {
confirm = false;
ask_confirmdialog.destroy();
ask_confirmdialog = null;
});
askgrid.show_all();
ask_confirmdialog.run();
return confirm;
}
private string get_value_forrule (string? found, bool tofile = false) {
// converting readable value of workspace from filedata and vice versa
/*
we need to keep: if set_workspace != default_set (in -> togui),
in case someone already used the dev version,
but it won't end up in new rules files, because it is language-dependent
(files wouldn't work anymore if set language would change)
*/
string set_forrule = default_set;
int add = 1;
if (tofile) {
add = -1;
set_forrule = "";
}
if (found != "" && found != default_set && found != null) {
set_forrule = (int.parse(found) + add).to_string();
}
return set_forrule;
}
private void call_dialog (string? wmclass = null) {
bool wmclass_changed = false;
string? initial_wm = wmclass;
OwnSpinButton[] allspins;
string check_changes = "";
unowned X.Window xwindow = Gdk.X11.get_default_root_xwindow();
unowned X.Display xdisplay = Gdk.X11.get_default_xdisplay();
Gdk.X11.Display display = Gdk.X11.Display.lookup_for_xdisplay(xdisplay);
timestamp_window = new Gdk.X11.Window.foreign_for_display(display, xwindow);
// just to skip first 'false' positive on window focus change
bool initial_change = true;
// if wm class is given, we obviously are updating existing rule
bool update = (wmclass != null);
// tooltips
// This is a tooltip, string length is not particularly critical
string class_tooltip = _("Window class of the window to be launched").concat(
" (", "*", _("mandatory"), ")"
);
// This is a tooltip, string length is not particularly critical
string gridxsize_tooltip = _("Grid size - columns");
// This is a tooltip, string length is not particularly critical
string gridysize_tooltip = _("Grid size - rows");
// This is a tooltip, string length is not particularly critical
string targetpositionx_tooltip = _("Window target position on grid - horizontally");
// This is a tooltip, string length is not particularly critical
string targetpositiony_tooltip = _("Window target position on grid - vertically");
// This is a tooltip, string length is not particularly critical
string xspan_tooltip = _("Window size - columns");
// This is a tooltip, string length is not particularly critical
string yspan_tooltip = _("Window size - rows");
// This is a tooltip, string length is not particularly critical
string monitor_tooltip = _("Target monitor, default is on active monitor");
// This is a tooltip, string length is not particularly critical
string workspace_tooltip = _("Target workspace, default is on active workspace");
get_task = new Dialog();
var contentarea = get_task.get_content_area();
contentarea.orientation = Gtk.Orientation.VERTICAL;
// mastergrid
Grid master_grid = new Gtk.Grid();
set_margins(master_grid, 30, 30, 30, 30);
contentarea.pack_start(master_grid, false, false, 0);
// 1. APPLICATION FRAME
// This is a Frame title, string length is not particularly critical
Frame applicationframe = new Gtk.Frame(_("Application"));
var app_label = applicationframe.get_label_widget();
set_widgetstyle(app_label, "justbold");
// application grid
Grid applicationgrid = new Gtk.Grid();
set_margins(applicationgrid, 20, 20, 20, 20);
applicationgrid.set_row_spacing(4);
// - wmclass
// This is a field label, should be as short as possible
Label wmclass_label = makelabel((_("WM class group") + "*"), 0);
Entry wmclass_entry = new Entry();
wmclass_entry.changed.connect(()=> {
set_widgetstyle(wmclass_entry, "red_text", true);
wmclass_changed = false;
if (initial_wm != null &&
initial_wm != wmclass_entry.get_text()) {
wmclass_changed = true;
}
});
wmclass_entry.set_tooltip_text(class_tooltip);
wmclass_entry.set_text("");
wmclass_entry.set_size_request(250, 10);
// Placeholder text
wmclass_entry.set_placeholder_text(_("Click a window to fetch WM-class"));
applicationgrid.attach(wmclass_label, 1, 4, 1, 1);
applicationgrid.attach(new Label("\t\t"), 2, 4, 1, 1);
applicationgrid.attach(wmclass_entry, 3, 4, 20, 1);
applicationframe.add(applicationgrid);
master_grid.attach(applicationframe, 1, 10, 10, 1);
master_grid.attach(new Label(""), 1, 20, 1, 1);
// 2. GEOMETRY FRAME
// This is a Frame title, string length is not particularly critical
Frame geometryframe = new Gtk.Frame(_("Window position & size"));
var geo_label = geometryframe.get_label_widget();
set_widgetstyle(geo_label, "justbold");
master_grid.attach(geometryframe, 1, 30, 10, 1);
// geometry grid
Grid geogrid = new Gtk.Grid();
set_margins(geogrid, 20, 20, 20, 20);
geogrid.set_row_spacing(0);
// grid cols / rows
// This is a field label, should be short
Label grid_size_label = makelabel(_("Grid size; columns & rows"), 0);
geogrid.attach(grid_size_label, 1, 10, 1, 1);
geogrid.attach(new Label("\t"), 2, 10, 1, 1);
// get current gridsize
OwnSpinButton grid_xsize_spin = new OwnSpinButton("hor", "", 1, 10);
grid_xsize_spin.set_value(shufflersettings.get_int("cols"));
grid_xsize_spin.set_tooltip_text(gridxsize_tooltip);
OwnSpinButton grid_ysize_spin = new OwnSpinButton("vert", "", 1, 10);
grid_ysize_spin.set_value(shufflersettings.get_int("rows"));
grid_ysize_spin.set_tooltip_text(gridysize_tooltip);
Box gridsize_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
gridsize_box.pack_start(grid_xsize_spin, false, false, 0);
gridsize_box.pack_start(new Label("\t"), false, false, 0);
gridsize_box.pack_start(grid_ysize_spin, false, false, 0);
geogrid.attach(gridsize_box, 3, 10, 1, 1);
geogrid.attach(new Label(""), 1, 11, 1, 1);
// window position
// This is a field label, should be short
Label winpos_label = makelabel(_("Target window position, x / y"), 0);
geogrid.attach(winpos_label, 1, 12, 1, 1);
geogrid.attach(new Label("\t"), 2, 12, 1, 1);
OwnSpinButton xpos_spin = new OwnSpinButton("hor", "", 0, 10);
xpos_spin.set_tooltip_text(targetpositionx_tooltip);
xpos_spin.set_value(0);
OwnSpinButton ypos_spin = new OwnSpinButton("vert", "", 0, 10);
ypos_spin.set_tooltip_text(targetpositiony_tooltip);
ypos_spin.set_value(0);
Box winpos_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
winpos_box.pack_start(xpos_spin, false, false, 0);
winpos_box.pack_start(new Label("\t"), false, false, 0);
winpos_box.pack_start(ypos_spin, false, false, 0);
geogrid.attach(winpos_box, 3, 12, 1, 1);
geogrid.attach(new Label(""), 1, 13, 1, 1);
// window span
// This is a field label, should be short
Label cellspan_label = makelabel(_("Window cell span, hor / vert"), 0);
geogrid.attach(cellspan_label, 1, 14, 1, 1);
geogrid.attach(new Label("\t"), 2, 14, 1, 1);
OwnSpinButton yspan_spin = new OwnSpinButton("vert", "", 1, 10);
yspan_spin.set_tooltip_text(yspan_tooltip);
yspan_spin.set_value(1);
OwnSpinButton xspan_spin = new OwnSpinButton("hor", "", 1, 10);
xspan_spin.set_tooltip_text(xspan_tooltip);
xspan_spin.set_value(1);
Box winspan_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
winspan_box.pack_start(xspan_spin, false, false, 0);
winspan_box.pack_start(new Label("\t"), false, false, 0);
winspan_box.pack_start(yspan_spin, false, false, 0);
geogrid.attach(winspan_box, 3, 14, 1, 1);
geogrid.attach(new Label(""), 1, 13, 1, 1);
geometryframe.add(geogrid);
master_grid.attach(new Label(""), 1, 31, 1, 1);
// Frame title, string length is not particularly critical
Gtk.Frame miscframe = new Gtk.Frame(_("Miscellaneous"));
var misc_label = miscframe.get_label_widget();
set_widgetstyle(misc_label, "justbold");
master_grid.attach(miscframe, 1, 50, 10, 1);
Grid miscgrid = new Gtk.Grid();
set_margins(miscgrid, 20, 20, 20, 20);
miscgrid.set_row_spacing(4);
miscframe.add(miscgrid);
allspins = {
grid_xsize_spin, grid_ysize_spin, xpos_spin,
ypos_spin, xspan_spin, yspan_spin
};
// target monitor
// Field label, should be short
Label targetmonitor_label = new Label(_("Target monitor"));
targetmonitor_label.xalign = 0;
miscgrid.attach(targetmonitor_label, 1, 1, 1, 1);
ComboBoxText screendropdown = new ComboBoxText();
screendropdown.set_tooltip_text(monitor_tooltip);
screendropdown.append_text(default_set);
screendropdown.active = 0;
string[] mons = {default_set};
foreach (string m in get_monitornames()) {
screendropdown.append_text(m);
mons += m;
}
miscgrid.attach(new Label("\t"), 2, 1, 1, 1);
miscgrid.attach(screendropdown, 3, 1, 1, 1);
master_grid.attach(new Label(""), 1, 109, 1, 1);
// target workspace
// Field label, should be short
Label targetworkspace_label = new Label(_("Target workspace"));
targetworkspace_label.xalign = 0;
miscgrid.attach(targetworkspace_label, 1, 2, 1, 1);
ComboBoxText workspacedropdown = new ComboBoxText();
workspacedropdown.set_tooltip_text(workspace_tooltip);
int nspaces = 1;
string[] allspaces = {default_set};
workspacedropdown.append_text(default_set);
workspacedropdown.active = 0;
try {
// populate workspace-dropdown & list
nspaces = client.get_numberof_workspaces();
for (int i=0;i<nspaces; i++) {
int readable_space = i + 1;
allspaces += @"$readable_space";
workspacedropdown.append_text(@"$readable_space");
}
}
catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
}
miscgrid.attach(new Label("\t"), 2, 2, 1, 1);
miscgrid.attach(workspacedropdown, 3, 2, 1, 1);
master_grid.attach(new Label(""), 1, 109, 1, 1);
// Done button
Gtk.Box dialogaction_box = new Gtk.Box(
Gtk.Orientation.HORIZONTAL, 0
);
applytask_button = new Gtk.Button();
applytask_button.label = _("Done");
applytask_button.set_size_request(90, 10);
applytask_button.clicked.connect(()=> {
string tocompare = makecheckstring(
wmclass_entry, allspins, screendropdown, workspacedropdown
);
bool anythingchanged = tocompare != check_changes;
// print(@"anythingchanged?\n$anythingchanged\n$tocompare\n$check_changes\n");
if (anythingchanged) {
if (apply_newrule(
wmclass_entry, grid_xsize_spin, grid_ysize_spin,
xpos_spin, ypos_spin, xspan_spin, yspan_spin,
screendropdown, workspacedropdown, update,
wmclass_changed
) && wmclass_changed) {
/*
when updating, we need to keep in mind the possibility
of an updated wmclass name -> remove old named file
only if wmclass was edited
*/
try {
File remove_oldname = File.new_for_path(
windowrule_location.concat(@"/$wmclass.windowrule")
);
remove_oldname.delete();
}
catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
}
}
}
else {
get_task.destroy();
}
});
Button canceltask_button = new Gtk.Button();
canceltask_button.label = _("Cancel");
canceltask_button.set_size_request(90, 10);
canceltask_button.clicked.connect(()=> {
get_task.destroy();
});
dialogaction_box.pack_end(applytask_button, false, false, 2);
dialogaction_box.pack_end(canceltask_button, false, false, 2);
master_grid.attach(dialogaction_box, 1, 110, 10, 1); //
get_task.set_transient_for(this);
get_task.decorated = false;
contentarea.show_all();
foreach (OwnSpinButton spin in allspins) {
spin.spinvalue.changed.connect(()=> {
set_spincolor(allspins);
});
};
wnck_scr.active_window_changed.connect(()=> {
if (wmclass_entry.is_focus && initial_change == false) {
Wnck.Window? newactive = wnck_scr.get_active_window();
if (newactive != null) {
Wnck.WindowType wtype = newactive.get_window_type();
string classname = newactive.get_class_group_name().down();
if (
classname != "new_shuffler_control" &&
wtype == Wnck.WindowType.NORMAL
) {
wmclass_entry.set_text(classname);
}
}
makesure_offocus();
};
initial_change = false;
});
if (update) {
wmclass_entry.set_text(wmclass);
foreach (string k in foundrules.get_keys()) {
if (k == wmclass) {
// lookup values, set widgets
Variant match = foundrules[k];
string set_monitor = (string)match.get_child_value(0);
if (set_monitor == "") {
set_monitor = default_set;
}
int xpos = int.parse((string)match.get_child_value(1));
int ypos = int.parse((string)match.get_child_value(2));
int grid_ysize = int.parse((string)match.get_child_value(3));
int grid_xsize = int.parse((string)match.get_child_value(4));
int xspan = int.parse((string)match.get_child_value(5));
int yspan = int.parse((string)match.get_child_value(6));
string set_workspace = (string)match.get_child_value(7);
// lazy check; instead of checking all fields separately, create a string of all
xpos_spin.set_value(xpos);
ypos_spin.set_value(ypos);
grid_ysize_spin.set_value(grid_ysize);
grid_xsize_spin.set_value(grid_xsize);
xspan_spin.set_value(xspan);
yspan_spin.set_value(yspan);
int foundmonindex = string_inlist(set_monitor, mons);
screendropdown.active = foundmonindex;
string readable_workspace = get_value_forrule(set_workspace);
int foundwsindex = string_inlist(readable_workspace, allspaces);
if (foundwsindex == -1) {
// if a workspace outside range is set, keep it!
allspaces += readable_workspace;
workspacedropdown.append_text(readable_workspace);
// renew index
foundwsindex = string_inlist(readable_workspace, allspaces);
}
workspacedropdown.active = foundwsindex;
check_changes = @"$k$set_monitor$readable_workspace$grid_xsize$grid_ysize$xpos$ypos$xspan$yspan";
}
}
}
get_task.run();
}
private string makecheckstring(
Gtk.Entry wmclass, OwnSpinButton[] allspins,
ComboBoxText screendropdown, ComboBoxText workspacedropdown
) {
string valuestring = wmclass.get_text().concat(
screendropdown.get_active_text().concat(workspacedropdown.get_active_text())
);
foreach (OwnSpinButton sp in allspins) {
valuestring += sp.get_value().to_string();
}
return valuestring;
}
private uint get_now() {
// get timestamp
return Gdk.X11.get_server_time(timestamp_window);
}
private void makesure_offocus () {
foreach (Wnck.Window w in wnck_scr.get_windows()) {
/*
/ Window title. "Window Shuffler is the application name
/ and should not be translated, only the "Control" -part
*/
if (w.get_name().down() == _("Window Shuffler Control")) {
w.activate(get_now());
break;
}
else if (w.get_name() == "new_shuffler_control") {
w.activate(get_now());
}
}
}
private bool apply_newrule(
Entry e, OwnSpinButton x, OwnSpinButton y, OwnSpinButton xpos,
OwnSpinButton ypos, OwnSpinButton xspan, OwnSpinButton yspan,
ComboBoxText dropdown, ComboBoxText wsdropdown, bool update,
bool wmclass_changed
) {
/*
only ask for confirmation if
- an existing file changed
- a new file will overwrite an existing file
*/
// defaults:
bool apply = true;
string monitorline = "";
string workspaceline = "";
string warninghead = _("Replace window-rule"); // when creating new file, but name exists
if (update) {
warninghead = _("Save changes to window-rule"); // when just updating
}
string classname = e.get_text();
// 1. let's first check if input classname is correct
if (classname == "") {
apply = false;
set_widgetstyle(e, "red_text");
}
// 2. if so, if wm entry, thus filename changes:
else if (wmclass_changed) {
warninghead = _("Save changes to renamed window-rule");
apply = ask_confirm(@"$warninghead: $classname?");
}
// 3. if file is "only" updated:
else {
foreach (string k in foundrules.get_keys()) {
if (e.get_text() == k) {
apply = ask_confirm(
@"$warninghead: $classname?"
);
break;
}
}
}
int cols = x.get_value();
int rows = y.get_value();
int xposval = xpos.get_value();
int yposval = ypos.get_value();
int xspanval = xspan.get_value();
int yspanval = yspan.get_value();
string? disp = dropdown.get_active_text();
workspaceline = "";
string ws = get_value_forrule(wsdropdown.get_active_text(), true);
if (ws != "") {
workspaceline = @"\nTargetWorkspace=$ws";
}
if (disp != null && disp != default_set) {
monitorline = @"\nMonitor=$disp";
}
string filecontent = @"Cols=$cols".concat(
@"\nRows=$rows", @"\nXPosition=$xposval",
@"\nYPosition=$yposval",@"\nXSpan=$xspanval",
@"\nYSpan=$yspanval", monitorline, workspaceline
);
if (apply) {
write_edit(filecontent, classname);
get_task.destroy();
return true;
}
get_task.destroy();
return false;
}
private void write_edit(string filecontent, string classname) {
string filepath = windowrule_location.concat(
@"/$classname.windowrule"
);
try {
File targetfile = File.new_for_path(filepath);
if (targetfile.query_exists ()) {
// ask for replace permission first
targetfile.delete();
}
// Create a new file with this name
var file_stream = targetfile.create (
FileCreateFlags.REPLACE_DESTINATION
);
var data_stream = new DataOutputStream (file_stream);
data_stream.put_string (filecontent);
}
catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
}
}
private void set_spincolor(OwnSpinButton[] spins) {
var xsize = spins[0];
var ysize = spins[1];
var xpos = spins[2];
var ypos = spins[3];
var xspan = spins[4];
var yspan = spins[5];
OwnSpinButton[] xes = {xsize, xpos, xspan};
OwnSpinButton[] yses = {ysize, ypos, yspan};
bool xred = xsize.get_value() < (xpos.get_value() + xspan.get_value());
bool yred = ysize.get_value() < (ypos.get_value() + yspan.get_value());
foreach (OwnSpinButton spbx in xes) {
spbx.set_warning_color(xred);
}
foreach (OwnSpinButton spby in yses) {
spby.set_warning_color(yred);
}
bool sens = !(xred || yred);
applytask_button.set_sensitive(sens);
}
private string create_dirs_file (string subpath) {
// defines, and if needed, creates directory for rules
string homedir = Environment.get_home_dir();
string fullpath = GLib.Path.build_path(
GLib.Path.DIR_SEPARATOR_S, homedir, subpath
);
GLib.File file = GLib.File.new_for_path(fullpath);
try {
file.make_directory_with_parents();
}
catch (Error e) {
/* the directory exists, nothing to be done */
}
return fullpath;
}
private void update_currentrules() {
foreach (Widget w in newrulesgrid.get_children()) {
w.destroy();
}
// update ruleslist
newrulesgrid.set_column_spacing(15);
// headers
string[] rules_columnheaders = {
// TRANSLATORS: it is important to keep these column headers as short as possible
_("WM-class"),
_("Grid"), "X, Y",
_("Span"),
_("Display"),
_("Workspace")
};
int col = 0;
foreach (string s in rules_columnheaders) {
Label newheader = new Label(s);
if (col == 0) {
newheader.set_size_request(120, 10);
newheader.xalign = 0;
}
newheader.get_style_context().add_class("justbold");
newrulesgrid.attach(newheader, col, 0, 1, 1);
col += 1;
}
int currow = 1;
try {
client = Bus.get_proxy_sync (
BusType.SESSION, "org.UbuntuBudgie.ShufflerInfoDaemon",
("/org/ubuntubudgie/shufflerinfodaemon")
);
foundrules = client.get_rules();
GLib.List<weak string> keys = foundrules.get_keys();
foreach (string k in keys) {
Gtk.Button taskeditbutton = new Button.from_icon_name(
"document-edit-symbolic", Gtk.IconSize.BUTTON
);
Gtk.Button taskdeletebutton = new Button.from_icon_name(
"user-trash-symbolic", Gtk.IconSize.BUTTON
);
taskdeletebutton.set_size_request(10, 10);
taskdeletebutton.set_relief(Gtk.ReliefStyle.NONE);
taskeditbutton.set_size_request(10, 10);
taskeditbutton.set_relief(Gtk.ReliefStyle.NONE);
taskeditbutton.clicked.connect(()=> {
call_dialog(k);
});
Variant windowrule = foundrules[k];
string monitor = (string)windowrule.get_child_value(0);
if (monitor == "") {
monitor = default_set;
}
string xposition = (string)windowrule.get_child_value(1);
string yposition = (string)windowrule.get_child_value(2);
string rows = (string)windowrule.get_child_value(3);
string cols = (string)windowrule.get_child_value(4);
string xspan = (string)windowrule.get_child_value(5);
string yspan = (string)windowrule.get_child_value(6);
string ws = get_value_forrule(
(string)windowrule.get_child_value(7)
);
Label newlabel = makelabel(k, 0);
Label newgridsize = new Label(cols + "x" + rows);
Label newposition = new Label(xposition + ", " + yposition);
Label newspan = new Label(xspan + "x" + yspan);
Label newmonitor = new Label(monitor);
Label newworkspace = new Label(ws);
newrulesgrid.attach(newlabel, 0, currow, 1, 1);
newrulesgrid.attach(newgridsize, 1, currow, 1, 1);
newrulesgrid.attach(newposition, 2, currow, 1, 1);
newrulesgrid.attach(newspan, 3, currow, 1, 1);
newrulesgrid.attach(newmonitor, 4, currow, 1, 1);
newrulesgrid.attach(newworkspace, 5, currow, 1, 1);
newrulesgrid.attach(new Label(" "), 6, currow, 1, 1);
newrulesgrid.attach(taskeditbutton, 7, currow, 1, 1);
newrulesgrid.attach(taskdeletebutton, 8, currow, 1, 1);
string filepath = windowrule_location.concat(
@"/$k.windowrule"
);
taskdeletebutton.clicked.connect(()=> {
string del = _("Delete");
string wrule = _("windowrule");
if (ask_confirm(@"$del $wrule $k?")) {
File targetfile = File.new_for_path(filepath);
try {
targetfile.delete();
}
catch (Error e) {
stderr.printf ("%s\n", e.message);
}
}
});
currow += 1;
}
newrulesgrid.show_all();
rulesgrid.show_all();
}
catch (Error e) {
stderr.printf ("%s\n", e.message);
}
}
private void add_series_toggrid(
Grid grid, string[] leftitems, string[] rightitems,
int startint = 0) {
// just an optimizasition to add arrays of items to a grid
for (int i = 0; i < leftitems.length; i++) {
Label newlabel = makelabel(leftitems[i], 0);
grid.attach(newlabel, 0, i + startint, 1, 1);
grid.attach(new Label("\t\t"), 1, i + startint, 1, 1);
Label newshortcut = makelabel(rightitems[i], 0);
grid.attach(newshortcut, 2, i + startint, 1, 1);
}
}
public ShufflerControlWindow(int page=0) {
setup_client();
initialiseLocaleLanguageSupport();
wnck_scr = Wnck.Screen.get_default();
// window stuff
/*
/ Window title. "Window Shuffler is the application name
/ and should not be translated, only the "Control" -part
*/
this.title = _("Window Shuffler Control");
// this.default_width = 800;
this.set_resizable(false);
// watch rulesdir
windowrule_location = create_dirs_file(
".config/budgie-extras/shuffler/windowrules"
);