1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes combinators fonts fry
4 hashtables inspector io io.styles kernel math.vectors mirrors
5 models models.arrow namespaces prettyprint refs sequences
6 sorting ui ui.commands ui.gadgets ui.gadgets.labeled
7 ui.gadgets.panes ui.gadgets.scrollers ui.gadgets.slots
8 ui.gadgets.status-bar ui.gadgets.tables
9 ui.gadgets.tables.private ui.gadgets.toolbar ui.gadgets.tracks
10 ui.gadgets.worlds ui.gestures ui.operations ui.theme
11 ui.tools.browser ui.tools.common ;
12 IN: ui.tools.inspector
14 TUPLE: inspector-gadget < tool table ;
16 TUPLE: slot-description key key-string value value-string ;
18 : <slot-description> ( key value -- slot-description )
19 [ dup unparse-short ] bi@ slot-description boa ;
21 SINGLETON: inspector-renderer
23 M: inspector-renderer row-columns
24 drop [ key-string>> ] [ value-string>> ] bi 2array ;
26 M: inspector-renderer row-value
29 M: inspector-renderer column-titles
30 drop { "Key" "Value" } ;
32 : <summary-gadget> ( model -- gadget )
34 standard-table-style [
38 [ "Class:" write ] with-cell
39 [ class-of pprint ] with-cell
44 [ "Object:" write ] with-cell
45 [ pprint-short ] with-cell
50 [ "Summary:" write ] with-cell
51 [ print-summary ] with-cell
57 [ "Content:" write ] with-cell
58 [ output-stream get write-gadget ] with-cell
66 GENERIC: make-slot-descriptions ( obj -- seq )
68 M: object make-slot-descriptions
69 make-mirror [ <slot-description> ] { } assoc>map ;
71 M: hashtable make-slot-descriptions
72 call-next-method [ key-string>> ] sort-with ;
74 TUPLE: inspector-table < table ;
76 ! Improve performance for big arrays or large hashtables by
77 ! only calculating column width for the longest key.
78 M: inspector-table compute-column-widths
79 dup rows>> [ drop 0 { } ] [
82 [ keys longest "" 2array row-column-widths ] 2tri
83 vmax [ compute-total-width ] keep
86 : <inspector-table> ( model -- table )
87 [ make-slot-descriptions ] <arrow> inspector-renderer
88 inspector-table new-table
89 [ invoke-primary-operation ] >>action
91 line-color >>column-line-color
98 : <inspector-gadget> ( model -- gadget )
99 vertical inspector-gadget new-track with-lines
102 dup model>> <inspector-table> >>table
103 dup model>> <summary-gadget> margins white-interior "Object" object-color <labeled> f track-add
104 dup table>> <scroller> margins white-interior "Contents" contents-color <labeled> 1 track-add ;
106 M: inspector-gadget focusable-child*
109 : com-refresh ( inspector -- )
110 model>> notify-connections ;
112 : com-push ( inspector -- obj )
115 \ com-push H{ { +listener+ t } } define-command
117 : slot-editor-window ( close-hook update-hook assoc key key-string -- )
118 [ <value-ref> <slot-editor> ]
121 swap "Slot editor: " prepend >>title
122 [ { dialog-window } append ] change-window-controls
126 : com-edit-slot ( inspector -- )
127 [ close-window ] swap
128 [ '[ _ com-refresh ] ]
129 [ control-value make-mirror ]
130 [ table>> (selected-row) ] tri [
131 [ key>> ] [ key-string>> ] bi
135 : inspector-help ( -- ) "ui-inspector" com-browse ;
137 \ inspector-help H{ { +nullary+ t } } define-command
139 inspector-gadget "toolbar" f {
140 { T{ key-down f f "r" } com-refresh }
141 { T{ key-down f f "p" } com-push }
142 { T{ key-down f f "e" } com-edit-slot }
143 { T{ key-down f f "F1" } inspector-help }
146 inspector-gadget "multi-touch" f {
147 { up-action com-refresh }
150 : inspect-model ( model -- )
151 <inspector-gadget> "Inspector" open-status-window ;
153 : inspector ( obj -- )
154 <model> inspect-model ;
156 inspector-gadget { 550 400 } set-tool-dim