-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.c
More file actions
2719 lines (2505 loc) · 71.9 KB
/
Copy pathsettings.c
File metadata and controls
2719 lines (2505 loc) · 71.9 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
// SPDX-License-Identifier: GPL-3.0-only
/*
* Copyright (C) 2026 robinpie <robin@dreamstation.systems>
*
* 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, version 3.
*
* 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/>.
*/
/* settings.c - the in-app settings panel.
*
* A desktop launcher icon opens a modal panel that lands on a Control-Panel
* grid of tiles ("Keybindings", "Terminal", "Appearance"); each tile opens a
* sub-view and captures all input while open (see the hooks in input.c).
* Chord parsing/rebinding lives in input.c/config.c; this file is UI + state.
*/
#define _GNU_SOURCE
#include "viewpoint.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ICON_Y 1
#define ICON_X 1
#define ICON_W 12
#define ICON_H 4
#define PANEL_W 56
#define PANEL_CHROME 4 /* top border + status + hint + bottom border */
/* Control-Panel grid: a fixed lattice of tiles (à la a classic Control Panel),
* the panel's landing page. Only the first GRID_ENTRY_COUNT cells are populated;
* the rest are drawn as empty placeholders for future settings. */
#define GRID_COLS 3
#define GRID_ROWS 3
#define GRID_CELLS (GRID_COLS * GRID_ROWS)
#define TILE_W 16
#define TILE_H 5
#define TILE_GAP 1
/* The grid sits inside the panel border with a one-cell margin; a title row and
* a status row bracket it. */
#define GRID_ORIGIN_Y 2 /* first tile row (row 0 border, row 1 gap) */
#define GRID_ORIGIN_X 2 /* first tile col (col 0 border, col 1 margin) */
typedef struct {
const char *glyph;
const char *label;
settings_view view;
} grid_entry;
static const grid_entry g_grid_entries[] = {
{ "⌨", "Keybindings", SETTINGS_VIEW_KEYBINDINGS },
{ "▤", "Terminal", SETTINGS_VIEW_TERMINAL },
{ "▦", "Appearance", SETTINGS_VIEW_APPEARANCE },
{ "▣", "Desktop Icons", SETTINGS_VIEW_ICONS },
{ "◐", "Animations", SETTINGS_VIEW_ANIMATIONS },
};
#define GRID_ENTRY_COUNT \
((int)(sizeof(g_grid_entries) / sizeof(g_grid_entries[0])))
/* ----- the Terminal view's rows -----
* A data-driven list of integer settings. Each row points at a VpConfig field
* (by offset, so it works against any WM's config), carries its bounds/step and
* the vp_setting it maps to (for the manual-shadow warning), and an optional
* live-apply hook. Adding a numeric Terminal setting is one row here plus its
* config plumbing - the editor UI, clamping, and shadow warning all follow. */
typedef struct {
const char *label;
vp_setting setting; /* for config_manual_shadows_setting() */
size_t field_off; /* offsetof(VpConfig, <int field>) */
int min, max, step;
const char *zero_label; /* shown in place of "0" (e.g. "off"), or NULL */
void (*on_change)(WM *); /* live-apply to open windows, or NULL */
} term_row;
static void term_apply_scrollback(WM *wm); /* defined below */
static const term_row g_term_rows[] = {
{ "Scrollback lines", SETTING_SCROLLBACK,
offsetof(VpConfig, scrollback_max), 0, VP_SCROLLBACK_LIMIT, 100,
"off", term_apply_scrollback },
{ "Scroll step (lines)", SETTING_SCROLL_STEP,
offsetof(VpConfig, scroll_step), 1, VP_SCROLL_STEP_MAX, 1, NULL,
NULL },
};
#define TERM_ROWS ((int)(sizeof(g_term_rows) / sizeof(g_term_rows[0])))
/* The Terminal view's one non-numeric row: the shell command a copy pipes the
* selection into. It sits below the adjustable rows and is typed, not nudged,
* so everything that walks the view counts rows with term_total_rows(). */
#define TERM_ROW_CLIPBOARD TERM_ROWS
static int term_total_rows(void)
{
return TERM_ROWS + 1;
}
static int *term_field(WM *wm, int i)
{
return (int *)((char *)&wm->config + g_term_rows[i].field_off);
}
/* ----- the Animations view's rows -----
* The same shape as term_row, for the on/off switches over animated chrome.
* A row is a bool in VpConfig plus the vp_setting it maps to; nothing here
* applies live, because the animations read their switch as they run. */
typedef struct {
const char *label;
vp_setting setting; /* for config_manual_shadows_setting() */
size_t field_off; /* offsetof(VpConfig, <bool field>) */
} anim_row;
static const anim_row g_anim_rows[] = {
{ "Size indicator fade-out", SETTING_SIZEOSD_FADE,
offsetof(VpConfig, sizeosd_fade) },
};
#define ANIM_ROWS ((int)(sizeof(g_anim_rows) / sizeof(g_anim_rows[0])))
static bool *anim_field(WM *wm, int i)
{
return (bool *)((char *)&wm->config + g_anim_rows[i].field_off);
}
/* What in-app text entry is currently building (Settings.edit_kind). */
enum {
EDIT_BG_IMAGE = 0, /* desktop background image path */
EDIT_COLOR, /* a color override, naming edit_color_idx */
EDIT_ICON_LABEL, /* a launcher's caption, naming edit_launcher */
EDIT_ICON_CMD, /* a launcher's command */
EDIT_CLIPBOARD_CMD, /* the Terminal view's clipboard helper command */
};
static void clamp_icon_pos(const WM *wm, int h, int w, int *y, int *x)
{
int maxy = (int)wm->scr_rows - (wm->taskbar ? 1 : 0) - h;
int maxx = (int)wm->scr_cols - w;
if (*y > maxy)
*y = maxy;
if (*x > maxx)
*x = maxx;
if (*y < 0)
*y = 0;
if (*x < 0)
*x = 0;
}
static int app_total_rows(void); /* Appearance view row count (defined below) */
static int icons_total_rows(const WM *wm); /* Desktop Icons view row count */
static int total_rows(void)
{
return keymap_action_count() + 1;
}
static bool is_toggle_row(int r)
{
return r == keymap_action_count();
}
static int grid_inner_w(void)
{
return GRID_COLS * TILE_W + (GRID_COLS - 1) * TILE_GAP;
}
static int grid_inner_h(void)
{
return GRID_ROWS * TILE_H + (GRID_ROWS - 1) * TILE_GAP;
}
static void tile_rect(int i, int *ty, int *tx)
{
int row = i / GRID_COLS;
int col = i % GRID_COLS;
*ty = GRID_ORIGIN_Y + row * (TILE_H + TILE_GAP);
*tx = GRID_ORIGIN_X + col * (TILE_W + TILE_GAP);
}
static void panel_geom(const WM *wm, int *y, int *x, int *h, int *w)
{
int W, H;
if (wm->settings.view == SETTINGS_VIEW_GRID) {
/* border + margin on each side, a title row above and status row below. */
W = grid_inner_w() + 2 * GRID_ORIGIN_X;
H = grid_inner_h() + GRID_ORIGIN_Y +
2; /* +status row +bottom border */
} else {
W = PANEL_W;
if (W < 24)
W = 24;
int rows = total_rows();
if (wm->settings.view == SETTINGS_VIEW_TERMINAL) {
rows = term_total_rows();
} else if (wm->settings.view == SETTINGS_VIEW_APPEARANCE) {
rows = app_total_rows();
} else if (wm->settings.view == SETTINGS_VIEW_ICONS) {
rows = icons_total_rows(wm);
} else if (wm->settings.view == SETTINGS_VIEW_ANIMATIONS) {
rows = ANIM_ROWS;
}
H = rows + PANEL_CHROME;
if (H < PANEL_CHROME + 1)
H = PANEL_CHROME + 1;
}
if (W > (int)wm->scr_cols - 2)
W = (int)wm->scr_cols - 2;
if (H > (int)wm->scr_rows - 2)
H = (int)wm->scr_rows - 2;
int py = ((int)wm->scr_rows - H) / 2;
int px = ((int)wm->scr_cols - W) / 2;
*y = py < 0 ? 0 : py;
*x = px < 0 ? 0 : px;
*h = H;
*w = W;
}
static int viewport_rows(const WM *wm)
{
int H = wm->settings.panel ? comp_layer_rect(wm->settings.panel).h : 0;
int v = H - PANEL_CHROME;
return v < 1 ? 1 : v;
}
/* The Appearance view's fixed rows (a row per themeable color follows them). */
enum {
APP_ROW_THEME = 0,
APP_ROW_BG,
APP_ROW_FIT,
APP_ROW_IMAGE,
APP_ROW_KEEP,
APP_FIXED_ROWS,
};
static int app_total_rows(void)
{
return APP_FIXED_ROWS + vp_theme_field_count();
}
/* The Desktop Icons view: a row per configured launcher, then an "add" row that
* acts as the button for creating one. */
static int icons_total_rows(const WM *wm)
{
return wm->config.nlaunchers + 1;
}
static bool is_add_row(const WM *wm, int row)
{
return row == wm->config.nlaunchers;
}
static void clamp_scroll_n(WM *wm, int n)
{
Settings *s = &wm->settings;
int v = viewport_rows(wm);
if (s->sel < 0)
s->sel = 0;
if (s->sel >= n)
s->sel = n - 1;
if (s->sel < s->scroll)
s->scroll = s->sel;
if (s->sel >= s->scroll + v)
s->scroll = s->sel - v + 1;
int maxscroll = n - v;
if (maxscroll < 0)
maxscroll = 0;
if (s->scroll > maxscroll)
s->scroll = maxscroll;
if (s->scroll < 0)
s->scroll = 0;
}
static void clamp_scroll(WM *wm)
{
Settings *s = &wm->settings;
int v = viewport_rows(wm);
int n = total_rows();
if (s->sel < 0)
s->sel = 0;
if (s->sel >= n)
s->sel = n - 1;
if (s->sel < s->scroll)
s->scroll = s->sel;
if (s->sel >= s->scroll + v)
s->scroll = s->sel - v + 1;
int maxscroll = n - v;
if (maxscroll < 0)
maxscroll = 0;
if (s->scroll > maxscroll)
s->scroll = maxscroll;
if (s->scroll < 0)
s->scroll = 0;
}
/* ----- the list scrollbar's geometry -------------------------------------
*
* Shared by the painter and the mouse handlers, so a click always lands on the
* thumb the user can actually see. The rail is the last interior column of the
* panel; its track is the viewport rows, i.e. panel rows 1..viewport_rows(). */
/* Thumb geometry for a `v`-row track showing `n` rows from `scroll`. The thumb
* is sized proportionally and its travel spans the whole track, so it sits
* flush at the top at scroll 0 and flush at the bottom at the last scroll
* position. */
static void scrollbar_thumb(int v, int n, int scroll, int *top, int *len)
{
int thumb = (v * v + n - 1) / n; /* round up, so it is never 0 */
/* Always leave a cell of travel: a thumb filling the track would read as
* "nothing to scroll" even with rows hidden (a list one row too long). */
if (thumb > v - 1)
thumb = v - 1;
if (thumb < 1)
thumb = 1;
int maxscroll = n - v;
if (scroll < 0)
scroll = 0;
if (scroll > maxscroll)
scroll = maxscroll;
*len = thumb;
*top = maxscroll > 0 ? (scroll * (v - thumb) + maxscroll / 2) / maxscroll :
0;
}
/* Row count of the current view's scrolling list, or 0 for a view that has no
* scrollbar (the grid, and Terminal/Animations - both sized to always fit). */
static int list_len(const WM *wm)
{
if (wm->settings.view == SETTINGS_VIEW_KEYBINDINGS) {
return total_rows();
}
if (wm->settings.view == SETTINGS_VIEW_APPEARANCE) {
return app_total_rows();
}
if (wm->settings.view == SETTINGS_VIEW_ICONS) {
return icons_total_rows(wm);
}
return 0;
}
/* True when the current view is showing a scrollbar, filling in the list
* length, track height and the rail's column within the panel. */
static bool scrollbar_geom(const WM *wm, int *n, int *v, int *railx)
{
if (!wm->settings.panel) {
return false;
}
int len = list_len(wm);
int rows = viewport_rows(wm);
if (len <= rows || rows < 1) {
return false;
}
*n = len;
*v = rows;
*railx = comp_layer_rect(wm->settings.panel).w - 2;
return true;
}
/* Scroll the list to `scroll` without going through the selection. The painters
* re-clamp scroll to keep the selection on screen, so the selection is carried
* along with the viewport rather than left behind to drag it back. */
static void set_scroll(WM *wm, int n, int scroll)
{
Settings *s = &wm->settings;
int v = viewport_rows(wm);
int maxscroll = n - v;
if (maxscroll < 0)
maxscroll = 0;
if (scroll < 0)
scroll = 0;
if (scroll > maxscroll)
scroll = maxscroll;
s->scroll = scroll;
if (s->sel < s->scroll)
s->sel = s->scroll;
if (s->sel > s->scroll + v - 1)
s->sel = s->scroll + v - 1;
if (s->sel < 0)
s->sel = 0;
if (s->sel >= n)
s->sel = n - 1;
settings_damage(s);
}
/* Put the thumb's top at track row `top`, inverting scrollbar_thumb(). */
static void scroll_to_thumb(WM *wm, int n, int v, int top)
{
int tt, len;
scrollbar_thumb(v, n, wm->settings.scroll, &tt, &len);
int travel = v - len;
if (travel < 1) {
return; /* a one-row track: nowhere to drag to */
}
if (top < 0)
top = 0;
if (top > travel)
top = travel;
int maxscroll = n - v;
set_scroll(wm, n, (top * maxscroll + travel / 2) / travel);
}
/* Resting top-left of the Settings tile: a user-dragged position from the
* config if there is one, otherwise the built-in top-left corner. Always
* clamped onto the current screen. */
static void settings_icon_geom(const WM *wm, int *y, int *x)
{
if (wm->config.settings_icon_y >= 0) {
*y = wm->config.settings_icon_y;
*x = wm->config.settings_icon_x;
} else {
*y = ICON_Y;
*x = ICON_X;
}
clamp_icon_pos(wm, ICON_H, ICON_W, y, x);
}
/* ----- the shared desktop-tile look ---------------------------------------
*
* Every desktop icon is the same tile: a rounded border, a glyph on the first
* line and a caption centered under it. Only the palette, glyph and caption
* differ, so they are drawn by one painter rather than three that can drift. */
static void draw_icon_tile(struct ncplane *p, int h, int w, vp_rgb fg,
vp_rgb bg, vp_rgb border, vp_rgb glyphc,
const char *glyph, const char *label)
{
if (!p) {
return;
}
uint64_t base = 0;
ncchannels_set_fg_rgb8(&base, (fg >> 16) & 0xff, (fg >> 8) & 0xff,
fg & 0xff);
ncchannels_set_bg_rgb8(&base, (bg >> 16) & 0xff, (bg >> 8) & 0xff,
bg & 0xff);
ncplane_set_base(p, " ", 0, base);
vp_setbg(p, bg);
ncplane_erase(p);
vp_setfg(p, border);
ncplane_putegc_yx(p, 0, 0, "╭", NULL);
ncplane_putegc_yx(p, 0, w - 1, "╮", NULL);
ncplane_putegc_yx(p, h - 1, 0, "╰", NULL);
ncplane_putegc_yx(p, h - 1, w - 1, "╯", NULL);
for (int c = 1; c < w - 1; c++) {
ncplane_putegc_yx(p, 0, c, "─", NULL);
ncplane_putegc_yx(p, h - 1, c, "─", NULL);
}
for (int r = 1; r < h - 1; r++) {
ncplane_putegc_yx(p, r, 0, "│", NULL);
ncplane_putegc_yx(p, r, w - 1, "│", NULL);
}
vp_setfg(p, glyphc);
ncplane_putegc_yx(p, 1, w / 2 - 1, glyph, NULL);
/* Captions come from the config, so they can be longer than the tile.
* Truncate to the interior, backing off a partial UTF-8 sequence rather
* than emitting half a codepoint. */
char buf[64];
int max = w - 2;
if (max > (int)sizeof(buf) - 1) {
max = (int)sizeof(buf) - 1;
}
int len = (int)strlen(label);
if (len > max) {
len = max;
while (len > 0 && ((unsigned char)label[len] & 0xc0) == 0x80) {
len--;
}
}
snprintf(buf, sizeof(buf), "%.*s", len, label);
vp_setfg(p, fg);
ncplane_putstr_yx(p, h - 2, 1 + (max - len) / 2, buf);
}
static void settings_icon_paint(struct ncplane *p, bool full, void *user);
void settings_init(WM *wm)
{
int iy, ix;
settings_icon_geom(wm, &iy, &ix);
vp_rect r = { iy, ix, ICON_H, ICON_W };
wm->settings.icon = comp_layer_new(wm->comp, VP_BAND_DESKTOP, r,
settings_icon_paint, wm);
}
/* The launcher tiles repaint from the live theme, so recolouring them is just a
* damage - there is no separate "redraw now" path that could disagree with what
* the compositor last put on screen. */
void settings_icon_redraw(WM *wm)
{
comp_layer_damage(wm->settings.icon);
}
static void settings_icon_paint(struct ncplane *p, bool full, void *user)
{
(void)full;
WM *wm = user;
draw_icon_tile(p, ICON_H, ICON_W, wm->theme.icon_fg, wm->theme.icon_bg,
wm->theme.icon_border, wm->theme.icon_glyph, "⚙",
"Settings");
}
void settings_icon_reflow(WM *wm)
{
if (!wm->settings.icon) {
return;
}
int iy, ix;
settings_icon_geom(wm, &iy, &ix);
comp_layer_move(wm->settings.icon, iy, ix);
}
/* Hit tests read the compositor's geometry rather than the plane's, so they stay
* correct for a layer that is currently hidden or mid-drag. */
static bool layer_hit(const vp_layer *l, int y, int x)
{
if (!l) {
return false;
}
vp_rect r = comp_layer_abs(l);
return y >= r.y && y < r.y + r.h && x >= r.x && x < r.x + r.w;
}
bool settings_icon_hit(WM *wm, int y, int x)
{
return layer_hit(wm->settings.icon, y, x);
}
/* Siblings of the Settings tile, anchored to the bottom-right corner just above
* the taskbar. Persist detaches the UI; Die kills the daemon-owned PTYs too. */
#define EXIT_W 12
#define EXIT_H 4
/* Resting top-left of the Exit tile. If the user dragged it, the stored config
* position wins; otherwise it auto-anchors to the bottom-right, one cell in from
* the right edge with a one-row gap above the taskbar. Always clamped on-screen. */
static void exit_icon_geom(const WM *wm, int *y, int *x)
{
if (wm->config.exit_icon_y >= 0) {
*y = wm->config.exit_icon_y;
*x = wm->config.exit_icon_x;
} else {
int bottom = (int)wm->scr_rows - (wm->taskbar ? 1 : 0);
*y = bottom - EXIT_H -
1; /* leave a blank row between the icon and taskbar */
*x = (int)wm->scr_cols - EXIT_W - 1;
}
clamp_icon_pos(wm, EXIT_H, EXIT_W, y, x);
}
static bool icon_rect_overlap(int ay, int ax, int by, int bx)
{
return ax < bx + EXIT_W && ax + EXIT_W > bx && ay < by + EXIT_H &&
ay + EXIT_H > by;
}
static void place_next_to_exit(const WM *wm, int ey, int ex, int *y, int *x)
{
int bottom = (int)wm->scr_rows - (wm->taskbar ? 1 : 0);
*y = ey;
*x = ex - EXIT_W - 1;
if (*x >= 0) {
return;
}
*x = ex + EXIT_W + 1;
if (*x + EXIT_W <= (int)wm->scr_cols) {
return;
}
*x = ex;
*y = ey - EXIT_H - 1;
if (*y >= 0) {
return;
}
*y = ey + EXIT_H + 1;
if (*y + EXIT_H <= bottom) {
return;
}
*y = ey;
*x = ex;
}
static void die_icon_geom(const WM *wm, int *y, int *x)
{
int ey, ex;
exit_icon_geom(wm, &ey, &ex);
if (wm->config.die_icon_y >= 0) {
*y = wm->config.die_icon_y;
*x = wm->config.die_icon_x;
clamp_icon_pos(wm, EXIT_H, EXIT_W, y, x);
if (!icon_rect_overlap(*y, *x, ey, ex)) {
return;
}
} else {
place_next_to_exit(wm, ey, ex, y, x);
}
clamp_icon_pos(wm, EXIT_H, EXIT_W, y, x);
if (icon_rect_overlap(*y, *x, ey, ex)) {
place_next_to_exit(wm, ey, ex, y, x);
clamp_icon_pos(wm, EXIT_H, EXIT_W, y, x);
}
}
static void draw_exit_tile(WM *wm, struct ncplane *p, const char *glyph,
const char *label)
{
draw_icon_tile(p, EXIT_H, EXIT_W, wm->theme.exit_fg, wm->theme.exit_bg,
wm->theme.exit_border, wm->theme.exit_glyph, glyph,
label);
}
/* Painters for the two exit tiles: same tile, different glyph and label. */
static void exit_icon_paint(struct ncplane *p, bool full, void *user)
{
(void)full;
draw_exit_tile(user, p, "⏻", "Persist");
}
static void die_icon_paint(struct ncplane *p, bool full, void *user)
{
(void)full;
draw_exit_tile(user, p, "×", "Die");
}
void exit_icon_init(WM *wm)
{
int iy, ix;
exit_icon_geom(wm, &iy, &ix);
vp_rect r = { iy, ix, EXIT_H, EXIT_W };
wm->exit_icon = comp_layer_new(wm->comp, VP_BAND_DESKTOP, r,
exit_icon_paint, wm);
die_icon_geom(wm, &iy, &ix);
r.y = iy;
r.x = ix;
wm->die_icon = comp_layer_new(wm->comp, VP_BAND_DESKTOP, r,
die_icon_paint, wm);
}
void exit_icon_redraw(WM *wm)
{
comp_layer_damage(wm->exit_icon);
}
void die_icon_redraw(WM *wm)
{
comp_layer_damage(wm->die_icon);
}
void exit_icon_reflow(WM *wm)
{
if (!wm->exit_icon) {
return;
}
int iy, ix;
exit_icon_geom(wm, &iy, &ix);
comp_layer_move(wm->exit_icon, iy, ix);
}
void die_icon_reflow(WM *wm)
{
if (!wm->die_icon) {
return;
}
int iy, ix;
die_icon_geom(wm, &iy, &ix);
comp_layer_move(wm->die_icon, iy, ix);
}
bool exit_icon_hit(WM *wm, int y, int x)
{
return layer_hit(wm->exit_icon, y, x);
}
bool die_icon_hit(WM *wm, int y, int x)
{
return layer_hit(wm->die_icon, y, x);
}
void exit_icon_teardown(WM *wm)
{
comp_layer_destroy(wm->exit_icon);
wm->exit_icon = NULL;
comp_layer_destroy(wm->die_icon);
wm->die_icon = NULL;
}
/* ----- the user's own launcher icons --------------------------------------
*
* One tile per config.launchers entry, drawn like the Settings tile and used
* the same way: a click runs the icon's command in a new window, a drag moves
* it. Editing the list rebuilds every tile (launcher_icons_rebuild) rather than
* patching them, so no layer can be left pointing at an entry that has shifted
* along the array or gone away. */
static void launcher_icon_paint(struct ncplane *p, bool full, void *user)
{
(void)full;
VpLauncherIcon *ic = user;
WM *wm = ic->wm;
if (ic->idx < 0 || ic->idx >= wm->config.nlaunchers) {
return; /* the list shrank; this layer is on its way out */
}
const VpLauncher *l = &wm->config.launchers[ic->idx];
draw_icon_tile(p, ICON_H, ICON_W, wm->theme.icon_fg, wm->theme.icon_bg,
wm->theme.icon_border, wm->theme.icon_glyph,
l->cmd[0] ? "▸" : "❯", l->label);
}
/* Resting top-left of launcher `idx`: wherever the user dragged it, else a slot
* in a column running down from under the Settings tile, wrapping into the next
* column when it reaches the bottom of the screen. */
static void launcher_icon_geom(const WM *wm, int idx, int *y, int *x)
{
const VpLauncher *l = &wm->config.launchers[idx];
if (l->y >= 0) {
*y = l->y;
*x = l->x;
} else {
int bottom = (int)wm->scr_rows - (wm->taskbar ? 1 : 0);
int per_col = (bottom - ICON_Y) / (ICON_H + 1);
if (per_col < 1) {
per_col = 1;
}
int slot = idx + 1; /* slot 0 is the Settings tile itself */
*y = ICON_Y + (slot % per_col) * (ICON_H + 1);
*x = ICON_X + (slot / per_col) * (ICON_W + 1);
}
clamp_icon_pos(wm, ICON_H, ICON_W, y, x);
}
static void launcher_icons_free(WM *wm)
{
for (int i = 0; i < wm->nlaunchers; i++) {
/* These are the only desktop icons that can be destroyed while the
* program runs, so they are also the only ones a live drag can be
* left holding a freed pointer to. */
if (wm->drag_icon == wm->launchers[i]->layer) {
wm->drag_icon = NULL;
wm->drag = DRAG_NONE;
}
comp_layer_destroy(wm->launchers[i]->layer);
free(wm->launchers[i]);
}
free(wm->launchers);
wm->launchers = NULL;
wm->nlaunchers = 0;
}
void launcher_icons_rebuild(WM *wm)
{
launcher_icons_free(wm);
int n = wm->config.nlaunchers;
if (n <= 0) {
return;
}
wm->launchers = calloc((size_t)n, sizeof(*wm->launchers));
if (!wm->launchers) {
return;
}
for (int i = 0; i < n; i++) {
VpLauncherIcon *ic = calloc(1, sizeof(*ic));
if (!ic) {
return; /* the tiles we did build stay usable */
}
ic->wm = wm;
ic->idx = i;
int iy, ix;
launcher_icon_geom(wm, i, &iy, &ix);
vp_rect r = { iy, ix, ICON_H, ICON_W };
ic->layer = comp_layer_new(wm->comp, VP_BAND_DESKTOP, r,
launcher_icon_paint, ic);
if (!ic->layer) {
free(ic);
return;
}
wm->launchers[wm->nlaunchers++] = ic;
}
}
void launcher_icons_reflow(WM *wm)
{
for (int i = 0; i < wm->nlaunchers; i++) {
int iy, ix;
launcher_icon_geom(wm, wm->launchers[i]->idx, &iy, &ix);
comp_layer_move(wm->launchers[i]->layer, iy, ix);
}
}
void launcher_icons_redraw(WM *wm)
{
for (int i = 0; i < wm->nlaunchers; i++) {
comp_layer_damage(wm->launchers[i]->layer);
}
}
vp_layer *launcher_icon_at(WM *wm, int y, int x)
{
/* Front to back, so overlapping tiles resolve to the one on top. */
for (int i = wm->nlaunchers - 1; i >= 0; i--) {
if (layer_hit(wm->launchers[i]->layer, y, x)) {
return wm->launchers[i]->layer;
}
}
return NULL;
}
int launcher_icon_index(WM *wm, const vp_layer *l)
{
for (int i = 0; i < wm->nlaunchers; i++) {
if (wm->launchers[i]->layer == l) {
return wm->launchers[i]->idx;
}
}
return -1;
}
void launcher_icon_activate(WM *wm, int idx)
{
if (idx < 0 || idx >= wm->config.nlaunchers) {
return;
}
const VpLauncher *l = &wm->config.launchers[idx];
Window *win = wm_spawn_command(wm, l->cmd[0] ? l->cmd : NULL);
vp_log("launcher: '%s' -> %s%s\n", l->label, l->cmd,
win ? "" : " (failed to open a window)");
}
void launcher_icons_teardown(WM *wm)
{
launcher_icons_free(wm);
}
static void panel_paint(struct ncplane *p, bool full, void *user);
void settings_open(WM *wm)
{
Settings *s = &wm->settings;
if (s->open) {
return;
}
int py, px, H, W;
panel_geom(wm, &py, &px, &H, &W);
/* VP_BAND_MODAL is above every window and above the taskbar, which is
* the whole of what "modal" means to the display. */
vp_rect r = { py, px, H, W };
s->panel = comp_layer_new(wm->comp, VP_BAND_MODAL, r, panel_paint, wm);
if (!s->panel) {
return;
}
s->open = true;
s->view = SETTINGS_VIEW_GRID;
s->grid_sel = 0;
s->capturing = false;
s->sel = 0;
s->scroll = 0;
s->sb_drag = false;
snprintf(s->status, sizeof(s->status),
"↑/↓/←/→: select Enter: open Esc: close");
vp_log("settings: open\n");
}
void settings_damage(Settings *s)
{
comp_layer_damage(s->panel);
}
/* Switch the panel to a sub-view (or back to the grid), seeding its UI state. */
static void settings_set_view(WM *wm, settings_view v)
{
Settings *s = &wm->settings;
s->view = v;
s->capturing = false;
s->editing = false;
s->sb_drag = false;
settings_damage(s);
if (v == SETTINGS_VIEW_KEYBINDINGS) {
s->sel = 0;
s->scroll = 0;
snprintf(s->status, sizeof(s->status),
"Enter: rebind D: unbind S: save Esc: back");
} else if (v == SETTINGS_VIEW_TERMINAL) {
s->sel = 0;
s->scroll = 0;
snprintf(s->status, sizeof(s->status),
"←/→: adjust Enter: edit S: save Esc: back");
} else if (v == SETTINGS_VIEW_APPEARANCE) {
s->sel = 0;
s->scroll = 0;
snprintf(s->status, sizeof(s->status),
"←/→: change Enter: edit D: reset S: save");
} else if (v == SETTINGS_VIEW_ICONS) {
s->sel = 0;
s->scroll = 0;
snprintf(s->status, sizeof(s->status),
"A: add Enter: command R: rename D: delete");
} else if (v == SETTINGS_VIEW_ANIMATIONS) {
s->sel = 0;
s->scroll = 0;
snprintf(s->status, sizeof(s->status),
"←/→: toggle S: save Esc: back");
} else {
snprintf(s->status, sizeof(s->status),
"↑/↓/←/→: select Enter: open Esc: close");
}
vp_log("settings: view -> %d\n", (int)v);
}
/* "Back": from a sub-view, return to the grid; from the grid, close the panel. */
static void settings_back(WM *wm)
{
if (wm->settings.view == SETTINGS_VIEW_GRID) {
settings_close(wm);
} else {
settings_set_view(wm, SETTINGS_VIEW_GRID);
}
}
void settings_close(WM *wm)
{
Settings *s = &wm->settings;
if (!s->open) {
return;
}
s->open = false;
s->capturing = false;
s->editing = false;
s->sb_drag = false;
comp_layer_destroy(s->panel);
s->panel = NULL;
config_save(&wm->config);
vp_log("settings: close (saved)\n");
}
void settings_teardown(WM *wm)
{
Settings *s = &wm->settings;
comp_layer_destroy(s->panel);
s->panel = NULL;
comp_layer_destroy(s->icon);
s->icon = NULL;
}
static void apply_capture(WM *wm, uint32_t id, unsigned mods)
{
Settings *s = &wm->settings;
char cb[32];
keymap_format_chord(id, mods, cb, sizeof(cb));
if (is_toggle_row(s->sel)) {
wm->config.toggle_key =
id; /* modifiers ignored for the toggle */
if (config_manual_shadows_setting(&wm->config,
SETTING_TOGGLE_KEY)) {
snprintf(
s->status, sizeof(s->status),
"Toggle set to %s - your manual config overrides it on restart",
cb);
} else {
snprintf(s->status, sizeof(s->status),
"Toggle key set to %s", cb);
}
} else {
vp_action act;
const char *label = NULL;
keymap_action_info(s->sel, &act, &label);
keymap_rebind_action(&wm->config, act, id, mods);
if (config_manual_shadows_action(&wm->config, act, id, mods)) {
snprintf(
s->status, sizeof(s->status),
"%s = %s - your manual config overrides it on restart",
label ? label : "", cb);
} else {
snprintf(s->status, sizeof(s->status), "%s = %s",
label ? label : "", cb);
}
}
s->capturing = false;
settings_damage(s);
}
static void do_unbind(WM *wm)
{
Settings *s = &wm->settings;
if (is_toggle_row(s->sel)) {
snprintf(s->status, sizeof(s->status),
"The toggle key cannot be unbound");