Simplify active windows

This commit is contained in:
Krzosa Karol
2024-08-10 09:08:35 +02:00
parent d5f903eaa2
commit 08a385b050
13 changed files with 65 additions and 100 deletions

View File

@@ -289,7 +289,7 @@ end
function MatchWindowsPath(_s) function MatchWindowsPath(_s)
local s, file_path, drive = SkipPath(_s) local s, file_path, drive = SkipPath(_s)
if not drive then if not drive then
local d = GetCurrentBufferDir() local d = GetActiveMainWindowBufferName()
file_path = d..'/'..file_path file_path = d..'/'..file_path
end end
local line, col, s = SkipLineAndColumn(s) local line, col, s = SkipLineAndColumn(s)
@@ -303,7 +303,7 @@ function MatchGitCommit(s)
if i then if i then
s = s:sub(8) s = s:sub(8)
local command = "git --no-pager show "..s local command = "git --no-pager show "..s
return {kind = "exec", cmd = command, working_dir = GetCurrentBufferDir()} return {kind = "exec", cmd = command, working_dir = GetActiveMainWindowBufferName()}
end end
return nil return nil
end end

View File

@@ -310,11 +310,11 @@ bool GlobalCommand(Event event) {
if (event.ctrl && event.shift && Mouse(RIGHT)) { if (event.ctrl && event.shift && Mouse(RIGHT)) {
MouseExecWord(event); MouseExecWord(event);
} else if (event.alt && event.ctrl && Mouse(RIGHT)) { } else if (event.alt && event.ctrl && Mouse(RIGHT)) {
GotoForward(GetLastActiveWindow()); GotoForward(GetActiveMainWindowID());
} else if (event.ctrl && Mouse(RIGHT)) { } else if (event.ctrl && Mouse(RIGHT)) {
MouseLoadWord(event); MouseLoadWord(event);
} else if (event.alt && Mouse(RIGHT)) { } else if (event.alt && Mouse(RIGHT)) {
GotoBackward(GetLastActiveWindow()); GotoBackward(GetActiveMainWindowID());
} else if (Mouse(RIGHT)) { } else if (Mouse(RIGHT)) {
Vec2I mouse = MouseVec2I(); Vec2I mouse = MouseVec2I();
Window *window = GetActiveWindow(); Window *window = GetActiveWindow();
@@ -510,7 +510,7 @@ void ReportWarningf(const char *fmt, ...) {
Command_Append(NullViewID, string, true); Command_Append(NullViewID, string, true);
Window *window = GetWindowWithView(NullViewID); Window *window = GetWindowWithView(NullViewID);
if (!window) { if (!window) {
WindowID last_active_window_id = GetLastActiveWindow(); WindowID last_active_window_id = GetActiveMainWindowID();
window = GetWindow(last_active_window_id); window = GetWindow(last_active_window_id);
} }
CheckpointBeforeGoto(window->id); CheckpointBeforeGoto(window->id);

View File

@@ -652,7 +652,7 @@ void WindowCommand(Event event, Window *window, View *view) {
if (Press(SDLK_ESCAPE)) { if (Press(SDLK_ESCAPE)) {
if (window->deactivate_on_escape) { if (window->deactivate_on_escape) {
SetActiveWindow(GetLastActiveWindow()); SetActiveWindow(GetActiveMainWindowID());
} else { } else {
view->carets.len = 1; view->carets.len = 1;
} }
@@ -866,7 +866,7 @@ void WindowCommand(Event event, Window *window, View *view) {
} }
if (Ctrl(SDLK_PERIOD)) { if (Ctrl(SDLK_PERIOD)) {
Window *window = GetWindow(GetLastActiveWindow()); Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
@@ -909,7 +909,7 @@ void WindowCommand(Event event, Window *window, View *view) {
Command_EvalLua(view, string); Command_EvalLua(view, string);
} else if (CtrlAlt(SDLK_Q)) { } else if (CtrlAlt(SDLK_Q)) {
GotoForward(GetLastActiveWindow()); GotoForward(GetActiveMainWindowID());
} else if (Ctrl(SDLK_Q)) { } else if (Ctrl(SDLK_Q)) {
Caret caret = view->carets[0]; Caret caret = view->carets[0];
Range range = caret.range; Range range = caret.range;
@@ -919,7 +919,7 @@ void WindowCommand(Event event, Window *window, View *view) {
window->active_goto_list = view->id; window->active_goto_list = view->id;
Open(string); Open(string);
} else if (Alt(SDLK_Q)) { } else if (Alt(SDLK_Q)) {
GotoBackward(GetLastActiveWindow()); GotoBackward(GetActiveMainWindowID());
} }
IF_DEBUG(AssertRanges(view->carets)); IF_DEBUG(AssertRanges(view->carets));
} }

View File

@@ -178,7 +178,7 @@ end
function MatchWindowsPath(_s) function MatchWindowsPath(_s)
local s, file_path, drive = SkipPath(_s) local s, file_path, drive = SkipPath(_s)
if not drive then if not drive then
local d = GetCurrentBufferDir() local d = GetActiveMainWindowBufferName()
file_path = d..'/'..file_path file_path = d..'/'..file_path
end end
local line, col, s = SkipLineAndColumn(s) local line, col, s = SkipLineAndColumn(s)
@@ -192,7 +192,7 @@ function MatchGitCommit(s)
if i then if i then
s = s:sub(8) s = s:sub(8)
local command = "git --no-pager show "..s local command = "git --no-pager show "..s
return {kind = "exec", cmd = command, working_dir = GetCurrentBufferDir()} return {kind = "exec", cmd = command, working_dir = GetActiveMainWindowBufferName()}
end end
return nil return nil
end end

View File

@@ -37,9 +37,9 @@ String GetUniqueBufferName(Allocator allocator, String working_dir, String p
void ExecInNewBuffer(String cmd, String working_dir) { void ExecInNewBuffer(String cmd, String working_dir) {
Scratch scratch; Scratch scratch;
CheckpointBeforeGoto(GetLastActiveWindow()); CheckpointBeforeGoto(GetActiveMainWindowID());
String buffer_name = GetUniqueBufferName(scratch, working_dir, "+cmd-"); String buffer_name = GetUniqueBufferName(scratch, working_dir, "+cmd-");
Window *window = GetWindow(GetLastActiveWindow()); Window *window = GetActiveMainWindow();
View *view = WindowOpenBufferView(window, buffer_name); View *view = WindowOpenBufferView(window, buffer_name);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
view->carets[0] = MakeCaret({}); view->carets[0] = MakeCaret({});
@@ -66,7 +66,7 @@ void Open(String path) {
String col_string = FieldString(LuaState, "col"); String col_string = FieldString(LuaState, "col");
Int col = strtoll(col_string.data, NULL, 10); Int col = strtoll(col_string.data, NULL, 10);
WindowID window_id = GetLastActiveWindow(); WindowID window_id = GetActiveMainWindowID();
CheckpointBeforeGoto(window_id); CheckpointBeforeGoto(window_id);
Window *window = GetWindow(window_id); Window *window = GetWindow(window_id);
View *view = WindowOpenBufferView(window, file_path); View *view = WindowOpenBufferView(window, file_path);
@@ -98,9 +98,8 @@ int Lua_FuzzySort(lua_State *L) {
Scratch scratch; Scratch scratch;
String16 string16 = ToString16(scratch, string); String16 string16 = ToString16(scratch, string);
WindowID window_id = GetLastActiveWindow(); Window *window = GetActiveMainWindow();
Window *window = GetWindow(window_id); View *view = GetView(window->active_view);
View *view = GetView(window->active_view);
Command_FuzzySort(view, string16); Command_FuzzySort(view, string16);
return 0; return 0;
} }
@@ -108,7 +107,7 @@ int Lua_FuzzySort(lua_State *L) {
int Lua_AppendCmd(lua_State *L) { int Lua_AppendCmd(lua_State *L) {
String string = lua_tostring(L, 1); String string = lua_tostring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
String working_dir = GetCurrentBufferDir(); String working_dir = GetActiveMainWindowBufferDir();
Exec(NullViewID, true, string, working_dir); Exec(NullViewID, true, string, working_dir);
return 0; return 0;
} }
@@ -117,13 +116,13 @@ int Lua_NewCmd(lua_State *L) {
String string = lua_tostring(L, 1); String string = lua_tostring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
String working_dir = GetCurrentBufferDir(); String working_dir = GetActiveMainWindowBufferDir();
ExecInNewBuffer(string, working_dir); ExecInNewBuffer(string, working_dir);
return 0; return 0;
} }
int Lua_Kill(lua_State *L) { int Lua_Kill(lua_State *L) {
Window *window = GetCurrentWindow(); Window *window = GetActiveMainWindow();
KillProcess(window->active_view); KillProcess(window->active_view);
return 0; return 0;
} }
@@ -145,7 +144,7 @@ int Lua_Print(lua_State *L) {
} }
int Lua_ListBuffers(lua_State *L) { int Lua_ListBuffers(lua_State *L) {
Window *window = GetWindow(GetLastActiveWindow()); Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
CheckpointBeforeGoto(window->id); CheckpointBeforeGoto(window->id);
@@ -153,7 +152,7 @@ int Lua_ListBuffers(lua_State *L) {
Scratch scratch; Scratch scratch;
Array<String> strings = {scratch}; Array<String> strings = {scratch};
For(Buffers) { For(Buffers) {
String string = Format(scratch, "%.*s", FmtString(it.name)); String string = Format(scratch, "%.*s id=%d is_dir=%d", FmtString(it.name), (int)it.id.id, it.is_directory);
Add(&strings, string); Add(&strings, string);
} }
String result = Merge(scratch, strings, "\n"); String result = Merge(scratch, strings, "\n");
@@ -196,30 +195,30 @@ int Lua_GetWorkingDir(lua_State *L) {
return 1; return 1;
} }
int Lua_GetCurrentBufferName(lua_State *L) { int Lua_GetActiveMainWindowBufferName(lua_State *L) {
String name = GetCurrentBufferName(); String name = GetActiveMainWindowBufferName();
lua_pushlstring(LuaState, name.data, name.len); lua_pushlstring(LuaState, name.data, name.len);
return 1; return 1;
} }
int Lua_GetCurrentBufferDir(lua_State *L) { int Lua_GetActiveMainWindowBufferDir(lua_State *L) {
String name = GetCurrentBufferDir(); String name = GetActiveMainWindowBufferDir();
lua_pushlstring(LuaState, name.data, name.len); lua_pushlstring(LuaState, name.data, name.len);
return 1; return 1;
} }
int Lua_OpenFilesHere(lua_State *L) { int Lua_OpenFilesHere(lua_State *L) {
Window *window = GetWindow(GetLastActiveWindow()); Window *window = GetActiveMainWindow();
Scratch scratch; Scratch scratch;
for (FileIter it = IterateFiles(scratch, GetCurrentBufferDir()); IsValid(it); Advance(&it)) { for (FileIter it = IterateFiles(scratch, GetActiveMainWindowBufferDir()); IsValid(it); Advance(&it)) {
WindowOpenBufferView(window, it.absolute_path); WindowOpenBufferView(window, it.absolute_path);
} }
return 0; return 0;
} }
int Lua_Search(lua_State *L) { int Lua_Search(lua_State *L) {
Window *seek_window = GetCurrentWindow(); Window *seek_window = GetActiveMainWindow();
seek_window->search_string = lua_tostring(L, 1); seek_window->search_string = lua_tostring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
View *seek_view = GetView(seek_window->active_view); View *seek_view = GetView(seek_window->active_view);
@@ -229,7 +228,7 @@ int Lua_Search(lua_State *L) {
} }
int Lua_SearchB(lua_State *L) { int Lua_SearchB(lua_State *L) {
Window *seek_window = GetCurrentWindow(); Window *seek_window = GetActiveMainWindow();
seek_window->search_string = lua_tostring(L, 1); seek_window->search_string = lua_tostring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
View *seek_view = GetView(seek_window->active_view); View *seek_view = GetView(seek_window->active_view);

View File

@@ -9,8 +9,8 @@ luaL_Reg LuaFunctions[] = {
{"GetBufferList", Lua_GetBufferList}, {"GetBufferList", Lua_GetBufferList},
{"FileExists", Lua_FileExists}, {"FileExists", Lua_FileExists},
{"GetWorkingDir", Lua_GetWorkingDir}, {"GetWorkingDir", Lua_GetWorkingDir},
{"GetCurrentBufferName", Lua_GetCurrentBufferName}, {"GetActiveMainWindowBufferName", Lua_GetActiveMainWindowBufferName},
{"GetCurrentBufferDir", Lua_GetCurrentBufferDir}, {"GetActiveMainWindowBufferDir", Lua_GetActiveMainWindowBufferDir},
{"OpenFilesHere", Lua_OpenFilesHere}, {"OpenFilesHere", Lua_OpenFilesHere},
{"Search", Lua_Search}, {"Search", Lua_Search},
{"SearchB", Lua_SearchB}, {"SearchB", Lua_SearchB},

View File

@@ -117,22 +117,8 @@ View *CreateView(BufferID active_buffer) {
return w; return w;
} }
WindowID GetLastActiveWindow() {
For(IterateInReverse(&WindowSwitchHistory)) {
return it;
}
return NullWindowID;
}
void SetActiveWindow(WindowID window) { void SetActiveWindow(WindowID window) {
bool new_active_window = window.id != ActiveWindow.id; ActiveWindow = window;
if (new_active_window) {
ActiveWindow = window;
Window *w = GetWindow(window);
if (!w->dont_save_in_active_window_history) {
Add(&WindowSwitchHistory, window);
}
}
} }
View *FindView(BufferID buffer_id) { View *FindView(BufferID buffer_id) {
@@ -181,7 +167,7 @@ Window *GetTitlebarWindow(WindowID id) {
return window; return window;
} }
Window *GetCurrentWindow() { Window *GetActiveMainWindow() {
Window *window = GetWindow(ActiveWindow); Window *window = GetWindow(ActiveWindow);
if (window->is_title_bar) { if (window->is_title_bar) {
window = GetWindow(window->title_bar_window); window = GetWindow(window->title_bar_window);
@@ -189,8 +175,12 @@ Window *GetCurrentWindow() {
return window; return window;
} }
String GetCurrentBufferName() { WindowID GetActiveMainWindowID() {
Window *window = GetCurrentWindow(); return GetActiveMainWindow()->id;
}
String GetActiveMainWindowBufferName() {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
return buffer->name; return buffer->name;
@@ -201,8 +191,8 @@ String GetDir(Buffer *buffer) {
return name; return name;
} }
String GetCurrentBufferDir() { String GetActiveMainWindowBufferDir() {
Window *window = GetCurrentWindow(); Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
String name = buffer->is_directory ? buffer->name : ChopLastSlash(buffer->name); String name = buffer->is_directory ? buffer->name : ChopLastSlash(buffer->name);
@@ -262,6 +252,9 @@ Buffer *BufferOpenFile(String path) {
if (!IsNull(buffer)) { if (!IsNull(buffer)) {
return buffer; return buffer;
} }
if (IsNull(buffer) && buffer->name == path) {
return buffer;
}
if (!FileExists(path)) { if (!FileExists(path)) {
path = GetAbsolutePath(scratch, path); path = GetAbsolutePath(scratch, path);
@@ -284,6 +277,7 @@ Buffer *BufferOpenFile(String path) {
buffer->len = ConvertUTF8ToUTF16UnixLine(string, buffer->str, buffer->cap); buffer->len = ConvertUTF8ToUTF16UnixLine(string, buffer->str, buffer->cap);
UpdateLines(buffer, {}, String16{(wchar_t *)buffer->data, buffer->len}); UpdateLines(buffer, {}, String16{(wchar_t *)buffer->data, buffer->len});
} }
return buffer; return buffer;
} }
@@ -294,16 +288,6 @@ View *OpenBufferView(String name) {
} }
View *WindowOpenBufferView(Window *new_parent_window, String name) { View *WindowOpenBufferView(Window *new_parent_window, String name) {
// @todo: window history
// For(new_parent_window->views) {
// View *it_view = GetView(it);
// Buffer *it_buffer = GetBuffer(it_view->active_buffer);
// if (it_buffer->name == name) {
// new_parent_window->active_view = it;
// return it_view;
// }
// }
View *view = FindViewWithBufferName(name); View *view = FindViewWithBufferName(name);
if (!view) { if (!view) {
View *result = OpenBufferView(name); View *result = OpenBufferView(name);

View File

@@ -189,18 +189,7 @@ void Update(Event event) {
For(IterateInReverse(&order)) { For(IterateInReverse(&order)) {
Window *window = &Windows[it]; Window *window = &Windows[it];
{ if (!window->visible) continue;
if (window->invisible_when_inactive) {
bool title_bar_is_active = ActiveWindow == window->title_bar_window;
bool is_active = IsActive(window);
if (is_active || title_bar_is_active) {
SetVisibility(window->id, true);
} else {
SetVisibility(window->id, false);
}
}
if (!window->visible) continue;
}
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
UpdateScroll(window, !AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && view->update_scroll); UpdateScroll(window, !AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && view->update_scroll);

View File

@@ -84,13 +84,11 @@ struct Window {
bool draw_scrollbar : 1; bool draw_scrollbar : 1;
bool draw_line_numbers : 1; bool draw_line_numbers : 1;
bool invisible_when_inactive : 1;
bool visible : 1; bool visible : 1;
bool absolute_position : 1; bool absolute_position : 1;
bool is_title_bar : 1; bool is_title_bar : 1;
bool is_column : 1; bool is_column : 1;
bool dont_save_in_active_window_history : 1;
bool deactivate_on_escape : 1; bool deactivate_on_escape : 1;
}; };
}; };

View File

@@ -30,7 +30,7 @@ void UpdateDebugBuffer() {
View *view = GetActiveView(window); View *view = GetActiveView(window);
if (view->active_buffer.id == buffer->id.id) return; if (view->active_buffer.id == buffer->id.id) return;
Window *last_window = GetWindow(GetLastActiveWindow()); Window *last_window = GetActiveMainWindow();
View *last_view = GetActiveView(last_window); View *last_view = GetActiveView(last_window);
Buffer *last_buffer = GetBuffer(last_view->active_buffer); Buffer *last_buffer = GetBuffer(last_view->active_buffer);

View File

@@ -1,5 +1,8 @@
- Remove pointers and use ViewIDs (enable array debug while doing this) - Remove pointers and use ViewIDs (enable array debug while doing this)
- Remove console and command window, provide alternatives but unify the interface? - Remove console and command window, provide alternatives but unify the interface?
- We can create 2 buffers with same name
- window splitting leaves a whitegap at the end
- PageUp for some reason stops at 2 line before last line
- apply clang format - apply clang format
- apply clang format on save - apply clang format on save
@@ -9,27 +12,21 @@
- ctrl + f - should find Search and select content or add Search - ctrl + f - should find Search and select content or add Search
- some split selection commands - some split selection commands
- assign commands or lua functions to F1-F8 keys - assign commands or lua functions to F1-F8 keys
- generate the lua function table for all functions prefixed with Lua_
- A lister which is going to show project without the full path and sorted by recency - A lister which is going to show project without the full path and sorted by recency
- Fix fuzzy search look
- word complete - word complete
- Search all buffers in 10X style, incrementally searched results popping up on every key press (maybe we need coroutine library in C so this is easier?) - Search all buffers in 10X style, incrementally searched results popping up on every key press (maybe we need coroutine library in C so this is easier?)
- Search and replace - Search and replace
- load in a next window
- load in new window
- kill view - killing all views for buffer ejects buffer (maybe also introduce kill buffer) - kill view - killing all views for buffer ejects buffer (maybe also introduce kill buffer)
- ask if you want to close without saving on exit - ask if you want to close without saving on exit
- how to be better at marking buffers as modified?
- ask if you want to create new file? - ask if you want to create new file?
- open lua file with proper name it should do the work for setting up a project (double-click on desktop)
- when do we regen directory buffers? - when do we regen directory buffers?
- load project command which loads files and config - load project command which loads files and config
- load all files in a directory
- global config and local config - global config and local config
- open project from cmd
- draw indentation levels like in sublime (those lines) - we render chars one by one so seems relatively easy to figure out if whitespace belongs to beginning of line (make sure to add max value like 40 because of big files) - draw indentation levels like in sublime (those lines) - we render chars one by one so seems relatively easy to figure out if whitespace belongs to beginning of line (make sure to add max value like 40 because of big files)
- code sections, visual demarkation if beginning of line has a very specific text + goto next / goto prev section hotkey! - code sections, visual demarkation if beginning of line has a very specific text + goto next / goto prev section hotkey!

View File

@@ -39,11 +39,10 @@ Array<Int> GetWindowZOrder(Allocator allocator) {
} }
Window *CreateTitlebar(WindowID parent_window_id) { Window *CreateTitlebar(WindowID parent_window_id) {
Window *window = CreateWindow(); Window *window = CreateWindow();
window->draw_scrollbar = false; window->draw_scrollbar = false;
window->dont_save_in_active_window_history = true; window->deactivate_on_escape = true;
window->deactivate_on_escape = true; window->is_title_bar = true;
window->is_title_bar = true;
static int TitlebarCount; static int TitlebarCount;
Allocator sys_allocator = GetSystemAllocator(); Allocator sys_allocator = GetSystemAllocator();
@@ -148,16 +147,15 @@ void InitWindows() {
} }
{ {
Window *window = CreateWindow(); Window *window = CreateWindow();
WindowID window_id = window->id; WindowID window_id = window->id;
DebugWindowID = window->id; DebugWindowID = window->id;
window->draw_line_numbers = false; window->draw_line_numbers = false;
window->absolute_position = true; window->absolute_position = true;
window->draw_line_numbers = false; window->draw_line_numbers = false;
window->draw_scrollbar = false; window->draw_scrollbar = false;
window->dont_save_in_active_window_history = true; window->visible = false;
window->visible = false; window->z = 2;
window->z = 2;
Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("+debug")); Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("+debug"));
DebugBufferID = buffer->id; DebugBufferID = buffer->id;

View File

@@ -109,7 +109,7 @@ void DrawWindow(Window *window, Event &event) {
View *view = GetActiveView(window); View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
SetScissor(GetScreenRectF()); SetScissor(GetScreenRectF());
bool is_active = IsActive(window) || window->id.id == GetLastActiveWindow().id; bool is_active = IsActive(window) || window->id.id == GetActiveMainWindowID().id;
Color color_whitespace_during_selection = ColorWhitespaceDuringSelection; Color color_whitespace_during_selection = ColorWhitespaceDuringSelection;
Color color_background = ColorBackground; Color color_background = ColorBackground;