Moving stuff over to new system

This commit is contained in:
Krzosa Karol
2026-01-09 09:09:58 +01:00
parent 005a22a93d
commit 57c179970e
8 changed files with 142 additions and 129 deletions

View File

@@ -930,6 +930,17 @@ void AddCommand(Array<Hook> *arr, String name, String binding, HookFunction *fun
Add(arr, hook); Add(arr, hook);
} }
void AddHook(Array<Hook> *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) { void Coro_Rename(mco_coro *co) {
BSet main = GetBSet(PrimaryWindowID); BSet main = GetBSet(PrimaryWindowID);
Buffer *buffer = main.buffer; 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"); } 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);
}
}

View File

@@ -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); 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_history);
Dealloc(&it->goto_redo); Dealloc(&it->goto_redo);
Dealloc(&it->hooks);
Dealloc(sys_allocator, it); Dealloc(sys_allocator, it);
remove_item = true; remove_item = true;
} else { } else {
@@ -581,10 +582,15 @@ void Update(Event event) {
} }
OnCommand(event); OnCommand(event);
StatusWindowUpdate();
DebugWindowUpdate(); For (GlobalHooks) {
if (it.kind == HookKind_AppUpdate) {
ProfileScopeEx(it.name);
it.function(HookParam{});
}
}
FuzzySearchViewUpdate(); FuzzySearchViewUpdate();
SearchWindowUpdate();
UpdateProcesses(); UpdateProcesses();
CoUpdate(&event); CoUpdate(&event);

View File

