From 596ec20dd81fc4a48a528bfed9e241343ee03eaa Mon Sep 17 00:00:00 2001 From: Rudy Dellomas III Date: Sun, 21 Apr 2024 20:25:40 +1000 Subject: Emit an event (ui_draw) immediately before drawing the screen This allows better control over styling, as well as potential for entirely new UI elements implemented entirely using the Lua API. --- lua/vis.lua | 2 ++ main.c | 1 + ui-terminal.c | 1 + vis-core.h | 1 + vis-lua.c | 14 +++++++++++++- vis-lua.h | 1 + vis.c | 4 ++++ vis.h | 1 + 8 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lua/vis.lua b/lua/vis.lua index 32fb2a1..5473f17 100644 --- a/lua/vis.lua +++ b/lua/vis.lua @@ -167,6 +167,7 @@ local events = { WIN_STATUS = "Event::WIN_STATUS", -- see @{win_status} TERM_CSI = "Event::TERM_CSI", -- see @{term_csi} PROCESS_RESPONSE = "Event::PROCESS_RESPONSE", -- see @{process_response} + UI_DRAW = "Event::UI_DRAW", -- see @{ui_draw} } events.file_close = function(...) events.emit(events.FILE_CLOSE, ...) end @@ -183,6 +184,7 @@ events.win_open = function(...) events.emit(events.WIN_OPEN, ...) end events.win_status = function(...) events.emit(events.WIN_STATUS, ...) end events.term_csi = function(...) events.emit(events.TERM_CSI, ...) end events.process_response = function(...) events.emit(events.PROCESS_RESPONSE, ...) end +events.ui_draw = function(...) events.emit(events.UI_DRAW, ...) end local handlers = {} diff --git a/main.c b/main.c index 8e5ad42..8aa31c6 100644 --- a/main.c +++ b/main.c @@ -2229,6 +2229,7 @@ int main(int argc, char *argv[]) { .win_highlight = vis_lua_win_highlight, .win_status = vis_lua_win_status, .term_csi = vis_lua_term_csi, + .ui_draw = vis_lua_ui_draw, }; vis = vis_new(ui_term_new(), &event); diff --git a/ui-terminal.c b/ui-terminal.c index 6ec1e41..09e618e 100644 --- a/ui-terminal.c +++ b/ui-terminal.c @@ -392,6 +392,7 @@ static void ui_draw(Ui *ui) { ui_window_draw((UiWin*)win); if (tui->info[0]) ui_draw_string(tui, 0, tui->height-1, tui->info, NULL, UI_STYLE_INFO); + vis_event_emit(tui->vis, VIS_EVENT_UI_DRAW); ui_term_backend_blit(tui); } diff --git a/vis-core.h b/vis-core.h index 8d3980c..4f81e4c 100644 --- a/vis-core.h +++ b/vis-core.h @@ -236,6 +236,7 @@ enum VisEvents { VIS_EVENT_WIN_HIGHLIGHT, VIS_EVENT_WIN_STATUS, VIS_EVENT_TERM_CSI, + VIS_EVENT_UI_DRAW, }; bool vis_event_emit(Vis*, enum VisEvents, ...); diff --git a/vis-lua.c b/vis-lua.c index 3d80293..91ee428 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -166,7 +166,7 @@ void vis_lua_win_status(Vis *vis, Win *win) { window_status_update(vis, win); } void vis_lua_term_csi(Vis *vis, const long *csi) { } void vis_lua_process_response(Vis *vis, const char *name, char *buffer, size_t len, ResponseType rtype) { } - +void vis_lua_ui_draw(Vis *vis) { } #else @@ -3689,4 +3689,16 @@ void vis_lua_process_response(Vis *vis, const char *name, lua_pop(L, 1); } +/*** + * Emitted immediately before the UI is drawn to the screen. + * Allows last-minute overrides to the styling of UI elements. + * + * *WARNING:* This is emitted every screen draw! + * Use sparingly and check for `nil` values! + * @function ui_draw + */ +void vis_lua_ui_draw(Vis *vis) { + vis_lua_event_call(vis, "ui_draw"); +} + #endif diff --git a/vis-lua.h b/vis-lua.h index d622ff5..b4f3f51 100644 --- a/vis-lua.h +++ b/vis-lua.h @@ -42,5 +42,6 @@ void vis_lua_win_highlight(Vis*, Win*); void vis_lua_win_status(Vis*, Win*); void vis_lua_term_csi(Vis*, const long *); void vis_lua_process_response(Vis *, const char *, char *, size_t, ResponseType); +void vis_lua_ui_draw(Vis*); #endif diff --git a/vis.c b/vis.c index e287405..0c05558 100644 --- a/vis.c +++ b/vis.c @@ -105,6 +105,10 @@ bool vis_event_emit(Vis *vis, enum VisEvents id, ...) { if (vis->event->term_csi) vis->event->term_csi(vis, va_arg(ap, const long *)); break; + case VIS_EVENT_UI_DRAW: + if (vis->event->ui_draw) + vis->event->ui_draw(vis); + break; } va_end(ap); diff --git a/vis.h b/vis.h index b67d3c5..8287a45 100644 --- a/vis.h +++ b/vis.h @@ -58,6 +58,7 @@ typedef struct { void (*win_highlight)(Vis*, Win*); void (*win_status)(Vis*, Win*); void (*term_csi)(Vis*, const long *); + void (*ui_draw)(Vis*); } VisEvent; /** Union used to pass arguments to key action functions. */ -- cgit v1.2.3