diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index bfe543b..c97ee6a 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -930,6 +930,17 @@ void AddCommand(Array *arr, String name, String binding, HookFunction *fun Add(arr, hook); } +void AddHook(Array *arr, HookKind kind, String name, String binding, HookFunction *function) { + Hook hook = {}; + hook.kind = kind; + hook.name = name; + hook.binding = binding; + hook.function = function; + hook.trigger = ParseKeyCached(binding); + hook.docs = "Not listing hooks anywhere currently, maybe should change!!"; + Add(arr, hook); +} + void Coro_Rename(mco_coro *co) { BSet main = GetBSet(PrimaryWindowID); Buffer *buffer = main.buffer; @@ -1466,20 +1477,3 @@ void CMD_ClearCarets(HookParam param) { } } } RegisterCommand(CMD_ClearCarets, "escape", "Clear all carets and reset to 1 caret, also do some windowing stuff that closes things on escape"); - -void GenerateConfig(View *view) { - For (Variables) { - if (it.type == VariableType_String) { - Appendf(view, "// :Set %S '%S'\n", it.name, *it.string); - } else if (it.type == VariableType_Int) { - Appendf(view, "// :Set %S '%lld'\n", it.name, (long long)*it.i); - } else if (it.type == VariableType_Float) { - Appendf(view, "// :Set %S '%f'\n", it.name, *it.f); - } else if (it.type == VariableType_Color) { - Appendf(view, "// :Set %S %x\n", it.name, it.color->value); - } ElseInvalidCodepath(); - } - For (GlobalHooks) { - Appendf(view, "// :Set %S '%S'\n", it.name, it.binding); - } -} diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 71132e3..7ce8a6c 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -547,6 +547,7 @@ void GarbageCollect() { RawAppendf(GCInfoBuffer, "Wind %d %d %d %d %d\n", (int)it->id.id, (int)it->total_rect.min.x, (int)it->total_rect.min.y, (int)it->total_rect.max.x, (int)it->total_rect.max.y); Dealloc(&it->goto_history); Dealloc(&it->goto_redo); + Dealloc(&it->hooks); Dealloc(sys_allocator, it); remove_item = true; } else { @@ -581,10 +582,15 @@ void Update(Event event) { } OnCommand(event); - StatusWindowUpdate(); - DebugWindowUpdate(); + + For (GlobalHooks) { + if (it.kind == HookKind_AppUpdate) { + ProfileScopeEx(it.name); + it.function(HookParam{}); + } + } + FuzzySearchViewUpdate(); - SearchWindowUpdate(); UpdateProcesses(); CoUpdate(&event); diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 7b2d71b..8cc673b 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -67,7 +67,7 @@ enum HookKind { HookKind_Command, HookKind_BeforeLayoutWindow, - HookKind_HandleLayoutWindow, + HookKind_LayoutWindow, HookKind_AfterLayoutWindow, HookKind_BeforeRenderWindow, @@ -88,6 +88,14 @@ enum HookKind { // Global hooks, per (window, view, buffer) hooks struct HookParam { + union { + struct { + Window *window; + Rect2I *rect; + int16_t wx; + int16_t wy; + } layout; + }; }; typedef void HookFunction(HookParam param); @@ -155,6 +163,7 @@ struct Window { ViewID active_goto_list; Int goto_list_pos; + Array hooks; struct { uint32_t draw_scrollbar : 1; uint32_t draw_line_numbers : 1; @@ -298,6 +307,7 @@ struct ResolvedOpen { bool existing_buffer; }; +void AddHook(Array *arr, HookKind kind, String name, String binding, HookFunction *function); void AddCommand(Array *arr, String name, String binding, HookFunction *function); #define RegisterCommand(name, ...) Register_Command RC__##name(&GlobalHooks, HookKind_Command, name, #name, __VA_ARGS__) #define RegisterHook(name, kind, bindings, docs) Register_Command RC__##name(&GlobalHooks, kind, name, #name, bindings, docs) @@ -361,14 +371,6 @@ void TrimWhitespace(Buffer *buffer, bool trim_lines_with_caret = false); void JumpTempBuffer(BSet *set, String buffer_name = ""); void CommandWindowLayout(Rect2I *rect, Int wx, Int wy); void CommandWindowInit(); -void SearchWindowLayout(Rect2I *rect, Int wx, Int wy); -void SearchWindowInit(); -void StatusWindowInit(); -void StatusWindowLayout(Rect2I *rect, Int wx, Int wy); -void DebugWindowInit(); -void DebugWindowLayout(Rect2I *rect, Int wx, Int wy); -void BuildWindowInit(); -void BuildWindowLayout(Rect2I *rect, Int wx, Int wy); View *FindView(BufferID buffer_id, View *default_view = NULL); bool operator==(BufferID a, BufferID b) { return a.id == b.id; } diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index 5dd8ace..663291a 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -117,10 +117,6 @@ View *WindowOpenBufferView(Window *new_parent_window, String name) { void InitWindows() { CreateWind(); CommandWindowInit(); - StatusWindowInit(); - DebugWindowInit(); - SearchWindowInit(); - BuildWindowInit(); } void CalcNiceties(Window *n) { @@ -147,11 +143,21 @@ void LayoutWindows(int16_t wx, int16_t wy) { ProfileFunction(); Rect2I screen_rect = RectI0Size(wx, wy); - StatusWindowLayout(&screen_rect, wx, wy); + ForItem (n, Windows) { + ForItem (hook, n->hooks) { + if (hook.kind == HookKind_LayoutWindow) { + ProfileScopeEx(it.name); + HookParam param = {}; + param.layout.window = n; + param.layout.rect = &screen_rect; + param.layout.wx = wx; + param.layout.wy = wy; + hook.function(param); + } + } + } + CommandWindowLayout(&screen_rect, wx, wy); - DebugWindowLayout(&screen_rect, wx, wy); - SearchWindowLayout(&screen_rect, wx, wy); - BuildWindowLayout(&screen_rect, wx, wy); // Column layout Int c = 0; diff --git a/src/text_editor/window_build.cpp b/src/text_editor/window_build.cpp index aeb8ce4..e090bcf 100644 --- a/src/text_editor/window_build.cpp +++ b/src/text_editor/window_build.cpp @@ -1,4 +1,24 @@ -void BuildWindowInit() { +void LayoutBuildWindow(HookParam param) { + auto p = param.layout; + Rect2I copy_rect = *p.rect; + if (!p.window->visible) { + p.rect = ©_rect; + } + Int barsize = p.window->font->line_spacing * 10; + p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize); +} + +void CMD_ShowBuildWindow(HookParam param) { + BSet main = GetBSet(BuildWindowID); + if (ActiveWindowID != BuildWindowID) { + main.window->visible = true; + NextActiveWindowID = BuildWindowID; + } else { + main.window->visible = false; + } +} RegisterCommand(CMD_ShowBuildWindow, "ctrl-grave"); + +void InitBuildWindow(HookParam param) { Window *window = CreateWind(); BuildWindowID = window->id; Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "build")); @@ -15,24 +35,5 @@ void BuildWindowInit() { window->visible = false; window->lose_visibility_on_escape = true; window->jump_history = false; -} - -void BuildWindowLayout(Rect2I *rect, Int wx, Int wy) { - Window *n = GetWindow(BuildWindowID); - Rect2I copy_rect = *rect; - if (!n->visible) { - rect = ©_rect; - } - Int barsize = n->font->line_spacing * 10; - n->document_rect = n->total_rect = CutBottom(rect, barsize); -} - -void CMD_ShowBuildWindow(HookParam param) { - BSet main = GetBSet(BuildWindowID); - if (ActiveWindowID != BuildWindowID) { - main.window->visible = true; - NextActiveWindowID = BuildWindowID; - } else { - main.window->visible = false; - } -} RegisterCommand(CMD_ShowBuildWindow, "ctrl-grave"); \ No newline at end of file + AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutBuildWindow", "", LayoutBuildWindow); +} RegisterHook(InitBuildWindow, HookKind_AppInit, "", "Init the build window"); diff --git a/src/text_editor/window_debug.cpp b/src/text_editor/window_debug.cpp index 6fb7139..63fc968 100644 --- a/src/text_editor/window_debug.cpp +++ b/src/text_editor/window_debug.cpp @@ -1,37 +1,15 @@ -void DebugWindowInit() { - Window *window = CreateWind(); - DebugWindowID = window->id; - window->draw_line_numbers = false; - window->draw_scrollbar = false; - window->visible = false; - window->z = 2; - window->primary = false; - window->jump_history = false; - - Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "debug")); - DebugBufferID = buffer->id; - buffer->no_history = true; - buffer->special = true; - - View *view = CreateView(buffer->id); - view->special = true; - DebugViewID = view->id; - window->active_view = view->id; - window->visible = false; -} - -void DebugWindowLayout(Rect2I *rect, Int wx, Int wy) { - Window *n = GetWindow(DebugWindowID); - Rect2 screen_rect = Rect0Size((float)wx, (float)wy); +void LayoutDebugWindow(HookParam param) { + auto p = param.layout; + Rect2 screen_rect = Rect0Size((float)p.wx, (float)p.wy); Vec2 size = GetSize(screen_rect); Rect2 a = CutRight(&screen_rect, 0.3f * size.x); Rect2 b = CutTop(&a, 0.4f * size.y); Rect2 c = Shrink(b, 20); - n->document_rect = n->total_rect = ToRect2I(c); + p.window->document_rect = p.window->total_rect = ToRect2I(c); } -void DebugWindowUpdate() { +void UpdateDebugWindow(HookParam param) { ProfileFunction(); BSet set = GetBSet(DebugWindowID); if (!set.window->visible) { @@ -77,7 +55,31 @@ void DebugWindowUpdate() { RawAppendf(set.buffer, "int dirty = %d\n", main.buffer->dirty); RawAppendf(set.buffer, "int changed_on_disk = %d\n", main.buffer->changed_on_disk); RawAppendf(set.buffer, "int temp = %d\n", main.buffer->temp); -} +} RegisterHook(UpdateDebugWindow, HookKind_AppUpdate, "", "Update the debug window"); + +void InitDebugWindow(HookParam param) { + Window *window = CreateWind(); + DebugWindowID = window->id; + window->draw_line_numbers = false; + window->draw_scrollbar = false; + window->visible = false; + window->z = 2; + window->primary = false; + window->jump_history = false; + + Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "debug")); + DebugBufferID = buffer->id; + buffer->no_history = true; + buffer->special = true; + + View *view = CreateView(buffer->id); + view->special = true; + DebugViewID = view->id; + window->active_view = view->id; + window->visible = false; + + AddHook(&window->hooks, HookKind_LayoutWindow, "@todo: LayoutDebugWindow", "", LayoutDebugWindow); +} RegisterHook(InitDebugWindow, HookKind_AppInit, "", "Init the debug window"); void CMD_ToggleDebug(HookParam param) { Window *window = GetWindow(DebugWindowID); diff --git a/src/text_editor/window_search.cpp b/src/text_editor/window_search.cpp index cd1b8a5..682ebad 100644 --- a/src/text_editor/window_search.cpp +++ b/src/text_editor/window_search.cpp @@ -55,7 +55,7 @@ void CMD_ToggleSearchWordBoundary(HookParam param) { SearchWordBoundary = !SearchWordBoundary; } RegisterCommand(CMD_ToggleSearchWordBoundary, "alt-w", "Text editor wide search toggle, should apply to most search things"); -void SearchWindowUpdate() { +void UpdateSearchWindow(HookParam param) { BSet active = GetBSet(ActiveWindowID); if (active.window->id == SearchWindowID && active.buffer->begin_frame_change_id != active.buffer->change_id) { BSet main = GetBSet(PrimaryWindowID); @@ -65,20 +65,20 @@ void SearchWindowUpdate() { Find(main.view, seek, true); CenterView(main.window->id); } -} +} RegisterHook(UpdateSearchWindow, HookKind_AppUpdate, "", "Update the search window"); -void SearchWindowLayout(Rect2I *rect, Int wx, Int wy) { - Window *n = GetWindow(SearchWindowID); - Rect2I copy_rect = *rect; - if (!n->visible) { - rect = ©_rect; +void LayoutSearchWindow(HookParam param) { + auto p = param.layout; + Rect2I copy_rect = *p.rect; + if (!p.window->visible) { + p.rect = ©_rect; } - Int barsize = GetExpandingBarSize(n); - n->document_rect = n->total_rect = CutBottom(rect, barsize); - n->line_numbers_rect = CutLeft(&n->document_rect, n->font->char_spacing * 6); + Int barsize = GetExpandingBarSize(p.window); + p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize); + p.window->line_numbers_rect = CutLeft(&p.window->document_rect, p.window->font->char_spacing * 6); } -void SearchWindowInit() { +void InitSearchWindow(HookParam param) { Window *window = CreateWind(); SearchWindowID = window->id; Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "search")); @@ -99,4 +99,5 @@ void SearchWindowInit() { AddCommand(&view->hooks, "SearchAll", "alt-enter", CMD_SearchAll); AddCommand(&view->hooks, "SearchPrevInSearch", "shift-enter", CMD_SearchPrevInSearch); AddCommand(&view->hooks, "SearchNextInSearch", "enter", CMD_SearchNextInSearch); -} + AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutSearchWindow", "", LayoutSearchWindow); +} RegisterHook(InitSearchWindow, HookKind_AppInit, "", "Init the search window"); diff --git a/src/text_editor/window_status.cpp b/src/text_editor/window_status.cpp index 252c11c..fa558e6 100644 --- a/src/text_editor/window_status.cpp +++ b/src/text_editor/window_status.cpp @@ -1,32 +1,4 @@ -void StatusWindowInit() { - Window *window = CreateWind(); - StatusBarWindowID = window->id; - Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "status_bar")); - buffer->special = true; - View *view = CreateView(buffer->id); - view->special = true; - window->active_view = view->id; - // window->font = &SecondaryFont; - window->draw_line_numbers = false; - window->draw_scrollbar = false; - window->draw_line_highlight = true; - window->secondary_window_style = true; - window->primary = false; - window->jump_history = false; - window->lose_focus_on_escape = true; -} - -void StatusWindowLayout(Rect2I *rect, Int wx, Int wy) { - Window *n = GetWindow(StatusBarWindowID); - Rect2I copy_rect = *rect; - if (!n->visible) { - rect = ©_rect; - } - Int barsize = GetExpandingBarSize(n); - n->document_rect = n->total_rect = CutBottom(rect, barsize); -} - -void StatusWindowUpdate() { +void StatusWindowUpdate(HookParam param) { ProfileFunction(); Window *status_bar_window = GetWindow(StatusBarWindowID, NULL); Scratch scratch; @@ -95,4 +67,33 @@ void StatusWindowUpdate() { SelectRange(title.view, MakeRange(0)); ResetHistory(title.buffer); -} +} RegisterHook(StatusWindowUpdate, HookKind_AppUpdate, "", ""); + +void LayoutStatusWindow(HookParam param) { + auto p = param.layout; + Rect2I copy_rect = *p.rect; + if (!p.window->visible) { + p.rect = ©_rect; + } + Int barsize = GetExpandingBarSize(p.window); + p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize); +} RegisterHook(LayoutStatusWindow, HookKind_LayoutWindow, "", "Layout the status window"); + +void StatusWindowInit(HookParam param) { + Window *window = CreateWind(); + StatusBarWindowID = window->id; + Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "status_bar")); + buffer->special = true; + View *view = CreateView(buffer->id); + view->special = true; + window->active_view = view->id; + // window->font = &SecondaryFont; + window->draw_line_numbers = false; + window->draw_scrollbar = false; + window->draw_line_highlight = true; + window->secondary_window_style = true; + window->primary = false; + window->jump_history = false; + window->lose_focus_on_escape = true; + AddHook(&window->hooks, HookKind_LayoutWindow, "@todo LayoutStatusWindow", "", LayoutStatusWindow); +} RegisterHook(StatusWindowInit, HookKind_AppInit, "", "Init the status window"); \ No newline at end of file