factor/basis/ui/ui.factor

261 lines
7.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2006, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs io kernel math models namespaces make dlists
deques sequences threads words continuations init
combinators combinators.short-circuit hashtables concurrency.flags
sets accessors calendar fry destructors ui.gadgets ui.gadgets.private
ui.gadgets.worlds ui.gadgets.tracks ui.gestures ui.backend ui.render
strings classes.tuple classes.tuple.parser lexer vocabs.parser parser ;
2007-09-20 18:09:08 -04:00
IN: ui
2009-02-19 17:54:27 -05:00
<PRIVATE
2007-09-20 18:09:08 -04:00
! Assoc mapping aliens to gadgets
SYMBOL: windows
: window ( handle -- world ) windows get-global at ;
2007-09-20 18:09:08 -04:00
: window-focus ( handle -- gadget ) window world-focus ;
: register-window ( world handle -- )
#! Add the new window just below the topmost window. Why?
#! So that if the new window doesn't actually receive focus
#! (eg, we're using focus follows mouse and the mouse is not
#! in the new window when it appears) Factor doesn't get
#! confused and send workspace operations to the new window,
#! etc.
swap 2array windows get-global push
windows get-global dup length 1 >
[ [ length 1 - dup 1 - ] keep exchange ] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: unregister-window ( handle -- )
windows [ [ first = not ] with filter ] change-global ;
2007-09-20 18:09:08 -04:00
: raised-window ( world -- )
2008-05-06 10:16:48 -04:00
windows get-global
[ [ second eq? ] with find drop ] keep
2009-10-28 00:41:57 -04:00
[ nth ] [ remove-nth! drop ] [ nip ] 2tri push ;
2007-09-20 18:09:08 -04:00
2007-11-22 01:40:17 -05:00
: focus-gestures ( new old -- )
drop-prefix <reversed>
lose-focus swap each-gesture
gain-focus swap each-gesture ;
2007-11-22 01:40:17 -05:00
2009-05-08 16:07:15 -04:00
: ?grab-input ( world -- )
dup grab-input?>> [ handle>> (grab-input) ] [ drop ] if ;
: ?ungrab-input ( world -- )
dup grab-input?>> [ handle>> (ungrab-input) ] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: focus-world ( world -- )
t >>focused?
2009-05-08 16:07:15 -04:00
[ ?grab-input ] [
dup raised-window
focus-path f focus-gestures
] bi ;
2007-09-20 18:09:08 -04:00
: unfocus-world ( world -- )
f >>focused?
2009-05-08 16:07:15 -04:00
[ ?ungrab-input ]
[ focus-path f swap focus-gestures ] bi ;
2007-09-20 18:09:08 -04:00
: set-up-window ( world -- )
{
2009-07-01 18:57:21 -04:00
[ set-gl-context ]
[ [ title>> ] keep set-title ]
[ begin-world ]
[ resize-world ]
[ t >>active? drop ]
[ request-focus ]
} cleave ;
: clean-up-broken-window ( world -- )
[
dup { [ focused?>> ] [ grab-input?>> ] } 1&&
[ handle>> (ungrab-input) ] [ drop ] if
] [ handle>> (close-window) ] bi ;
M: world graft*
[ (open-window) ]
[
[ set-up-window ]
[ [ clean-up-broken-window ] [ ui-error ] bi* ] recover
] bi ;
2007-11-22 01:40:17 -05:00
2007-09-20 18:09:08 -04:00
: reset-world ( world -- )
2007-11-22 01:40:17 -05:00
#! This is used when a window is being closed, but also
#! when restoring saved worlds on image startup.
f >>handle unfocus-world ;
: (ungraft-world) ( world -- )
{
2009-07-01 18:57:21 -04:00
[ set-gl-context ]
[ text-handle>> [ dispose ] when* ]
[ images>> [ dispose ] when* ]
[ hand-clicked close-global ]
[ hand-gadget close-global ]
[ end-world ]
[ [ <reversed> [ [ dispose ] when* ] each V{ } clone ] change-window-resources drop ]
} cleave ;
2007-09-20 18:09:08 -04:00
2007-11-22 01:40:17 -05:00
M: world ungraft*
[ (ungraft-world) ]
[ handle>> (close-window) ]
[ reset-world ] tri ;
2007-09-20 18:09:08 -04:00
2007-11-22 01:40:17 -05:00
: init-ui ( -- )
<dlist> \ graft-queue set-global
<dlist> \ layout-queue set-global
<dlist> \ gesture-queue set-global
2007-11-22 01:40:17 -05:00
V{ } clone windows set-global ;
: restore-gadget-later ( gadget -- )
2008-08-29 19:44:19 -04:00
dup graft-state>> {
2007-11-22 01:40:17 -05:00
{ { f f } [ ] }
{ { f t } [ ] }
{ { t t } [ { f f } >>graft-state ] }
{ { t f } [ dup unqueue-graft { f f } >>graft-state ] }
2007-11-22 01:40:17 -05:00
} case graft-later ;
: restore-gadget ( gadget -- )
dup restore-gadget-later
2008-08-29 19:44:19 -04:00
children>> [ restore-gadget ] each ;
2007-11-22 01:40:17 -05:00
: restore-world ( world -- )
{
[ reset-world ]
[ f >>text-handle f >>images drop ]
[ restore-gadget ]
} cleave ;
2007-11-22 01:40:17 -05:00
2007-09-20 18:09:08 -04:00
: update-hand ( world -- )
dup hand-world get-global eq?
[ hand-loc get-global swap move-hand ] [ drop ] if ;
: layout-queued ( -- seq )
[
2007-11-16 01:19:13 -05:00
in-layout? on
layout-queue [
dup layout find-world [ , ] when*
2008-08-19 15:06:20 -04:00
] slurp-deque
] { } make members ;
2007-09-20 18:09:08 -04:00
: redraw-worlds ( seq -- )
2007-11-23 17:23:53 -05:00
[ dup update-hand draw-world ] each ;
2007-09-20 18:09:08 -04:00
: send-queued-gestures ( -- )
gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
2008-02-25 17:48:11 -05:00
: update-ui ( -- )
notify-queued
layout-queued
redraw-worlds
send-queued-gestures ;
2007-09-20 18:09:08 -04:00
2008-02-25 20:37:43 -05:00
SYMBOL: ui-thread
2008-02-25 17:48:11 -05:00
: ui-running ( quot -- )
t \ ui-running set-global
[ f \ ui-running set-global ] [ ] cleanup ; inline
2009-02-19 17:54:27 -05:00
PRIVATE>
: find-window ( quot -- world )
[ windows get values ] dip
'[ dup children>> [ ] [ nip first ] if-empty @ ]
find-last nip ; inline
2009-02-19 17:54:27 -05:00
2008-02-25 20:37:43 -05:00
: ui-running? ( -- ? )
\ ui-running get-global ;
2009-02-19 17:54:27 -05:00
<PRIVATE
2008-02-25 17:48:11 -05:00
: update-ui-loop ( -- )
#! Note the logic: if update-ui fails, we open an error window
#! and run one iteration of update-ui. If that also fails, well,
#! the whole UI subsystem is broken so we exit out of the
#! update-ui-loop.
[ { [ ui-running? ] [ ui-thread get-global self eq? ] } 0&& ]
[
ui-notify-flag get lower-flag
[ update-ui ] [ ui-error update-ui ] recover
] while ;
2008-02-25 17:48:11 -05:00
: start-ui-thread ( -- )
2008-02-25 20:37:43 -05:00
[ self ui-thread set-global update-ui-loop ]
"UI update" spawn drop ;
2008-02-25 17:48:11 -05:00
2009-02-19 17:54:27 -05:00
: start-ui ( quot -- )
2009-03-16 04:01:47 -04:00
call( -- ) notify-ui-thread start-ui-thread ;
2009-02-19 17:54:27 -05:00
: restore-windows ( -- )
[
windows get [ values ] [ delete-all ] bi
[ restore-world ] each
forget-rollover
] (with-ui) ;
: restore-windows? ( -- ? )
windows get empty? not ;
: ?attributes ( gadget title/attributes -- attributes )
2009-06-18 23:01:31 -04:00
dup string? [ world-attributes new swap >>title ] [ clone ] if
swap [ [ [ 1array ] [ f ] if* ] curry unless* ] curry change-gadgets ;
2009-02-19 17:54:27 -05:00
PRIVATE>
: open-world-window ( world -- )
dup pref-dim >>dim dup relayout graft ;
: open-window* ( gadget title/attributes -- window )
?attributes <world> [ open-world-window ] keep ;
: open-window ( gadget title/attributes -- )
open-window* drop ;
: set-fullscreen ( gadget ? -- )
[ find-world ] dip (set-fullscreen) ;
: fullscreen? ( gadget -- ? )
find-world (fullscreen?) ;
: toggle-fullscreen ( gadget -- )
dup fullscreen? not set-fullscreen ;
2008-02-21 00:13:31 -05:00
: raise-window ( gadget -- )
find-world raise-window* ;
: topmost-window ( -- world )
windows get last second ;
HOOK: close-window ui-backend ( gadget -- )
M: object close-window
find-world [ ungraft ] when* ;
2008-02-25 20:37:43 -05:00
[
f \ ui-running set-global
<flag> ui-notify-flag set-global
] "ui" add-startup-hook
2007-09-20 18:09:08 -04:00
: with-ui ( quot -- )
2009-03-16 04:01:47 -04:00
ui-running? [ call( -- ) ] [ '[ init-ui @ ] (with-ui) ] if ;
HOOK: beep ui-backend ( -- )
HOOK: system-alert ui-backend ( caption text -- )
: parse-main-window-attributes ( class -- attributes )
"{" expect dup all-slots parse-tuple-literal-slots ;
: define-main-window ( word attributes quot -- )
[
2010-01-15 20:06:50 -05:00
'[ [ f _ clone @ open-window ] with-ui ] (( -- )) define-declared
] [ 2drop current-vocab (>>main) ] 3bi ;
SYNTAX: MAIN-WINDOW:
CREATE
world-attributes parse-main-window-attributes
parse-definition
define-main-window ;