| 
									
										
										
										
											2009-01-15 16:22:25 -05:00
										 |  |  | ! Copyright (C) 2006, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-07-11 01:01:22 -04:00
										 |  |  | USING: accessors arrays definitions kernel ui.commands | 
					
						
							| 
									
										
										
										
											2009-01-15 16:22:25 -05:00
										 |  |  | ui.gestures sequences strings math words generic namespaces | 
					
						
							| 
									
										
										
										
											2009-03-16 21:11:36 -04:00
										 |  |  | hashtables help.markup quotations assocs fry linked-assocs ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: ui.operations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: +keyboard+ | 
					
						
							|  |  |  | SYMBOL: +primary+ | 
					
						
							|  |  |  | SYMBOL: +secondary+ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:22:25 -05:00
										 |  |  | TUPLE: operation predicate command translator listener? ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <operation> ( predicate command -- operation )
 | 
					
						
							| 
									
										
										
										
											2008-07-11 01:01:22 -04:00
										 |  |  |     operation new
 | 
					
						
							|  |  |  |         [ ] >>translator | 
					
						
							|  |  |  |         swap >>command | 
					
						
							|  |  |  |         swap >>predicate ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: listener-operation < operation | 
					
						
							| 
									
										
										
										
											2008-09-27 15:36:04 -04:00
										 |  |  |     [ command>> listener-command? ] [ listener?>> ] bi or ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: operation command-name | 
					
						
							| 
									
										
										
										
											2008-09-01 00:53:07 -04:00
										 |  |  |     command>> command-name ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: operation command-description | 
					
						
							| 
									
										
										
										
											2008-09-01 00:53:07 -04:00
										 |  |  |     command>> command-description ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-01 00:53:07 -04:00
										 |  |  | M: operation command-word command>> command-word ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : operation-gesture ( operation -- gesture )
 | 
					
						
							| 
									
										
										
										
											2008-09-01 00:53:07 -04:00
										 |  |  |     command>> +keyboard+ word-prop ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: operations | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-16 22:42:47 -05:00
										 |  |  | operations [ <linked-hash> ] initialize
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : object-operations ( obj -- operations )
 | 
					
						
							| 
									
										
										
										
											2009-02-16 22:42:47 -05:00
										 |  |  |     operations get values
 | 
					
						
							| 
									
										
										
										
											2009-02-16 22:47:35 -05:00
										 |  |  |     [ predicate>> call( obj -- ? ) ] with filter ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-31 03:39:38 -05:00
										 |  |  | : gesture>operation ( gesture object -- operation/f )
 | 
					
						
							|  |  |  |     object-operations [ operation-gesture = ] with find nip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : find-operation ( obj quot -- command )
 | 
					
						
							| 
									
										
										
										
											2008-11-28 01:02:02 -05:00
										 |  |  |     [ object-operations ] dip find-last nip ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-18 22:00:53 -05:00
										 |  |  | : primary-operation? ( operation -- ? )
 | 
					
						
							|  |  |  |     command>> +primary+ word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : primary-operation ( obj -- operation )
 | 
					
						
							| 
									
										
										
										
											2009-02-18 22:00:53 -05:00
										 |  |  |     [ primary-operation? ] find-operation ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-17 09:26:33 -05:00
										 |  |  | : invoke-primary-operation ( obj -- )
 | 
					
						
							|  |  |  |     dup primary-operation invoke-command ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : secondary-operation ( obj -- operation )
 | 
					
						
							|  |  |  |     dup
 | 
					
						
							| 
									
										
										
										
											2008-09-01 00:53:07 -04:00
										 |  |  |     [ command>> +secondary+ word-prop ] find-operation | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     [ ] [ primary-operation ] ?if ;
 | 
					
						
							| 
									
										
										
										
											2009-02-17 09:26:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : invoke-secondary-operation ( obj -- )
 | 
					
						
							|  |  |  |     dup secondary-operation invoke-command ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : default-flags ( -- assoc )
 | 
					
						
							|  |  |  |     H{ { +keyboard+ f } { +primary+ f } { +secondary+ f } } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-16 22:42:47 -05:00
										 |  |  | : (define-operation) ( operation -- )
 | 
					
						
							|  |  |  |     dup [ command>> ] [ predicate>> ] bi
 | 
					
						
							|  |  |  |     2array operations get set-at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : define-operation ( pred command flags -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-13 23:58:07 -04:00
										 |  |  |     default-flags swap assoc-union
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     dupd define-command <operation> | 
					
						
							| 
									
										
										
										
											2009-02-16 22:42:47 -05:00
										 |  |  |     (define-operation) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:22:25 -05:00
										 |  |  | : modify-operation ( translator operation -- operation )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     clone
 | 
					
						
							| 
									
										
										
										
											2008-09-27 15:36:04 -04:00
										 |  |  |         swap >>translator | 
					
						
							|  |  |  |         t >>listener? ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:22:25 -05:00
										 |  |  | : modify-operations ( operations translator -- operations )
 | 
					
						
							|  |  |  |     '[ [ _ ] dip modify-operation ] map ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:22:25 -05:00
										 |  |  | : operations>commands ( object translator -- pairs )
 | 
					
						
							|  |  |  |     [ object-operations ] dip modify-operations | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     [ [ operation-gesture ] keep ] { } map>assoc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:22:25 -05:00
										 |  |  | : define-operation-map ( class group blurb object translator -- )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     operations>commands define-command-map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : operation-quot ( target command -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-01-15 16:22:25 -05:00
										 |  |  |     [ translator>> ] [ command>> ] bi '[ _ @ _ execute ] ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: operation invoke-command ( target command -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-09 01:49:48 -05:00
										 |  |  |     operation-quot call( -- ) ;
 |