mirror of
https://github.com/EnumeratedDev/Typer.git
synced 2025-07-01 07:48:20 +00:00
Simplify cursor and dropdown code
This commit is contained in:
parent
a2876c2086
commit
e14f2d9a74
@ -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) {
|
||||
|
@ -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
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user