factor/basis/ui/gadgets/gadgets.factor

401 lines
8.9 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays hashtables kernel math namespaces
make sequences quotations math.vectors combinators sorting
binary-search vectors dlists deques models threads
concurrency.flags math.order math.rectangles fry locals ;
2007-09-20 18:09:08 -04:00
IN: ui.gadgets
! Values for orientation slot
CONSTANT: horizontal { 1 0 }
CONSTANT: vertical { 0 1 }
TUPLE: gadget < rect
pref-dim
parent
children
{ orientation initial: { 0 1 } }
focus
{ visible? initial: t }
root?
clipped?
layout-state
{ graft-state initial: { f f } }
graft-node
interior
boundary
model ;
2007-09-20 18:09:08 -04:00
M: gadget equal? 2drop f ;
M: gadget hashcode* nip identity-hashcode ;
2007-09-20 18:09:08 -04:00
M: gadget model-changed 2drop ;
2007-11-13 18:51:10 -05:00
: gadget-child ( gadget -- child ) children>> first ; inline
2007-09-20 18:09:08 -04:00
: nth-gadget ( n gadget -- child ) children>> nth ; inline
2007-09-20 18:09:08 -04:00
: <gadget> ( -- gadget )
2012-09-17 20:21:31 -04:00
gadget new ; inline
2007-09-20 18:09:08 -04:00
2007-11-13 18:51:10 -05:00
: control-value ( control -- value )
2008-08-31 17:21:18 -04:00
model>> value>> ;
2007-11-13 18:51:10 -05:00
: set-control-value ( value control -- )
2008-07-12 18:54:57 -04:00
model>> set-model ;
2007-09-20 18:09:08 -04:00
: relative-loc ( fromgadget togadget -- loc )
2dup eq? [
2drop { 0 0 }
] [
[ [ parent>> ] dip relative-loc ] [ drop loc>> ] 2bi v+
2007-09-20 18:09:08 -04:00
] if ;
GENERIC: user-input* ( str gadget -- ? )
M: gadget user-input* 2drop t ;
GENERIC: children-on ( rect gadget -- seq )
2007-09-20 18:09:08 -04:00
2008-07-12 18:48:08 -04:00
M: gadget children-on nip children>> ;
2007-09-20 18:09:08 -04:00
<PRIVATE
:: (fast-children-on) ( point axis children quot -- i )
children [
[ point ] dip
quot call( value -- loc ) v-
axis v. 0 <=>
] search drop ; inline
2007-09-20 18:09:08 -04:00
PRIVATE>
:: fast-children-on ( rect axis children quot -- slice )
rect loc>> axis children quot (fast-children-on) 0 or
rect rect-bounds v+ axis children quot (fast-children-on) ?1+
children <slice> ; inline
2007-09-20 18:09:08 -04:00
M: gadget contains-rect? ( bounds gadget -- ? )
dup visible?>> [ call-next-method ] [ 2drop f ] if ;
2007-09-20 18:09:08 -04:00
M: gadget contains-point? ( loc gadget -- ? )
dup visible?>> [ call-next-method ] [ 2drop f ] if ;
2007-09-20 18:09:08 -04:00
: pick-up ( point gadget -- child/f )
2dup [ dup point>rect ] dip children-on
[ contains-point? ] with find-last nip
[ [ loc>> v- ] [ pick-up ] bi ] [ nip ] ?if ;
2007-09-20 18:09:08 -04:00
: max-dims ( seq -- dim )
[ 0 0 ] dip [ first2 swapd [ max ] 2bi@ ] each 2array ;
2007-09-20 18:09:08 -04:00
: sum-dims ( seq -- dim )
[ 0 0 ] dip [ first2 swapd [ + ] 2bi@ ] each 2array ;
2007-09-20 18:09:08 -04:00
: each-child ( gadget quot -- )
[ children>> ] dip each ; inline
2007-09-20 18:09:08 -04:00
! Selection protocol
GENERIC: gadget-selection? ( gadget -- ? )
M: gadget gadget-selection? drop f ;
GENERIC: gadget-selection ( gadget -- string/f )
M: gadget gadget-selection drop f ;
! Text protocol
GENERIC: gadget-text* ( gadget -- )
GENERIC: gadget-text-separator ( gadget -- str )
M: gadget gadget-text-separator
orientation>> vertical = "\n" "" ? ;
2007-09-20 18:09:08 -04:00
: gadget-seq-text ( seq gadget -- )
gadget-text-separator '[ _ % ] [ gadget-text* ] interleave ;
2007-09-20 18:09:08 -04:00
M: gadget gadget-text*
[ children>> ] keep gadget-seq-text ;
2007-09-20 18:09:08 -04:00
M: array gadget-text*
[ gadget-text* ] each ;
: gadget-text ( gadget -- string ) [ gadget-text* ] "" make ;
DEFER: relayout
<PRIVATE
SYMBOL: ui-notify-flag
: notify-ui-thread ( -- ) ui-notify-flag get-global raise-flag ;
2007-09-20 18:09:08 -04:00
: invalidate ( gadget -- )
2008-09-27 17:45:20 -04:00
\ invalidate >>layout-state drop ;
2007-09-20 18:09:08 -04:00
2008-09-27 17:45:20 -04:00
: forget-pref-dim ( gadget -- ) f >>pref-dim drop ;
2007-09-20 18:09:08 -04:00
2009-03-25 23:54:31 -04:00
: layout-queue ( -- queue ) \ layout-queue get ;
2007-09-20 18:09:08 -04:00
2007-11-16 01:19:13 -05:00
: layout-later ( gadget -- )
2007-09-20 18:09:08 -04:00
#! When unit testing gadgets without the UI running, the
#! invalid queue is not initialized and we simply ignore
#! invalidation requests.
layout-queue [ push-front notify-ui-thread ] [ drop ] if* ;
2007-09-20 18:09:08 -04:00
: invalidate* ( gadget -- )
\ invalidate* >>layout-state
2007-09-20 18:09:08 -04:00
dup forget-pref-dim
2008-08-29 19:44:19 -04:00
dup root?>>
2008-07-12 18:45:57 -04:00
[ layout-later ] [ parent>> [ relayout ] when* ] if ;
2007-09-20 18:09:08 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
: relayout ( gadget -- )
2008-07-12 18:54:57 -04:00
dup layout-state>> \ invalidate* eq?
2007-09-20 18:09:08 -04:00
[ drop ] [ invalidate* ] if ;
: relayout-1 ( gadget -- )
2008-07-12 18:54:57 -04:00
dup layout-state>>
2007-11-16 01:19:13 -05:00
[ drop ] [ dup invalidate layout-later ] if ;
2007-09-20 18:09:08 -04:00
2008-09-27 17:45:20 -04:00
: show-gadget ( gadget -- ) t >>visible? drop ;
2012-09-17 20:21:31 -04:00
2008-09-27 17:45:20 -04:00
: hide-gadget ( gadget -- ) f >>visible? drop ;
2007-09-20 18:09:08 -04:00
<PRIVATE
SYMBOL: in-layout?
2008-09-27 18:54:51 -04:00
GENERIC: dim-changed ( gadget -- )
M: gadget dim-changed
in-layout? get [ invalidate ] [ invalidate* ] if ;
PRIVATE>
M: gadget dim<< ( dim gadget -- )
2009-01-23 19:20:47 -05:00
2dup dim>> =
[ 2drop ]
[ [ nip ] [ call-next-method ] 2bi dim-changed ] if ;
2007-09-20 18:09:08 -04:00
GENERIC: pref-dim* ( gadget -- dim )
: pref-dim ( gadget -- dim )
2008-07-12 18:54:57 -04:00
dup pref-dim>> [ ] [
[ pref-dim* ] [ ] [ layout-state>> ] tri
[ drop ] [ dupd pref-dim<< ] if
2007-09-20 18:09:08 -04:00
] ?if ;
2012-09-17 20:21:31 -04:00
: pref-dims ( gadgets -- seq ) [ pref-dim ] map ; inline
2007-09-20 18:09:08 -04:00
M: gadget pref-dim* dim>> ;
2007-09-20 18:09:08 -04:00
GENERIC: layout* ( gadget -- )
M: gadget layout* drop ;
2008-09-27 17:45:20 -04:00
: prefer ( gadget -- ) dup pref-dim >>dim drop ;
2007-09-20 18:09:08 -04:00
: layout ( gadget -- )
2008-07-12 18:54:57 -04:00
dup layout-state>> [
f >>layout-state
2007-09-20 18:09:08 -04:00
dup layout*
dup [ layout ] each-child
] when drop ;
GENERIC: graft* ( gadget -- )
M: gadget graft* drop ;
GENERIC: ungraft* ( gadget -- )
M: gadget ungraft* drop ;
<PRIVATE
2009-03-25 23:54:31 -04:00
: graft-queue ( -- dlist )
\ graft-queue get [ "UI not running" throw ] unless* ;
2007-11-16 01:19:13 -05:00
: unqueue-graft ( gadget -- )
2008-06-11 03:58:38 -04:00
[ graft-node>> graft-queue delete-node ]
[ [ first { t t } { f f } ? ] change-graft-state drop ] bi ;
2007-11-16 01:19:13 -05:00
: (queue-graft) ( gadget flags -- )
2008-06-11 03:58:38 -04:00
>>graft-state
dup graft-queue push-front* >>graft-node drop
notify-ui-thread ;
2007-11-16 01:19:13 -05:00
: queue-graft ( gadget -- )
{ f t } (queue-graft) ;
2007-11-16 01:19:13 -05:00
: queue-ungraft ( gadget -- )
{ t f } (queue-graft) ;
2007-11-16 01:19:13 -05:00
: graft-later ( gadget -- )
2008-07-12 19:02:32 -04:00
dup graft-state>> {
2007-11-16 01:19:13 -05:00
{ { f t } [ drop ] }
{ { t t } [ drop ] }
{ { t f } [ unqueue-graft ] }
{ { f f } [ queue-graft ] }
} case ;
: graft ( gadget -- )
dup graft-later [ graft ] each-child ;
2007-11-16 01:19:13 -05:00
: ungraft-later ( gadget -- )
2008-07-12 19:02:32 -04:00
dup graft-state>> {
2007-11-16 01:19:13 -05:00
{ { f f } [ drop ] }
{ { t f } [ drop ] }
{ { f t } [ unqueue-graft ] }
{ { t t } [ queue-ungraft ] }
} case ;
: ungraft ( gadget -- )
dup [ ungraft ] each-child ungraft-later ;
2007-09-20 18:09:08 -04:00
: activate-control ( gadget -- )
dup model>> dup [
2dup add-connection
swap model-changed
] [
2drop
] if ;
2007-09-20 18:09:08 -04:00
: deactivate-control ( gadget -- )
dup model>> dup [ 2dup remove-connection ] when 2drop ;
2007-09-20 18:09:08 -04:00
: notify ( gadget -- )
dup graft-state>>
[ first { f f } { t t } ? >>graft-state ] [ ] bi
{
{ { f t } [ dup activate-control graft* ] }
{ { t f } [ dup deactivate-control ungraft* ] }
} case ;
2007-09-20 18:09:08 -04:00
: notify-queued ( -- )
graft-queue [ notify ] slurp-deque ;
2007-09-20 18:09:08 -04:00
: (unparent) ( gadget -- )
dup ungraft
dup forget-pref-dim
2008-09-27 17:45:20 -04:00
f >>parent drop ;
2007-09-20 18:09:08 -04:00
: (clear-gadget) ( gadget -- )
dup [ (unparent) ] each-child
f >>focus f >>children drop ;
2007-09-20 18:09:08 -04:00
: unfocus-gadget ( child gadget -- )
2009-01-23 19:20:47 -05:00
[ nip ] [ focus>> eq? ] 2bi [ f >>focus ] when drop ;
2007-09-20 18:09:08 -04:00
PRIVATE>
2007-11-16 01:19:13 -05:00
2008-06-08 16:32:55 -04:00
: not-in-layout ( -- )
2007-11-16 01:19:13 -05:00
in-layout? get
[ "Cannot add/remove gadgets in layout*" throw ] when ;
GENERIC: remove-gadget ( gadget parent -- )
M: gadget remove-gadget 2drop ;
2007-09-20 18:09:08 -04:00
: unparent ( gadget -- )
2007-11-16 01:19:13 -05:00
not-in-layout
[
dup parent>> dup
[
[ remove-gadget ] [
over (unparent)
[ unfocus-gadget ]
2009-10-28 00:25:35 -04:00
[ children>> remove! drop ]
[ nip relayout ]
2tri
] 2bi
] [ 2drop ] if
] when* ;
2007-09-20 18:09:08 -04:00
: clear-gadget ( gadget -- )
2007-11-16 01:19:13 -05:00
not-in-layout
[ (clear-gadget) ] [ relayout ] bi ;
2007-09-20 18:09:08 -04:00
<PRIVATE
: (add-gadget) ( child parent -- )
{
[ drop unparent ]
[ >>parent drop ]
[ [ ?push ] change-children drop ]
[ graft-state>> second [ graft ] [ drop ] if ]
} 2cleave ;
PRIVATE>
: add-gadget ( parent child -- parent )
2007-11-16 01:19:13 -05:00
not-in-layout
over (add-gadget)
dup relayout ;
: add-gadgets ( parent children -- parent )
2007-11-16 01:19:13 -05:00
not-in-layout
2012-07-13 18:39:18 -04:00
over '[ _ (add-gadget) ] each
dup relayout ;
2007-09-20 18:09:08 -04:00
: parents ( gadget -- seq )
2008-07-12 18:45:57 -04:00
[ parent>> ] follow ;
2007-09-20 18:09:08 -04:00
: each-parent ( gadget quot -- ? )
[ parents ] dip all? ; inline
2007-09-20 18:09:08 -04:00
: find-parent ( gadget quot -- parent )
[ parents ] dip find nip ; inline
2007-09-20 18:09:08 -04:00
: screen-loc ( gadget -- loc )
parents { 0 0 } [ loc>> v+ ] reduce ;
<PRIVATE
2007-09-20 18:09:08 -04:00
: (screen-rect) ( gadget -- loc ext )
2008-07-12 18:45:57 -04:00
dup parent>> [
[ rect-extent ] dip (screen-rect)
[ [ nip ] [ v+ ] 2bi ] dip [ v+ ] [ vmin ] 2bi*
2007-09-20 18:09:08 -04:00
] [
rect-extent
] if* ;
PRIVATE>
2007-09-20 18:09:08 -04:00
: screen-rect ( gadget -- rect )
(screen-rect) <extent-rect> ;
: child? ( parent child -- ? )
{
{ [ 2dup eq? ] [ 2drop t ] }
{ [ dup not ] [ 2drop f ] }
2008-07-12 18:45:57 -04:00
[ parent>> child? ]
2007-09-20 18:09:08 -04:00
} cond ;
GENERIC: focusable-child* ( gadget -- child/t )
M: gadget focusable-child* drop t ;
: focusable-child ( gadget -- child )
dup focusable-child*
dup t eq? [ drop ] [ nip focusable-child ] if ;
GENERIC: request-focus-on ( child gadget -- )
2008-07-12 18:45:57 -04:00
M: gadget request-focus-on parent>> request-focus-on ;
2007-09-20 18:09:08 -04:00
M: f request-focus-on 2drop ;
: request-focus ( gadget -- )
[ focusable-child ] keep request-focus-on ;
2007-09-20 18:09:08 -04:00
: focus-path ( gadget -- seq )
2008-07-12 18:54:57 -04:00
[ focus>> ] follow ;
USE: vocabs.loader
{ "ui.gadgets" "prettyprint" } "ui.gadgets.prettyprint" require-when