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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// 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::ops::{Index, IndexMut};
#[impl_self]
mod Page {
/// A stack page (also known as a tab page)
#[widget]
#[layout(self.inner)]
pub struct Page<A> {
core: widget_core!(),
#[widget]
pub inner: Box<dyn Widget<Data = A>>,
}
impl Tile for Self {
fn role(&self, _: &mut dyn RoleCx) -> Role<'_> {
Role::TabPage
}
}
impl Events for Self {
type Data = A;
}
impl Self {
/// Construct from a widget
pub fn new(widget: impl Widget<Data = A> + 'static) -> Self {
Page::new_boxed(Box::new(widget))
}
/// Construct from a boxed widget
#[inline]
pub fn new_boxed(inner: Box<dyn Widget<Data = A>>) -> Self {
Page {
core: Default::default(),
inner,
}
}
}
}
#[impl_self]
mod Stack {
/// 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`].
///
/// 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`].
///
/// # Messages
///
/// [`kas::messages::SetIndex`] may be used to change the page.
#[widget]
pub struct Stack<A> {
core: widget_core!(),
align_hints: AlignHints,
// Page and key used in Id::make_child (if not usize::MAX)
widgets: Vec<(Page<A>, usize)>,
active: usize,
size_limit: usize,
next: usize,
}
impl Default for Self {
fn default() -> Self {
Stack {
core: Default::default(),
align_hints: AlignHints::NONE,
widgets: Vec::new(),
active: 0,
size_limit: usize::MAX,
next: 0,
}
}
}
impl Layout for Self {
fn size_rules(&mut self, cx: &mut SizeCx, axis: AxisInfo) -> SizeRules {
let (mut min, mut ideal) = (0, 0);
let mut m = (0, 0);
let mut stretch = Stretch::None;
for (i, entry) in self.widgets.iter_mut().enumerate() {
if entry.1 != usize::MAX {
let rules = entry.0.size_rules(cx, axis);
ideal = ideal.max(rules.ideal_size());
m = (m.0.max(rules.margins().0), m.1.max(rules.margins().1));
stretch = stretch.max(rules.stretch());
if i == self.active {
min = rules.min_size();
}
}
}
SizeRules::new(min, ideal, stretch).with_margins(m)
}
fn set_rect(&mut self, cx: &mut SizeCx, rect: Rect, hints: AlignHints) {
self.core.set_rect(rect);
self.align_hints = hints;
for entry in self.widgets.iter_mut() {
if entry.1 != usize::MAX {
entry.0.set_rect(cx, rect, hints);
}
}
}
fn draw(&self, mut draw: DrawCx) {
if let Some(entry) = self.widgets.get(self.active) {
debug_assert!(entry.1 != usize::MAX);
entry.0.draw(draw.re());
}
}
}
impl Tile for Self {
fn role(&self, _: &mut dyn RoleCx) -> Role<'_> {
Role::None
}
#[inline]
fn child_indices(&self) -> ChildIndices {
if self.active < self.widgets.len() {
ChildIndices::one(self.active)
} else {
ChildIndices::none()
}
}
fn get_child(&self, index: usize) -> Option<&dyn Tile> {
self.widgets
.get(index)
.filter(|w| w.1 != usize::MAX)
.map(|w| w.0.as_tile())
}
fn find_child_index(&self, id: &Id) -> Option<usize> {
// NOTE: this approach is O(n) where n = number of pages. Since a
// Stack should have a small number of pages this is acceptable.
let key = id.next_key_after(self.id_ref())?;
for (i, w) in self.widgets.iter().enumerate() {
if w.1 == key {
return Some(i);
}
}
None
}
fn nav_next(&self, _: bool, from: Option<usize>) -> Option<usize> {
let active = match from {
None => self.active,
Some(active) if active != self.active => self.active,
_ => return None,
};
if let Some(entry) = self.widgets.get(active) {
debug_assert!(entry.1 != usize::MAX);
return Some(active);
}
None
}
}
impl Events for Self {
fn make_child_id(&mut self, index: usize) -> Id {
let Some((child, key)) = self.widgets.get(index) else {
return Id::default();
};
let id = child.id_ref();
if id.is_valid()
&& let Some(k) = id.next_key_after(self.id_ref())
&& (*key == k || self.widgets.iter().all(|entry| k != entry.1))
{
let id = id.clone();
self.widgets[index].1 = k;
return id;
}
loop {
let key = self.next;
self.next += 1;
if self.widgets.iter().any(|entry| entry.1 == key) {
continue;
}
self.widgets[index].1 = key;
return self.id_ref().make_child(key);
}
}
fn probe(&self, coord: Coord) -> Id {
if let Some(entry) = self.widgets.get(self.active) {
debug_assert!(entry.1 != usize::MAX);
if let Some(id) = entry.0.try_probe(coord) {
return id;
}
}
self.id()
}
fn configure(&mut self, _: &mut ConfigCx) {
for entry in self.widgets.iter_mut() {
entry.1 = usize::MAX;
}
}
#[inline]
fn recurse_indices(&self) -> ChildIndices {
let end = self.widgets.len().min(self.size_limit.max(self.active + 1));
let start = end.saturating_sub(self.size_limit).min(self.active);
ChildIndices::range(start..end)
}
fn handle_messages(&mut self, cx: &mut EventCx, data: &A) {
if let Some(kas::messages::SetIndex(index)) = cx.try_pop() {
self.set_active(cx, data, index);
}
}
}
impl Widget for Self {
type Data = A;
fn child_node<'n>(&'n mut self, data: &'n A, index: usize) -> Option<Node<'n>> {
self.widgets
.get_mut(index)
.filter(|w| w.1 != usize::MAX)
.map(|w| w.0.as_node(data))
}
}
impl Index<usize> for Self {
type Output = Page<A>;
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<A> Stack<A> {
/// Construct a new, empty instance
///
/// See also [`Stack::from`].
pub fn new() -> Self {
Stack::default()
}
/// Limit the number of pages configured 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.
///
/// This affects configuration, sizing and message handling for inactive 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 this method is only used before the UI is run.
#[inline]
pub fn with_active(mut self, active: usize) -> Self {
debug_assert_eq!(
self.widgets
.get(self.active)
.map(|e| e.1)
.unwrap_or_default(),
usize::MAX
);
self.active = active;
self
}
/// Change the active page and update
///
/// If `index == self.active()` then nothing happens.
/// If `index >= self.len()` then nothing will be displayed.
/// If the page is changed successfully, the newly active page is updated.
pub fn set_active(&mut self, cx: &mut ConfigCx, data: &A, index: usize) {
let old_index = self.active;
if old_index == index {
return;
}
self.active = index;
let rect = self.rect();
if index < self.widgets.len() {
let id = (self.widgets[index].1 == usize::MAX).then_some(self.make_child_id(index));
let entry = &mut self.widgets[index];
let node = entry.0.as_node(data);
if let Some(id) = id {
cx.configure(node, id);
debug_assert!(entry.1 != usize::MAX);
} else {
cx.update(node);
}
let Size(w, _h) = rect.size;
// HACK: we should pass the known height here, but it causes
// even distribution of excess space. Maybe SizeRules::solve_widths
// should not always distribute excess space?
solve_size_rules(&mut entry.0, &mut cx.size_cx(), Some(w), None);
entry.0.set_rect(&mut cx.size_cx(), rect, self.align_hints);
cx.region_moved();
} else {
if old_index < self.widgets.len() {
cx.region_moved();
}
}
}
/// Get a direct reference to the active child page, if any
pub fn get_active(&self) -> Option<&Page<A>> {
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<&Page<A>> {
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 Page<A>> {
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: &A, index: usize) {
let Size(w, h) = self.rect().size;
let id = self.make_child_id(index);
if let Some(entry) = self.widgets.get_mut(index) {
cx.configure(entry.0.as_node(data), id);
solve_size_rules(&mut entry.0, &mut cx.size_cx(), Some(w), Some(h));
debug_assert!(entry.1 != usize::MAX);
}
}
/// 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: &A, page: Page<A>) -> usize {
let index = self.widgets.len();
if index == self.active {
self.active = usize::MAX;
}
self.widgets.push((page, usize::MAX));
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<Page<A>> {
let result = self.widgets.pop().map(|w| w.0);
if result.is_some() {
if self.active > 0 && self.active == self.widgets.len() {
cx.region_moved();
}
}
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: &A, index: usize, page: Page<A>) {
if self.active >= index {
self.active = self.active.saturating_add(1);
}
self.widgets.insert(index, (page, usize::MAX));
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) -> Page<A> {
let w = self.widgets.remove(index);
if self.active == index {
self.active = usize::MAX;
cx.region_moved();
}
for entry in self.widgets[index..].iter_mut() {
entry.1 -= 1;
}
w.0
}
/// Replace the child at `index`
///
/// Panics if `index` is out of bounds.
///
/// If the new child replaces the active page then a resize is triggered.
pub fn replace(
&mut self,
cx: &mut ConfigCx,
data: &A,
index: usize,
mut page: Page<A>,
) -> Page<A> {
let entry = &mut self.widgets[index];
std::mem::swap(&mut page, &mut entry.0);
entry.1 = usize::MAX;
if index < self.size_limit || index == self.active {
self.configure_and_size(cx, data, index);
}
if index == self.active {
cx.resize();
}
page
}
/// 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 = Page<A>>>(
&mut self,
cx: &mut ConfigCx,
data: &A,
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, usize::MAX));
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) -> Page<A>>(
&mut self,
cx: &mut ConfigCx,
data: &A,
len: usize,
f: F,
) {
let old_len = self.widgets.len();
if len < old_len {
loop {
let _ = self.widgets.pop().unwrap();
if len == self.widgets.len() {
break;
}
}
}
if len > old_len {
self.widgets.reserve(len - old_len);
for index in old_len..len {
self.widgets.push((f(index), usize::MAX));
if index < self.size_limit {
self.configure_and_size(cx, data, index);
}
}
if (old_len..len).contains(&self.active) {
self.active = usize::MAX;
}
}
}
}
impl<A, I> From<I> for Stack<A>
where
I: IntoIterator<Item = Page<A>>,
{
#[inline]
fn from(iter: I) -> Self {
Self {
widgets: iter.into_iter().map(|w| (w, usize::MAX)).collect(),
..Default::default()
}
}
}