]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/tools/inspector/inspector.factor
Change tabular-output and smash-pane behavior to fix panes unit tests; re-organize...
[factor.git] / basis / ui / tools / inspector / inspector.factor
1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors inspector namespaces kernel models fry
4 colors.constants models.arrow prettyprint sequences mirrors assocs
5 classes io io.styles arrays hashtables math.order sorting refs fonts
6 ui.tools.browser ui.commands ui.operations ui.gadgets ui.gadgets.panes
7 ui.gadgets.scrollers ui.gadgets.slots ui.gadgets.tracks ui.gestures
8 ui.gadgets.buttons ui.gadgets.tables ui.gadgets.status-bar
9 ui.gadgets.labeled ui.tools.common ui ;
10 IN: ui.tools.inspector
11
12 TUPLE: inspector-gadget < tool table ;
13
14 TUPLE: slot-description key key-string value value-string ;
15
16 : <slot-description> ( key value -- slot-description )
17     [ dup unparse-short ] bi@ slot-description boa ;
18
19 SINGLETON: inspector-renderer
20
21 M: inspector-renderer row-columns
22     drop [ key-string>> ] [ value-string>> ] bi 2array ;
23
24 M: inspector-renderer row-value
25     drop value>> ;
26
27 M: inspector-renderer column-titles
28     drop { "Key" "Value" } ;
29
30 : <summary-gadget> ( model -- gadget )
31     [
32         standard-table-style [
33             [
34                 [
35                     [ "Class:" write ] with-cell
36                     [ class pprint ] with-cell
37                 ] with-row
38             ]
39             [
40                 [
41                     [ "Object:" write ] with-cell
42                     [ pprint-short ] with-cell
43                 ] with-row
44             ]
45             [
46                 [
47                     [ "Summary:" write ] with-cell
48                     [ print-summary ] with-cell
49                 ] with-row
50             ] tri
51         ] tabular-output
52     ] <pane-control> ;
53
54 GENERIC: make-slot-descriptions ( obj -- seq )
55
56 M: object make-slot-descriptions
57     make-mirror [ <slot-description> ] { } assoc>map ;
58
59 M: hashtable make-slot-descriptions
60     call-next-method [ [ key-string>> ] compare ] sort ;
61
62 : <inspector-table> ( model -- table )
63     [ make-slot-descriptions ] <arrow> inspector-renderer <table>
64         [ invoke-primary-operation ] >>action
65         monospace-font >>font
66         COLOR: dark-gray >>column-line-color
67         6 >>gap
68         15 >>min-rows
69         15 >>max-rows
70         40 >>min-cols
71         40 >>max-cols ;
72
73 : <inspector-gadget> ( model -- gadget )
74     vertical inspector-gadget new-track
75         { 3 3 } >>gap
76         add-toolbar
77         swap >>model
78         dup model>> <inspector-table> >>table
79         dup model>> <summary-gadget> "Object" <labeled-gadget> f track-add
80         dup table>> <scroller> "Contents" <labeled-gadget> 1 track-add ;
81
82 M: inspector-gadget focusable-child*
83     table>> ;
84
85 : com-refresh ( inspector -- )
86     model>> notify-connections ;
87
88 : com-push ( inspector -- obj )
89     control-value ;
90
91 \ com-push H{ { +listener+ t } } define-command
92
93 : slot-editor-window ( close-hook update-hook assoc key key-string -- )
94     [ <value-ref> <slot-editor> ] [ "Slot editor: " prepend ] bi*
95     open-window ;
96
97 : com-edit-slot ( inspector -- )
98     [ close-window ] swap
99     [ '[ _ com-refresh ] ]
100     [ control-value make-mirror ]
101     [ table>> (selected-row) ] tri [
102         [ key>> ] [ key-string>> ] bi
103         slot-editor-window
104     ] [ 2drop 2drop ] if ;
105
106 : inspector-help ( -- ) "ui-inspector" com-browse ;
107
108 \ inspector-help H{ { +nullary+ t } } define-command
109
110 inspector-gadget "toolbar" f {
111     { T{ update-object } com-refresh }
112     { T{ key-down f f "p" } com-push }
113     { T{ key-down f f "e" } com-edit-slot }
114     { T{ key-down f f "F1" } inspector-help }
115 } define-command-map
116
117 inspector-gadget "multi-touch" f {
118     { up-action com-refresh }
119 } define-command-map
120
121 : inspect-model ( model -- )
122     <inspector-gadget> "Inspector" open-status-window ;
123
124 : inspector ( obj -- )
125     <model> inspect-model ;