Improving window/view/buffer management

This commit is contained in:
Krzosa Karol
2024-07-24 07:12:50 +02:00
parent fb19abb69f
commit 02b63bf8d2
5 changed files with 66 additions and 60 deletions

View File

@@ -226,26 +226,3 @@ void HandleGlobalCommands() {
SetActiveWindow({2});
}
}
void ChangeActiveWindowAndScroll(Array<Int> &order) {
For(order) {
Window *window = &Windows[it];
View *view = GetActiveView(window);
Vec2 mouse = GetMousePosition();
bool mouse_in_window = CheckCollisionPointRec(mouse, ToRectangle(window->total_rect));
if (mouse_in_window) {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
SetActiveWindow(window->id);
}
if (LastFrameIDWhenSwitchedActiveWindow != FrameID) {
if (IsKeyDown(KEY_F1)) {
view->scroll.x -= (Int)(GetMouseWheelMove() * 48);
} else {
view->scroll.y -= (Int)(GetMouseWheelMove() * 48);
}
LastFrameIDWhenSwitchedActiveWindow = FrameID;
}
}
}
}

View File

@@ -507,4 +507,27 @@ void HandleWindowBindings(Window *window) {
// calculating this value incrementally but do we even need X scrollbar or x clipping?
view.scroll.x = ClampBottom(view.scroll.x, (Int)0);
}
}
}
void ChangeActiveWindowAndScroll(Array<Int> &order) {
For(order) {
Window *window = &Windows[it];
View *view = GetActiveView(window);
Vec2 mouse = GetMousePosition();
bool mouse_in_window = CheckCollisionPointRec(mouse, ToRectangle(window->total_rect));
if (mouse_in_window) {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
SetActiveWindow(window->id);
}
if (LastFrameIDWhenSwitchedActiveWindow != FrameID) {
if (IsKeyDown(KEY_F1)) {
view->scroll.x -= (Int)(GetMouseWheelMove() * 48);
} else {
view->scroll.y -= (Int)(GetMouseWheelMove() * 48);
}
LastFrameIDWhenSwitchedActiveWindow = FrameID;
}
}
}
}

View File

@@ -3,10 +3,8 @@ lua_State *LuaState = NULL;
int LuaOpenFile(lua_State *L) {
const char *text = luaL_checkstring(L, 1);
View *view = ViewOpenFile(text);
Window *window = GetWindow(LastActiveWindow);
AddView(window, view->id);
window->active_view = view->id;
View *view = ViewOpenFile(window, text);
SetActiveWindow(window->id);
return 0; // number of results
}

View File

@@ -18,9 +18,7 @@ inline View *GetView(ViewID id) {
return &Views[0];
}
inline bool IsNull(Buffer *buffer) { return buffer->id.id == NullBufferID.id; }
void SetActiveWindow(WindowID window);
inline bool IsNull(Buffer *buffer) { return buffer->id.id == NullBufferID.id; }
inline bool IsActive(Window *window) { return window->id.id == ActiveWindow.id; }
inline bool IsActive(Window *window, View *view) { return window->active_view.id == view->id.id; }
inline Window *GetActiveWindow() { return GetWindow(ActiveWindow); }
@@ -145,40 +143,50 @@ Buffer *BufferOpenFile(String path) {
}
}
View *ViewOpenFile(String name) {
// if there is no view for this file, create one
View *view = FindViewWithBufferName(name);
if (!view) {
Buffer *buffer = BufferOpenFile(name);
if (IsNull(buffer)) Assert(0); // @todo
view = CreateView(buffer->id);
return view;
}
View *_WindowOpenViewAndBuffer(Window *window, String name, bool set_active = true) {
Buffer *buffer = BufferOpenFile(name);
if (IsNull(buffer)) Assert(0); // @todo
View *view = CreateView(buffer->id);
Add(&window->views, view->id);
if (set_active) window->active_view = view->id;
return view;
}
// If there is a buffer and view for this file then figure out
// if we need another view or can we reuse
Window *window = FindParentWindow(view->id);
if (!window) {
Buffer *buffer = BufferOpenFile(name);
if (IsNull(buffer)) Assert(0); // @todo
view = CreateView(buffer->id);
return view;
}
// Create new view, 2 view into same buffer at the same time
if (window->active_view.id == view->id.id) {
Buffer *buffer = BufferOpenFile(name);
if (IsNull(buffer)) Assert(0); // @todo
view = CreateView(buffer->id);
return view;
}
// Unlink and reuse
void UnlinkView(Window *window, ViewID view) {
For(window->views) {
if (it.id == view->id.id) {
if (it.id == view.id) {
UnorderedRemove(&window->views, it);
break;
}
}
Assert(window->active_view.id != view.id);
}
View *ViewOpenFile(Window *new_parent_window, String name) {
For(new_parent_window->views) {
View *it_view = GetView(it);
Buffer *it_buffer = GetBuffer(it_view->buffer_id);
if (it_buffer->name == name) {
new_parent_window->active_view = it;
return it_view;
}
}
View *view = FindViewWithBufferName(name);
if (!view) return _WindowOpenViewAndBuffer(new_parent_window, name);
Window *window = FindParentWindow(view->id);
if (!window) {
Add(&new_parent_window->views, view->id);
new_parent_window->active_view = view->id;
return view;
}
bool view_actively_used = window->active_view.id == view->id.id;
if (view_actively_used) return _WindowOpenViewAndBuffer(new_parent_window, name);
UnlinkView(window, view->id);
Add(&new_parent_window->views, view->id);
new_parent_window->active_view = view->id;
return view;
}

View File

@@ -18,8 +18,8 @@
#include "buffer_multi_cursor.cpp"
#include "buffer_history.cpp"
#include "buffer_test_load.cpp"
#include "management.cpp"
#include "management.cpp"
#include "commands.cpp"
#include "commands_clipboard.cpp"
#include "commands_window.cpp"