1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Building the view tree.
use std::time::Duration;
use super::flex::{Axis, Flex};
use super::idle::{IdleScope, IdleWatch};
use super::mapped::Mapped;
use super::{Align, Container, Key, Length, Node, Widget};
use crate::env::Env;
use crate::geometry::{Padding, Size};
/// Collects the nodes of one container while an application's `view` runs.
pub struct View<'a, Msg> {
nodes: &'a mut Vec<Node<Msg>>,
env: &'a Env,
size: Size,
idle: &'a IdleScope<Msg>,
}
impl<'a, Msg: 'static> View<'a, Msg> {
pub(crate) fn new(nodes: &'a mut Vec<Node<Msg>>, env: &'a Env, size: Size, idle: &'a IdleScope<Msg>) -> Self {
Self { nodes, env, size, idle }
}
/// A builder for the children of a container inside this one: the same environment and size.
/// The idleness this view reads and declares watches in, for builders that make views of
/// their own.
pub(crate) fn idle_scope(&self) -> &'a IdleScope<Msg> {
self.idle
}
pub(crate) fn nested<'b>(&self, nodes: &'b mut Vec<Node<Msg>>) -> View<'b, Msg>
where
'a: 'b,
{
View::new(nodes, self.env, self.size, self.idle)
}
/// The environment: theme, icons, language and keymap.
#[must_use]
pub fn env(&self) -> &Env {
self.env
}
/// The room the application is drawing into: the whole terminal, in columns and rows.
///
/// This is the value for an application's own layout decision, such as "below 48 columns,
/// fold the three columns into one": `if ui.size().width < 48 { .. } else { .. }` in `view`.
///
/// The application's view fills the screen, so at the top of `view` this is exactly the
/// area it lays out. Every nested builder reports the same value: the children of `column`,
/// `row`, `stack`, `page` and `add_with`, the parts of an `AppShell`, `SidePanel`,
/// `Splitter` or `Popover`, the content of a `Modal` or other layer. The view is built
/// before layout divides the screen, so a container's own share is not known yet while its
/// children are being built; the number never pretends to be that share. A widget that
/// adapts to its own rectangle (a column that shortens its labels) does so in `measure` and
/// `paint`, which receive it.
///
/// Reading it performs no I/O: it is the size of the frame the framework is about to draw,
/// which it already holds. After a terminal resize the next frame reports the new size, and
/// [`Harness::resize`](crate::runtime::Harness::resize) does the same in tests.
#[must_use]
pub fn size(&self) -> Size {
self.size
}
/// How long no input has reached this terminal: the time since the last key, mouse event or
/// paste the runtime received, or since the application started when none came yet.
///
/// Everything the user does in this terminal counts: a key going down, repeating or coming
/// up, a mouse button, the wheel, the pointer moving over the window, a paste, and the end of
/// a [`Handoff`](crate::runtime::Handoff), because the program that had the terminal was
/// being used meanwhile. A terminal resize does not count: a window manager or a monitor
/// change resizes a window nobody is sitting at. Messages, background work and timers do not
/// count either; they are the application, not the user. Other programs and other terminals
/// are out of reach: this is idleness *here*, not idleness of the machine.
///
/// Reading the value keeps it current on screen: while `view` reads it, the runtime draws
/// again each time it passes a whole second, and stops once `view` no longer reads it. A
/// view that shows minutes therefore redraws once a second while it shows them; one that
/// only needs to act after a silence uses [`View::on_idle`], which wakes the application
/// once, at that moment, without drawing in between.
///
/// [`Harness::advance`](crate::runtime::Harness::advance) moves it forward in tests, and
/// every simulated input starts it again from zero.
#[must_use]
pub fn idle_for(&self) -> Duration {
self.idle.read.set(true);
self.idle.silent
}
/// Tells the application when no input has arrived for `after`, and when input comes back.
///
/// `message(true)` is delivered once, at the moment the silence reaches `after`: the runtime
/// wakes for it even when nothing else happens, and does not draw in between. The first
/// input afterwards delivers `message(false)`, before that input reaches any widget, and
/// starts the next wait. What counts as input is listed at [`View::idle_for`].
///
/// ```
/// use std::time::Duration;
///
/// use qframe::prelude::*;
///
/// #[derive(Default)]
/// struct Focus {
/// away: bool,
/// }
///
/// impl App for Focus {
/// type Msg = bool;
/// fn update(&mut self, away: bool) -> Command<bool> {
/// self.away = away;
/// Command::none()
/// }
/// fn view(&self, ui: &mut View<'_, bool>) {
/// ui.on_idle(Duration::from_secs(300), |away| away);
/// ui.add(Text::new(if self.away { "away" } else { "working" }));
/// }
/// }
///
/// let mut app = Harness::new(Focus::default(), 20, 1);
/// app.advance(Duration::from_secs(299));
/// assert!(app.screen().contains("working"));
/// app.advance(Duration::from_secs(1));
/// assert!(app.screen().contains("away"));
/// app.press("x");
/// assert!(app.screen().contains("working"));
/// ```
///
/// Declare the watch in every frame it should stay active, like a widget: the runtime
/// answers the watches of the latest frame. One that is no longer declared is not told the
/// silence ended. A watch declared when the silence has already lasted `after` is told at
/// once. Watches with different `after` are independent, so an application can dim the
/// screen after one minute and pause a timer after five.
pub fn on_idle(&mut self, after: Duration, message: impl Fn(bool) -> Msg + 'static) {
self.idle.watches.borrow_mut().push(IdleWatch { after, message: Box::new(message) });
}
/// Adds a widget.
pub fn add<W: Widget<Msg>>(&mut self, widget: W) -> NodeMut<'_, Msg> {
let index = self.nodes.len();
self.nodes.push(Node::new(widget, index));
NodeMut { node: self.nodes.last_mut().expect("a node was just pushed") }
}
/// Adds a widget that contains other widgets, built by `build`.
pub fn add_with<W: Container<Msg>>(
&mut self,
mut widget: W,
build: impl FnOnce(&mut View<'_, Msg>),
) -> NodeMut<'_, Msg> {
let mut children = Vec::new();
build(&mut self.nested(&mut children));
widget.set_children(children);
self.add(widget)
}
/// Adds a column whose children are built by `build`.
pub fn column(&mut self, build: impl FnOnce(&mut View<'_, Msg>)) -> NodeMut<'_, Msg> {
self.container(Axis::Column, build)
}
/// Adds a row whose children are built by `build`.
pub fn row(&mut self, build: impl FnOnce(&mut View<'_, Msg>)) -> NodeMut<'_, Msg> {
self.container(Axis::Row, build)
}
/// Adds a stack: children are drawn on top of each other in the same area, later ones on top.
pub fn stack(&mut self, build: impl FnOnce(&mut View<'_, Msg>)) -> NodeMut<'_, Msg> {
self.container(Axis::Stack, build)
}
/// Adds a column that remembers its widgets' state (focus, scroll, cursors) while it is not
/// shown. Give every page of a router its own `key`.
pub fn page(&mut self, key: impl Into<String>, build: impl FnOnce(&mut View<'_, Msg>)) -> NodeMut<'_, Msg> {
let node = self.container(Axis::Column, build);
node.node.persistent = true;
node.node.key = Key::Named(key.into());
node.fill()
}
/// Adds a column whose children are built by `build` with messages of their own type
/// `Inner`, each converted by `map` on its way to the application. A screen with its own
/// messages writes its view for them, and the application places it in one line:
///
/// ```
/// use qframe::prelude::*;
///
/// mod search {
/// use qframe::prelude::*;
///
/// #[derive(Clone)]
/// pub enum Msg {
/// Run,
/// }
///
/// pub fn view(ui: &mut View<'_, Msg>) {
/// ui.add(Button::new("Search").on_press(Msg::Run));
/// }
/// }
///
/// enum Msg {
/// Search(search::Msg),
/// }
///
/// fn view(ui: &mut View<'_, Msg>) {
/// ui.map(Msg::Search, search::view).fill();
/// }
/// ```
///
/// Everything the screen does inside arrives converted: the messages of its widgets and
/// handlers, the children of [`add_with`](Self::add_with) and nested containers, layers such
/// as a `Modal` and the widgets in them, overlays such as an open dropdown. Focus, memory and
/// ids work as for any column; [`Command::map`](crate::runtime::Command::map) converts the
/// commands the screen's `update` returns the same way.
pub fn map<Inner: 'static>(
&mut self,
map: impl Fn(Inner) -> Msg + 'static,
build: impl FnOnce(&mut View<'_, Inner>),
) -> NodeMut<'_, Msg> {
let map = std::rc::Rc::new(map);
let mut children = Vec::new();
// The screen reads and watches the same silence as the application; what it read and the
// watches it declared are handed up, their messages converted like any other.
let idle = IdleScope::new(self.idle.silent);
build(&mut View::new(&mut children, self.env, self.size, &idle));
if idle.read.get() {
self.idle.read.set(true);
}
for watch in idle.watches.into_inner() {
let map = std::rc::Rc::clone(&map);
let message = watch.message;
self.idle
.watches
.borrow_mut()
.push(IdleWatch { after: watch.after, message: Box::new(move |away| map(message(away))) });
}
self.add(Mapped::new(children, move |inner| map(inner)))
}
/// Adds empty space that takes the room left in a row or column.
pub fn spacer(&mut self) -> NodeMut<'_, Msg> {
self.container(Axis::Stack, |_| {}).fill()
}
fn container(&mut self, axis: Axis, build: impl FnOnce(&mut View<'_, Msg>)) -> NodeMut<'_, Msg> {
let mut children = Vec::new();
build(&mut self.nested(&mut children));
self.add(Flex::new(axis, children))
}
}
/// Adjusts the node just added. Every method changes the node in place, so the result can be
/// ignored or chained.
pub struct NodeMut<'a, Msg> {
node: &'a mut Node<Msg>,
}
impl<'a, Msg> NodeMut<'a, Msg> {
/// Names the node. Name widgets whose position among their siblings can change (list rows,
/// optional widgets) so their state and focus follow them.
pub fn id(self, name: impl Into<String>) -> Self {
self.node.key = Key::Named(name.into());
self
}
/// Sets the width.
pub fn width(self, width: Length) -> Self {
self.node.layout.width = width;
self
}
/// Sets the height.
pub fn height(self, height: Length) -> Self {
self.node.layout.height = height;
self
}
/// Takes all space left in both directions.
pub fn fill(self) -> Self {
self.width(Length::Fill(1)).height(Length::Fill(1))
}
/// Takes all width left.
pub fn fill_width(self) -> Self {
self.width(Length::Fill(1))
}
/// Takes all height left.
pub fn fill_height(self) -> Self {
self.height(Length::Fill(1))
}
/// Keeps `padding` free inside the node.
pub fn padding(self, padding: Padding) -> Self {
self.node.layout.padding = padding;
self
}
/// Leaves `cells` between the children of a row or column.
pub fn gap(self, cells: u16) -> Self {
self.node.layout.gap = cells;
self
}
/// Places children along the main axis of a row or column (both axes of a stack).
pub fn justify(self, align: Align) -> Self {
self.node.layout.justify = align;
self
}
/// Whether a mouse drag may select text in this node. Nothing is selectable unless asked:
/// `true` makes the node a selection region, so a drag that starts inside it selects text
/// within the node only (widgets such as `CodeView` and `Markdown` are regions by
/// themselves). `false` keeps selection out of the node and everything inside it, also out
/// of regions within it, e.g. for a secret shown inside a selectable log.
pub fn selectable(self, selectable: bool) -> Self {
self.node.selectable = Some(selectable);
self
}
/// Places children across the main axis of a row or column.
pub fn align(self, align: Align) -> Self {
self.node.layout.align = align;
self
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use crate::geometry::Size;
use crate::runtime::{App, Command, Harness};
use crate::widget::{Length, View};
use crate::widgets::{Modal, Text};
/// Records the size every builder of its view saw: the root, a nested column and a row with
/// a fixed width inside it, and the content of a modal layer.
#[derive(Default)]
struct Probe {
seen: RefCell<Vec<Size>>,
}
impl Probe {
fn take(&self) -> Vec<Size> {
std::mem::take(&mut *self.seen.borrow_mut())
}
}
impl App for Probe {
type Msg = ();
fn update(&mut self, (): ()) -> Command<()> {
Command::none()
}
fn view(&self, ui: &mut View<'_, ()>) {
self.seen.borrow_mut().push(ui.size());
ui.column(|ui| {
self.seen.borrow_mut().push(ui.size());
ui.row(|ui| {
self.seen.borrow_mut().push(ui.size());
ui.add(Text::new("probe"));
})
.width(Length::Cells(10));
});
ui.add_with(Modal::new(), |ui| {
self.seen.borrow_mut().push(ui.size());
ui.add(Text::new("layer"));
});
}
}
#[test]
fn every_builder_of_the_view_sees_the_terminal_size() {
let mut harness = Harness::new(Probe::default(), 83, 27);
let seen = harness.app().take();
assert!(seen.len() >= 4, "{seen:?}");
assert!(seen.iter().all(|size| *size == Size::new(83, 27)), "{seen:?}");
harness.resize(31, 9);
let seen = harness.app().take();
assert!(seen.len() >= 4, "{seen:?}");
assert!(seen.iter().all(|size| *size == Size::new(31, 9)), "{seen:?}");
harness.resize(0, 0);
let seen = harness.app().take();
assert!(seen.len() >= 4, "{seen:?}");
assert!(seen.iter().all(|size| *size == Size::new(0, 0)), "{seen:?}");
}
/// Three columns side by side from 48 columns up; below that the groups fold into a strip
/// above the list and the detail is left out.
struct Folding;
impl App for Folding {
type Msg = ();
fn update(&mut self, (): ()) -> Command<()> {
Command::none()
}
fn view(&self, ui: &mut View<'_, ()>) {
if ui.size().width < 48 {
ui.column(|ui| {
ui.add(Text::new("groups"));
ui.add(Text::new("items"));
})
.fill();
} else {
ui.row(|ui| {
ui.add(Text::new("groups"));
ui.add(Text::new("items"));
ui.add(Text::new("detail"));
})
.gap(2)
.fill();
}
}
}
#[test]
fn an_application_folds_its_layout_below_a_width() {
let mut harness = Harness::new(Folding, 120, 10);
assert_eq!(harness.screen().lines().next(), Some("groups items detail"), "{}", harness.screen());
harness.resize(40, 10);
let screen = harness.screen();
let lines: Vec<&str> = screen.lines().collect();
assert_eq!(lines.get(..2), Some(&["groups", "items"][..]), "{screen}");
assert!(!screen.contains("detail"), "{screen}");
harness.resize(120, 10);
assert_eq!(harness.screen().lines().next(), Some("groups items detail"), "{}", harness.screen());
}
}