factor/library/ui/commands.factor

98 lines
2.5 KiB
Factor
Raw Normal View History

2006-08-24 18:23:48 -04:00
! Copyright (C) 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays definitions kernel gadgets sequences strings math
words generic namespaces hashtables help ;
2006-08-25 20:52:13 -04:00
IN: gadgets
TUPLE: command group name gesture quot ;
2006-08-24 18:23:48 -04:00
2006-08-25 20:52:13 -04:00
M: command equal? eq? ;
2006-08-24 18:23:48 -04:00
GENERIC: invoke-command ( target command -- )
M: command invoke-command ( target command -- )
command-quot call ;
2006-08-24 18:23:48 -04:00
GENERIC: gesture>string ( gesture -- string )
: modifiers>string ( modifiers -- string )
[ word-name ] map concat >string ;
2006-08-24 18:23:48 -04:00
M: key-down gesture>string
dup key-down-mods modifiers>string
2006-08-24 18:23:48 -04:00
swap key-down-sym append ;
M: button-up gesture>string
[
dup button-up-mods modifiers>string %
"Mouse Up" %
button-up-# [ " " % # ] when*
] "" make ;
2006-08-24 18:23:48 -04:00
M: button-down gesture>string
[
dup button-down-mods modifiers>string %
2006-09-02 01:59:35 -04:00
"Mouse Down" %
2006-08-28 00:53:55 -04:00
button-down-# [ " " % # ] when*
] "" make ;
2006-08-24 18:23:48 -04:00
M: object gesture>string drop f ;
: command-gestures ( commands -- hash )
2006-08-24 18:23:48 -04:00
[ command-gesture ] subset
[ dup command-gesture swap [ invoke-command ] curry ]
map>hash ;
: define-commands* ( class specs -- )
2dup "commands" set-word-prop
command-gestures "gestures" set-word-prop ;
: <commands> ( specs -- commands )
2006-09-01 03:58:47 -04:00
#! Specs is an array of { group { name gesture quot }* }
unclip swap [ first3 <command> ] map-with ;
: define-commands ( class specs -- )
2006-09-01 03:58:47 -04:00
[ <commands> ] map concat define-commands* ;
2006-08-24 18:23:48 -04:00
2006-09-19 02:30:21 -04:00
: commands ( class -- seq ) "commands" word-prop ;
2006-08-24 19:15:50 -04:00
2006-09-19 02:30:21 -04:00
: all-commands ( gadget -- seq )
delegates [ class commands ] map concat ;
2006-08-25 20:52:13 -04:00
SYMBOL: +name+
SYMBOL: +button+
SYMBOL: +group+
SYMBOL: +quot+
SYMBOL: +listener+
SYMBOL: +gesture+
2006-09-01 03:58:47 -04:00
TUPLE: operation predicate button gesture listener? ;
2006-08-31 21:59:57 -04:00
: (operation) ( -- command )
2006-09-01 03:58:47 -04:00
+group+ get +name+ get +gesture+ get +quot+ get <command> ;
2006-09-01 01:20:38 -04:00
C: operation ( predicate hash -- operation )
swap [
2006-08-31 21:59:57 -04:00
(operation) over set-delegate
2006-09-01 03:58:47 -04:00
+button+ get over set-operation-button
+listener+ get over set-operation-listener?
] bind
2006-09-01 01:20:38 -04:00
[ set-operation-predicate ] keep ;
2006-08-24 22:44:42 -04:00
SYMBOL: operations
2006-09-01 01:20:38 -04:00
: object-operations ( obj -- operations )
operations get [ operation-predicate call ] subset-with ;
: class-operations ( class -- operations )
2006-09-01 01:20:38 -04:00
"predicate" word-prop
operations get [ operation-predicate = ] subset-with ;
2006-09-01 01:20:38 -04:00
: mouse-operation ( obj button# -- command )
2006-09-01 03:58:47 -04:00
swap object-operations
[ operation-button = ] subset-with
dup empty? [ drop f ] [ peek ] if ;
2006-08-28 15:54:40 -04:00
2006-09-01 01:20:38 -04:00
: mouse-operations ( obj -- seq )
3 [ 1+ mouse-operation ] map-with ;