@@ -67,7 +67,7 @@ enum HookKind {
HookKind_Command, HookKind_Command,
HookKind_BeforeLayoutWindow, HookKind_BeforeLayoutWindow,
HookKind_HandleLayoutWindow, HookKind_LayoutWindow,
HookKind_AfterLayoutWindow, HookKind_AfterLayoutWindow,
HookKind_BeforeRenderWindow, HookKind_BeforeRenderWindow,
@@ -88,6 +88,14 @@ enum HookKind {
// Global hooks, per (window, view, buffer) hooks // Global hooks, per (window, view, buffer) hooks
struct HookParam { struct HookParam {
union {
struct {
Window *window;
Rect2I *rect;
int16_t wx;
int16_t wy;
} layout;
};
}; };
typedef void HookFunction(HookParam param); typedef void HookFunction(HookParam param);
@@ -155,6 +163,7 @@ struct Window {
ViewID active_goto_list; ViewID active_goto_list;
Int goto_list_pos; Int goto_list_pos;
Array<Hook> hooks;
struct { struct {
uint32_t draw_scrollbar : 1; uint32_t draw_scrollbar : 1;
uint32_t draw_line_numbers : 1; uint32_t draw_line_numbers : 1;
@@ -298,6 +307,7 @@ struct ResolvedOpen {
bool existing_buffer; bool existing_buffer;
}; };
void AddHook(Array<Hook> *arr, HookKind kind, String name, String binding, HookFunction *function);
void AddCommand(Array<Hook> *arr, String name, String binding, HookFunction *function); void AddCommand(Array<Hook> *arr, String name, String binding, HookFunction *function);
#define RegisterCommand(name, ...) Register_Command RC__##name(&GlobalHooks, HookKind_Command, name, #name, __VA_ARGS__) #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) #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 JumpTempBuffer(BSet *set, String buffer_name = "");
void CommandWindowLayout(Rect2I *rect, Int wx, Int wy); void CommandWindowLayout(Rect2I *rect, Int wx, Int wy);
void CommandWindowInit(); 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); View *FindView(BufferID buffer_id, View *default_view = NULL);
bool operator==(BufferID a, BufferID b) { return a.id == b.id; } bool operator==(BufferID a, BufferID b) { return a.id == b.id; }

View File

@@ -117,10 +117,6 @@ View *WindowOpenBufferView(Window *new_parent_window, String name) {
void InitWindows() { void InitWindows() {
CreateWind(); CreateWind();
CommandWindowInit(); CommandWindowInit();
StatusWindowInit();
DebugWindowInit();
SearchWindowInit();
BuildWindowInit();
} }
void CalcNiceties(Window *n) { void CalcNiceties(Window *n) {
@@ -147,11 +143,21 @@ void LayoutWindows(int16_t wx, int16_t wy) {
ProfileFunction(); ProfileFunction();
Rect2I screen_rect = RectI0Size(wx, wy); 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); CommandWindowLayout(&screen_rect, wx, wy);
DebugWindowLayout(&screen_rect, wx, wy);
SearchWindowLayout(&screen_rect, wx, wy);
BuildWindowLayout(&screen_rect, wx, wy);
// Column layout // Column layout
Int c = 0; Int c = 0;

View File

@@ -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 = &copy_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(); Window *window = CreateWind();
BuildWindowID = window->id; BuildWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "build")); Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "build"));
@@ -15,24 +35,5 @@ void BuildWindowInit() {
window->visible = false; window->visible = false;
window->lose_visibility_on_escape = true; window->lose_visibility_on_escape = true;
window->jump_history = false; window->jump_history = false;
} AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutBuildWindow", "", LayoutBuildWindow);
} RegisterHook(InitBuildWindow, HookKind_AppInit, "", "Init the build window");
void BuildWindowLayout(Rect2I *rect, Int wx, Int wy) {
Window *n = GetWindow(BuildWindowID);
Rect2I copy_rect = *rect;
if (!n->visible) {
rect = &copy_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");

View File

@@ -1,37 +1,15 @@
void DebugWindowInit() { void LayoutDebugWindow(HookParam param) {
Window *window = CreateWind(); auto p = param.layout;
DebugWindowID = window->id; Rect2 screen_rect = Rect0Size((float)p.wx, (float)p.wy);
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);
Vec2 size = GetSize(screen_rect); Vec2 size = GetSize(screen_rect);
Rect2 a = CutRight(&screen_rect, 0.3f * size.x); Rect2 a = CutRight(&screen_rect, 0.3f * size.x);
Rect2 b = CutTop(&a, 0.4f * size.y); Rect2 b = CutTop(&a, 0.4f * size.y);
Rect2 c = Shrink(b, 20); 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(); ProfileFunction();
BSet set = GetBSet(DebugWindowID); BSet set = GetBSet(DebugWindowID);
if (!set.window->visible) { if (!set.window->visible) {
@@ -77,7 +55,31 @@ void DebugWindowUpdate() {
RawAppendf(set.buffer, "int dirty = %d\n", main.buffer->dirty); 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 changed_on_disk = %d\n", main.buffer->changed_on_disk);
RawAppendf(set.buffer, "int temp = %d\n", main.buffer->temp); 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) { void CMD_ToggleDebug(HookParam param) {
Window *window = GetWindow(DebugWindowID); Window *window = GetWindow(DebugWindowID);

View File

@@ -55,7 +55,7 @@ void CMD_ToggleSearchWordBoundary(HookParam param) {
SearchWordBoundary = !SearchWordBoundary; SearchWordBoundary = !SearchWordBoundary;
} RegisterCommand(CMD_ToggleSearchWordBoundary, "alt-w", "Text editor wide search toggle, should apply to most search things"); } 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); BSet active = GetBSet(ActiveWindowID);
if (active.window->id == SearchWindowID && active.buffer->begin_frame_change_id != active.buffer->change_id) { if (active.window->id == SearchWindowID && active.buffer->begin_frame_change_id != active.buffer->change_id) {
BSet main = GetBSet(PrimaryWindowID); BSet main = GetBSet(PrimaryWindowID);
@@ -65,20 +65,20 @@ void SearchWindowUpdate() {
Find(main.view, seek, true); Find(main.view, seek, true);
CenterView(main.window->id); CenterView(main.window->id);
} }
} RegisterHook(UpdateSearchWindow, HookKind_AppUpdate, "", "Update the search window");
void LayoutSearchWindow(HookParam param) {
auto p = param.layout;
Rect2I copy_rect = *p.rect;
if (!p.window->visible) {
p.rect = &copy_rect;
}
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 SearchWindowLayout(Rect2I *rect, Int wx, Int wy) { void InitSearchWindow(HookParam param) {
Window *n = GetWindow(SearchWindowID);
Rect2I copy_rect = *rect;
if (!n->visible) {
rect = &copy_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);
}
void SearchWindowInit() {
Window *window = CreateWind(); Window *window = CreateWind();
SearchWindowID = window->id; SearchWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "search")); Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "search"));
@@ -99,4 +99,5 @@ void SearchWindowInit() {
AddCommand(&view->hooks, "SearchAll", "alt-enter", CMD_SearchAll); AddCommand(&view->hooks, "SearchAll", "alt-enter", CMD_SearchAll);
AddCommand(&view->hooks, "SearchPrevInSearch", "shift-enter", CMD_SearchPrevInSearch); AddCommand(&view->hooks, "SearchPrevInSearch", "shift-enter", CMD_SearchPrevInSearch);
AddCommand(&view->hooks, "SearchNextInSearch", "enter", CMD_SearchNextInSearch); AddCommand(&view->hooks, "SearchNextInSearch", "enter", CMD_SearchNextInSearch);
} AddHook(&window->hooks, HookKind_LayoutWindow, "LayoutSearchWindow", "", LayoutSearchWindow);
} RegisterHook(InitSearchWindow, HookKind_AppInit, "", "Init the search window");

View File

@@ -1,32 +1,4 @@
void StatusWindowInit() { void StatusWindowUpdate(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;
}
void StatusWindowLayout(Rect2I *rect, Int wx, Int wy) {
Window *n = GetWindow(StatusBarWindowID);
Rect2I copy_rect = *rect;
if (!n->visible) {
rect = &copy_rect;
}
Int barsize = GetExpandingBarSize(n);
n->document_rect = n->total_rect = CutBottom(rect, barsize);
}
void StatusWindowUpdate() {
ProfileFunction(); ProfileFunction();
Window *status_bar_window = GetWindow(StatusBarWindowID, NULL); Window *status_bar_window = GetWindow(StatusBarWindowID, NULL);
Scratch scratch; Scratch scratch;
@@ -95,4 +67,33 @@ void StatusWindowUpdate() {
SelectRange(title.view, MakeRange(0)); SelectRange(title.view, MakeRange(0));
ResetHistory(title.buffer); 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 = &copy_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");