1 USING: help.syntax help.markup kernel math classes tuples ;
5 { $class-description "A mutable cell holding a single value. When the value is changed, a sequence of connected objects are notified. Models have the following slots:"
7 { { $link model-value } " - the value of the model. Use " { $link set-model } " to change the value." }
8 { { $link model-connections } " - a sequence of objects implementing the " { $link model-changed } " generic word, to be notified when the model's value changes." }
9 { { $link model-dependencies } " - a sequence of models which should have this model added to their sequence of connections when activated." }
10 { { $link model-ref } " - a reference count tracking the number of models which depend on this one." }
12 "Other classes may delegate to " { $link model } "."
16 { $values { "value" object } { "model" "a new " { $link model } } }
17 { $description "Creates a new model with an initial value." } ;
20 { $values { "dep" model } { "model" model } }
21 { $description "Registers a dependency. When " { $snippet "model" } " is activated, it will be added to " { $snippet "dep" } "'s connections and notified when " { $snippet "dep" } " changes." }
22 { $notes "This word should not be called directly unless you are implementing your own model class." } ;
24 { add-dependency remove-dependency activate-model deactivate-model } related-words
26 HELP: remove-dependency
27 { $values { "dep" model } { "model" model } }
28 { $description "Unregisters a dependency." }
29 { $notes "This word should not be called directly unless you are implementing your own model class." } ;
32 { $values { "model" model } }
33 { $contract "Called after a model has been activated." } ;
35 { model-activated activate-model deactivate-model } related-words
38 { $values { "model" model } }
39 { $description "Increments the reference count of the model. If it was previously zero, this model is added as a connection to all models registered as dependencies by " { $link add-dependency } "." }
40 { $warning "Calls to " { $link activate-model } " and " { $link deactivate-model } " should be balanced to keep the reference counting consistent, otherwise " { $link model-changed } " might be called at the wrong time or not at all." } ;
42 HELP: deactivate-model
43 { $values { "model" model } }
44 { $description "Decrements the reference count of the model. If it reaches zero, this model is removed as a connection from all models registered as dependencies by " { $link add-dependency } "." }
45 { $warning "Calls to " { $link activate-model } " and " { $link deactivate-model } " should be balanced to keep the reference counting consistent, otherwise " { $link model-changed } " might be called at the wrong time or not at all." } ;
48 { $values { "observer" object } }
49 { $contract "Called to notify observers of a model that the model value has changed as a result of a call to " { $link set-model } ". Observers can be registered with " { $link add-connection } "." } ;
51 { add-connection remove-connection model-changed } related-words
54 { $values { "observer" object } { "model" model } }
55 { $contract "Registers an object interested in being notified of changes to the model's value. When the value is changed as a result of a call to " { $link set-model } ", the " { $link model-changed } " word is called on the observer." } ;
57 HELP: remove-connection
58 { $values { "observer" object } { "model" model } }
59 { $contract "Unregisters an object no longer interested in being notified of changes to the model's value." } ;
62 { $values { "value" object } { "model" model } }
63 { $description "Changes the value of a model and calls " { $link model-changed } " on all observers registered with " { $link add-connection } "." } ;
65 { set-model set-model-value change-model (change-model) } related-words
67 HELP: set-model-value ( value model -- )
68 { $values { "value" object } { "model" model } }
69 { $description "Changes the value of a model without notifying any observers registered with " { $link add-connection } "." }
70 { $notes "There are very few reasons for user code to call this word. Instead, call " { $link set-model } ", which notifies observers." } ;
73 { $values { "model" model } { "quot" "a quotation with stack effect " { $snippet "( obj -- newobj )" } } }
74 { $description "Applies the quotation to the current value of the model to yield a new value, then changes the value of the model to the new value, and calls " { $link model-changed } " on all observers registered with " { $link add-connection } "." } ;
77 { $values { "model" model } { "quot" "a quotation with stack effect " { $snippet "( obj -- newobj )" } } }
78 { $description "Applies the quotation to the current value of the model to yield a new value, then changes the value of the model to the new value without notifying any observers registered with " { $link add-connection } "." }
79 { $notes "There are very few reasons for user code to call this word. Instead, call " { $link change-model } ", which notifies observers." } ;
82 { $class-description "Filter model values are computed by applying a quotation to the value of another model. Filters are automatically updated when the underlying model changes. Filters are constructed by " { $link <filter> } "." }
84 "The following code displays a label showing the result of applying " { $link sq } " to the value 5:"
86 "USING: models gadgets-labels gadgets-panes ;"
87 "5 <model> [ sq ] <filter> [ number>string ] <filter>"
88 "<label-control> gadget."
90 "An exercise for the reader is to keep the original model around on the stack, and change its value to 6, observing that the label will immediately display 36."
94 { $values { "model" model } { "quot" "a quotation with stack effect " { $snippet "( obj -- newobj )" } } { "filter" "a new " { $link filter } } }
95 { $description "Creates a new instance of " { $link filter } ". The value of the new filter model is computed by applying the quotation to the value." }
96 { $examples "See the example in the documentation for " { $link filter } "." } ;
99 { $class-description "Composed model values are computed by collecting the values from a sequence of underlying models into a new sequence. Composed models are automatically updated when underlying models change. Composed models are constructed by " { $link <compose> } "."
101 "A composed model whose children are all " { $link "models-range" } " conforms to the " { $link "range-model-protocol" } " and represents a point in n-dimensional space which is bounded by a rectangle." }
103 "The following code displays a pair of sliders, and an updating label showing their current values:"
105 "USING: models ui.gadgets.labels ui.gadgets.sliders ui.gadgets.panes ;"
106 ": <funny-slider> <x-slider> 100 over set-slider-max ;"
107 "<funny-slider> <funny-slider> 2array"
108 "dup make-pile gadget."
109 "dup [ control-model ] map <compose> [ unparse ] <filter>"
110 "<label-control> gadget."
115 { $values { "models" "a sequence of models" } { "compose" "a new " { $link compose } } }
116 { $description "Creates a new instance of " { $link compose } ". The value of the new compose model is obtained by mapping " { $link model-value } " over the given sequence of models." }
117 { $examples "See the example in the documentation for " { $link compose } "." } ;
120 { $class-description "History models record a timeline of previous values on calls to " { $link add-history } ", and can travel back and forth on the timeline with " { $link go-back } " and " { $link go-forward } ". History models are constructed by " { $link <history> } "." } ;
123 { $values { "value" object } { "history" "a new " { $link history } } }
124 { $description "Creates a new history model with an initial value." } ;
126 { <history> add-history go-back go-forward } related-words
129 { $values { "history" history } }
130 { $description "Restores the previous value and calls " { $link model-changed } " on all observers registered with " { $link add-connection } "." } ;
133 { $values { "history" history } }
134 { $description "Restores the value set prior to the last call to " { $link go-back } " and calls " { $link model-changed } " on all observers registered with " { $link add-connection } "." } ;
137 { $values { "history" history } }
138 { $description "Adds the current value to the history." } ;
141 { $class-description "Delay models have the same value as their underlying model, however the value only changes after a timer expires. If the underlying model's value changes again before the timer expires, the timer restarts. Delay models are constructed by " { $link <delay> } "." }
143 "The following code displays a sliders and a label which is updated half a second after the slider stops changing:"
145 "USING: models gadgets-labels gadgets-sliders gadgets-panes ;"
147 " 0 0 0 100 <range> <x-slider> 500 over set-slider-max ;"
148 "<funny-slider> dup gadget."
149 "control-model 500 <delay> [ number>string ] <filter>"
150 "<label-control> gadget."
155 { $values { "model" model } { "timeout" "a positive integer" } { "delay" delay } }
156 { $description "Creates a new instance of " { $link delay } ". A timer of " { $snippet "timeout" } " milliseconds must elapse from the time the underlying model last changed to when the delay model value is changed and its connections are notified." }
157 { $examples "See the example in the documentation for " { $link delay } "." } ;
160 { $values { "model" model } { "value" object } }
161 { $contract "Outputs the current value of a range model." } ;
163 HELP: range-page-value
164 { $values { "model" model } { "value" object } }
165 { $contract "Outputs the page size of a range model." } ;
167 HELP: range-min-value
168 { $values { "model" model } { "value" object } }
169 { $contract "Outputs the minimum value of a range model." } ;
171 HELP: range-max-value
172 { $values { "model" model } { "value" object } }
173 { $contract "Outputs the maximum value of a range model." } ;
175 HELP: range-max-value*
176 { $values { "model" model } { "value" object } }
177 { $contract "Outputs the slider position for a range model. Since the bottom of the slider cannot exceed the maximum value, this is equal to the maximum value minus the page size." } ;
179 HELP: set-range-value
180 { $values { "value" object } { "model" model } }
181 { $description "Sets the current value of a range model." }
182 { $side-effects "model" } ;
184 HELP: set-range-page-value
185 { $values { "value" object } { "model" model } }
186 { $description "Sets the page size of a range model." }
187 { $side-effects "model" } ;
189 HELP: set-range-min-value
190 { $values { "value" object } { "model" model } }
191 { $description "Sets the minimum value of a range model." }
192 { $side-effects "model" } ;
194 HELP: set-range-max-value
195 { $values { "value" object } { "model" model } }
196 { $description "Sets the maximum value of a range model." }
197 { $side-effects "model" } ;
200 { $class-description "Range models implement the " { $link "range-model-protocol" } " with real numbers as the minimum, current, maximum, and page size. Range models are created with " { $link <range> } "." }
201 { $notes { $link "ui.gadgets.sliders" } " use range models." } ;
204 { $values { "range" range } { "model" model } }
205 { $description "Outputs a model holding a range model's current value." }
206 { $notes "This word is not part of the " { $link "range-model-protocol" } ", and can only be used on direct instances of " { $link range } "." } ;
209 { $values { "range" range } { "model" model } }
210 { $description "Outputs a model holding a range model's minimum value." }
211 { $notes "This word is not part of the " { $link "range-model-protocol" } ", and can only be used on direct instances of " { $link range } "." } ;
214 { $values { "range" range } { "model" model } }
215 { $description "Outputs a model holding a range model's maximum value." }
216 { $notes "This word is not part of the " { $link "range-model-protocol" } ", and can only be used on direct instances of " { $link range } "." } ;
219 { $values { "range" range } { "model" model } }
220 { $description "Outputs a model holding a range model's page size." }
221 { $notes "This word is not part of the " { $link "range-model-protocol" } ", and can only be used on direct instances of " { $link range } "." } ;
224 { $values { "amount" real } { "range" range } }
225 { $description "Adds a number to a range model's current value." }
226 { $side-effects "range" } ;
229 { $values { "amount" real } { "range" range } }
230 { $description "Adds a multiple of the page size to a range model's current value." }
231 { $side-effects "range" } ;
233 ARTICLE: "models" "Models"
234 "The Factor UI provides basic support for dataflow programming via " { $emphasis "models" } " and " { $emphasis "controls" } ". A model is an observable value. Changing a model's value notifies other objects which depend on the model automatically, and models may depend on each other's values."
237 { $subsection <model> }
238 "Adding and removing connections:"
239 { $subsection add-connection }
240 { $subsection remove-connection }
241 "Generic word called on model connections when the model value changes:"
242 { $subsection model-changed }
243 "When using models which are not associated with controls (or when unit testing controls), you must activate and deactivate models manually:"
244 { $subsection activate-model }
245 { $subsection deactivate-model }
246 "Special types of models:"
247 { $subsection "models-filter" }
248 { $subsection "models-compose" }
249 { $subsection "models-history" }
250 { $subsection "models-delay" }
251 { $subsection "models-range" }
252 { $subsection "models-impl" } ;
254 ARTICLE: "models-filter" "Filter models"
255 "Filter model values are computed by applying a quotation to the value of another model."
256 { $subsection filter }
257 { $subsection <filter> } ;
259 ARTICLE: "models-compose" "Composed models"
260 "Composed model values are computed by collecting the values from a sequence of underlying models into a new sequence."
261 { $subsection compose }
262 { $subsection <compose> } ;
264 ARTICLE: "models-history" "History models"
265 "History models record previous values."
266 { $subsection history }
267 { $subsection <history> }
269 { $subsection add-history }
270 "Navigating the history:"
271 { $subsection go-back }
272 { $subsection go-forward } ;
274 ARTICLE: "models-delay" "Delay models"
275 "Delay models are used to implement delayed updating of gadgets in response to user input."
276 { $subsection delay }
277 { $subsection <delay> } ;
279 ARTICLE: "models-range" "Range models"
280 "Range models ensure their value is a real number within a fixed range."
281 { $subsection range }
282 { $subsection <range> }
283 "Range models conform to a protocol for getting and setting the current value, as well as the endpoints of the range."
284 { $subsection "range-model-protocol" } ;
286 ARTICLE: "range-model-protocol" "Range model protocol"
287 "The range model protocol is implemented by the " { $link range } " and " { $link compose } " classes. User-defined models may implement it too."
288 { $subsection range-value }
289 { $subsection range-page-value }
290 { $subsection range-min-value }
291 { $subsection range-max-value }
292 { $subsection range-max-value* }
293 { $subsection set-range-value }
294 { $subsection set-range-page-value }
295 { $subsection set-range-min-value }
296 { $subsection set-range-max-value } ;
298 ARTICLE: "models-impl" "Implementing models"
299 "New types of models can be defined, along the lines of " { $link filter } " and such."
301 "Models can execute hooks when activated:"
302 { $subsection model-activated }
303 "Models can override requests to change their value, for example to perform validation:"
304 { $subsection set-model } ;