factor/basis/ui/tools/inspector/inspector.factor

157 lines
4.7 KiB
Factor
Raw Normal View History

! 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 arrays assocs classes combinators fonts fry
hashtables inspector io io.styles kernel math.vectors mirrors
models models.arrow namespaces prettyprint refs sequences
sorting ui ui.commands ui.gadgets ui.gadgets.labeled
ui.gadgets.panes ui.gadgets.scrollers ui.gadgets.slots
ui.gadgets.status-bar ui.gadgets.tables
ui.gadgets.tables.private ui.gadgets.toolbar ui.gadgets.tracks
ui.gadgets.worlds ui.gestures ui.operations ui.theme
ui.tools.browser ui.tools.common ;
2007-09-20 18:09:08 -04:00
IN: ui.tools.inspector
TUPLE: inspector-gadget < tool table ;
2007-09-20 18:09:08 -04:00
2009-01-07 00:30:08 -05:00
TUPLE: slot-description key key-string value value-string ;
: <slot-description> ( key value -- slot-description )
[ dup unparse-short ] bi@ slot-description boa ;
2009-01-06 17:53:08 -05:00
SINGLETON: inspector-renderer
2007-09-20 18:09:08 -04:00
2009-01-06 17:53:08 -05:00
M: inspector-renderer row-columns
2009-01-07 00:30:08 -05:00
drop [ key-string>> ] [ value-string>> ] bi 2array ;
2009-01-06 17:53:08 -05:00
M: inspector-renderer row-value
drop value>> ;
M: inspector-renderer column-titles
drop { "Key" "Value" } ;
2009-01-06 17:53:08 -05:00
: <summary-gadget> ( model -- gadget )
[
standard-table-style [
{
2009-01-06 17:53:08 -05:00
[
[
[ "Class:" write ] with-cell
[ class-of pprint ] with-cell
] with-row
]
2009-01-06 17:53:08 -05:00
[
[
[ "Object:" write ] with-cell
[ pprint-short ] with-cell
] with-row
]
2009-01-06 17:53:08 -05:00
[
[
[ "Summary:" write ] with-cell
[ print-summary ] with-cell
] with-row
]
[
content-gadget [
[
[ "Content:" write ] with-cell
[ output-stream get write-gadget ] with-cell
] with-row
] when*
]
} cleave
2009-01-06 17:53:08 -05:00
] tabular-output
] <pane-control> ;
GENERIC: make-slot-descriptions ( obj -- seq )
M: object make-slot-descriptions
2009-01-07 00:30:08 -05:00
make-mirror [ <slot-description> ] { } assoc>map ;
2009-01-06 17:53:08 -05:00
M: hashtable make-slot-descriptions
call-next-method [ key-string>> ] sort-with ;
TUPLE: inspector-table < table ;
! Improve performance for big arrays or large hashtables by
! only calculating column width for the longest key.
M: inspector-table compute-column-widths
dup rows>> [ drop 0 { } ] [
[ drop gap>> ]
[ initial-widths ]
[ keys longest "" 2array row-column-widths ] 2tri
vmax [ compute-total-width ] keep
] if-empty ;
2009-01-06 17:53:08 -05:00
: <inspector-table> ( model -- table )
[ make-slot-descriptions ] <arrow> inspector-renderer
inspector-table new-table
2009-02-17 09:26:33 -05:00
[ invoke-primary-operation ] >>action
monospace-font >>font
2016-05-04 15:57:04 -04:00
line-color >>column-line-color
2009-02-17 09:26:33 -05:00
6 >>gap
15 >>min-rows
15 >>max-rows
40 >>min-cols
40 >>max-cols ;
2009-01-06 17:53:08 -05:00
: <inspector-gadget> ( model -- gadget )
2015-07-21 15:11:42 -04:00
vertical inspector-gadget new-track with-lines
2008-11-20 22:58:30 -05:00
add-toolbar
swap >>model
2009-01-06 17:53:08 -05:00
dup model>> <inspector-table> >>table
dup model>> <summary-gadget> margins white-interior "Object" object-color <labeled> f track-add
2015-07-25 05:53:00 -04:00
dup table>> <scroller> margins white-interior "Contents" contents-color <labeled> 1 track-add ;
2009-01-06 17:53:08 -05:00
M: inspector-gadget focusable-child*
table>> ;
2007-09-20 18:09:08 -04:00
2009-01-06 17:53:08 -05:00
: com-refresh ( inspector -- )
model>> notify-connections ;
2007-09-20 18:09:08 -04:00
2009-01-06 17:53:08 -05:00
: com-push ( inspector -- obj )
control-value ;
\ com-push H{ { +listener+ t } } define-command
2007-09-20 18:09:08 -04:00
: slot-editor-window ( close-hook update-hook assoc key key-string -- )
[ <value-ref> <slot-editor> ]
[
<world-attributes>
swap "Slot editor: " prepend >>title
[ { dialog-window } append ] change-window-controls
] bi*
2009-09-23 23:51:25 -04:00
open-status-window ;
: com-edit-slot ( inspector -- )
[ close-window ] swap
[ '[ _ com-refresh ] ]
[ control-value make-mirror ]
[ table>> (selected-row) ] tri [
[ key>> ] [ key-string>> ] bi
slot-editor-window
2012-10-23 15:21:30 -04:00
] [ 4drop ] if ;
2009-02-18 22:01:19 -05:00
: inspector-help ( -- ) "ui-inspector" com-browse ;
2007-09-20 18:09:08 -04:00
\ inspector-help H{ { +nullary+ t } } define-command
inspector-gadget "toolbar" f {
{ T{ key-down f f "r" } com-refresh }
2009-01-06 17:53:08 -05:00
{ T{ key-down f f "p" } com-push }
{ T{ key-down f f "e" } com-edit-slot }
2007-09-20 18:09:08 -04:00
{ T{ key-down f f "F1" } inspector-help }
} define-command-map
inspector-gadget "multi-touch" f {
{ up-action com-refresh }
} define-command-map
: inspect-model ( model -- )
<inspector-gadget> "Inspector" open-status-window ;
2009-01-07 00:30:08 -05:00
: inspector ( obj -- )
<model> inspect-model ;
inspector-gadget { 550 400 } set-tool-dim