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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! [`Kanban`]: a **columns + cards** board with **drag and drop** between columns.
//!
//! Like the rest of frus, the widget is **controlled**: the application holds the columns and
//! their cards, and reacts to a single `on_move(from_col, from_pos, to_col, to_pos)` message
//! emitted when a card is dropped. Drag and drop reuses the framework's **reordering**
//! mechanism (`reorder_index` / `on_reorder`): every slot carries a **flat index**
//! `col * STRIDE + pos`, and the card grabbed decodes the **target** slot (another card, or
//! the drop zone at the bottom of a column) to work out the destination.
use std::rc::Rc;
use frus_core::{Color, Insets, Point, Rect, ResolvedTextStyle, Scene, TextStyle};
use frus_layout::{Align, Dimension, FlexDirection, Style};
use crate::button::{Button, Variant};
use crate::flex::Flex;
use crate::interaction::Status;
use crate::scroll::{Axis, SingleChildScrollView};
use crate::text::Text;
use crate::theme::Theme;
use crate::widget::{CellFn, ReorderAxis, Widget};
/// A card moving between columns: `(from_col, from_pos, to_col, to_pos)` into a
/// message.
type MoveFn<Msg> = Rc<dyn Fn(usize, usize, usize, usize) -> Msg>;
/// The encoding stride of a `(column, position)` slot into a flat index: it bounds the number
/// of cards per column (amply enough for a board). See [`kanban_slot`].
const STRIDE: usize = 1000;
/// A column's width.
const COL_W: f32 = 220.0;
/// A column panel's (uniform) inner padding.
const COL_PAD: f32 = 12.0;
/// A card's height.
const CARD_H: f32 = 44.0;
/// A text card's label: what the caller said, else what the theme says, else `bodyLarge`.
///
/// The reference has no board, so this one is argued rather than read: a card is a small
/// piece of content read on its own, which is the step the reference gives a list tile's
/// title and this framework already gives a `Tree` row.
///
/// **Resolved once**, so that the number the box is measured with is the number the glyphs
/// are drawn at.
fn label_style(over: Option<TextStyle>, theme: Option<&Theme>) -> ResolvedTextStyle {
over.or(theme.and_then(|t| t.widgets.kanban.card_text_style))
.unwrap_or_else(|| crate::theme::type_scale(theme).body_large)
.resolved()
}
/// A column's heading: `titleMedium`, the step a heading over a short list takes.
///
/// Left as a [`TextStyle`] rather than resolved, because the heading is a [`Text`] child
/// and a `Text` resolves its own.
fn column_title_style(over: Option<TextStyle>, theme: Option<&Theme>) -> TextStyle {
over.or(theme.and_then(|t| t.widgets.kanban.column_title_text_style))
.unwrap_or_else(|| crate::theme::type_scale(theme).title_medium)
}
/// The **flat** index of a `(col, pos)` slot: `col * STRIDE + pos`. This is a card's
/// [`reorder_index`](Widget::reorder_index) value (both source **and** target). Reusable to
/// test the routing of drag and drop.
pub fn kanban_slot(col: usize, pos: usize) -> usize {
// Past STRIDE cards in a column, `pos` would overflow into the column field (the flat
// index would silently target the next column). A debug guard; STRIDE stays generous.
debug_assert!(
pos < STRIDE,
"position {pos} out of bounds (STRIDE = {STRIDE}): column overflow"
);
col * STRIDE + pos
}
/// Decodes a flat index into `(col, pos)` (the inverse of [`kanban_slot`]).
fn decode(slot: usize) -> (usize, usize) {
(slot / STRIDE, slot % STRIDE)
}
/// A card: both **source** and **target** of drag and drop. Painted as a raised tile, it shows
/// either a **label** (a text card) or **widget content** supplied by the application (a rich
/// card: a label + tags + a delete button…), placed as a child.
struct Card<Msg> {
label: String,
/// **Rich** content (0 or 1 widget): when present, the card hosts it instead of the label.
content: Vec<Box<dyn Widget<Msg>>>,
/// Its own slot (a flat index): serves as `reorder_index`, both grabbed source **and** drop target.
slot: usize,
from_col: usize,
from_pos: usize,
on_move: Option<MoveFn<Msg>>,
text_style: Option<TextStyle>,
}
impl<Msg> Card<Msg> {
fn sizing(&self, theme: Option<&Theme>) -> Style {
if self.content.is_empty() {
// A text card: a fixed height, with the label painted by the card.
Style {
width: Dimension::Auto,
height: Dimension::Length(frus_text::line_box(
CARD_H,
&label_style(self.text_style, theme),
0.0,
)),
..Default::default()
}
} else {
// A rich card: the content is a child, the card adapts (floor `CARD_H`), and has padding.
Style {
width: Dimension::Auto,
height: Dimension::Auto,
min_height: Dimension::Length(CARD_H),
padding: Insets::uniform(8.0),
align: Align::Center,
..Default::default()
}
}
}
}
impl<Msg: Clone> Widget<Msg> for Card<Msg> {
fn style(&self) -> Style {
self.sizing(None)
}
fn style_themed(&self, theme: &Theme) -> Style {
self.sizing(Some(theme))
}
fn children(&self) -> &[Box<dyn Widget<Msg>>] {
&self.content
}
fn paint(&self, bounds: Rect, status: Status, theme: &Theme, scene: &mut Scene) {
let o = status.opacity;
// A raised tile; tinted on hover, as a grab affordance.
let base = theme.surface.lerp(theme.on_surface, 0.05);
let fill = theme.state_layer(base, theme.on_surface, &status);
scene.draw_rect(
bounds,
fill.fade(o),
theme.radius,
1.0,
theme.scheme.outline_variant.fade(o),
);
// A label only for a **text** card (a rich card paints its own content).
if self.content.is_empty() {
let style = label_style(self.text_style, Some(theme));
let ty = bounds.y + (bounds.height - style.line_height()) * 0.5;
scene.text(
Point::new(bounds.x + 12.0, ty),
self.label.clone(),
&style,
theme.on_surface.fade(o),
);
}
}
fn on_click(&self) -> Option<Msg> {
None
}
fn reorder_index(&self) -> Option<usize> {
Some(self.slot)
}
fn on_reorder(&self, to: usize) -> Option<Msg> {
// The target `to` is the flat index of the hovered slot (another card, or a drop zone).
let (to_col, to_pos) = decode(to);
self.on_move
.as_ref()
.map(|f| f(self.from_col, self.from_pos, to_col, to_pos))
}
fn reorder_axis(&self) -> ReorderAxis {
ReorderAxis::Vertical // cards slide vertically
}
}
/// The drop zone at the bottom of a column: the insertion target **at the end** of the column
/// (and the only target of an empty one). Not a useful source (its `on_reorder` moves nothing).
struct DropZone {
slot: usize,
}
impl<Msg: Clone> Widget<Msg> for DropZone {
fn style(&self) -> Style {
Style {
width: Dimension::Auto,
height: Dimension::Length(CARD_H * 0.8),
..Default::default()
}
}
fn children(&self) -> &[Box<dyn Widget<Msg>>] {
&[]
}
fn paint(&self, bounds: Rect, status: Status, theme: &Theme, scene: &mut Scene) {
// A discreet simulated dashed outline (a faded border): "drop here".
scene.draw_rect(
bounds,
Color::TRANSPARENT,
theme.radius,
1.0,
theme.scheme.outline_variant.fade(status.opacity * 0.5),
);
}
fn on_click(&self) -> Option<Msg> {
None
}
fn reorder_index(&self) -> Option<usize> {
Some(self.slot)
}
fn on_reorder(&self, _to: usize) -> Option<Msg> {
None // the drop zone is not a move source
}
fn reorder_axis(&self) -> ReorderAxis {
ReorderAxis::Vertical
}
fn reorder_draggable(&self) -> bool {
false // target **only**: you drop onto it, you do not lift it
}
}
/// A column's **panel**: title + cards + drop zone, on a discreet themed background. It is a
/// vertical `Flex` container that paints its own background (the panel's clip holds its cards).
struct Column<Msg> {
children: Vec<Box<dyn Widget<Msg>>>,
}
impl<Msg: Clone> Widget<Msg> for Column<Msg> {
fn style(&self) -> Style {
Style {
width: Dimension::Length(COL_W),
flex_direction: FlexDirection::Column,
gap: 8.0,
padding: Insets::uniform(COL_PAD),
..Default::default()
}
}
fn children(&self) -> &[Box<dyn Widget<Msg>>] {
&self.children
}
fn paint(&self, bounds: Rect, status: Status, theme: &Theme, scene: &mut Scene) {
// The panel's **themed** background (a default overridable through the theme): a discreet veil.
let bg = theme.surface.lerp(theme.on_surface, 0.04);
scene.draw_rect(
bounds,
bg.fade(status.opacity),
theme.radius,
0.0,
Color::TRANSPARENT,
);
}
fn on_click(&self) -> Option<Msg> {
None
}
}
/// A **Kanban** board: titled columns of cards, with drag and drop between columns.
///
/// ```
/// use frus_widgets::Kanban;
/// let board: Kanban = Kanban::new(|fc, fp, tc, tp| { let _ = (fc, fp, tc, tp); })
/// .column("To do", ["Design", "Spec"])
/// .column("Doing", ["Build"])
/// .column("Done", ["Kickoff"]);
/// ```
/// The factory for a card's **rich content**: called again on every rebuild (a fresh widget), for
/// cards beyond plain text (a label + tags + a delete button…).
pub type CardFactory<Msg> = Rc<dyn Fn() -> Box<dyn Widget<Msg>>>;
/// A column's cards: plain **text**, or rich **widgets**, one per card.
enum ColCards<Msg> {
Text(Vec<String>),
Widgets(Vec<CardFactory<Msg>>),
}
impl<Msg> ColCards<Msg> {
fn len(&self) -> usize {
match self {
ColCards::Text(v) => v.len(),
ColCards::Widgets(v) => v.len(),
}
}
}
/// A **board** of columns holding cards, which can be dragged from one column to another
/// or reordered inside one.
///
/// It is controlled: the application holds the columns and their cards, and the board only
/// says where a card was dropped, through the callback given to [`Kanban::new`].
pub struct Kanban<Msg = crate::callback::Callback> {
on_move: Option<MoveFn<Msg>>,
/// A "+ Add card" button at the bottom of every column (milestone 249): `on_add(col)` on adding.
on_add: Option<Rc<dyn Fn(usize) -> Msg>>,
/// An explicit height for each column's **scrollable card area** (milestone 264, Trello
/// style). `Some(h)`: the cards scroll vertically in a region of height `h` (with the title
/// fixed above and the "+ Add card" button fixed below). `None`: the column stretches to
/// the height of its content (the original behaviour). See [`Kanban::card_area_height`].
card_area_height: Option<f32>,
/// Per-column vertical scrolling **Trello style, with no explicit height** (milestone 266):
/// the columns **fill** the board's height (through a stretched `Row`) and each column's
/// card area is a `flex(1)` `SingleChildScrollView` that takes the rest and then scrolls. It takes
/// precedence over `card_area_height`. See [`Kanban::scrollable_columns`].
fill_columns: bool,
card_text_style: Option<TextStyle>,
column_title_text_style: Option<TextStyle>,
columns: Vec<(String, ColCards<Msg>)>,
children: Vec<Box<dyn Widget<Msg>>>,
}
impl<Msg: Clone + 'static> Kanban<Msg> {
/// Creates a board; `on_move(from_col, from_pos, to_col, to_pos)` is emitted when a card is
/// **dropped** onto a slot (another card, or the end of a column).
pub fn new<R: crate::callback::IntoMsg<Msg>>(
on_move: impl Fn(usize, usize, usize, usize) -> R + 'static,
) -> Self {
Self {
on_move: Some(Rc::new(crate::callback::handler4(on_move))),
on_add: None,
card_area_height: None,
fill_columns: false,
card_text_style: None,
column_title_text_style: None,
columns: Vec::new(),
children: Vec::new(),
}
}
/// The text cards' type, over the theme's and the framework's.
#[must_use]
pub fn card_text_style(mut self, style: TextStyle) -> Self {
self.card_text_style = Some(style);
self.rebuild();
self
}
/// The column headings' type, over the theme's and the framework's.
#[must_use]
pub fn column_title_text_style(mut self, style: TextStyle) -> Self {
self.column_title_text_style = Some(style);
self.rebuild();
self
}
/// Adds a titled **column** with its (text) cards, in order.
pub fn column(
mut self,
title: impl Into<String>,
cards: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
let cards = ColCards::Text(cards.into_iter().map(Into::into).collect());
self.columns.push((title.into(), cards));
self.rebuild();
self
}
/// Adds a column in which every card is a **widget** (a rich card: a label + tags + a delete
/// button…), supplied by a **factory** called again on every rebuild. The card stays a drag
/// and drop source and target; its clickable elements (a × button…) capture their own click.
pub fn column_widgets(
mut self,
title: impl Into<String>,
cards: impl IntoIterator<Item = CellFn<Msg>>,
) -> Self {
let cards = ColCards::Widgets(cards.into_iter().map(Rc::from).collect());
self.columns.push((title.into(), cards));
self.rebuild();
self
}
/// Adds a **"+ Add card"** button at the bottom of every column; `on_add(col)` on click (the
/// app adds a card to the column). Without this call, there is no add button.
pub fn on_add<R: crate::callback::IntoMsg<Msg>>(
mut self,
on_add: impl Fn(usize) -> R + 'static,
) -> Self {
self.on_add = Some(Rc::new(crate::callback::handler1(on_add)));
self.rebuild();
self
}
/// Makes each column's cards **scroll vertically** within a region of height `h` (in logical
/// pixels), Trello style: the **title** stays fixed above, the "+ Add card" button fixed
/// below, and only the cards (with the final drop zone) scroll. Without this call, the
/// column stretches to the height of its content.
///
/// This is a stopgap **controlled by the application**, which supplies the height: a
/// `flex(1)` `SingleChildScrollView` receives no usable height until its chain of ancestors gives it a
/// **definite** height (milestone 263), hence an explicit height here.
pub fn card_area_height(mut self, h: f32) -> Self {
self.card_area_height = Some(h);
self.rebuild();
self
}
/// Per-column vertical scrolling **Trello style, with no explicit height** (milestone 266):
/// the columns **fill** the board's height and each column's card area takes the rest
/// (below the title, above the "+ Add card" button) and then **scrolls**. Unlike
/// [`Kanban::card_area_height`], the app has no height to compute: the board sits in an
/// ancestor of **definite height** (a window, a bounded horizontal `SingleChildScrollView`…) and the flex
/// fill does the rest. Takes precedence over `card_area_height` when both are set.
pub fn scrollable_columns(mut self) -> Self {
self.fill_columns = true;
self.rebuild();
self
}
fn rebuild(&mut self) {
self.children = self
.columns
.iter()
.enumerate()
.map(|(c, (title, cards))| self.build_column(c, title, cards))
.collect();
}
/// Builds one column: title + cards (each a drop source and target) + the final drop zone +
/// an optional add button.
fn build_column(&self, col: usize, title: &str, cards: &ColCards<Msg>) -> Box<dyn Widget<Msg>> {
let make_card = |pos: usize, content: Vec<Box<dyn Widget<Msg>>>, label: String| Card {
label,
content,
slot: kanban_slot(col, pos),
from_col: col,
from_pos: pos,
on_move: self.on_move.clone(),
text_style: self.card_text_style,
};
// The cards (each both source **and** target) + the final drop zone: this is the
// column's **scrollable** part (the title and the add button stay fixed).
let mut cards_v: Vec<Box<dyn Widget<Msg>>> = Vec::new();
match cards {
ColCards::Text(labels) => {
for (pos, label) in labels.iter().enumerate() {
cards_v.push(Box::new(make_card(pos, Vec::new(), label.clone())));
}
}
ColCards::Widgets(factories) => {
for (pos, make) in factories.iter().enumerate() {
cards_v.push(Box::new(make_card(pos, vec![make()], String::new())));
}
}
}
// The insertion slot at the end of the column (and the target of an empty one).
cards_v.push(Box::new(DropZone {
slot: kanban_slot(col, cards.len()),
}));
let mut children: Vec<Box<dyn Widget<Msg>>> = vec![Box::new(Text::styled(
title.to_string(),
column_title_style(self.column_title_text_style, None),
))];
// The column's inner width (the column width minus its padding). The scrollable card
// area wraps the cards in a vertical `Flex` (a single child for the `SingleChildScrollView`).
let inner_w = COL_W - 2.0 * COL_PAD;
let list = |cards: Vec<Box<dyn Widget<Msg>>>| {
cards
.into_iter()
.fold(Flex::column().gap(8.0).width(inner_w), Flex::child_boxed)
};
if self.fill_columns {
// **Filling** (milestone 266): the column fills the board's height (a stretched Row);
// the card area, a `flex(1)` `SingleChildScrollView`, takes the rest (below the title, above the
// button) and then scrolls. No explicit height: the flex does the arithmetic.
children.push(Box::new(
SingleChildScrollView::new()
.axis(Axis::Vertical)
.width(inner_w)
.flex(1.0)
.child(list(cards_v)),
));
} else if let Some(h) = self.card_area_height {
// An **explicit** height (milestone 264): the fallback when the ancestor has no definite height.
children.push(Box::new(
SingleChildScrollView::new()
.axis(Axis::Vertical)
.width(inner_w)
.height(h)
.child(list(cards_v)),
));
} else {
// A column that stretches to the height of its content (the original behaviour, bare cards).
children.extend(cards_v);
}
// The "+ Add card" button (milestone 249), if requested.
if let Some(on_add) = &self.on_add {
let on_add = on_add.clone();
children.push(Box::new(
Button::new("+ Add card")
.variant(Variant::Outlined)
.size(13.0)
.on_press(on_add(col)),
));
}
Box::new(Column { children })
}
}
impl<Msg: Clone> Widget<Msg> for Kanban<Msg> {
fn style(&self) -> Style {
// **Filling** mode (milestone 266): the row takes its parent's height (`Percent(1.0)`
// — the ancestor has a definite height) and **stretches** its columns
// (`Align::Stretch`) so each fills the board; their `flex(1)` card area then takes
// the rest and scrolls. Otherwise: the content's height, columns aligned to the top.
let (align, height) = if self.fill_columns {
(Align::Stretch, Dimension::Percent(1.0))
} else {
(Align::Start, Dimension::Auto)
};
Style {
height,
flex_direction: FlexDirection::Row,
gap: 12.0,
align,
padding: Insets::ZERO,
..Default::default()
}
}
fn children(&self) -> &[Box<dyn Widget<Msg>>] {
&self.children
}
fn paint(&self, _bounds: Rect, _status: Status, _theme: &Theme, _scene: &mut Scene) {}
fn on_click(&self) -> Option<Msg> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Clone, Debug, PartialEq)]
enum Msg {
Move(usize, usize, usize, usize),
Add(usize),
Del(usize),
}
/// Collects, in tree order, every non-null `on_click` message of a subtree.
fn collect_clicks(w: &dyn Widget<Msg>, out: &mut Vec<Msg>) {
if let Some(m) = w.on_click() {
out.push(m);
}
for c in w.children() {
collect_clicks(c.as_ref(), out);
}
}
/// Finds a subtree's first card (a widget with a `reorder_index`) and returns
/// `(reorder_index, on_reorder(target))`.
fn first_card(w: &dyn Widget<Msg>, target: usize) -> Option<(usize, Option<Msg>)> {
if let Some(idx) = w.reorder_index() {
return Some((idx, w.on_reorder(target)));
}
for c in w.children() {
if let Some(found) = first_card(c.as_ref(), target) {
return Some(found);
}
}
None
}
#[test]
fn slot_encoding_roundtrips() {
assert_eq!(decode(kanban_slot(0, 0)), (0, 0));
assert_eq!(decode(kanban_slot(2, 5)), (2, 5));
assert_eq!(decode(kanban_slot(1, 3)), (1, 3));
}
#[test]
fn dropping_a_card_routes_a_cross_column_move() {
let board = Kanban::new(Msg::Move)
.column("To do", ["A", "B"])
.column("Doing", ["C"]);
// The first card = column 0, position 0 ("A"): its flat index, and the move produced
// when it is dropped onto slot (1, 0) of the "Doing" column.
let col0 = &Widget::<Msg>::children(&board)[0];
let (idx, moved) = first_card(col0.as_ref(), kanban_slot(1, 0)).expect("a card");
assert_eq!(idx, kanban_slot(0, 0), "the source card's flat index");
assert_eq!(
moved,
Some(Msg::Move(0, 0, 1, 0)),
"a drop at (1,0): a cross-column move"
);
}
#[test]
fn cards_declare_vertical_reorder_axis() {
fn first_axis(w: &dyn Widget<Msg>) -> Option<ReorderAxis> {
if w.reorder_index().is_some() {
return Some(w.reorder_axis());
}
w.children().iter().find_map(|c| first_axis(c.as_ref()))
}
let board = Kanban::new(Msg::Move).column("A", ["x"]);
let col0 = &Widget::<Msg>::children(&board)[0];
assert_eq!(
first_axis(col0.as_ref()),
Some(ReorderAxis::Vertical),
"the cards slide vertically"
);
}
#[test]
fn rich_cards_host_content_and_add_button_is_present() {
// A **rich** column: each card hosts a × (delete) button; plus an add button.
let cards: Vec<CellFn<Msg>> = (0..2)
.map(|i| {
Box::new(move || {
Box::new(Button::new("x").on_press(Msg::Del(i))) as Box<dyn Widget<Msg>>
}) as CellFn<Msg>
})
.collect();
let board = Kanban::new(Msg::Move)
.on_add(Msg::Add)
.column_widgets("Col", cards);
let col0 = &Widget::<Msg>::children(&board)[0];
// The rich card stays **reorderable** (dragging wired up) and routes a Move.
let (idx, moved) = first_card(col0.as_ref(), kanban_slot(0, 1)).expect("a card");
assert_eq!(idx, kanban_slot(0, 0), "the rich card keeps its flat index");
assert_eq!(
moved,
Some(Msg::Move(0, 0, 0, 1)),
"and still routes the move"
);
// The reachable clicks include deletion (each card's ×) and adding (the column button).
let mut clicks = Vec::new();
collect_clicks(col0.as_ref(), &mut clicks);
assert!(
clicks.contains(&Msg::Del(0)) && clicks.contains(&Msg::Del(1)),
"a × delete per card"
);
assert!(
clicks.contains(&Msg::Add(0)),
"the column's + Add card button"
);
}
#[test]
fn cards_are_draggable_but_the_drop_zone_is_target_only() {
/// Returns the `reorder_draggable` of the first reorderable (a card) and looks for a
/// drop zone (a reorderable that is **not** grabbable) in the subtree.
fn scan(w: &dyn Widget<Msg>, out: &mut Vec<bool>) {
if w.reorder_index().is_some() {
out.push(w.reorder_draggable());
}
for c in w.children() {
scan(c.as_ref(), out);
}
}
let board = Kanban::new(Msg::Move).column("To do", ["A", "B"]);
let col0 = &Widget::<Msg>::children(&board)[0];
let mut flags = Vec::new();
scan(col0.as_ref(), &mut flags);
// Two grabbable cards + one non-grabbable drop zone.
assert_eq!(
flags.iter().filter(|&&d| d).count(),
2,
"both cards are grabbable"
);
assert_eq!(
flags.iter().filter(|&&d| !d).count(),
1,
"the drop zone is target-only"
);
}
#[test]
fn board_lays_out_one_widget_per_column() {
let board = Kanban::new(Msg::Move)
.column("A", ["x"])
.column("B", Vec::<String>::new());
assert_eq!(
Widget::<Msg>::children(&board).len(),
2,
"one widget per column"
);
}
}