Skip to content

Instantly share code, notes, and snippets.

@luce80
Last active January 6, 2024 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luce80/43b5abad0a8fc149caccadef35f860f8 to your computer and use it in GitHub Desktop.
Save luce80/43b5abad0a8fc149caccadef35f860f8 to your computer and use it in GitHub Desktop.
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"
08-10-2022 "Removed useless to-percent"
19-11-2022 "Fix problem with win/data initialization"
12-12-2022 "Removed BUG woraround, moved scrolling to 'on-change"
]
]
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)
do-actor sc none 'change
]
sc: scroller 16x200 on-created [
face/extra/old-selected: face/selected
face/extra/old-data: face/data
face/selected: 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%
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/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
]
]
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
do-actor sc none 'change
]
]
]
@luce80
Copy link
Author

luce80 commented Jan 6, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment