factor/extra/ui/gadgets/lists/lists.factor

126 lines
3.3 KiB
Factor
Raw Normal View History

2009-01-06 16:54:17 -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.
USING: accessors math.vectors classes.tuple math.rectangles colors
2009-11-05 23:22:21 -05:00
kernel locals sequences models opengl math math.order namespaces
2009-03-07 16:58:14 -05:00
ui.commands ui.gestures ui.render ui.gadgets ui.gadgets.labels
ui.gadgets.scrollers ui.gadgets.presentations ui.gadgets.viewports
ui.gadgets.packs ;
2007-09-20 18:09:08 -04:00
IN: ui.gadgets.lists
TUPLE: list < pack index presenter color hook ;
2007-09-20 18:09:08 -04:00
: list-theme ( list -- list )
2009-01-06 16:54:17 -05:00
selection-color >>color ; inline
2007-09-20 18:09:08 -04:00
: <list> ( hook presenter model -- gadget )
2009-03-07 04:20:47 -05:00
list new
{ 0 1 } >>orientation
1 >>fill
0 >>index
swap >>model
swap >>presenter
swap >>hook
list-theme ;
2007-09-20 18:09:08 -04:00
: calc-bounded-index ( n list -- m )
control-value length 1 - min 0 max ;
2007-09-20 18:09:08 -04:00
: bound-index ( list -- )
2008-09-27 17:45:20 -04:00
dup index>> over calc-bounded-index >>index drop ;
2007-09-20 18:09:08 -04:00
: list-presentation-hook ( list -- quot )
2008-08-23 00:20:49 -04:00
hook>> [ [ list? ] find-parent ] prepend ;
2007-09-20 18:09:08 -04:00
: <list-presentation> ( hook elt presenter -- gadget )
2009-03-07 16:58:14 -05:00
[ call( elt -- obj ) ] [ drop ] 2bi [ >label text-theme ] dip
<presentation>
swap >>hook ; inline
2007-09-20 18:09:08 -04:00
: <list-items> ( list -- seq )
2008-06-18 23:30:54 -04:00
[ list-presentation-hook ]
[ presenter>> ]
[ control-value ]
tri [
[ 2dup ] dip swap <list-presentation>
2007-09-20 18:09:08 -04:00
] map 2nip ;
M: list model-changed
nip
2007-09-20 18:09:08 -04:00
dup clear-gadget
dup <list-items> add-gadgets
2007-09-20 18:09:08 -04:00
bound-index ;
: selected-rect ( list -- rect )
dup index>> swap children>> ?nth ;
2007-09-20 18:09:08 -04:00
M: list draw-gadget*
origin get [
dup color>> gl-color
selected-rect [
rect-bounds gl-fill-rect
] when*
2007-09-20 18:09:08 -04:00
] with-translation ;
M: list focusable-child* drop t ;
: list-value ( list -- object )
dup index>> swap control-value ?nth ;
2007-09-20 18:09:08 -04:00
: scroll>selected ( list -- )
#! We change the rectangle's width to zero to avoid
#! scrolling right.
[ selected-rect rect-bounds { 0 1 } v* <rect> ] keep
scroll>rect ;
: list-empty? ( list -- ? ) control-value empty? ;
: select-index ( n list -- )
dup list-empty? [
2drop
] [
2009-11-05 23:22:21 -05:00
[ control-value length rem ] [ (>>index) ] [ ] tri
2009-01-06 16:54:17 -05:00
[ relayout-1 ] [ scroll>selected ] bi
2007-09-20 18:09:08 -04:00
] if ;
: select-previous ( list -- )
[ index>> 1 - ] keep select-index ;
2007-09-20 18:09:08 -04:00
: select-next ( list -- )
[ index>> 1 + ] keep select-index ;
2007-09-20 18:09:08 -04:00
: invoke-value-action ( list -- )
dup list-empty? [
2009-04-15 20:03:44 -04:00
dup hook>> call( list -- )
2007-09-20 18:09:08 -04:00
] [
2009-01-06 16:54:17 -05:00
[ index>> ] keep nth-gadget invoke-secondary
2007-09-20 18:09:08 -04:00
] if ;
2009-11-05 23:22:21 -05:00
:: select-gadget ( gadget list -- )
gadget list children>> index
[ list select-index ] when* ;
2007-09-20 18:09:08 -04:00
: clamp-loc ( point max -- point )
vmin { 0 0 } vmax ;
: select-at ( point list -- )
[ dim>> clamp-loc ] keep
2007-09-20 18:09:08 -04:00
[ pick-up ] keep
select-gadget ;
: list-page ( list vec -- )
[ dup selected-rect rect-bounds 2 v/n v+ over visible-dim ] dip
v* v+ swap select-at ;
2007-09-20 18:09:08 -04:00
: list-page-up ( list -- ) { 0 -1 } list-page ;
: list-page-down ( list -- ) { 0 1 } list-page ;
list "keyboard-navigation" "Lists can be navigated from the keyboard." {
{ T{ button-down } request-focus }
{ T{ key-down f f "UP" } select-previous }
{ T{ key-down f f "DOWN" } select-next }
{ T{ key-down f f "PAGE_UP" } list-page-up }
{ T{ key-down f f "PAGE_DOWN" } list-page-down }
{ T{ key-down f f "RET" } invoke-value-action }
} define-command-map