factor/library/ui/commands.factor

95 lines
2.4 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 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 %
"Click Button" %
button-up-# [ " " % # ] when*
] "" make ;
2006-08-24 18:23:48 -04:00
M: button-down gesture>string
[
dup button-down-mods modifiers>string %
"Press Button" %
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 ;
: commands ( class -- hash )
dup "commands" word-prop [ ] [
H{ } clone [ "commands" set-word-prop ] keep
] ?if ;
: commands>gestures ( class -- hash )
commands hash-values concat
2006-08-24 18:23:48 -04:00
[ command-gesture ] subset
[ dup command-gesture swap [ invoke-command ] curry ]
map>hash ;
: define-commands ( class group specs -- )
[ dup array? [ first3 <command> ] when ] map
swap pick commands set-hash
dup commands>gestures "gestures" set-word-prop ;
2006-08-24 18:23:48 -04:00
SYMBOL: +name+
SYMBOL: +quot+
SYMBOL: +listener+
SYMBOL: +keyboard+
2006-10-07 02:17:32 -04:00
SYMBOL: +default+
2006-10-07 02:17:32 -04:00
TUPLE: operation predicate listener? default? ;
2006-08-31 21:59:57 -04:00
: (command) ( -- command )
+name+ get +keyboard+ get +quot+ get <command> ;
2006-09-01 01:20:38 -04:00
C: operation ( predicate hash -- operation )
swap [
(command) over set-delegate
2006-10-07 02:17:32 -04:00
+default+ get over set-operation-default?
+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-10-07 02:17:32 -04:00
: default-operation ( obj -- command )
object-operations [ operation-default? ] find-last nip ;
2006-10-04 23:57:34 -04:00
: modify-operation ( quot operation -- operation )
clone
[ command-quot append ] keep
[ set-command-quot ] keep ;
: modify-operations ( operations quot -- operations )
swap [ modify-operation ] map-with ;