Repository navigation
Expand file tree
/
Copy pathgridwindow.vala
More file actions
644 lines (604 loc) · 22.4 KB
/
Copy pathgridwindow.vala
File metadata and controls
644 lines (604 loc) · 22.4 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
using Gtk;
using Cairo;
using Gdk;
using Gdk.X11;
/*
* ShufflerIII
* 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/>.
*/
/*
/ Button color management
/ -----------------------
/ variables played with:
/ - int[] currentlycolored - array of indexes (buttonarr) of currently (permanently, not hovered) colored buttons
/ - int[] currselected - array of indices of corners of the area (can represent either one or two buttons)
/
/ on click:
/ if currselected.length == 1, -and- shift is pressed, the second button(index) is added
/ to currselected and the area in between is calculated -> color button area, move subject window to spanning size.
/ if currselected.length == 2 or 1, (new) currselected only contains the newly pressed button ->
/ move subject window. [send_to_pos(), manage_selection(), manage_selected_color()]
/ while the above is managed, the array currentlycolored is maintained to include all actually colored buttons,
/ to simplify button hover (-color) management [manage_selected_color()].
/ on the occasion of a grid change or a change in window subject, all arrays and button colors are reset
/
/ on hover:
/ temporarily combine data from currselected and hovered button -> calculate min/max, set color.
/ on leave, reset to currentlycolored.
*/
namespace GridWindowSection {
Wnck.Screen wnckscr;
ShufflerInfoClient client;
Gtk.Window? gridgui;
Gdk.X11.Window timestamp_window;
File gridtrigger;
[DBus (name = "org.UbuntuBudgie.ShufflerInfoDaemon")]
interface ShufflerInfoClient : Object {
public abstract int[] get_grid () throws Error;
public abstract int get_greyshade () throws Error;
public abstract void set_greyshade (int newbrightness) throws Error;
public abstract void set_grid (int cols, int rows) throws Error;
public abstract void show_tilepreview (int col, int row, int width = 1, int height = 1) throws Error;
public abstract void kill_tilepreview () throws Error;
}
private void setup_client () {
try {
client = Bus.get_proxy_sync (
BusType.SESSION, "org.UbuntuBudgie.ShufflerInfoDaemon",
("/org/ubuntubudgie/shufflerinfodaemon")
);
}
catch (Error e) {
stderr.printf ("%s\n", e.message);
}
}
public class GridWindow: Gtk.Window {
bool shiftispressed;
int[] currselected; // max 2, only corners of selection
int gridcols;
int gridrows;
Gtk.Button[] buttonarr;
int[] xpos;
int[] ypos;
Gtk.Grid buttongrid;
ulong? previously_active;
Gtk.Grid maingrid;
int[] currentlycolored; // arr of all currently colored buttons
string gridcss;
Gtk.CssProvider css_provider;
string raw_gridcss = """
.gridmanage {
border-radius: 3px;
background-color: curr_greyshade;
box-shadow: none;
}
.gridmanage:hover {
background-color: rgb(0, 100, 148);
transition: 0.05s linear;
}
.selected {
background-color: rgb(0, 77, 128);
transition: 0.05s linear;
}
""";
public GridWindow() {
this.title = "Gridwindows";
this.set_skip_taskbar_hint(true);
this.set_position(Gtk.WindowPosition.CENTER_ALWAYS);
this.key_press_event.connect(on_shiftpress);
this.key_release_event.connect(on_shiftrelease);
int initial_greyshade = fetch_greyshade();
gridcss = calculate_css(initial_greyshade);
// greyshade-scrolling
this.add_events(Gdk.EventMask.SCROLL_MASK);
this.scroll_event.connect(set_brightnessfromscroll);
wnckscr.active_window_changed.connect(get_subject);
Wnck.Window? curr_active = wnckscr.get_active_window();
previously_active = -1;
if (curr_active != null) {
previously_active = curr_active.get_xid();
}
int[] colsrows = get_setcolsrows();
gridcols = colsrows[0];
gridrows = colsrows[1];
// whole bunch of styling
var screen = this.get_screen();
this.set_app_paintable(true);
var visual = screen.get_rgba_visual();
this.set_visual(visual);
this.draw.connect(on_draw);
css_provider = new Gtk.CssProvider();
load_css();
// grid and stuff
maingrid = new Gtk.Grid();
maingrid.set_column_spacing(6);
maingrid.set_row_spacing(6);
this.add(maingrid);
setgrid();
//add_gridcontrols();
currselected = {};
maingrid.show_all();
this.set_decorated(false);
this.set_keep_above(true);
this.show_all();
}
private void load_css () {
try {
css_provider.load_from_data(gridcss);
Gtk.StyleContext.add_provider_for_screen(
screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER
);
}
catch (Error e) {
}
}
private bool set_brightnessfromscroll (Gdk.EventScroll scrollevent) {
Gdk.ScrollDirection upordown = scrollevent.direction;
if (upordown == Gdk.ScrollDirection.UP) {
set_brightnessongrid("plus");
}
else if (upordown == Gdk.ScrollDirection.DOWN) {
set_brightnessongrid("minus");
}
return false;
}
private int fetch_greyshade () {
// fallback to 90 on error
int currshade = 90;
try {
currshade = client.get_greyshade();
}
catch (Error e) {
print("cannot get greyshade\n");
}
return currshade;
}
private string calculate_css (int currshade) {
// convert percent to 255
int shade = (int)(255 * currshade / 100);
string newshade = @"rgb($shade, $shade, $shade)";
gridcss = raw_gridcss.replace("curr_greyshade", newshade);
return gridcss;
}
private bool set_brightnessongrid (string pressed) {
// falback
int currbrightness = fetch_greyshade();
int newbrightness = currbrightness;
if (pressed == "KP_Add" || pressed == "plus") {
if (currbrightness <= 98) {
newbrightness = currbrightness + 2;
}
}
else if (pressed == "KP_Subtract" || pressed == "minus") {
if (currbrightness >= 2) {
newbrightness = currbrightness - 2;
}
}
try {
client.set_greyshade(newbrightness);
}
catch (Error e) {
print("unable to change greyshade setting\n");
}
gridcss = calculate_css(newbrightness);
load_css();
killpreview();
buttongrid.destroy();
setgrid();
maingrid.show_all();
return false;
}
private int[] get_setcolsrows () {
// get cols & rows from dconf
int[] colsrows = {0, 0};
try {
colsrows = client.get_grid();
}
catch (Error E) {
}
return colsrows;
}
private int find_buttonindex(Gtk.Button b) {
// look up button index from array (to look up col/row)
int i = 0;
foreach (Gtk.Button button in buttonarr) {
if (button == b) {
return i;
}
i += 1;
}
return -1;
}
private bool winstillexists (ulong subj) {
/*
/ not-so-elegant way to re-check if the subject hasn't just
/ been closed by user. its xid still exists then
*/
foreach (Wnck.Window w in wnckscr.get_windows()) {
if (w.get_xid() == subj) {
return true;
}
}
print("Oops, no window was selected!\n");
return false;
}
private void send_to_pos (Gtk.Button b) {
// here we send the subject window to its targeted position, using tile_active
int index = find_buttonindex(b);
if (index != -1 && previously_active != -1 && winstillexists(previously_active)) {
string cmd_args = manage_selection(b);
// manage preview shade separately: different rules, algorithm (first make this work)
string cm = Config.SHUFFLER_DIR + "/tile_active ".concat(
cmd_args, " id=", @"$previously_active", " fromgrid", " nosoftmove"
);
try {
Process.spawn_command_line_async(cm);
}
catch (SpawnError e) {
}
}
}
private int[] get_selectedarea (
int[] b_indices, int minx = 100,
int miny = 100, int maxx = 0, int maxy = 0
) {
// get min/max x, min/max y, span x/y of given button-indices
foreach (int n in b_indices) {
int x_comp = xpos[n];
int y_comp = ypos[n];
if (x_comp < minx) {
minx = x_comp;
}
if (y_comp < miny) {
miny = y_comp;
}
if (x_comp > maxx) {
maxx = x_comp;
}
if (y_comp > maxy) {
maxy = y_comp;
}
}
int w = maxx + 1 - minx;
int h = maxy + 1 - miny;
return {minx, miny, maxx, maxy, w, h};
}
private string manage_selection (Gtk.Button b) {
/*
/ on-click functionality
/ here we check if we have a multi-span selection
/ & create args for move
// check if active window != null!
*/
int n_arrcontent = currselected.length;
int latest_pressed = find_buttonindex(b);
if (n_arrcontent == 0 || (n_arrcontent == 1 && shiftispressed)) {
currselected += latest_pressed;
}
else {
currselected = {latest_pressed};
}
// update n_arrcontent
n_arrcontent = currselected.length;
int minx = 100;
int miny = 100;
int maxx = 0;
int maxy = 0;
int w = 1;
int h = 1;
if (n_arrcontent == 2) {
int[] areadata = get_selectedarea(currselected);
minx = areadata[0];
miny = areadata[1];
maxx = areadata[2];
maxy = areadata[3];
w = maxx + 1 - minx;
h = maxy + 1 - miny;
}
else {
minx = xpos[latest_pressed];
miny = ypos[latest_pressed];
}
manage_selected_color(n_arrcontent, minx, miny, maxx, maxy);
return @"$minx $miny $gridcols $gridrows $w $h";
}
private bool check_int(int n, int[] arr) {
// see if int in array
for (int i=0; i < arr.length; i++) {
if(n == arr[i]) return true;
} return false;
}
private void unsetsetcolor_onhover () {
// on leave, reset colored to current selection
foreach (Gtk.Button b in buttonarr) {
if (!check_int(find_buttonindex(b), currentlycolored)) {
var ct = b.get_style_context();
ct.remove_class("selected");
}
}
}
private void manage_selected_color (
int n_arrcontent, int minx, int miny, int maxx, int maxy
) {
// after click, set the selected color for button or area
// do your bookkeeping on colored buttons
currentlycolored = {};
if (n_arrcontent == 1) {
Gtk.Button newselected = buttonarr[currselected[0]];
foreach (Gtk.Button b in buttonarr) {
var st_gb = b.get_style_context();
if (b == newselected) {
st_gb.add_class("selected");
currentlycolored += find_buttonindex(b);
}
else {
st_gb.remove_class("selected");
}
}
}
else {
foreach (Gtk.Button b in buttonarr) {
int index = find_buttonindex(b);
int x_comp = xpos[index];
int y_comp = ypos[index];
var st_gb = b.get_style_context();
if (maxx >= x_comp >= minx && maxy >= y_comp >= miny) {
st_gb.add_class("selected");
currentlycolored += index;
}
else {
st_gb.remove_class("selected");
}
}
}
}
private void unset_colors () {
foreach (Gtk.Button b in buttonarr) {
var st_gb = b.get_style_context();
st_gb.remove_class("selected");
}
}
private void get_subject () {
// bookkeeping on the window to move
ulong old_active = previously_active;
Wnck.Window? curr_active = wnckscr.get_active_window();
if (curr_active != null) {
Wnck.WindowType type = curr_active.get_window_type ();
string wname = curr_active.get_name();
if (
wname != "tilingpreview" &&
wname != "Gridwindows" &&
type == Wnck.WindowType.NORMAL
) {
previously_active = curr_active.get_xid();
}
makesure_offocus();
}
// unset colors on subject change
if (old_active != previously_active) {
unset_colors();
currentlycolored = {};
}
}
private void setcolor_onhover (Gtk.Button hovered) {
if (shiftispressed && currentlycolored.length == 1) {
int[] temporarycolored = currentlycolored;
temporarycolored += find_buttonindex(hovered);
int[] temp_area = get_selectedarea(temporarycolored);
int minx = temp_area[0];
int miny = temp_area[1];
int maxx = temp_area[2];
int maxy = temp_area[3];
foreach (Gtk.Button b in buttonarr) {
int index = find_buttonindex(b);
int x_comp = xpos[index];
int y_comp = ypos[index];
var st_gb = b.get_style_context();
if (maxx >= x_comp >= minx && maxy >= y_comp >= miny) {
st_gb.add_class("selected");
}
}
try {
client.show_tilepreview(
minx, miny, maxx - minx + 1, maxy - miny + 1
);
}
catch (Error e) {
}
}
else {
int b_index = find_buttonindex(hovered);
int col = xpos[b_index];
int row = ypos[b_index];
try {
client.show_tilepreview(col, row);
}
catch (Error e) {
}
}
}
private bool on_shiftrelease (Gdk.EventKey key) {
// to keep record of Shift state
string released = Gdk.keyval_name(key.keyval);
if (released.contains("Shift")) {
shiftispressed = false;
}
return false;
}
private bool on_shiftpress (Gdk.EventKey key) {
// to keep record of Shift state & send through if not Shift
string pressed = Gdk.keyval_name(key.keyval);
if (pressed.contains("Shift")) {
shiftispressed = true;
}
else if (pressed == "Escape") {
gridgui.destroy();
try {
gridtrigger.delete();
}
catch (Error e) {
}
}
else if (
pressed == "KP_Add" ||
pressed == "KP_Subtract" ||
pressed == "plus" ||
pressed == "minus"
) {
set_brightnessongrid(pressed);
}
else {
managegrid(pressed);
}
return false;
}
private bool killpreview () {
// kill the preview shade
try {
client.kill_tilepreview();
}
catch (Error e) {
}
return false;
}
private bool managegrid (string pressed) {
// here we set cols.rows on the grid gui,
// set dconf vals accordingly
killpreview();
currselected = {};
currentlycolored = {};
int[] currgrid = get_setcolsrows();
int currcols = currgrid[0];
int currrows = currgrid[1];
switch (pressed) {
case "Up":
if (currrows > 1) {
gridrows = currrows - 1;
}
break;
case "Down":
if (currrows < 10) {
gridrows = currrows + 1;
}
break;
case "Left":
if (currcols > 1) {
gridcols = currcols - 1;
}
break;
case "Right":
if (currcols < 10) {
gridcols = currcols + 1;
}
break;
}
try {
client.set_grid(gridcols, gridrows);
}
catch (Error e) {
}
buttongrid.destroy();
setgrid();
maingrid.show_all();
this.resize(10,10);
return false;
}
private bool on_draw (Widget da, Context ctx) {
// transparency
ctx.set_source_rgba(0.15, 0.15, 0.15, 0.0);
ctx.set_operator(Cairo.Operator.SOURCE);
ctx.paint();
ctx.set_operator(Cairo.Operator.OVER);
return false;
}
private void setgrid() {
// create the gui grid + buttons on it
buttongrid = new Gtk.Grid();
buttongrid.set_column_spacing(3);
buttongrid.set_row_spacing(3);
maingrid.attach(buttongrid, 0, 0, 1, 1);
for (int iy=0; iy < gridrows; iy++) {
for (int ix=0; ix < gridcols; ix++) {;
var gridbutton = new Gtk.Button();
gridbutton.set_size_request(50, 50);
var st_gb = gridbutton.get_style_context();
st_gb.add_class("gridmanage");
buttongrid.attach(gridbutton, ix, iy, 1, 1);
xpos += ix;
ypos += iy;
buttonarr += gridbutton;
gridbutton.clicked.connect(send_to_pos);
gridbutton.enter_notify_event.connect(()=> {
setcolor_onhover(gridbutton);
return false;
});
gridbutton.leave_notify_event.connect(()=> {
unsetsetcolor_onhover();
return false;
});
gridbutton.leave_notify_event.connect(killpreview);
}
}
}
}
private void actonfile(File file, File? otherfile, FileMonitorEvent event) {
if (event == FileMonitorEvent.CREATED) {
gridgui = new GridWindow();
}
else if (event == FileMonitorEvent.DELETED) {
gridgui.destroy();
}
}
private uint get_now () {
// get timestamp
return Gdk.X11.get_server_time(timestamp_window);
}
private void makesure_offocus () {
foreach (Wnck.Window w in wnckscr.get_windows()) {
if (w.get_name() == "Gridwindows") {
w.activate(get_now());
}
}
}
public static void main(string[] args) {
/*
/ minimal main. eventually need to insert a signal watcher to
/ create/destroy the grid window
*/
setup_client();
Gtk.init(ref args);
wnckscr = Wnck.Screen.get_default();
wnckscr.force_update();
wnckscr.window_opened.connect(makesure_offocus);
// X11 stuff, non-dynamic part
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);
// monitoring files / dirs
FileMonitor monitor;
string user = Environment.get_user_name();
var tmp = Environment.get_tmp_dir() + "/";
gridtrigger = File.new_for_path(
tmp.concat(user, "_gridtrigger")
);
try {
monitor = gridtrigger.monitor(FileMonitorFlags.NONE, null);
monitor.changed.connect(actonfile);
}
catch (Error e) {
}
Gtk.main();
}
}