Skip to content

Commit 05b1489

Browse files
committed
File browser: Current item is scrolled into view when navigating with keys. Fixed some small things.
1 parent b08bcea commit 05b1489

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/gui.gloa

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,8 @@ export onKeyPressed :: (state:State, key:LK.KeyConstant, scancode:LK.Scancode, i
601601
local found, i = indexWith(buttons.buttons, "selected", true)
602602

603603
i = found
604-
? ((i-1+dir) % #buttons.buttons + 1)
605-
: (dir < 0 ? #buttons.buttons : 1)
604+
? math.clamp(i+dir, 1, #buttons.buttons)--((i-1+dir) % #buttons.buttons + 1)
605+
: 1--(dir < 0 ? #buttons.buttons : 1)
606606

607607
local anyChange = false
608608

@@ -3825,15 +3825,54 @@ export scrollTo :: (state:State, scrollable:Scrollable, scroll:int, limit=false)
38253825
scrollTo(scrollable, scroll, limit)
38263826
}
38273827

3828+
export scrollIntoView :: (state:State, el:Element, subid:int) {
3829+
updateLayoutIfNeeded(state)
38283830

3831+
local scrollCurrent = getScrollOffset(el)
3832+
local targetY1 = el.layoutY + scrollCurrent
3833+
local targetY2 = targetY1 + el.layoutHeight
38293834

3830-
export scrollIntoView :: (state:State, el:Element, subid:int) {
3831-
-- @Incomplete
3835+
if el.type == Buttons {
3836+
local buttons = cast(Buttons) el
3837+
if buttons.vertical {
3838+
targetY2 = math.round(targetY1+(subid )*buttons.buttonHeight) -- Note: We change targetY1 on the next line!
3839+
targetY1 = math.round(targetY1+(subid-1)*buttons.buttonHeight)
3840+
}
3841+
}
3842+
3843+
while true {
3844+
el = el.parent
3845+
if el == NULL break
3846+
3847+
if el.type == Scrollable {
3848+
local scrollable = cast(Scrollable) el
3849+
local scrollOld = scrollable.scrollTarget
3850+
scrollCurrent = scrollCurrent - scrollOld
3851+
3852+
local scrollDelta = 0
3853+
local y1 = scrollable.layoutY + scrollCurrent + (SCROLLABLE_PADDING + SPACING) -- @UX: The top of the target element is partially behind the scrollable's shadow. Not great!
3854+
local y2 = scrollable.layoutY + scrollCurrent + scrollable.layoutHeight - (SCROLLABLE_PADDING + SPACING)
3855+
3856+
if targetY2 > y2 scrollDelta = y2 - targetY2
3857+
if targetY1 < y1 scrollDelta = y1 - targetY1
3858+
3859+
if scrollDelta {
3860+
scrollTo(scrollable, scrollable.scrollTarget+scrollDelta, limit=true)
3861+
scrollDelta = scrollable.scrollTarget - scrollOld
3862+
3863+
scrollCurrent = scrollCurrent + scrollDelta
3864+
targetY1 = targetY1 + scrollDelta
3865+
targetY2 = targetY2 + scrollDelta
3866+
}
3867+
}
3868+
}
38323869
}
38333870

38343871

38353872

38363873
local getVisibleAreaInLocalSpace :: (state:State, el0:Element) -> (x,y,w,h:int) {
3874+
updateLayoutIfNeeded(state)
3875+
38373876
-- @Incomplete: Check isElementVisible().
38383877
local el = el0
38393878
local x1 = el.layoutX
@@ -3852,7 +3891,7 @@ local getVisibleAreaInLocalSpace :: (state:State, el0:Element) -> (x,y,w,h:int)
38523891
y1 = math.max(y1, scrollable.layoutY)
38533892
x2 = math.min(x2, scrollable.layoutX+scrollable.layoutWidth)
38543893
y2 = math.min(y2, scrollable.layoutY+scrollable.layoutHeight)
3855-
offset += math.round(scrollable.scroll)
3894+
offset += scrollable.scrollTarget
38563895
}
38573896
}
38583897

src/guiSetup.gloa

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,9 +2613,12 @@ export setupGuiCallbacks :: () {
26132613
local item = cast(Item) button.value
26142614
if item.name ~= fileDialog_filename continue
26152615

2616+
button.selected = true
2617+
26162618
local x, y, w, h = guiState.getButtonLayout!(itemsButtons, itIndex)
26172619
local itemsScrollable = guiState.getElement!("fileDialog_itemsSrollable", gui.Scrollable)
26182620
guiState.scrollTo!(itemsScrollable, -math.round(y-itemsScrollable.layoutY-(itemsScrollable.layoutHeight-h)/2), limit=true)
2621+
26192622
break
26202623
}
26212624
}

src/main.gloa

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,13 +3249,15 @@ export showSaveProjectAsDialog :: () {
32493249
onHighlight = (pathObj:Path, fullPathObj:Path) {
32503250
loadPreview(pathObj.toString!())
32513251

3252+
--[[
32523253
local ok, filename = pathObj.getFilename!()
32533254
if ok {
32543255
local basename, ext = splitBasenameAndExtension(filename)
32553256
local input = guiState.getElement!("fileDialog_filename", gui.InputText)
32563257
guiState.setFocus!(input)
32573258
input.field.setSelection!(0, utf8.getLength(basename))
32583259
}
3260+
--]]
32593261
},
32603262
onChoose = (pathObj:Path, fullPathObj:Path) {
32613263
local _, filename = fullPathObj.getFilename!()

0 commit comments

Comments
 (0)