]> gitweb.factorcode.org Git - factor.git/commitdiff
Factor out menu and popup positioning into math.rectangles.positioning, and write...
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Fri, 13 Feb 2009 03:46:49 +0000 (21:46 -0600)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Fri, 13 Feb 2009 03:46:49 +0000 (21:46 -0600)
basis/math/rectangles/positioning/authors.txt [new file with mode: 0644]
basis/math/rectangles/positioning/positioning-tests.factor [new file with mode: 0644]
basis/math/rectangles/positioning/positioning.factor [new file with mode: 0644]
basis/ui/gadgets/menus/menus.factor
basis/ui/tools/listener/listener.factor
basis/ui/tools/listener/popups/popups.factor

diff --git a/basis/math/rectangles/positioning/authors.txt b/basis/math/rectangles/positioning/authors.txt
new file mode 100644 (file)
index 0000000..d4f5d6b
--- /dev/null
@@ -0,0 +1 @@
+Slava Pestov
\ No newline at end of file
diff --git a/basis/math/rectangles/positioning/positioning-tests.factor b/basis/math/rectangles/positioning/positioning-tests.factor
new file mode 100644 (file)
index 0000000..0a17d70
--- /dev/null
@@ -0,0 +1,39 @@
+! Copyright (C) 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test math.rectangles math.rectangles.positioning ;
+IN: math.rectangles.positioning.tests
+
+[ { 0 1 } ] [
+    { 0 0 } { 1 1 } <rect>
+    { 30 30 }
+    { 100 100 }
+    popup-loc
+] unit-test
+
+[ { 10 21 } ] [
+    { 10 20 } { 1 1 } <rect>
+    { 30 30 }
+    { 100 100 }
+    popup-loc
+] unit-test
+
+[ { 10 30 } ] [
+    { 10 20 } { 1 10 } <rect>
+    { 30 30 }
+    { 100 100 }
+    popup-loc
+] unit-test
+
+[ { 20 20 } ] [
+    { 40 10 } { 1 10 } <rect>
+    { 80 30 }
+    { 100 100 }
+    popup-loc
+] unit-test
+
+[ { 50 20 } ] [
+    { 50 70 } { 0 0 } <rect>
+    { 50 50 }
+    { 100 100 }
+    popup-loc
+] unit-test
\ No newline at end of file
diff --git a/basis/math/rectangles/positioning/positioning.factor b/basis/math/rectangles/positioning/positioning.factor
new file mode 100644 (file)
index 0000000..1af8b6e
--- /dev/null
@@ -0,0 +1,25 @@
+! Copyright (C) 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: sequences kernel accessors math math.order arrays ;
+IN: math.rectangles.positioning
+
+! Some geometry code for positioning popups and menus
+! in a semi-intelligent manner
+
+: popup-x ( visible-rect popup-dim screen-dim -- x )
+    [ loc>> first ] 2dip swap [ first ] bi@ - min ;
+
+: preferred-y ( visible-rect -- y )
+    [ loc>> ] [ dim>> ] bi [ second ] bi@ + ;
+
+: alternate-y ( visible-rect popup-dim -- y )
+    [ loc>> ] dip [ second ] bi@ - ;
+
+: popup-fits? ( visible-rect popup-dim screen-dim -- ? )
+    [ [ preferred-y ] [ second ] bi* + ] dip second < ;
+
+: popup-y ( visible-rect popup-dim screen-dim -- y )
+    3dup popup-fits? [ 2drop preferred-y ] [ drop alternate-y ] if ;
+
+: popup-loc ( visible-rect popup-dim screen-dim -- loc )
+    [ popup-x ] [ popup-y ] 3bi 2array ;
index ca6e1977c8ff3d5789ea84ee057971e214a50902..3c2caf65206c29683582e474500a61713af39579 100644 (file)
@@ -1,13 +1,15 @@
 ! Copyright (C) 2005, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: locals accessors kernel math namespaces sequences math.vectors
