Add support for double-click actions to table gadget
parent
5c7af1cf2d
commit
29f9d61c90
|
@ -3,8 +3,8 @@
|
||||||
USING: accessors arrays colors fry io.styles kernel math
|
USING: accessors arrays colors fry io.styles kernel math
|
||||||
math.geometry.rect math.order math.vectors namespaces opengl
|
math.geometry.rect math.order math.vectors namespaces opengl
|
||||||
sequences ui.gadgets ui.gadgets.scrollers ui.gadgets.status-bar
|
sequences ui.gadgets ui.gadgets.scrollers ui.gadgets.status-bar
|
||||||
ui.gadgets.worlds ui.gestures ui.render models math.ranges sequences
|
ui.gadgets.worlds ui.gadgets.theme ui.gestures ui.render models
|
||||||
combinators ;
|
math.ranges sequences combinators ;
|
||||||
IN: ui.gadgets.tables
|
IN: ui.gadgets.tables
|
||||||
|
|
||||||
! Row rendererer protocol
|
! Row rendererer protocol
|
||||||
|
@ -15,7 +15,7 @@ SINGLETON: trivial-renderer
|
||||||
M: trivial-renderer row-columns drop ;
|
M: trivial-renderer row-columns drop ;
|
||||||
|
|
||||||
TUPLE: table < gadget
|
TUPLE: table < gadget
|
||||||
renderer filled-column column-alignment
|
renderer filled-column column-alignment action
|
||||||
column-widths total-width
|
column-widths total-width
|
||||||
font text-color selection-color mouse-color
|
font text-color selection-color mouse-color
|
||||||
selected-index selected-value
|
selected-index selected-value
|
||||||
|
@ -26,9 +26,10 @@ focused? ;
|
||||||
table new-gadget
|
table new-gadget
|
||||||
swap >>model
|
swap >>model
|
||||||
trivial-renderer >>renderer
|
trivial-renderer >>renderer
|
||||||
|
[ drop ] >>action
|
||||||
f <model> >>selected-value
|
f <model> >>selected-value
|
||||||
{ "sans-serif" plain 12 } >>font
|
sans-serif-font >>font
|
||||||
T{ rgba f 0.8 0.8 1.0 1.0 } >>selection-color
|
selection-color >>selection-color
|
||||||
black >>mouse-color
|
black >>mouse-color
|
||||||
black >>text-color ;
|
black >>text-color ;
|
||||||
|
|
||||||
|
@ -103,17 +104,17 @@ M: table layout*
|
||||||
: validate-row ( m table -- n )
|
: validate-row ( m table -- n )
|
||||||
control-value length 1- min 0 max ;
|
control-value length 1- min 0 max ;
|
||||||
|
|
||||||
: first-visible-row ( table -- n )
|
: visible-row ( table quot -- n )
|
||||||
[
|
'[
|
||||||
[ clip get loc>> second origin get second - ] dip
|
[ clip get @ origin get [ second ] bi@ - ] dip
|
||||||
y>row
|
y>row
|
||||||
] keep validate-row ;
|
] keep validate-row ; inline
|
||||||
|
|
||||||
|
: first-visible-row ( table -- n )
|
||||||
|
[ loc>> ] visible-row ;
|
||||||
|
|
||||||
: last-visible-row ( table -- n )
|
: last-visible-row ( table -- n )
|
||||||
[
|
[ rect-extent nip ] visible-row 1+ ;
|
||||||
[ clip get rect-extent nip second origin get second - ] dip
|
|
||||||
y>row
|
|
||||||
] keep validate-row 1+ ;
|
|
||||||
|
|
||||||
: column-loc ( font column width align -- loc )
|
: column-loc ( font column width align -- loc )
|
||||||
[ [ [ open-font ] dip string-width ] dip swap - ] dip
|
[ [ [ open-font ] dip string-width ] dip swap - ] dip
|
||||||
|
@ -188,25 +189,35 @@ M: table model-changed
|
||||||
: (select-row) ( table row -- )
|
: (select-row) ( table row -- )
|
||||||
over validate-row
|
over validate-row
|
||||||
[ [ thin-row-rect ] [ drop ] 2bi scroll>rect ]
|
[ [ thin-row-rect ] [ drop ] 2bi scroll>rect ]
|
||||||
[ [ >>selected-index ] [ >>mouse-index ] bi relayout-1 ]
|
[ >>selected-index relayout-1 ]
|
||||||
2bi ;
|
2bi ;
|
||||||
|
|
||||||
: mouse-row ( table -- n )
|
: mouse-row ( table -- n )
|
||||||
[ hand-rel second ] keep y>row ;
|
[ hand-rel second ] keep y>row ;
|
||||||
|
|
||||||
: click-row ( table -- )
|
: table-button-down ( table -- )
|
||||||
dup request-focus
|
dup request-focus
|
||||||
dup control-value empty?
|
dup control-value empty? [ drop ] [
|
||||||
[ drop ] [ dup mouse-row (select-row) ] if ;
|
dup [ mouse-row ] keep validate-row
|
||||||
|
[ >>mouse-index ] [ (select-row) ] bi
|
||||||
|
] if ;
|
||||||
|
|
||||||
|
: row-action ( table -- )
|
||||||
|
dup selected-row dup
|
||||||
|
[ swap action>> call ] [ 2drop ] if ;
|
||||||
|
|
||||||
|
: table-button-up ( table -- )
|
||||||
|
hand-click# get 2 =
|
||||||
|
[ row-action ] [ update-selected-value ] if ;
|
||||||
|
|
||||||
: select-row ( table row -- )
|
: select-row ( table row -- )
|
||||||
[ (select-row) ] [ drop update-selected-value ] 2bi ;
|
[ (select-row) ] [ drop update-selected-value ] 2bi ;
|
||||||
|
|
||||||
: prev-row ( table -- )
|
: prev-row ( table -- )
|
||||||
dup selected-index>> 1- select-row ;
|
dup selected-index>> [ 1- ] [ 0 ] if* select-row ;
|
||||||
|
|
||||||
: next-row ( table -- )
|
: next-row ( table -- )
|
||||||
dup selected-index>> 1+ select-row ;
|
dup selected-index>> [ 1+ ] [ 0 ] if* select-row ;
|
||||||
|
|
||||||
: first-row ( table -- )
|
: first-row ( table -- )
|
||||||
0 select-row ;
|
0 select-row ;
|
||||||
|
@ -231,11 +242,12 @@ table H{
|
||||||
{ T{ mouse-enter } [ show-mouse-help ] }
|
{ T{ mouse-enter } [ show-mouse-help ] }
|
||||||
{ T{ mouse-leave } [ hide-mouse-help ] }
|
{ T{ mouse-leave } [ hide-mouse-help ] }
|
||||||
{ T{ motion } [ show-mouse-help ] }
|
{ T{ motion } [ show-mouse-help ] }
|
||||||
{ T{ button-down } [ click-row ] }
|
{ T{ button-down } [ table-button-down ] }
|
||||||
{ T{ button-up } [ update-selected-value ] }
|
{ T{ button-up } [ table-button-up ] }
|
||||||
{ T{ gain-focus } [ t >>focused? drop ] }
|
{ T{ gain-focus } [ t >>focused? drop ] }
|
||||||
{ T{ lose-focus } [ f >>focused? drop ] }
|
{ T{ lose-focus } [ f >>focused? drop ] }
|
||||||
{ T{ drag } [ click-row ] }
|
{ T{ drag } [ table-button-down ] }
|
||||||
|
{ T{ key-down f f "ENTER" } [ row-action ] }
|
||||||
{ T{ key-down f f "UP" } [ prev-row ] }
|
{ T{ key-down f f "UP" } [ prev-row ] }
|
||||||
{ T{ key-down f f "DOWN" } [ next-row ] }
|
{ T{ key-down f f "DOWN" } [ next-row ] }
|
||||||
{ T{ key-down f f "HOME" } [ first-row ] }
|
{ T{ key-down f f "HOME" } [ first-row ] }
|
||||||
|
|
Loading…
Reference in New Issue