factor/basis/ui/ui.factor

224 lines
5.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2006, 2008 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
2008-08-19 15:06:20 -04:00
prettyprint dlists deques sequences threads sequences words
debugger ui.gadgets ui.gadgets.worlds ui.gadgets.tracks
2008-02-18 06:07:40 -05:00
ui.gestures ui.backend ui.render continuations init combinators
hashtables concurrency.flags sets accessors calendar ;
2007-09-20 18:09:08 -04:00
IN: ui
! Assoc mapping aliens to gadgets
SYMBOL: windows
2008-05-08 17:58:13 -04:00
SYMBOL: stop-after-last-window?
: event-loop? ( -- ? )
{
{ [ stop-after-last-window? get not ] [ t ] }
2008-08-19 15:06:20 -04:00
{ [ graft-queue deque-empty? not ] [ t ] }
2008-05-08 17:58:13 -04:00
{ [ windows get-global empty? not ] [ t ] }
[ f ]
} cond ;
: event-loop ( -- ) [ event-loop? ] [ do-events ] [ ] while ;
2007-09-20 18:09:08 -04:00
: window ( handle -- world ) windows get-global at ;
: 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 ;
: unregister-window ( handle -- )
windows global [ [ first = not ] with filter ] change-at ;
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
[ nth ] [ delete-nth ] [ 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>
T{ lose-focus } swap each-gesture
T{ gain-focus } swap each-gesture ;
2007-09-20 18:09:08 -04:00
: focus-world ( world -- )
t >>focused?
2007-09-20 18:09:08 -04:00
dup raised-window
focus-path f focus-gestures ;
: unfocus-world ( world -- )
f >>focused?
2007-09-20 18:09:08 -04:00
focus-path f swap focus-gestures ;
2007-11-22 01:40:17 -05:00
M: world graft*
dup (open-window)
2008-08-31 02:42:30 -04:00
dup title>> over set-title
2007-11-22 01:40:17 -05:00
request-focus ;
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.
2008-08-31 02:42:30 -04:00
dup fonts>> clear-assoc
2007-09-20 18:09:08 -04:00
dup unfocus-world
2008-09-27 17:45:20 -04:00
f >>handle drop ;
2007-09-20 18:09:08 -04:00
2007-11-22 01:40:17 -05:00
M: world ungraft*
dup free-fonts
2007-09-20 18:09:08 -04:00
dup hand-clicked close-global
dup hand-gadget close-global
2008-08-31 02:42:30 -04:00
dup handle>> (close-window)
2007-09-20 18:09:08 -04:00
reset-world ;
: find-window ( quot -- world )
2007-11-16 01:19:13 -05:00
windows get values
2008-01-09 17:36:30 -05:00
[ gadget-child swap call ] with find-last nip ; inline
2007-09-20 18:09:08 -04:00
2007-11-22 01:40:17 -05:00
SYMBOL: ui-hook
: 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 -- )
dup reset-world restore-gadget ;
2007-09-20 18:09:08 -04:00
: restore-windows ( -- )
2007-11-16 01:19:13 -05:00
windows get [ values ] keep delete-all
2007-11-22 01:40:17 -05:00
[ restore-world ] each
2007-09-20 18:09:08 -04:00
forget-rollover ;
: restore-windows? ( -- ? )
2007-11-16 01:19:13 -05:00
windows get empty? not ;
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
2007-11-24 23:57:37 -05:00
] { } make prune ;
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
2007-11-16 01:19:13 -05:00
: notify ( gadget -- )
2008-08-29 19:44:19 -04:00
dup graft-state>>
2008-11-20 19:11:25 -05:00
[ first { f f } { t t } ? >>graft-state ] keep
{
{ { f t } [ dup activate-control graft* ] }
2008-01-29 03:04:14 -05:00
{ { t f } [ dup deactivate-control ungraft* ] }
} case ;
2007-11-16 01:19:13 -05:00
: notify-queued ( -- )
2008-08-19 15:06:20 -04:00
graft-queue [ notify ] slurp-deque ;
2007-11-16 01:19:13 -05:00
: send-queued-gestures ( -- )
gesture-queue [ send-queued-gesture ] slurp-deque ;
2008-02-25 17:48:11 -05:00
: update-ui ( -- )
[
[
notify-queued
layout-queued
redraw-worlds
send-queued-gestures
] assert-depth
] [ ui-error ] recover ;
2007-09-20 18:09:08 -04:00
: ui-wait ( -- )
10 milliseconds sleep ;
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
2008-02-25 20:37:43 -05:00
: ui-running? ( -- ? )
\ ui-running get-global ;
2008-02-25 17:48:11 -05:00
: update-ui-loop ( -- )
[ ui-running? ui-thread get-global self eq? and ]
[ ui-notify-flag get lower-flag update-ui ]
[ ] 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
: open-world-window ( world -- )
dup pref-dim >>dim dup relayout graft ;
: open-window ( gadget title -- )
f <world> open-world-window ;
: set-fullscreen? ( ? gadget -- )
find-world set-fullscreen* ;
: fullscreen? ( gadget -- ? )
find-world fullscreen* ;
2008-02-21 00:13:31 -05:00
: raise-window ( gadget -- )
find-world raise-window* ;
HOOK: close-window ui-backend ( gadget -- )
M: object close-window
find-world [ ungraft ] when* ;
2007-11-16 01:19:13 -05:00
: start-ui ( -- )
restore-windows? [
restore-windows
] [
init-ui ui-hook get call
2008-02-25 20:37:43 -05:00
] if
notify-ui-thread start-ui-thread ;
2007-09-20 18:09:08 -04:00
2008-02-25 20:37:43 -05:00
[
f \ ui-running set-global
<flag> ui-notify-flag set-global
] "ui" add-init-hook
2007-09-20 18:09:08 -04:00
HOOK: ui ui-backend ( -- )
MAIN: ui
: with-ui ( quot -- )
ui-running? [
call
] [
f windows set-global
2008-05-08 17:58:13 -04:00
[
ui-hook set
stop-after-last-window? on
ui
] with-scope
2007-09-20 18:09:08 -04:00
] if ;