Skip to content

Commit b08bcea

Browse files
committed
File browser: PageUp/PageDown keys work in the file list.
1 parent 7d5662c commit b08bcea

File tree

1 file changed

+76
-5
lines changed

1 file changed

+76
-5
lines changed

src/gui.gloa

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
drawChecker, drawCheckerBox
3131
getElement, getElementAt, getWidgetAt, findParent
3232
getTopmostActiveModal
33+
getVisibleAreaInLocalSpace
3334
isElementVisible
3435
isInsideElement, isOverGui
3536
onKeyPressed, onKeyReleased, onTextInput
@@ -181,6 +182,7 @@ export State :: struct {
181182
getElementAt :: _getElementAt,
182183
getScrollOffset :: _getScrollOffset,
183184
getTopmostActiveModal :: _getTopmostActiveModal,
185+
getVisibleAreaInLocalSpace :: _getVisibleAreaInLocalSpace,
184186
getWidgetAt :: _getWidgetAt,
185187
hideContextMenu :: _hideContextMenu,
186188
isElementVisible :: _isElementVisible,
@@ -219,6 +221,7 @@ local _getElement :: getElement
219221
local _getElementAt :: getElementAt
220222
local _getScrollOffset :: getScrollOffset
221223
local _getTopmostActiveModal :: getTopmostActiveModal
224+
local _getVisibleAreaInLocalSpace :: getVisibleAreaInLocalSpace
222225
local _getWidgetAt :: getWidgetAt
223226
local _hideContextMenu :: hideContextMenu
224227
local _isElementVisible :: isElementVisible
@@ -601,26 +604,66 @@ export onKeyPressed :: (state:State, key:LK.KeyConstant, scancode:LK.Scancode, i
601604
? ((i-1+dir) % #buttons.buttons + 1)
602605
: (dir < 0 ? #buttons.buttons : 1)
603606

604-
for buttons.buttons it.selected = (itIndex == i)
607+
local anyChange = false
608+
609+
for buttons.buttons {
610+
local isSelected = (itIndex == i)
611+
612+
if it.selected ~= isSelected {
613+
it.selected = isSelected
614+
anyChange = true
615+
}
616+
}
605617
state.scrollIntoView!(buttons, i)
606618

619+
if not anyChange return true
620+
607621
triggerBeginEvent (state, buttons, buttons.buttons[i].name, i)
608622
triggerActionEvent(state, buttons, buttons.buttons[i].name, i, .KEYBOARD, alsoEnd=true)
609623

610624
} elseif key == "pageup" or key == "pagedown" {
611-
-- @Incomplete
625+
if not buttons.buttons return true
626+
627+
local dir = (key == "pagedown") ? 1 : -1
628+
local x, y, w, h = state.getVisibleAreaInLocalSpace!(buttons)
629+
local visibleCount = math.max(math.floor(h/buttons.buttonHeight), 1)
630+
local step = math.max(visibleCount-1, 1)
631+
local found, i = indexWith(buttons.buttons, "selected", true)
632+
633+
i = found
634+
? math.clamp(i+step*dir, 1, #buttons.buttons)
635+
: 1
636+
637+
local anyChange = false
638+
639+
for buttons.buttons {
640+
local isSelected = (itIndex == i)
641+
642+
if it.selected ~= isSelected {
643+
it.selected = isSelected
644+
anyChange = true
645+
}
646+
}
647+
state.scrollIntoView!(buttons, i)
648+
649+
if not anyChange return true
650+
651+
triggerBeginEvent (state, buttons, buttons.buttons[i].name, i)
652+
triggerActionEvent(state, buttons, buttons.buttons[i].name, i, .KEYBOARD, alsoEnd=true)
612653

613654
} elseif key == "end" or key == "home" {
655+
if not buttons.buttons return true
656+
614657
local iTarget = (key == "end") ? #buttons.buttons : 1
615658
local found, i = indexWith(buttons.buttons, "selected", true)
616659

617-
if found and i == iTarget return true
660+
if found and i == iTarget return true -- @Incomplete: Don't return if the target button is selected, but there are also others selected.
618661

619662
for buttons.buttons it.selected = (itIndex == iTarget)
620663
state.scrollIntoView!(buttons, iTarget)
621664

622-
triggerBeginEvent (state, buttons, buttons.buttons[i].name, i)
623-
triggerActionEvent(state, buttons, buttons.buttons[i].name, i, .KEYBOARD, alsoEnd=true)
665+
triggerBeginEvent (state, buttons, buttons.buttons[iTarget].name, iTarget)
666+
triggerActionEvent(state, buttons, buttons.buttons[iTarget].name, iTarget, .KEYBOARD, alsoEnd=true)
624667
}
625668
}
626669

@@ -3790,6 +3833,34 @@ export scrollIntoView :: (state:State, el:Element, subid:int) {
37903833

37913834

37923835

3836+
local getVisibleAreaInLocalSpace :: (state:State, el0:Element) -> (x,y,w,h:int) {
3837+
-- @Incomplete: Check isElementVisible().
3838+
local el = el0
3839+
local x1 = el.layoutX
3840+
local y1 = el.layoutY
3841+
local x2 = x1 + el.layoutWidth
3842+
local y2 = y1 + el.layoutHeight
3843+
local offset = 0
3844+
3845+
while true {
3846+
el = el.parent
3847+
if el == NULL break
3848+
3849+
if el.type == Scrollable {
3850+
local scrollable = cast(Scrollable) el
3851+
x1 = math.max(x1, scrollable.layoutX)
3852+
y1 = math.max(y1, scrollable.layoutY)
3853+
x2 = math.min(x2, scrollable.layoutX+scrollable.layoutWidth)
3854+
y2 = math.min(y2, scrollable.layoutY+scrollable.layoutHeight)
3855+
offset += math.round(scrollable.scroll)
3856+
}
3857+
}
3858+
3859+
return x1-el0.layoutX, y1-el0.layoutY+offset, x2-x1, y2-y1
3860+
}
3861+
3862+
3863+
37933864
export getButtonLayout :: (state:State, buttons:Buttons, i:int) -> (x,y,w,h:int) {
37943865
updateLayoutIfNeeded(state)
37953866

0 commit comments

Comments
 (0)