Skip to content

Instantly share code, notes, and snippets.

@greggirwin
Created July 28, 2023 00:46
Show Gist options
  • Save greggirwin/1405f2f8d93c4c066474d6cf5f47e6c6 to your computer and use it in GitHub Desktop.
Save greggirwin/1405f2f8d93c4c066474d6cf5f47e6c6 to your computer and use it in GitHub Desktop.
View with context
Red []
; A quick hack for capturing VID set-words into a context, and limiting their global damange.
view-ctx: function [
"View a layout, binding set-words in it to a returned context."
spec [block!] "VID layout spec; LAY will refer to the root of the face tree in the result."
][
words: collect-words/set/deep spec ; what about draw words though?
values: copy []
values: foreach word words [ ; get current vals so we can restore them
append/only values get/any word ; collect can't keep unset!
]
ctx-spec: append copy words none ; make it a valid spec by adding none.
append ctx-spec [lay: (layout spec)] ; capture the layout
ctx: context ctx-spec ; this is what we'll return, with VID word refs
foreach word words [set in ctx word get word] ; need to assign them to the context
repeat i length? words [ ; restore values
set/any words/:i :values/:i
]
view/no-wait ctx/lay
ctx
]
; DO NOT use important names, like FUNCTION, just as you woudln't in normal code.
; While the global values will be restored, the resulting object will be...unhappy.
spec: [comment: button "hello" btn-2: button "world" function: button "Whaaat are you doing?"]
obj: view-ctx spec
do-events
print [:comment newline :bt2 newline :function]
print help-string obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment