forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibar.c
355 lines (307 loc) · 10.6 KB
/
ibar.c
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
/**
* @file
* Index Bar (status)
*
* @authors
* Copyright (C) 2021 Richard Russon <[email protected]>
*
* @copyright
* 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 2 of the License, or (at your option) 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 <http://www.gnu.org/licenses/>.
*/
/**
* @page index_ibar Index Bar (status)
*
* The Index Bar Window displays status info about the email list.
*
* ## Windows
*
* | Name | Type | See Also |
* | :--------------- | :------------ | :--------- |
* | Index Bar Window | WT_STATUS_BAR | ibar_new() |
*
* **Parent**
* - @ref index_ipanel
*
* **Children**
*
* None.
*
* ## Data
* - #IBarPrivateData
*
* The Index Bar Window stores its data (#IBarPrivateData) in
* MuttWindow::wdata.
*
* ## Events
*
* Once constructed, it is controlled by the following events:
*
* | Event Type | Handler |
* | :-------------------- | :---------------------- |
* | #NT_COLOR | ibar_color_observer() |
* | #NT_CONFIG | ibar_config_observer() |
* | #NT_INDEX | ibar_index_observer() |
* | #NT_MENU | ibar_menu_observer() |
* | #NT_WINDOW | ibar_window_observer() |
* | MuttWindow::recalc() | ibar_recalc() |
* | MuttWindow::repaint() | ibar_repaint() |
*/
#include "config.h"
#include <stdbool.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "lib.h"
#include "color/lib.h"
#include "private_data.h"
#include "shared_data.h"
#include "status.h"
/**
* struct IBarPrivateData - Data to draw the Index Bar
*/
struct IBarPrivateData
{
struct IndexSharedData *shared; ///< Shared Index data
struct IndexPrivateData *priv; ///< Private Index data
char *status_format; ///< Cached screen status string
char *ts_status_format; ///< Cached terminal status string
char *ts_icon_format; ///< Cached terminal icon string
};
/**
* ibar_recalc - Recalculate the Window data - Implements MuttWindow::recalc() - @ingroup window_recalc
*/
static int ibar_recalc(struct MuttWindow *win)
{
char buf[1024] = { 0 };
struct IBarPrivateData *ibar_data = win->wdata;
struct IndexSharedData *shared = ibar_data->shared;
struct IndexPrivateData *priv = ibar_data->priv;
const char *c_status_format = cs_subset_string(shared->sub, "status_format");
menu_status_line(buf, sizeof(buf), shared, priv->menu, win->state.cols,
NONULL(c_status_format));
if (!mutt_str_equal(buf, ibar_data->status_format))
{
mutt_str_replace(&ibar_data->status_format, buf);
win->actions |= WA_REPAINT;
mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
}
const bool c_ts_enabled = cs_subset_bool(shared->sub, "ts_enabled");
if (c_ts_enabled && TsSupported)
{
const char *c_ts_status_format = cs_subset_string(shared->sub, "ts_status_format");
menu_status_line(buf, sizeof(buf), shared, priv->menu, sizeof(buf),
NONULL(c_ts_status_format));
if (!mutt_str_equal(buf, ibar_data->ts_status_format))
{
mutt_str_replace(&ibar_data->ts_status_format, buf);
win->actions |= WA_REPAINT;
mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
}
const char *c_ts_icon_format = cs_subset_string(shared->sub, "ts_icon_format");
menu_status_line(buf, sizeof(buf), shared, priv->menu, sizeof(buf),
NONULL(c_ts_icon_format));
if (!mutt_str_equal(buf, ibar_data->ts_icon_format))
{
mutt_str_replace(&ibar_data->ts_icon_format, buf);
win->actions |= WA_REPAINT;
mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
}
}
return 0;
}
/**
* ibar_repaint - Repaint the Window - Implements MuttWindow::repaint() - @ingroup window_repaint
*/
static int ibar_repaint(struct MuttWindow *win)
{
struct IBarPrivateData *ibar_data = win->wdata;
struct IndexSharedData *shared = ibar_data->shared;
mutt_window_move(win, 0, 0);
mutt_curses_set_normal_backed_color_by_id(MT_COLOR_STATUS);
mutt_window_clrtoeol(win);
mutt_window_move(win, 0, 0);
mutt_draw_statusline(win, win->state.cols, ibar_data->status_format,
mutt_str_len(ibar_data->status_format));
mutt_curses_set_color_by_id(MT_COLOR_NORMAL);
const bool c_ts_enabled = cs_subset_bool(shared->sub, "ts_enabled");
if (c_ts_enabled && TsSupported)
{
mutt_ts_status(ibar_data->ts_status_format);
mutt_ts_icon(ibar_data->ts_icon_format);
}
mutt_debug(LL_DEBUG5, "repaint done\n");
return 0;
}
/**
* ibar_color_observer - Notification that a Color has changed - Implements ::observer_t - @ingroup observer_api
*/
static int ibar_color_observer(struct NotifyCallback *nc)
{
if (nc->event_type != NT_COLOR)
return 0;
if (!nc->global_data || !nc->event_data)
return -1;
struct EventColor *ev_c = nc->event_data;
// MT_COLOR_MAX is sent on `uncolor *`
if ((ev_c->cid != MT_COLOR_STATUS) && (ev_c->cid != MT_COLOR_NORMAL) &&
(ev_c->cid != MT_COLOR_MAX))
{
return 0;
}
struct MuttWindow *win_ibar = nc->global_data;
win_ibar->actions |= WA_REPAINT;
mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
return 0;
}
/**
* ibar_config_observer - Notification that a Config Variable has changed - Implements ::observer_t - @ingroup observer_api
*/
static int ibar_config_observer(struct NotifyCallback *nc)
{
if (nc->event_type != NT_CONFIG)
return 0;
if (!nc->global_data || !nc->event_data)
return -1;
struct EventConfig *ev_c = nc->event_data;
if (!ev_c->name)
return 0;
if ((ev_c->name[0] != 's') && (ev_c->name[0] != 't'))
return 0;
if (!mutt_str_equal(ev_c->name, "status_format") &&
!mutt_str_equal(ev_c->name, "ts_enabled") &&
!mutt_str_equal(ev_c->name, "ts_icon_format") &&
!mutt_str_equal(ev_c->name, "ts_status_format"))
{
return 0;
}
struct MuttWindow *win_ibar = nc->global_data;
win_ibar->actions |= WA_RECALC;
mutt_debug(LL_DEBUG5, "config done, request WA_RECALC\n");
return 0;
}
/**
* ibar_index_observer - Notification that the Index has changed - Implements ::observer_t - @ingroup observer_api
*
* This function receives two sorts of notification:
* - NT_INDEX:
* User has changed to a different Mailbox/Email
* - NT_ACCOUNT/NT_MVIEW/NT_MAILBOX/NT_EMAIL:
* The state of an object has changed
*/
static int ibar_index_observer(struct NotifyCallback *nc)
{
if (!nc->global_data)
return -1;
struct MuttWindow *win_ibar = nc->global_data;
win_ibar->actions |= WA_RECALC;
mutt_debug(LL_DEBUG5, "index done, request WA_RECALC\n");
return 0;
}
/**
* ibar_menu_observer - Notification that a Menu has changed - Implements ::observer_t - @ingroup observer_api
*/
static int ibar_menu_observer(struct NotifyCallback *nc)
{
if (nc->event_type != NT_MENU)
return 0;
if (!nc->global_data)
return -1;
struct MuttWindow *win_ibar = nc->global_data;
win_ibar->actions |= WA_RECALC;
mutt_debug(LL_DEBUG5, "menu done, request WA_RECALC\n");
return 0;
}
/**
* ibar_window_observer - Notification that a Window has changed - Implements ::observer_t - @ingroup observer_api
*/
static int ibar_window_observer(struct NotifyCallback *nc)
{
if (nc->event_type != NT_WINDOW)
return 0;
if (!nc->global_data)
return -1;
struct MuttWindow *win_ibar = nc->global_data;
struct EventWindow *ev_w = nc->event_data;
if (ev_w->win != win_ibar)
return 0;
if (nc->event_subtype == NT_WINDOW_STATE)
{
win_ibar->actions |= WA_REPAINT;
mutt_debug(LL_DEBUG5, "window state done, request WA_REPAINT\n");
}
else if (nc->event_subtype == NT_WINDOW_DELETE)
{
struct MuttWindow *dlg = window_find_parent(win_ibar, WT_DLG_INDEX);
struct IndexSharedData *shared = dlg->wdata;
mutt_color_observer_remove(ibar_color_observer, win_ibar);
notify_observer_remove(NeoMutt->sub->notify, ibar_config_observer, win_ibar);
notify_observer_remove(shared->notify, ibar_index_observer, win_ibar);
notify_observer_remove(win_ibar->parent->notify, ibar_menu_observer, win_ibar);
notify_observer_remove(win_ibar->notify, ibar_window_observer, win_ibar);
mutt_debug(LL_DEBUG5, "window delete done\n");
}
return 0;
}
/**
* ibar_data_free - Free the private data - Implements MuttWindow::wdata_free() - @ingroup window_wdata_free
*/
static void ibar_data_free(struct MuttWindow *win, void **ptr)
{
if (!ptr || !*ptr)
return;
struct IBarPrivateData *ibar_data = *ptr;
FREE(&ibar_data->status_format);
FREE(&ibar_data->ts_status_format);
FREE(&ibar_data->ts_icon_format);
FREE(ptr);
}
/**
* ibar_data_new - Create the private data for the Index Bar (status)
* @param shared Shared Index data
* @param priv Private Index data
* @retval ptr New IBarPrivateData
*/
static struct IBarPrivateData *ibar_data_new(struct IndexSharedData *shared,
struct IndexPrivateData *priv)
{
struct IBarPrivateData *ibar_data = mutt_mem_calloc(1, sizeof(struct IBarPrivateData));
ibar_data->shared = shared;
ibar_data->priv = priv;
return ibar_data;
}
/**
* ibar_new - Create the Index Bar (status)
* @param parent Parent Window
* @param shared Shared Index data
* @param priv Private Index data
* @retval ptr New Index Bar
*/
struct MuttWindow *ibar_new(struct MuttWindow *parent, struct IndexSharedData *shared,
struct IndexPrivateData *priv)
{
struct MuttWindow *win_ibar = mutt_window_new(WT_STATUS_BAR, MUTT_WIN_ORIENT_VERTICAL,
MUTT_WIN_SIZE_FIXED,
MUTT_WIN_SIZE_UNLIMITED, 1);
win_ibar->wdata = ibar_data_new(shared, priv);
win_ibar->wdata_free = ibar_data_free;
win_ibar->recalc = ibar_recalc;
win_ibar->repaint = ibar_repaint;
mutt_color_observer_add(ibar_color_observer, win_ibar);
notify_observer_add(NeoMutt->sub->notify, NT_CONFIG, ibar_config_observer, win_ibar);
notify_observer_add(shared->notify, NT_ALL, ibar_index_observer, win_ibar);
notify_observer_add(parent->notify, NT_MENU, ibar_menu_observer, win_ibar);
notify_observer_add(win_ibar->notify, NT_WINDOW, ibar_window_observer, win_ibar);
return win_ibar;
}