From e14f2d9a74a46ed6ae9efb5a464be90a817fc8ae Mon Sep 17 00:00:00 2001 From: EnumDev Date: Thu, 5 Jun 2025 17:17:23 +0300 Subject: [PATCH] Simplify cursor and dropdown code --- src/dropdown.go | 13 ++++--------- src/top_menu.go | 20 ++++++++++---------- src/window.go | 47 ++++++++++++++++++++++++++++------------------- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/dropdown.go b/src/dropdown.go index 47d408d..00c4190 100644 --- a/src/dropdown.go +++ b/src/dropdown.go @@ -5,7 +5,6 @@ import ( ) type Dropdown struct { - Active bool Selected int Options []string PosX, PosY int @@ -14,6 +13,7 @@ type Dropdown struct { } var dropdowns = make([]*Dropdown, 0) +var ActiveDropdown *Dropdown func CreateDropdownMenu(options []string, posX, posY, dropdownWidth int, action func(int)) *Dropdown { if len(options) == 0 { @@ -31,7 +31,6 @@ func CreateDropdownMenu(options []string, posX, posY, dropdownWidth int, action } d := &Dropdown{ - Active: false, Selected: 0, Options: options, PosX: posX, @@ -45,13 +44,9 @@ func CreateDropdownMenu(options []string, posX, posY, dropdownWidth int, action return d } -func GetActiveDropdown() *Dropdown { - for _, dropdown := range dropdowns { - if dropdown.Active { - return dropdown - } - } - return nil +func ClearDropdowns() { + dropdowns = make([]*Dropdown, 0) + ActiveDropdown = nil } func drawDropdowns(window *Window) { diff --git a/src/top_menu.go b/src/top_menu.go index 13e4bcd..8dc8b64 100644 --- a/src/top_menu.go +++ b/src/top_menu.go @@ -23,7 +23,7 @@ func initTopMenu() { Name: "File", Key: 'f', Action: func(window *Window) { - dropdowns = make([]*Dropdown, 0) + ClearDropdowns() d := CreateDropdownMenu([]string{"New", "Save", "Open", "Close", "Quit"}, 0, 1, 0, func(i int) { switch i { case 0: @@ -50,11 +50,11 @@ func initTopMenu() { case 4: window.Close() } - dropdowns = make([]*Dropdown, 0) - window.textArea.Typing = true + ClearDropdowns() + window.CursorMode = CursorModeBuffer }) - d.Active = true - window.textArea.Typing = false + ActiveDropdown = d + window.CursorMode = CursorModeDropdown }, } EditButton := TopMenuButton{ @@ -65,7 +65,7 @@ func initTopMenu() { Name: "Buffers", Key: 'b', Action: func(window *Window) { - dropdowns = make([]*Dropdown, 0) + ClearDropdowns() buffersSlice := make([]string, 0) for _, buffer := range Buffers { if window.textArea.CurrentBuffer == buffer { @@ -89,11 +89,11 @@ func initTopMenu() { window.textArea.CurrentBuffer = Buffers[id] window.SetCursorPos(0) - dropdowns = make([]*Dropdown, 0) - window.textArea.Typing = true + ClearDropdowns() + window.CursorMode = CursorModeBuffer }) - d.Active = true - window.textArea.Typing = false + ActiveDropdown = d + window.CursorMode = CursorModeDropdown }, } diff --git a/src/window.go b/src/window.go index 1d33c34..e030f35 100644 --- a/src/window.go +++ b/src/window.go @@ -7,9 +7,19 @@ import ( "slices" ) +type CursorMode uint8 + +const ( + CursorModeDisabled CursorMode = iota + CursorModeBuffer + CursorModeDropdown + CursorModeMessageBar +) + type Window struct { ShowTopMenu bool ShowLineIndex bool + CursorMode CursorMode textArea TextArea @@ -18,7 +28,6 @@ type Window struct { type TextArea struct { CursorPos int - Typing bool CurrentBuffer *Buffer } @@ -26,10 +35,10 @@ func CreateWindow() (*Window, error) { window := Window{ ShowTopMenu: true, ShowLineIndex: true, + CursorMode: CursorModeBuffer, textArea: TextArea{ CursorPos: 0, - Typing: true, CurrentBuffer: nil, }, @@ -117,7 +126,7 @@ func (window *Window) Draw() { drawDropdowns(window) // Draw cursor - if window.textArea.Typing { + if window.CursorMode == CursorModeBuffer { window.screen.ShowCursor(window.GetAbsoluteCursorPos()) } else { window.screen.HideCursor() @@ -140,38 +149,38 @@ func (window *Window) Draw() { func (window *Window) input(ev *tcell.EventKey) { if ev.Key() == tcell.KeyRight { // Navigation Keys - if window.textArea.Typing { + if window.CursorMode == CursorModeBuffer { window.SetCursorPos(window.textArea.CursorPos + 1) } } else if ev.Key() == tcell.KeyLeft { - if window.textArea.Typing { + if window.CursorMode == CursorModeBuffer { window.SetCursorPos(window.textArea.CursorPos - 1) } } else if ev.Key() == tcell.KeyUp { - if window.textArea.Typing { + if window.CursorMode == CursorModeBuffer { x, y := window.GetCursorPos2D() window.SetCursorPos2D(x, y-1) - } else if GetActiveDropdown() != nil { - dropdown := GetActiveDropdown() + } else if ActiveDropdown != nil { + dropdown := ActiveDropdown dropdown.Selected-- if dropdown.Selected < 0 { dropdown.Selected = 0 } } } else if ev.Key() == tcell.KeyDown { - if window.textArea.Typing { + if window.CursorMode == CursorModeBuffer { x, y := window.GetCursorPos2D() window.SetCursorPos2D(x, y+1) - } else if GetActiveDropdown() != nil { - dropdown := GetActiveDropdown() + } else if ActiveDropdown != nil { + dropdown := ActiveDropdown dropdown.Selected++ if dropdown.Selected >= len(dropdown.Options) { dropdown.Selected = len(dropdown.Options) - 1 } } } else if ev.Key() == tcell.KeyEscape { - dropdowns = make([]*Dropdown, 0) - window.textArea.Typing = true + ClearDropdowns() + window.CursorMode = CursorModeBuffer } else if ev.Key() == tcell.KeyCtrlC { // Close buffer key delete(Buffers, window.textArea.CurrentBuffer.Id) buffersSlice := slices.Collect(maps.Values(Buffers)) @@ -181,8 +190,8 @@ func (window *Window) input(ev *tcell.EventKey) { } window.textArea.CurrentBuffer = buffersSlice[0] window.SetCursorPos(0) - dropdowns = make([]*Dropdown, 0) - window.textArea.Typing = true + ClearDropdowns() + window.CursorMode = CursorModeBuffer } else if ev.Key() == tcell.KeyCtrlQ { // Exit key window.Close() } else if ev.Modifiers()&tcell.ModAlt != 0 { // Menu Bar @@ -202,7 +211,7 @@ func (window *Window) input(ev *tcell.EventKey) { window.textArea.CurrentBuffer.Contents = str } } else if ev.Key() == tcell.KeyTab { - if GetActiveDropdown() != nil { + if ActiveDropdown != nil { return } @@ -217,8 +226,8 @@ func (window *Window) input(ev *tcell.EventKey) { window.textArea.CursorPos++ window.textArea.CurrentBuffer.Contents = str } else if ev.Key() == tcell.KeyEnter { - if GetActiveDropdown() != nil { - d := GetActiveDropdown() + if ActiveDropdown != nil { + d := ActiveDropdown d.Action(d.Selected) } else { str := window.textArea.CurrentBuffer.Contents @@ -233,7 +242,7 @@ func (window *Window) input(ev *tcell.EventKey) { window.textArea.CurrentBuffer.Contents = str } } else if ev.Key() == tcell.KeyRune { - if GetActiveDropdown() != nil { + if ActiveDropdown != nil { return }