Tried to introduce NextActiveWindow but then we lose events like mouse press

This commit is contained in:
Krzosa Karol
2024-07-26 07:52:53 +02:00
parent 7eefa2e6c2
commit 4aa738fcc9
4 changed files with 62 additions and 59 deletions

View File

@@ -3,27 +3,6 @@ https://code.visualstudio.com/blogs/2018/03/23/text-buffer-reimplementation
*/ */
#define BUFFER_DEBUG 0 #define BUFFER_DEBUG 0
void InitBuffer(Allocator allocator, Buffer *buffer, String name, Int size = 4096) {
buffer->id = AllocBufferID();
buffer->name = name;
buffer->cap = size;
buffer->data = AllocArray(allocator, U16, buffer->cap);
buffer->line_starts.allocator = allocator;
Add(&buffer->line_starts, (Int)0);
}
Buffer *CreateBuffer(Allocator allocator, String name, Int size = 4096) {
Buffer *result = Alloc(&Buffers);
InitBuffer(allocator, result, name, size);
return result;
}
Buffer *CreateTempBuffer(Allocator allocator, Int size = 4096) {
Buffer *result = AllocType(allocator, Buffer);
InitBuffer(allocator, result, "*temp*", size);
return result;
}
void Grow(Buffer *buffer, Int change_size) { void Grow(Buffer *buffer, Int change_size) {
Int new_size = buffer->len + change_size; Int new_size = buffer->len + change_size;
if (new_size > buffer->cap) { if (new_size > buffer->cap) {

View File

@@ -436,9 +436,9 @@ void HandleActiveWindowBindings(Window *window, bool *update_scroll) {
} }
if (AltPress(KEY_ENTER)) { if (AltPress(KEY_ENTER)) {
Command_SelectAll(seek_view, needle); Command_SelectAll(seek_view, needle);
SetActiveWindow(seek_window->id);
} else if (Press(KEY_ENTER)) { } else if (Press(KEY_ENTER)) {
Caret caret = FindInBuffer(seek_buffer, needle, seek_view->carets[0], true); Caret caret = FindInBuffer(seek_buffer, needle, seek_view->carets[0], true);
SetActiveWindow(seek_window->id);
if (Ctrl()) { if (Ctrl()) {
Insert(&seek_view->carets, caret, 0); Insert(&seek_view->carets, caret, 0);
MergeCarets(seek_view); MergeCarets(seek_view);
@@ -512,8 +512,6 @@ void HandleActiveWindowBindings(Window *window, bool *update_scroll) {
Insert(&view.carets, MakeCaret(p, p), 0); Insert(&view.carets, MakeCaret(p, p), 0);
view.selection_anchor = view.carets[0].range; view.selection_anchor = view.carets[0].range;
} }
MergeCarets(&view, &view.selection_anchor);
} else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { } else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
window->mouse_selecting = true; window->mouse_selecting = true;
} }
@@ -521,6 +519,7 @@ void HandleActiveWindowBindings(Window *window, bool *update_scroll) {
if (window->mouse_selecting) { if (window->mouse_selecting) {
if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) window->mouse_selecting = false; if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT)) window->mouse_selecting = false;
MergeCarets(&view, &view.selection_anchor);
Caret &caret = view.carets[0]; Caret &caret = view.carets[0];
if (view.selection_anchor.min > p) { if (view.selection_anchor.min > p) {
caret = MakeCaret(p, view.selection_anchor.max); caret = MakeCaret(p, view.selection_anchor.max);
@@ -558,6 +557,7 @@ void HandleActiveWindowBindings(Window *window, bool *update_scroll) {
} }
} }
} }
MergeCarets(&view);
} }
void HandleWindowBindings(Window *window) { void HandleWindowBindings(Window *window) {

View File

@@ -1,3 +1,28 @@
WindowID WindowIDs = {0};
BufferID BufferIDs = {0};
ViewID ViewIDs = {0};
Array<Buffer> Buffers = {};
Array<View> Views = {};
Array<Window> Windows = {};
WindowID NullWindowID = {0};
BufferID NullBufferID = {0};
ViewID NullViewID = {0};
WindowID CommandWindowID = {0};
WindowID InfoBarWindowID = {0};
WindowID SearchWindowID = {0};
Array<WindowID> WindowSwitchHistory; // @todo: probably better as a circular buffer
WindowID ActiveWindow; // @todo: maybe NextActiveWindow and change at end of frame
Int CaretChangeID;
Int LastFrameIDWhenScrolled = -1;
Int LastFrameIDWhenSwitchedActiveWindow;
inline ViewID AllocViewID() { return {ViewIDs.id++}; }
inline WindowID AllocWindowID() { return {WindowIDs.id++}; }
inline BufferID AllocBufferID() { return {BufferIDs.id++}; }
inline Window *GetWindow(WindowID id) { inline Window *GetWindow(WindowID id) {
For(Windows) if (it.id.id == id.id) return &it; For(Windows) if (it.id.id == id.id) return &it;
return &Windows[0]; return &Windows[0];
@@ -27,6 +52,27 @@ inline View *GetActiveView(Window *window) {
else return GetView(window->active_view); else return GetView(window->active_view);
} }
void InitBuffer(Allocator allocator, Buffer *buffer, String name, Int size = 4096) {
buffer->id = AllocBufferID();
buffer->name = name;
buffer->cap = size;
buffer->data = AllocArray(allocator, U16, buffer->cap);
buffer->line_starts.allocator = allocator;
Add(&buffer->line_starts, (Int)0);
}
Buffer *CreateBuffer(Allocator allocator, String name, Int size = 4096) {
Buffer *result = Alloc(&Buffers);
InitBuffer(allocator, result, name, size);
return result;
}
Buffer *CreateTempBuffer(Allocator allocator, Int size = 4096) {
Buffer *result = AllocType(allocator, Buffer);
InitBuffer(allocator, result, "*temp*", size);
return result;
}
Window *CreateWindow() { Window *CreateWindow() {
Window *w = Alloc(&Windows); Window *w = Alloc(&Windows);
w->visible = true; w->visible = true;
@@ -59,11 +105,12 @@ WindowID GetLastActiveWindow() {
void SetActiveWindow(WindowID window) { void SetActiveWindow(WindowID window) {
if (window.id != ActiveWindow.id && LastFrameIDWhenSwitchedActiveWindow != FrameID) { if (window.id != ActiveWindow.id && LastFrameIDWhenSwitchedActiveWindow != FrameID) {
ActiveWindow = window;
LastFrameIDWhenSwitchedActiveWindow = FrameID; LastFrameIDWhenSwitchedActiveWindow = FrameID;
ActiveWindow = window;
Window *w = GetWindow(window); Window *w = GetWindow(window);
if (!w->dont_save_in_active_window_history) Add(&WindowSwitchHistory, window); if (!w->dont_save_in_active_window_history) {
Add(&WindowSwitchHistory, window);
}
} }
} }

View File

@@ -86,21 +86,6 @@ struct Scroller {
Int line_count; Int line_count;
}; };
WindowID WindowIDs = {0};
BufferID BufferIDs = {0};
ViewID ViewIDs = {0};
WindowID NullWindowID = {0};
BufferID NullBufferID = {0};
ViewID NullViewID = {0};
WindowID CommandWindowID = {0};
WindowID InfoBarWindowID = {0};
WindowID SearchWindowID = {0};
Array<Buffer> Buffers = {};
Array<View> Views = {};
Array<Window> Windows = {};
float MenuFontSize; float MenuFontSize;
Font MenuFont; Font MenuFont;
@@ -111,13 +96,8 @@ Int FontLineSpacing;
Int FontCharSpacing; Int FontCharSpacing;
Int FrameID; Int FrameID;
Int LastFrameIDWhenScrolled = -1;
Int CaretChangeID;
String WorkingDir; String WorkingDir;
Int LastFrameIDWhenSwitchedActiveWindow;
Array<WindowID> WindowSwitchHistory; // @todo: probably better as a circular buffer
WindowID ActiveWindow; // @todo: maybe NextActiveWindow and change at end of frame
String16 EvalString(Allocator allocator, String16 string16); String16 EvalString(Allocator allocator, String16 string16);
Rect2I GetVisibleCells(Window &window); Rect2I GetVisibleCells(Window &window);
@@ -125,7 +105,4 @@ void AfterEdit(View *view, Array<Edit> edits);
Scroller ComputeScrollerRect(Window &window); Scroller ComputeScrollerRect(Window &window);
void Command_EvalLua(View *view, String16 string); void Command_EvalLua(View *view, String16 string);
void MergeCarets(View *view, Range *mouse_selection_anchor = NULL); void MergeCarets(View *view, Range *mouse_selection_anchor = NULL);
inline BufferID AllocBufferID();
inline ViewID AllocViewID() { return {ViewIDs.id++}; }
inline WindowID AllocWindowID() { return {WindowIDs.id++}; }
inline BufferID AllocBufferID() { return {BufferIDs.id++}; }