-colors colors.constants math.rectangles ui.commands ui.operations
-ui.gadgets ui.gadgets.buttons ui.gadgets.worlds ui.gestures
-ui.gadgets.theme ui.gadgets.packs ui.gadgets.glass ui.gadgets.borders ;
+math.rectangles.positioning colors colors.constants math.rectangles
+ui.commands ui.operations ui.gadgets ui.gadgets.buttons
+ui.gadgets.worlds ui.gestures ui.gadgets.theme ui.gadgets.packs
+ui.gadgets.glass ui.gadgets.borders ;
 IN: ui.gadgets.menus
 
 : menu-loc ( world menu -- loc )
-    [ dim>> ] [ pref-dim ] bi* [v-] hand-loc get-global vmin ;
+    [ hand-loc get { 0 0 } <rect> ] 2dip
+    pref-dim swap dim>> popup-loc ;
 
 : show-menu ( owner menu -- )
     [ find-world ] dip 2dup menu-loc show-glass ;
index 36b92055c2c1e88cee1a46fcecf238cb8ce0aeae..8a8581b3b3fa8886a8945627da73d862139d0a90 100644 (file)
@@ -91,7 +91,11 @@ M: input (print-input)
     [ string>> H{ { font-style bold } } format ] with-nesting nl ;
 
 M: word (print-input)
-    "Command: " write . ;
+    "Command: "
+    [
+        "sans-serif" font-name set
+        bold font-style set
+    ] H{ } make-assoc format . ;
 
 : print-input ( object interactor -- )
     output>> [ (print-input) ] with-output-stream* ;
index 89c95f37b24b93aefbabcda9dd4d660f703440e2..18415f34c3c7761c77929348b2b577a641816e8e 100644 (file)
@@ -1,9 +1,9 @@
 ! Copyright (C) 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors documents.elements kernel math math.vectors
-sequences ui.gadgets ui.gadgets.editors ui.gadgets.glass
-ui.gadgets.tracks ui.gadgets.wrappers
-ui.gadgets.worlds ui.gestures ;
+math.rectangles math.rectangles.positioning sequences ui.gadgets
+ui.gadgets.editors ui.gadgets.glass ui.gadgets.tracks
+ui.gadgets.wrappers ui.gadgets.worlds ui.gestures ;
 IN: ui.tools.listener.popups
 
 SLOT: popup
@@ -25,34 +25,26 @@ popup H{
     { T{ key-down f f "ESC" } [ hide-popup ] }
 } set-gestures
 
-CONSTANT: popup-offset { -4 0 }
-
-: (popup-loc) ( interactor element -- loc )
+: caret-loc ( interactor element -- loc )
     [ drop screen-loc ] [
         [
             [ [ editor-caret ] [ model>> ] bi ] dip
             prev-elt
         ] [ drop ] 2bi
         loc>point
-    ] 2bi v+ popup-offset v+ ;
-
-: popup-loc-1 ( interactor element -- loc )
-    [ (popup-loc) ] [ drop caret-dim ] 2bi v+ ;
-
-: popup-loc-2 ( interactor element popup -- loc )
-    [ (popup-loc) ] dip pref-dim { 0 1 } v* v- ;
+    ] 2bi v+ ;
 
-: popup-fits? ( interactor element popup -- ? )
-    [ [ popup-loc-1 ] dip pref-dim v+ ]
-    [ 2drop find-world dim>> ]
-    3bi [ second ] bi@ <= ;
+: relevant-rect ( popup -- rect )
+    [ interactor>> ] [ element>> ] bi
+    [ caret-loc ] [ drop caret-dim { 0 1 } v+ ] 2bi
+    <rect> ;
 
-: popup-loc ( popup -- loc )
-    [ interactor>> ] [ element>> ] [ ] tri 3dup popup-fits?
-    [ drop popup-loc-1 ] [ popup-loc-2 ] if ;
+: listener-popup-loc ( popup -- loc )
+    [ relevant-rect ] [ pref-dim ] [ interactor>> find-world dim>> ] tri
+    popup-loc ;
 
 : show-popup ( interactor element popup -- )
     <popup>
     [ dup interactor>> (>>popup) ]
-    [ [ interactor>> find-world ] [ ] [ popup-loc ] tri show-glass ]
+    [ [ interactor>> find-world ] [ ] [ listener-popup-loc ] tri show-glass ]
     bi ;
\ No newline at end of file