1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays definitions kernel ui.commands
4 ui.gestures sequences strings math words generic namespaces
5 hashtables help.markup quotations assocs fry call ;
12 TUPLE: operation predicate command translator listener? ;
14 : <operation> ( predicate command -- operation )
20 PREDICATE: listener-operation < operation
21 [ command>> listener-command? ] [ listener?>> ] bi or ;
23 M: operation command-name
24 command>> command-name ;
26 M: operation command-description
27 command>> command-description ;
29 M: operation command-word command>> command-word ;
31 : operation-gesture ( operation -- gesture )
32 command>> +keyboard+ word-prop ;
36 : object-operations ( obj -- operations )
37 operations get [ predicate>> call( obj -- ? ) ] with filter ;
39 : gesture>operation ( gesture object -- operation/f )
40 object-operations [ operation-gesture = ] with find nip ;
42 : find-operation ( obj quot -- command )
43 [ object-operations ] dip find-last nip ; inline
45 : primary-operation ( obj -- operation )
46 [ command>> +primary+ word-prop ] find-operation ;
48 : secondary-operation ( obj -- operation )
50 [ command>> +secondary+ word-prop ] find-operation
51 [ ] [ primary-operation ] ?if ;
53 : default-flags ( -- assoc )
54 H{ { +keyboard+ f } { +primary+ f } { +secondary+ f } } ;
56 : define-operation ( pred command flags -- )
57 default-flags swap assoc-union
58 dupd define-command <operation>
61 : modify-operation ( translator operation -- operation )
66 : modify-operations ( operations translator -- operations )
67 '[ [ _ ] dip modify-operation ] map ;
69 : operations>commands ( object translator -- pairs )
70 [ object-operations ] dip modify-operations
71 [ [ operation-gesture ] keep ] { } map>assoc ;
73 : define-operation-map ( class group blurb object translator -- )
74 operations>commands define-command-map ;
76 : operation-quot ( target command -- quot )
77 [ translator>> ] [ command>> ] bi '[ _ @ _ execute ] ;
79 M: operation invoke-command ( target command -- )
80 operation-quot call( -- ) ;