ui.gadgets.tables: if model changes, try to preserve selection
parent
2e115dc5c3
commit
eb4981fb00
|
@ -1,6 +1,6 @@
|
||||||
IN: ui.gadgets.tables.tests
|
IN: ui.gadgets.tables.tests
|
||||||
USING: ui.gadgets.tables ui.gadgets.scrollers accessors
|
USING: ui.gadgets.tables ui.gadgets.scrollers ui.gadgets.debug accessors
|
||||||
models namespaces tools.test kernel ;
|
models namespaces tools.test kernel combinators ;
|
||||||
|
|
||||||
SINGLETON: test-renderer
|
SINGLETON: test-renderer
|
||||||
|
|
||||||
|
@ -8,15 +8,40 @@ M: test-renderer row-columns drop ;
|
||||||
|
|
||||||
M: test-renderer column-titles drop { "First" "Last" } ;
|
M: test-renderer column-titles drop { "First" "Last" } ;
|
||||||
|
|
||||||
[ ] [
|
: test-table ( -- table )
|
||||||
{
|
{
|
||||||
{ "Britney" "Spears" }
|
{ "Britney" "Spears" }
|
||||||
{ "Justin" "Timberlake" }
|
{ "Justin" "Timberlake" }
|
||||||
{ "Don" "Stewart" }
|
{ "Don" "Stewart" }
|
||||||
} <model> test-renderer <table>
|
} <model> test-renderer <table> ;
|
||||||
"table" set
|
|
||||||
|
[ ] [
|
||||||
|
test-table "table" set
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ ] [
|
[ ] [
|
||||||
"table" get <scroller> "scroller" set
|
"table" get <scroller> "scroller" set
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
[ { "Justin" "Timberlake" } { "Britney" "Spears" } ] [
|
||||||
|
test-table t >>selection-required? dup [
|
||||||
|
{
|
||||||
|
[ 1 select-row ]
|
||||||
|
[
|
||||||
|
model>> {
|
||||||
|
{ "Justin" "Timberlake" }
|
||||||
|
{ "Britney" "Spears" }
|
||||||
|
{ "Don" "Stewart" }
|
||||||
|
} swap set-model
|
||||||
|
]
|
||||||
|
[ selected-row drop ]
|
||||||
|
[
|
||||||
|
model>> {
|
||||||
|
{ "Britney" "Spears" }
|
||||||
|
{ "Don" "Stewart" }
|
||||||
|
} swap set-model
|
||||||
|
]
|
||||||
|
[ selected-row drop ]
|
||||||
|
} cleave
|
||||||
|
] with-grafted-gadget
|
||||||
] unit-test
|
] unit-test
|
|
@ -5,8 +5,8 @@ math.functions math.rectangles math.order math.vectors namespaces
|
||||||
opengl sequences ui.gadgets ui.gadgets.scrollers ui.gadgets.status-bar
|
opengl sequences ui.gadgets ui.gadgets.scrollers ui.gadgets.status-bar
|
||||||
ui.gadgets.worlds ui.gestures ui.render ui.pens.solid ui.text
|
ui.gadgets.worlds ui.gestures ui.render ui.pens.solid ui.text
|
||||||
ui.commands ui.images ui.gadgets.menus ui.gadgets.line-support
|
ui.commands ui.images ui.gadgets.menus ui.gadgets.line-support
|
||||||
math.rectangles models math.ranges sequences combinators fonts locals
|
math.rectangles models math.ranges sequences combinators
|
||||||
strings ;
|
combinators.short-circuit fonts locals strings ;
|
||||||
IN: ui.gadgets.tables
|
IN: ui.gadgets.tables
|
||||||
|
|
||||||
! Row rendererer protocol
|
! Row rendererer protocol
|
||||||
|
@ -246,9 +246,6 @@ PRIVATE>
|
||||||
: update-selected-value ( table -- )
|
: update-selected-value ( table -- )
|
||||||
[ selected-row drop ] [ selected-value>> ] bi set-model ;
|
[ selected-row drop ] [ selected-value>> ] bi set-model ;
|
||||||
|
|
||||||
: initial-selected-index ( model table -- n/f )
|
|
||||||
[ value>> length 1 >= ] [ selection-required?>> ] bi* and 0 f ? ;
|
|
||||||
|
|
||||||
: show-row-summary ( table n -- )
|
: show-row-summary ( table n -- )
|
||||||
over nth-row
|
over nth-row
|
||||||
[ swap [ renderer>> row-value ] keep show-summary ]
|
[ swap [ renderer>> row-value ] keep show-summary ]
|
||||||
|
@ -258,8 +255,28 @@ PRIVATE>
|
||||||
: hide-mouse-help ( table -- )
|
: hide-mouse-help ( table -- )
|
||||||
f >>mouse-index [ hide-status ] [ relayout-1 ] bi ;
|
f >>mouse-index [ hide-status ] [ relayout-1 ] bi ;
|
||||||
|
|
||||||
|
: find-row-index ( value table -- n/f )
|
||||||
|
[ model>> value>> ] [ renderer>> '[ _ row-value ] map index ] bi ;
|
||||||
|
|
||||||
|
: initial-selected-index ( table -- n/f )
|
||||||
|
{
|
||||||
|
[ model>> value>> empty? not ]
|
||||||
|
[ selection-required?>> ]
|
||||||
|
[ drop 0 ]
|
||||||
|
} 1&& ;
|
||||||
|
|
||||||
|
: (update-selected-index) ( table -- n/f )
|
||||||
|
[ selected-value>> value>> ] keep over
|
||||||
|
[ find-row-index ] [ 2drop f ] if ;
|
||||||
|
|
||||||
|
: update-selected-index ( table -- n/f )
|
||||||
|
{
|
||||||
|
[ (update-selected-index) ]
|
||||||
|
[ initial-selected-index ]
|
||||||
|
} 1|| ;
|
||||||
|
|
||||||
M: table model-changed
|
M: table model-changed
|
||||||
[ nip ] [ initial-selected-index ] 2bi {
|
nip dup update-selected-index {
|
||||||
[ >>selected-index f >>mouse-index drop ]
|
[ >>selected-index f >>mouse-index drop ]
|
||||||
[ show-row-summary ]
|
[ show-row-summary ]
|
||||||
[ drop update-selected-value ]
|
[ drop update-selected-value ]
|
||||||
|
@ -302,6 +319,8 @@ PRIVATE>
|
||||||
: table-button-up ( table -- )
|
: table-button-up ( table -- )
|
||||||
dup row-action? [ row-action ] [ update-selected-value ] if ;
|
dup row-action? [ row-action ] [ update-selected-value ] if ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
: select-row ( table n -- )
|
: select-row ( table n -- )
|
||||||
over validate-line
|
over validate-line
|
||||||
[ (select-row) ]
|
[ (select-row) ]
|
||||||
|
@ -309,6 +328,8 @@ PRIVATE>
|
||||||
[ show-row-summary ]
|
[ show-row-summary ]
|
||||||
2tri ;
|
2tri ;
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
: prev/next-row ( table n -- )
|
: prev/next-row ( table n -- )
|
||||||
[ dup selected-index>> ] dip '[ _ + ] [ 0 ] if* select-row ;
|
[ dup selected-index>> ] dip '[ _ + ] [ 0 ] if* select-row ;
|
||||||
|
|
||||||
|
@ -354,9 +375,9 @@ PRIVATE>
|
||||||
show-operations-menu
|
show-operations-menu
|
||||||
] [ drop ] if-mouse-row ;
|
] [ drop ] if-mouse-row ;
|
||||||
|
|
||||||
: focus-table ( table -- ) t >>focused? drop ;
|
: focus-table ( table -- ) t >>focused? relayout-1 ;
|
||||||
|
|
||||||
: unfocus-table ( table -- ) f >>focused? drop ;
|
: unfocus-table ( table -- ) f >>focused? relayout-1 ;
|
||||||
|
|
||||||
table "sundry" f {
|
table "sundry" f {
|
||||||
{ mouse-enter show-mouse-help }
|
{ mouse-enter show-mouse-help }
|
||||||
|
|
Loading…
Reference in New Issue