]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/operations/operations.factor
8e83f69edbb18ba5304259480cd1ad81fb79f746
[factor.git] / basis / ui / operations / operations.factor
1 ! Copyright (C) 2006, 2008 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 make
5 hashtables help.markup quotations assocs ;
6 IN: ui.operations
7
8 SYMBOL: +keyboard+
9 SYMBOL: +primary+
10 SYMBOL: +secondary+
11
12 TUPLE: operation predicate command translator hook listener? ;
13
14 : <operation> ( predicate command -- operation )
15     operation new
16         [ ] >>hook
17         [ ] >>translator
18         swap >>command
19         swap >>predicate ;
20
21 PREDICATE: listener-operation < operation
22     [ command>> listener-command? ] [ listener?>> ] bi or ;
23
24 M: operation command-name
25     command>> command-name ;
26
27 M: operation command-description
28     command>> command-description ;
29
30 M: operation command-word command>> command-word ;
31
32 : operation-gesture ( operation -- gesture )
33     command>> +keyboard+ word-prop ;
34
35 SYMBOL: operations
36
37 : object-operations ( obj -- operations )
38     operations get [ predicate>> call ] with filter ;
39
40 : find-operation ( obj quot -- command )
41     >r object-operations r> find-last nip ; inline
42
43 : primary-operation ( obj -- operation )
44     [ command>> +primary+ word-prop ] find-operation ;
45
46 : secondary-operation ( obj -- operation )
47     dup
48     [ command>> +secondary+ word-prop ] find-operation
49     [ ] [ primary-operation ] ?if ;
50
51 : default-flags ( -- assoc )
52     H{ { +keyboard+ f } { +primary+ f } { +secondary+ f } } ;
53
54 : define-operation ( pred command flags -- )
55     default-flags swap assoc-union
56     dupd define-command <operation>
57     operations get push ;
58
59 : modify-operation ( hook translator operation -- operation )
60     clone
61         swap >>translator
62         swap >>hook
63         t >>listener? ;
64
65 : modify-operations ( operations hook translator -- operations )
66     rot [ modify-operation ] with with map ;
67
68 : operations>commands ( object hook translator -- pairs )
69     [ object-operations ] 2dip modify-operations
70     [ [ operation-gesture ] keep ] { } map>assoc ;
71
72 : define-operation-map ( class group blurb object hook translator -- )
73     operations>commands define-command-map ;
74
75 : operation-quot ( target command -- quot )
76     [
77         swap literalize ,
78         dup translator>> %
79         command>> ,
80     ] [ ] make ;
81
82 M: operation invoke-command ( target command -- )
83     [ hook>> call ] keep operation-quot call ;