Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active October 13, 2019 22:11
Show Gist options
  • Save toomasv/66761977ce1347d238d1a621c7e994b2 to your computer and use it in GitHub Desktop.
Save toomasv/66761977ce1347d238d1a621c7e994b2 to your computer and use it in GitHub Desktop.
Dropdown autoselect mimick
Red [
Description: "Drop-down autoselect"
]
blk: [
["Blog" http://www.red-lang.org]
["About" https://www.red-lang.org/p/about.html]
["Getting Started" https://www.red-lang.org/p/getting-started.html]
["Download" https://www.red-lang.org/p/download.html]
["Documents" https://www.red-lang.org/p/documentation.html]
["Contributions" https://www.red-lang.org/p/contributions.html]
["Roadmap" https://www.red-lang.org/p/roadmap.html]
]
sel?: no
pick-selection: func [sel][
dd/text: copy pick tl/data sel
clear tl/data
tl/visible?: no
foreach b blk [if dd/text = b/1 [addr/text: form b/2]]
]
view [
dd: drop-down focus on-change [
collect/into [
foreach b blk [
case [
empty? face/text [clear tl/data tl/visible?: no]
find/match b/1 face/text [keep b/1]
]
]
] clear tl/data
tl/visible?: not empty? tl/data
] on-key [
switch event/key [
down [tl/selected: either tl/selected = -1 [1][min tl/selected + 1 length? tl/data]]
up [tl/selected: max -1 tl/selected - 1] ;either tl/selected <= 1 [-1][tl/selected - 1]]
]
] on-enter [
if all [tl/selected > 0 tl/visible?] [
pick-selection tl/selected
]
]
return addr: text 300 sky
button "Go" 30 [browse addr/data]
at 10x33 tl: text-list hidden data []
on-change [
either sel? [sel?: no][
dd/text: copy pick face/data face/selected
tl/visible?: no
]
]
on-select [sel?: yes pick-selection event/picked]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment