Skip to content

Commit

Permalink
#248 Adding tab for next input field
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentFarris committed Jun 12, 2024
1 parent f328f1e commit 530b019
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/markup/document/html_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ type Document struct {
tagElements map[string][]*Element
style rules.StyleSheet
stylizer func(rules.StyleSheet, *Document, *engine.Host)
// TODO: Should this be here?
firstInput *ui.Input
lastInput *ui.Input
}

func (d *Document) SetupStylizer(style rules.StyleSheet, host *engine.Host,
Expand Down Expand Up @@ -205,6 +208,14 @@ func (d *Document) createUIElement(host *engine.Host, e *Element, parent *ui.Pan
input := panel.ConvertToInput(e.Attribute("placeholder"))
input.SetText(e.Attribute("value"))
uiElm = input
if d.firstInput == nil {
d.firstInput = input
}
if d.lastInput != nil {
d.lastInput.SetNextFocusedInput(input)
}
d.lastInput = input
input.SetNextFocusedInput(d.firstInput)
}
}
entry := appendElement(uiElm, panel)
Expand Down
15 changes: 13 additions & 2 deletions src/ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type localInputData struct {
selectStart, selectEnd, dragStart int
inputType InputType
isActive bool
nextFocusInput *Input
}

type Input Panel
Expand All @@ -89,6 +90,10 @@ func (input *Input) Data() *localInputData {
return input.localData.(*localInputData)
}

func (input *Input) SetNextFocusedInput(next *Input) {
input.Data().nextFocusInput = next
}

func (p *Panel) ConvertToInput(placeholderText string) *Input {
input := (*Input)(p)
host := p.Host()
Expand Down Expand Up @@ -582,10 +587,16 @@ func (input *Input) keyPressed(keyId int, keyState hid.KeyState) {
fallthrough
case hid.KeyboardKeyEnter:
input.submit()
case hid.KeyboardKeyTab:
// Delay a frame so we don't hit a loop of going to next
input.host.RunAfterFrames(1, func() {
next := input.Data().nextFocusInput
if next != nil {
next.Select()
}
})
}
}
}
if keyState == hid.KeyStateDown {
input.requestEvent(EventTypeKeyDown)
} else if keyState == hid.KeyStateUp {
input.requestEvent(EventTypeKeyUp)
Expand Down

0 comments on commit 530b019

Please sign in to comment.