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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! A stack
use kas::layout::solve_size_rules;
use kas::prelude::*;
use std::collections::hash_map::{Entry, HashMap};
use std::fmt::Debug;
use std::ops::{Index, IndexMut};
/// A stack of boxed widgets
///
/// This is a parametrisation of [`Stack`].
pub type BoxStack<Data> = Stack<Box<dyn Widget<Data = Data>>>;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
enum State {
#[default]
None,
Configured,
Sized,
}
impl State {
fn is_configured(self) -> bool {
self != State::None
}
}
impl_scope! {
/// A stack of widgets
///
/// A stack consists a set of child widgets, "pages", all of equal size.
/// Only a single page is visible at a time. The page is "turned" by calling
/// [`Self::set_active`].
///
/// This may only be parametrised with a single widget type, thus usually
/// it will be necessary to box children (this is what [`BoxStack`] is).
///
/// By default, all pages are configured and sized. To avoid configuring
/// hidden pages (thus preventing these pages from affecting size)
/// call [`Self::set_size_limit`] or [`Self::with_size_limit`].
#[autoimpl(Default)]
#[derive(Clone, Debug)]
#[widget]
pub struct Stack<W: Widget> {
core: widget_core!(),
widgets: Vec<(W, State)>,
active: usize,
size_limit: usize,
next: usize,
id_map: HashMap<usize, usize>, // map key of WidgetId to index
}
impl Widget for Self {
type Data = W::Data;
fn for_child_node(
&mut self,
data: &W::Data,
index: usize,
closure: Box<dyn FnOnce(Node<'_>) + '_>,
) {
if let Some((w, _)) = self.widgets.get_mut(index) {
closure(w.as_node(data));
}
}
}
impl Layout for Self {
#[inline]
fn num_children(&self) -> usize {
self.widgets.len()
}
fn get_child(&self, index: usize) -> Option<&dyn Layout> {
self.widgets.get(index).map(|(w, _)| w.as_layout())
}
fn find_child_index(&self, id: &WidgetId) -> Option<usize> {
id.next_key_after(self.id_ref())
.and_then(|k| self.id_map.get(&k).cloned())
}
fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules {
let mut rules = SizeRules::EMPTY;
let mut index = 0;
let end = self.widgets.len().min(self.size_limit);
loop {
if index == end {
if self.active >= end {
index = self.active;
} else {
break rules;
}
} else if index > end {
break rules;
}
if let Some(entry) = self.widgets.get_mut(index) {
if entry.1.is_configured() {
rules = rules.max(entry.0.size_rules(sizer.re(), axis));
entry.1 = State::Sized;
}
}
index += 1;
}
}
fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect) {
self.core.rect = rect;
if let Some(entry) = self.widgets.get_mut(self.active) {
debug_assert_eq!(entry.1, State::Sized);
entry.0.set_rect(cx, rect);
}
}
fn nav_next(&self, _: bool, from: Option<usize>) -> Option<usize> {
match from {
None => Some(self.active),
Some(active) if active != self.active => Some(self.active),
_ => None,
}
}
fn find_id(&mut self, coord: Coord) -> Option<WidgetId> {
if let Some(entry) = self.widgets.get_mut(self.active) {
debug_assert_eq!(entry.1, State::Sized);
return entry.0.find_id(coord);
}
None
}
fn draw(&mut self, mut draw: DrawCx) {
if let Some(entry) = self.widgets.get_mut(self.active) {
debug_assert_eq!(entry.1, State::Sized);
draw.recurse(&mut entry.0);
}
}
}
impl Events for Self {
fn make_child_id(&mut self, index: usize) -> WidgetId {
if let Some((child, state)) = self.widgets.get(index) {
if state.is_configured() {
debug_assert!(child.id_ref().is_valid());
if let Some(key) = child.id_ref().next_key_after(self.id_ref()) {
debug_assert_eq!(self.id_map.get(&key), Some(&index));
} else {
debug_assert!(false);
}
return child.id();
}
// Use the widget's existing identifier, if any
if child.id_ref().is_valid() {
if let Some(key) = child.id_ref().next_key_after(self.id_ref()) {
self.id_map.insert(key, index);
return child.id();
}
}
}
loop {
let key = self.next;
self.next += 1;
if let Entry::Vacant(entry) = self.id_map.entry(key) {
entry.insert(index);
return self.id_ref().make_child(key);
}
}
}
fn configure(&mut self, _: &mut ConfigCx) {
self.id_map.clear();
}
fn configure_recurse(&mut self, cx: &mut ConfigCx, data: &Self::Data) {
let mut index = 0;
let end = self.widgets.len().min(self.size_limit);
loop {
if index == end {
if self.active >= end {
index = self.active;
} else {
break;
}
} else if index > end {
break;
}
let id = self.make_child_id(index);
if let Some(entry) = self.widgets.get_mut(index) {
cx.configure(entry.0.as_node(data), id);
if entry.1 == State::None {
entry.1 = State::Configured;
}
}
index += 1;
}
}
fn update_recurse(&mut self, cx: &mut ConfigCx, data: &Self::Data) {
if let Some((w, _)) = self.widgets.get_mut(self.active) {
cx.update(w.as_node(data));
}
}
}
impl Index<usize> for Self {
type Output = W;
fn index(&self, index: usize) -> &Self::Output {
&self.widgets[index].0
}
}
impl IndexMut<usize> for Self {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.widgets[index].0
}
}
}
impl<W: Widget> Stack<W> {
/// Construct a new, empty instance
///
/// See also [`Stack::from`].
pub fn new() -> Self {
Stack::default()
}
/// Limit the number of pages considered and sized
///
/// By default, this is `usize::MAX`: all pages are configured and affect
/// the stack's size requirements.
///
/// Set this to 0 to avoid configuring all hidden pages.
/// Set this to `n` to configure the active page *and* the first `n` pages.
pub fn set_size_limit(&mut self, limit: usize) {
self.size_limit = limit;
}
/// Limit the number of pages configured and sized (inline)
///
/// By default, this is `usize::MAX`: all pages are configured and affect
/// the stack's size requirements.
///
/// Set this to 0 to avoid configuring all hidden pages.
/// Set this to `n` to configure the active page *and* the first `n` pages.
pub fn with_size_limit(mut self, limit: usize) -> Self {
self.size_limit = limit;
self
}
/// Get the index of the active widget
#[inline]
pub fn active(&self) -> usize {
self.active
}
/// Set the active widget (inline)
///
/// Unlike [`Self::set_active`], this does not update anything; it is
/// assumed that sizing happens afterwards.
#[inline]
pub fn with_active(mut self, active: usize) -> Self {
self.active = active;
self
}
/// Set the active page
pub fn set_active(&mut self, cx: &mut ConfigCx, data: &W::Data, index: usize) {
let old_index = self.active;
if old_index == index {
return;
}
self.active = index;
let id = self.make_child_id(index);
if let Some(entry) = self.widgets.get_mut(index) {
let node = entry.0.as_node(data);
if entry.1 == State::None {
cx.configure(node, id);
entry.1 = State::Configured;
} else {
cx.update(node);
}
if entry.1 == State::Configured {
*cx |= Action::RESIZE;
} else {
debug_assert_eq!(entry.1, State::Sized);
entry.0.set_rect(cx, self.core.rect);
*cx |= Action::REGION_MOVED;
}
} else {
if old_index < self.widgets.len() {
*cx |= Action::REGION_MOVED;
}
}
}
/// Get a direct reference to the active child widget, if any
pub fn get_active(&self) -> Option<&W> {
if self.active < self.widgets.len() {
Some(&self.widgets[self.active].0)
} else {
None
}
}
/// True if there are no pages
pub fn is_empty(&self) -> bool {
self.widgets.is_empty()
}
/// Returns the number of pages
pub fn len(&self) -> usize {
self.widgets.len()
}
/// Remove all pages
///
/// This does not change the activen page index.
pub fn clear(&mut self) {
self.widgets.clear();
}
/// Returns a reference to the page, if any
pub fn get(&self, index: usize) -> Option<&W> {
self.widgets.get(index).map(|e| &e.0)
}
/// Returns a mutable reference to the page, if any
pub fn get_mut(&mut self, index: usize) -> Option<&mut W> {
self.widgets.get_mut(index).map(|e| &mut e.0)
}
/// Configure and size the page at index
fn configure_and_size(&mut self, cx: &mut ConfigCx, data: &W::Data, index: usize) {
let id = self.make_child_id(index);
if let Some(entry) = self.widgets.get_mut(index) {
cx.configure(entry.0.as_node(data), id);
let Size(w, h) = self.core.rect.size;
solve_size_rules(&mut entry.0, cx.size_cx(), Some(w), Some(h), None, None);
entry.1 = State::Sized;
}
}
/// Append a page
///
/// The new page is not made active (the active index may be changed to
/// avoid this). Consider calling [`Self::set_active`].
///
/// Returns the new page's index.
pub fn push(&mut self, cx: &mut ConfigCx, data: &W::Data, widget: W) -> usize {
let index = self.widgets.len();
if index == self.active {
self.active = usize::MAX;
}
self.widgets.push((widget, State::None));
if index < self.size_limit {
self.configure_and_size(cx, data, index);
}
index
}
/// Remove the last child widget (if any) and return
///
/// If this page was active then no page will be left active.
/// Consider also calling [`Self::set_active`].
pub fn pop(&mut self, cx: &mut EventState) -> Option<W> {
let result = self.widgets.pop().map(|(w, _)| w);
if let Some(w) = result.as_ref() {
if self.active > 0 && self.active == self.widgets.len() {
*cx |= Action::REGION_MOVED;
}
if w.id_ref().is_valid() {
if let Some(key) = w.id_ref().next_key_after(self.id_ref()) {
self.id_map.remove(&key);
}
}
}
result
}
/// Inserts a child widget position `index`
///
/// Panics if `index > len`.
///
/// The active page does not change (the index of the active page may change instead).
pub fn insert(&mut self, cx: &mut ConfigCx, data: &W::Data, index: usize, widget: W) {
if self.active >= index {
self.active = self.active.saturating_add(1);
}
self.widgets.insert(index, (widget, State::None));
for v in self.id_map.values_mut() {
if *v >= index {
*v += 1;
}
}
if index < self.size_limit {
self.configure_and_size(cx, data, index);
}
}
/// Removes the child widget at position `index`
///
/// Panics if `index` is out of bounds.
///
/// If this page was active then no page will be left active.
/// Consider also calling [`Self::set_active`].
pub fn remove(&mut self, cx: &mut EventState, index: usize) -> W {
let (w, _) = self.widgets.remove(index);
if w.id_ref().is_valid() {
if let Some(key) = w.id_ref().next_key_after(self.id_ref()) {
self.id_map.remove(&key);
}
}
if self.active == index {
self.active = usize::MAX;
*cx |= Action::REGION_MOVED;
}
for v in self.id_map.values_mut() {
if *v > index {
*v -= 1;
}
}
w
}
/// Replace the child at `index`
///
/// Panics if `index` is out of bounds.
///
/// If the new child replaces the active page then [`Action::RESIZE`] is triggered.
pub fn replace(&mut self, cx: &mut ConfigCx, data: &W::Data, index: usize, mut widget: W) -> W {
let entry = &mut self.widgets[index];
std::mem::swap(&mut widget, &mut entry.0);
entry.1 = State::None;
if widget.id_ref().is_valid() {
if let Some(key) = widget.id_ref().next_key_after(self.id_ref()) {
self.id_map.remove(&key);
}
}
if index < self.size_limit || index == self.active {
self.configure_and_size(cx, data, index);
}
if index == self.active {
*cx |= Action::RESIZE;
}
widget
}
/// Append child widgets from an iterator
///
/// The new pages are not made active (the active index may be changed to
/// avoid this). Consider calling [`Self::set_active`].
pub fn extend<T: IntoIterator<Item = W>>(
&mut self,
cx: &mut ConfigCx,
data: &W::Data,
iter: T,
) {
let old_len = self.widgets.len();
let iter = iter.into_iter();
if let Some(ub) = iter.size_hint().1 {
self.widgets.reserve(ub);
}
for w in iter {
let index = self.widgets.len();
self.widgets.push((w, State::None));
if index < self.size_limit {
self.configure_and_size(cx, data, index);
}
}
if (old_len..self.widgets.len()).contains(&self.active) {
self.active = usize::MAX;
}
}
/// Resize, using the given closure to construct new widgets
///
/// The new pages are not made active (the active index may be changed to
/// avoid this). Consider calling [`Self::set_active`].
pub fn resize_with<F: Fn(usize) -> W>(
&mut self,
cx: &mut ConfigCx,
data: &W::Data,
len: usize,
f: F,
) {
let old_len = self.widgets.len();
if len < old_len {
loop {
let (w, _) = self.widgets.pop().unwrap();
if w.id_ref().is_valid() {
if let Some(key) = w.id_ref().next_key_after(self.id_ref()) {
self.id_map.remove(&key);
}
}
if len == self.widgets.len() {
return;
}
}
}
if len > old_len {
self.widgets.reserve(len - old_len);
for index in old_len..len {
self.widgets.push((f(index), State::None));
if index < self.size_limit {
self.configure_and_size(cx, data, index);
}
}
if (old_len..len).contains(&self.active) {
self.active = usize::MAX;
}
}
}
}
impl<W: Widget, I> From<I> for Stack<W>
where
I: IntoIterator<Item = W>,
{
#[inline]
fn from(iter: I) -> Self {
Self {
widgets: iter.into_iter().map(|w| (w, State::None)).collect(),
..Default::default()
}
}
}