Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Resizing scroller example enhanced
Red [
title: "Resizing scroller example enhanced"
author: [@toomasv @hiiamboris @luce80]
date: 19-11-2022
History: [
13-09-2022 "Published %scroller-test-resize.red"
22-09-2022 "improved a bit with @toomasv idea of using face where applicable"
24-09-2022 "Enhanced with keep alignment while resizing, add on-wheel event"
02-10-2022 "Removed useless to-float"
07-10-2022 "Workaround to avoid changing scroller/selected"
19-11-2022 "Fix problem with win/data initialization"
]
]
win: layout [
size 390x220
across space 0x0
gp: panel 350x200 red [
origin 0x0 space 0x0
p: panel 350x400 [
origin 0x0 space 0x0
below
area "A" 350x100
area "B" 350x100
area "C" 350x100
area "D" 350x100
]
] on-wheel [
sc/data: sc/data + (sc/steps * negate event/picked)
sc/extra/scroll sc
]
sc: scroller 16x200 on-created [
face/extra/old-selected: face/selected
face/extra/old-data: face/data
face/selected: to-percent min (max 1 face/size/y) / (max 1 p/size/y) 1 ; visible-part / total ratio given as percentage
] extra object [
old-data: 0.0
old-selected: 100%
scroll: func [face][
face/selected: to-percent min (max 1 face/size/y) / (max 1 p/size/y) 1 ;@@ BUG workaround
face/data: min max 0.0 face/data (1.0 - face/selected) ; constrain to allowed range
p/offset/y: to integer! negate p/size/y * face/data ; scroll amount
p/offset/y: min max face/size/y - p/size/y p/offset/y 0 ; constrain to allowed range
]
align: func [face][
face/data: either any [old-data = 0.0 face/selected = 100% old-selected = 100%][
0.0
][
old-data * (1.0 - face/selected) / (1.0 - old-selected)
]
]
]
[ ; action (on-change)
face/extra/scroll face
]
]
view/flags/options win 'resize [
actors: object [
on-create: func [face][face/data: face/size] ; init value
on-focus: func [face event][face/data: face/size] ; store old size
on-resize: func [face event][face/actors/on-resizing face event] ; forward
on-resizing: func [face event /local siz][
siz: face/size - face/data ; compute size difference
face/data: face/size ; store new size
gp/size: gp/size + (siz * 0x1) ; resize (only along y)
sc/size/y: gp/size/y ; copy height
; re-calc scroller parameters and scroll amount
do-actor sc none 'created
sc/extra/align sc
sc/extra/scroll sc
]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment