-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskbar.c
More file actions
301 lines (273 loc) · 8.46 KB
/
Copy pathtaskbar.c
File metadata and controls
301 lines (273 loc) · 8.46 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
// 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/>.
*/
/* taskbar.c - a full-width bar pinned to the bottom row, above all windows.
*
* Shows one slot per window (id + short title, marked if minimized), highlights
* the focused window, and shows the global mode indicator + clickable toggle.
* Built as a custom plane (not ncmenu) for full layout control.
*/
#define _GNU_SOURCE
#include "viewpoint.h"
#include <stdio.h>
#define SLOT_W 18 /* columns per window slot */
#define MODE_W 13 /* " PASSTHROUGH " region width */
#define ARROW_W 3 /* " ◄ " / " ► " clickable scroll arrows */
/* Computed layout, shared between draw and click so they stay in lockstep. */
typedef struct {
int win_idx; /* index into wm->wins */
int xstart;
int xend; /* exclusive */
} Slot;
static Slot g_slots[256];
static int g_nslots;
static int g_mode_x0; /* start column of the mode toggle region */
static int g_arrow_l_x0; /* start column of the ◄ arrow, or -1 if not shown */
static int g_arrow_r_x0; /* start column of the ► arrow, or -1 if not shown */
static int g_maxscroll; /* largest valid wm->taskbar_scroll for this layout */
static int g_maxfit; /* number of slots that fit (the visible page size) */
static void compute_layout(WM *wm)
{
g_nslots = 0;
g_mode_x0 = (int)wm->scr_cols - MODE_W;
g_arrow_l_x0 = g_arrow_r_x0 = -1;
g_maxscroll = 0;
int maxfit = g_mode_x0 / SLOT_W;
if (maxfit < 0) {
maxfit = 0;
}
/* Overflow: more windows than fit. Reserve a ◄ / ► arrow region at each end
* (only if the bar is wide enough for the arrows plus at least one slot) and
* shrink the slot strip accordingly. */
int slot_x0 = 0;
int slot_x1 = g_mode_x0; /* exclusive */
if (wm->nwins > maxfit && g_mode_x0 >= ARROW_W * 2 + SLOT_W) {
g_arrow_l_x0 = 0;
g_arrow_r_x0 = g_mode_x0 - ARROW_W;
slot_x0 = ARROW_W;
slot_x1 = g_arrow_r_x0;
maxfit = (slot_x1 - slot_x0) / SLOT_W;
}
g_maxfit = maxfit;
g_maxscroll = wm->nwins - maxfit;
if (g_maxscroll < 0) {
g_maxscroll = 0;
}
if (wm->taskbar_scroll > g_maxscroll) {
wm->taskbar_scroll = g_maxscroll;
}
if (wm->taskbar_scroll < 0) {
wm->taskbar_scroll = 0;
}
int x = slot_x0;
for (int i = wm->taskbar_scroll;
i < wm->nwins &&
g_nslots < (int)(sizeof(g_slots) / sizeof(g_slots[0]));
i++) {
if (x + SLOT_W > slot_x1) {
break; /* no more room in the slot strip */
}
g_slots[g_nslots].win_idx = i;
g_slots[g_nslots].xstart = x;
g_slots[g_nslots].xend = x + SLOT_W;
g_nslots++;
x += SLOT_W;
}
}
static void taskbar_paint(struct ncplane *t, bool full, void *user);
void taskbar_create(WM *wm)
{
/* VP_BAND_OVERLAY is what keeps the bar above every window. It is stated
* once, here, instead of being re-asserted with a move_top on every
* focus change and every render pass. */
vp_rect r = { (int)wm->scr_rows - 1, 0, 1, (int)wm->scr_cols };
wm->taskbar =
comp_layer_new(wm->comp, VP_BAND_OVERLAY, r, taskbar_paint, wm);
}
void taskbar_reflow(WM *wm)
{
if (!wm->taskbar) {
return;
}
comp_layer_resize(wm->taskbar, 1, (int)wm->scr_cols);
comp_layer_move(wm->taskbar, (int)wm->scr_rows - 1, 0);
taskbar_damage(wm);
}
void taskbar_damage(WM *wm)
{
comp_layer_damage(wm->taskbar);
}
static void taskbar_paint(struct ncplane *t, bool full, void *user)
{
(void)full;
WM *wm = user;
compute_layout(wm);
/* Base cell = the bar's background, so the erase below fills the whole
* row with it in one shot. Re-applied every paint so a theme change
* needs nothing but a damage. */
uint64_t ch = 0;
vp_rgb bfg = wm->theme.bar_fg, bbg = wm->theme.bar_bg;
ncchannels_set_fg_rgb8(&ch, (bfg >> 16) & 0xff, (bfg >> 8) & 0xff,
bfg & 0xff);
ncchannels_set_bg_rgb8(&ch, (bbg >> 16) & 0xff, (bbg >> 8) & 0xff,
bbg & 0xff);
ncplane_set_base(t, " ", 0, ch);
ncplane_set_styles(t, NCSTYLE_NONE);
ncplane_erase(t);
for (int s = 0; s < g_nslots; s++) {
Window *win = wm->wins[g_slots[s].win_idx];
bool focused = (wm_focused(wm) == win);
if (focused) {
vp_setfg(t, wm->theme.bar_focus_fg);
vp_setbg(t, wm->theme.bar_focus_bg);
} else if (win->minimized) {
vp_setfg(t, wm->theme.bar_min_fg);
vp_setbg(t, wm->theme.bar_min_bg);
} else {
vp_setfg(t, wm->theme.bar_slot_fg);
vp_setbg(t, wm->theme.bar_slot_bg);
}
char buf[SLOT_W + 1];
/* "N:title" - wrap minimized titles in brackets. The slot field width
* below truncates; this buffer just holds the untruncated string. */
char title[VP_TITLE_MAX + 16];
snprintf(title, sizeof(title), "%d:%s", win->id, win->title);
if (win->minimized) {
snprintf(buf, sizeof(buf), " [%-*.*s] ", SLOT_W - 4,
SLOT_W - 4, title);
} else {
snprintf(buf, sizeof(buf), " %-*.*s ", SLOT_W - 2,
SLOT_W - 2, title);
}
ncplane_putstr_yx(t, 0, g_slots[s].xstart, buf);
}
/* ◄ / ► scroll arrows (only present when the slots overflow). Each is dimmed
* when there's nothing more to reveal in that direction. */
if (g_arrow_l_x0 >= 0) {
bool live = wm->taskbar_scroll > 0;
unsigned char g = live ? 0xff : 0x55;
ncplane_set_fg_rgb8(t, g, g, g);
vp_setbg(t, wm->theme.bar_arrow_bg);
ncplane_putstr_yx(t, 0, g_arrow_l_x0, " ◄ ");
}
if (g_arrow_r_x0 >= 0) {
bool live = wm->taskbar_scroll < g_maxscroll;
unsigned char g = live ? 0xff : 0x55;
ncplane_set_fg_rgb8(t, g, g, g);
vp_setbg(t, wm->theme.bar_arrow_bg);
ncplane_putstr_yx(t, 0, g_arrow_r_x0, " ► ");
}
if (g_mode_x0 >= 0) {
vp_setfg(t, wm->theme.bar_mode_fg);
if (wm->mode == MODE_PASSTHROUGH) {
vp_setbg(t, wm->theme.bar_pass_bg);
ncplane_putstr_yx(t, 0, g_mode_x0, " PASSTHROUGH ");
} else {
vp_setbg(t, wm->theme.bar_interp_bg);
ncplane_putstr_yx(t, 0, g_mode_x0, " INTERPRET ");
}
}
}
bool taskbar_click(WM *wm, int y, int x)
{
if (!wm->taskbar || y != (int)wm->scr_rows - 1) {
return false;
}
compute_layout(wm);
if (x >= g_mode_x0) {
wm->mode = (wm->mode == MODE_INTERPRET) ? MODE_PASSTHROUGH :
MODE_INTERPRET;
taskbar_damage(wm);
for (int i = 0; i < wm->nwins; i++) {
window_damage_frame(
wm->wins[i]); /* per-window indicator changes */
}
return true;
}
if (g_arrow_l_x0 >= 0 && x >= g_arrow_l_x0 &&
x < g_arrow_l_x0 + ARROW_W) {
taskbar_scroll_by(wm, -1);
return true;
}
if (g_arrow_r_x0 >= 0 && x >= g_arrow_r_x0 &&
x < g_arrow_r_x0 + ARROW_W) {
taskbar_scroll_by(wm, +1);
return true;
}
for (int s = 0; s < g_nslots; s++) {
if (x >= g_slots[s].xstart && x < g_slots[s].xend) {
Window *win = wm->wins[g_slots[s].win_idx];
/* The slot toggles the window's visibility: a
* minimized one comes back, the already-focused one
* drops out of the way, anything else is raised. */
if (win->minimized) {
wm_restore(wm, win);
} else if (wm_focused(wm) == win) {
wm_minimize(wm, win);
} else {
wm_focus_window(wm, win);
}
return true;
}
}
return true; /* a click anywhere on the bar is consumed */
}
void taskbar_scroll_by(WM *wm, int delta)
{
if (!wm->taskbar) {
return;
}
compute_layout(wm); /* refresh g_maxscroll for the current geometry */
int s = wm->taskbar_scroll + delta;
if (s < 0) {
s = 0;
}
if (s > g_maxscroll) {
s = g_maxscroll;
}
if (s != wm->taskbar_scroll) {
wm->taskbar_scroll = s;
taskbar_damage(wm);
}
}
void taskbar_reveal(WM *wm, int win_idx)
{
if (!wm->taskbar || win_idx < 0 || win_idx >= wm->nwins) {
return;
}
compute_layout(
wm); /* refresh g_maxfit / g_maxscroll for the current geometry */
if (g_maxfit < 1 || wm->nwins <= g_maxfit) {
return; /* everything fits - nothing to scroll into view */
}
int s = wm->taskbar_scroll;
if (win_idx < s) {
s = win_idx; /* off the left edge: make it leftmost */
} else if (win_idx >= s + g_maxfit) {
s = win_idx - g_maxfit +
1; /* off the right edge: make it rightmost */
}
if (s < 0) {
s = 0;
}
if (s > g_maxscroll) {
s = g_maxscroll;
}
if (s != wm->taskbar_scroll) {
wm->taskbar_scroll = s;
taskbar_damage(wm);
}
}