icedtea 0.2.0

Native desktop widgets and chrome for iced
Documentation
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
//! Application chrome: menu, toolbar, list/detail, prefs, about, palette.
//!
//! Pass an [`ActionTable`](crate::action::ActionTable) and tokens.
//! Children are icedtea widgets (they already carry [`A11y`](crate::a11y::A11y)).
//!
//! ```
//! use icedtea::action::{Action, ActionTable};
//! use icedtea::i18n::{Catalog, Direction};
//! use icedtea::pattern;
//! use icedtea::theme;
//! let tok = theme::named("dark").tokens;
//! let mut table = ActionTable::new();
//! table.insert(Action::new("file.save", "Save", ()));
//! let cat = Catalog::builtin();
//! let _: icedtea::Element<'_, ()> =
//!     pattern::toolbar(table.iter(), tok, Direction::Ltr);
//! let _ = cat;
//! ```

use iced::widget::{column, container, row, text, Column, Row};
use iced::{Element, Length};

use crate::a11y::{A11y, Role};
use crate::action::{Action, ActionTable};
use crate::collection::Tabs;
use crate::i18n::{order, Catalog, Direction};
use crate::icon::Icon;
use crate::layout;
use crate::nav::NavStack;
use crate::style;
use crate::theme::Tokens;
use crate::typo;
use crate::variant::Variant;
use crate::widget::{
    group_box, label, meta, tab_bar, themed_button, themed_scroll, themed_text_input,
};

/// Group actions by the id prefix before `.` (`file.save` → `file`).
pub fn menu_groups<M: Clone>(table: &ActionTable<M>) -> Vec<(&str, Vec<&Action<M>>)> {
    let mut groups: Vec<(&str, Vec<&Action<M>>)> = Vec::new();
    for a in table.iter() {
        let prefix = a.id.as_str().split('.').next().unwrap_or("app");
        if let Some((_, list)) = groups.iter_mut().find(|(p, _)| *p == prefix) {
            list.push(a);
        } else {
            groups.push((prefix, vec![a]));
        }
    }
    groups
}

/// Catalog title for a menu prefix (`file` → `File`).
pub fn menu_heading(cat: &Catalog, prefix: &str) -> String {
    let t = cat.t(prefix);
    if t != prefix {
        return t.to_string();
    }
    let mut chars = prefix.chars();
    match chars.next() {
        None => String::new(),
        Some(first) => first.to_uppercase().chain(chars).collect(),
    }
}

/// Label for a menu row: title plus shortcut, padded so keys line up.
pub fn menu_item_label<M>(a: &Action<M>) -> String {
    match &a.shortcut {
        Some(sc) => format!("{}    {sc}", a.title),
        None => a.title.clone(),
    }
}

/// Resolve a picked overlay label to its action message.
pub fn pick_menu_message<M: Clone>(entries: &[(String, M)], chosen: &str) -> M {
    entries
        .iter()
        .find(|(l, _)| l == chosen)
        .map(|(_, m)| m.clone())
        .expect("menu item")
}

fn bind_menu_pick<M: Clone>(entries: Vec<(String, M)>) -> impl Fn(String) -> M {
    move |chosen| pick_menu_message(&entries, &chosen)
}

/// In-window menu bar from one [`ActionTable`].
///
/// ```
/// use icedtea::action::{Action, ActionTable};
/// use icedtea::i18n::{Catalog, Direction};
/// use icedtea::pattern;
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let mut table = ActionTable::new();
/// table.insert(Action::new("file.save", "Save", ()));
/// let cat = Catalog::builtin();
/// let _: icedtea::Element<'_, ()> =
///     pattern::menu_bar(&table, tok, Direction::Ltr, &cat);
/// ```
pub fn menu_bar<'a, M: Clone + 'a>(
    table: &'a ActionTable<M>,
    tok: Tokens,
    dir: Direction,
    cat: &Catalog,
) -> Element<'a, M> {
    let groups = order(dir, menu_groups(table));
    let mut titles = Row::new().spacing(0).padding([2, 4]);
    for (prefix, acts) in groups {
        let heading = menu_heading(cat, prefix);
        let entries: Vec<(String, M)> = acts
            .into_iter()
            .filter_map(|a| a.invoke().map(|m| (menu_item_label(a), m)))
            .collect();
        if entries.is_empty() {
            titles = titles.push(meta(heading, tok, A11y::new(prefix, Role::Menu)));
            continue;
        }
        let labels: Vec<String> = entries.iter().map(|(l, _)| l.clone()).collect();
        titles = titles.push(crate::menubar::drop_menu(
            heading,
            labels,
            bind_menu_pick(entries),
            tok,
        ));
    }
    crate::a11y::attach(
        container(titles)
            .width(Length::Fill)
            .style(move |_| style::panel(tok))
            .into(),
        &A11y::new("menubar", Role::Menu),
    )
}

/// Toolbar row from the same action table as the menu.
///
/// ```
/// use icedtea::action::{Action, ActionTable};
/// use icedtea::i18n::Direction;
/// use icedtea::pattern;
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let mut table = ActionTable::new();
/// table.insert(Action::new("file.save", "Save", ()));
/// let _: icedtea::Element<'_, ()> =
///     pattern::toolbar(table.iter(), tok, Direction::Ltr);
/// ```
pub fn toolbar<'a, M: Clone + 'a>(
    actions: impl IntoIterator<Item = &'a Action<M>>,
    tok: Tokens,
    dir: Direction,
) -> Element<'a, M> {
    let actions: Vec<_> = order(dir, actions);
    let mut r = Row::new().spacing(4).padding(8);
    for a in actions {
        r = r.push(themed_button(
            a.title.clone(),
            a.invoke(),
            tok,
            Variant::Quiet,
            A11y::button(a.title.clone()).with_disabled(!a.enabled),
        ));
    }
    crate::a11y::attach(
        container(r)
            .width(Length::Fill)
            .style(move |_| style::panel(tok))
            .into(),
        &A11y::new("toolbar", Role::Group),
    )
}

/// Command bar: the same action row as [`toolbar`], denser.
///
/// ```
/// use icedtea::action::{Action, ActionTable};
/// use icedtea::i18n::Direction;
/// use icedtea::pattern;
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let mut table = ActionTable::new();
/// table.insert(Action::new("file.save", "Save", ()));
/// let _: icedtea::Element<'_, ()> =
///     pattern::command_bar(table.iter(), tok, Direction::Ltr);
/// ```
pub fn command_bar<'a, M: Clone + 'a>(
    actions: impl IntoIterator<Item = &'a Action<M>>,
    tok: Tokens,
    dir: Direction,
) -> Element<'a, M> {
    toolbar(actions, tok, dir)
}

/// Footer with action shortcut hints from the same table.
///
/// ```
/// use icedtea::action::{Action, ActionTable};
/// use icedtea::i18n::Direction;
/// use icedtea::pattern;
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let mut table = ActionTable::new();
/// table.insert(Action::new("file.save", "Save", ()));
/// let _: icedtea::Element<'_, ()> =
///     pattern::status_bar("ready", &table, tok, Direction::Ltr);
/// ```
pub fn status_bar<'a, M: Clone + 'a>(
    status: impl Into<String>,
    table: &ActionTable<M>,
    tok: Tokens,
    dir: Direction,
) -> Element<'a, M> {
    let status = status.into();
    let hints = table.footer_hints().join("  ·  ");
    let left = meta(status.clone(), tok, A11y::new(status, Role::Status));
    let right = meta(hints.clone(), tok, A11y::new(hints, Role::Status));
    let ends = order(dir, [left, right]);
    let mut ends = ends.into_iter();
    crate::a11y::attach(
        container(
            row![
                ends.next().unwrap(),
                iced::widget::Space::new().width(Length::Fill),
                ends.next().unwrap(),
            ]
            .padding([8, 12]),
        )
        .width(Length::Fill)
        .style(move |_| style::footer(tok))
        .into(),
        &A11y::new("statusbar", Role::Status),
    )
}

/// Command palette overlay card over the action table.
///
/// ```
/// use icedtea::action::{Action, ActionTable};
/// use icedtea::pattern;
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let mut table = ActionTable::new();
/// table.insert(Action::new("file.save", "Save", ()));
/// let hits: Vec<_> = table.iter().collect();
/// let _: icedtea::Element<'_, ()> = pattern::command_palette_view(
///     "",
///     &hits,
///     0,
///     |_| (),
///     |_| (),
///     tok,
/// );
/// ```
pub fn command_palette_view<'a, M: Clone + 'a>(
    query: &str,
    results: &[&Action<M>],
    selected: usize,
    on_query: impl Fn(String) -> M + 'a,
    on_pick: impl Fn(usize) -> M + Copy + 'a,
    tok: Tokens,
) -> Element<'a, M> {
    let mut list = Column::new().spacing(2);
    for (i, a) in results.iter().enumerate() {
        list = list.push(themed_button(
            a.title.clone(),
            a.enabled.then(|| on_pick(i)),
            tok,
            if i == selected {
                Variant::Primary
            } else {
                Variant::Ghost
            },
            A11y::new(a.title.clone(), Role::MenuItem)
                .with_checked(i == selected)
                .with_disabled(!a.enabled),
        ));
    }
    container(
        column![
            themed_text_input(
                "Type a command",
                query,
                on_query,
                None,
                tok,
                A11y::new("palette-query", Role::TextBox),
                Some(iced::widget::Id::new("palette-query")),
            ),
            themed_scroll(
                list.into(),
                tok,
                A11y::new("palette-list", Role::List),
                false,
                None,
                None::<fn(_) -> M>,
            ),
        ]
        .spacing(8),
    )
    .padding(12)
    .width(480)
    .style(move |_| style::raised_card(tok))
    .into()
}

/// Empty / status page. Centered title, body, and optional action.
///
/// ```
/// use icedtea::pattern;
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let _: icedtea::Element<'_, ()> = pattern::status_page(
///     "Nothing here",
///     "Create an item to begin.",
///     Some(("New".into(), ())),
///     tok,
/// );
/// ```
pub fn status_page<'a, M: Clone + 'a>(
    title: impl Into<String>,
    body: impl Into<String>,
    action: Option<(String, M)>,
    tok: Tokens,
) -> Element<'a, M> {
    let title = title.into();
    let body = body.into();
    let mut col = column![
        label(title.clone(), tok, A11y::new(title, Role::Header)),
        meta(body.clone(), tok, A11y::new(body, Role::Status)),
    ]
    .spacing(8)
    .width(Length::Fill);
    if let Some((t, m)) = action {
        col = col.push(themed_button(
            t.clone(),
            Some(m),
            tok,
            Variant::Primary,
            A11y::button(t),
        ));
    }
    container(col)
        .padding(32)
        .center_x(Length::Fill)
        .center_y(Length::Fill)
        .width(Length::Fill)
        .height(Length::Fill)
        .into()
}

/// About dialog body.
///
/// ```
/// use icedtea::i18n::Catalog;
/// use icedtea::pattern;
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let cat = Catalog::builtin();
/// let _: icedtea::Element<'_, ()> =
///     pattern::about_page("icedtea", "0.2.0", "MIT", "Credits", tok, &cat);
/// ```
pub fn about_page<'a, M: Clone + 'a>(
    name: &'a str,
    version: &'a str,
    license: &'a str,
    credits: &'a str,
    tok: Tokens,
    cat: &'a Catalog,
) -> Element<'a, M> {
    group_box(
        cat.t("about"),
        column![
            text(name).size(typo::PAGE).color(tok.text),
            meta(version, tok, A11y::new(version, Role::Status)),
            meta(license, tok, A11y::new(license, Role::Status)),
            meta(credits, tok, A11y::new(credits, Role::Status)),
        ]
        .spacing(6.0)
        .into(),
        tok,
        A11y::new(cat.t("about"), Role::Dialog),
    )
}

/// Searchable preferences groups.
#[derive(Debug, Clone)]
pub struct PrefGroup {
    pub title: String,
    pub keys: Vec<(String, String)>,
}

pub fn filter_prefs<'a>(groups: &'a [PrefGroup], query: &str) -> Vec<&'a PrefGroup> {
    let q = query.trim().to_ascii_lowercase();
    if q.is_empty() {
        return groups.iter().collect();
    }
    groups
        .iter()
        .filter(|g| {
            g.title.to_ascii_lowercase().contains(&q)
                || g.keys.iter().any(|(k, v)| {
                    k.to_ascii_lowercase().contains(&q) || v.to_ascii_lowercase().contains(&q)
                })
        })
        .collect()
}

/// Searchable preferences groups.
///
/// ```
/// use icedtea::i18n::Catalog;
/// use icedtea::pattern::{self, PrefGroup};
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let cat = Catalog::builtin();
/// let groups = [PrefGroup {
///     title: "Editor".into(),
///     keys: vec![("tab width".into(), "4".into())],
/// }];
/// let _: icedtea::Element<'_, ()> =
///     pattern::preferences_page(&groups, "", |_| (), tok, &cat);
/// ```
pub fn preferences_page<'a, M: Clone + 'a>(
    groups: &'a [PrefGroup],
    query: &str,
    on_query: impl Fn(String) -> M + 'a,
    tok: Tokens,
    cat: &Catalog,
) -> Element<'a, M> {
    let filtered = filter_prefs(groups, query);
    let mut body = Column::new().spacing(12);
    if filtered.is_empty() {
        body = body.push(meta(
            cat.t("empty"),
            tok,
            A11y::new(cat.t("empty"), Role::Status),
        ));
    }
    for g in filtered {
        let mut lines = Column::new().spacing(4);
        for (k, v) in &g.keys {
            let line = format!("{k}: {v}");
            lines = lines.push(meta(line.clone(), tok, A11y::new(line, Role::Status)));
        }
        body = body.push(group_box(
            g.title.clone(),
            lines.into(),
            tok,
            A11y::new(g.title.clone(), Role::Group),
        ));
    }
    column![
        themed_text_input(
            cat.t("search"),
            query,
            on_query,
            None,
            tok,
            A11y::new(cat.t("search"), Role::TextBox),
            None,
        ),
        themed_scroll(
            body.into(),
            tok,
            A11y::new("prefs", Role::Group),
            false,
            None,
            None::<fn(_) -> M>,
        ),
    ]
    .spacing(12)
    .into()
}

/// List + detail split. `sidebar` is [`crate::layout::fixed`] or
/// [`crate::layout::FILL`]. Children fill their panes.
///
/// ```
/// use icedtea::a11y::A11y;
/// use icedtea::pattern;
/// use icedtea::theme;
/// use icedtea::widget;
/// let tok = theme::named("dark").tokens;
/// let _: icedtea::Element<'_, ()> = pattern::list_detail(
///     widget::label("Inbox", tok, A11y::new("Inbox", icedtea::a11y::Role::Header)),
///     widget::label("Detail", tok, A11y::new("Detail", icedtea::a11y::Role::Header)),
///     icedtea::layout::fixed(260.0),
///     tok,
/// );
/// ```
pub fn list_detail<'a, M: 'a>(
    list: Element<'a, M>,
    detail: Element<'a, M>,
    sidebar: Length,
    tok: Tokens,
) -> Element<'a, M> {
    row![
        container(list)
            .width(sidebar)
            .height(Length::Fill)
            .style(move |_| style::panel(tok)),
        container(detail)
            .width(Length::Fill)
            .height(Length::Fill)
            .padding(iced::Padding {
                top: 8.0,
                right: 16.0,
                bottom: 0.0,
                left: 16.0,
            }),
    ]
    .width(Length::Fill)
    .height(Length::Fill)
    .into()
}

/// Navigation view: sidebar + content; compact shows back.
///
/// `width` is the window inner width. Subscribe with
/// `iced::window::resize_events` and a non-capturing
/// `Subscription::map`; store the width in `update`.
///
/// ```
/// use icedtea::a11y::A11y;
/// use icedtea::i18n::Catalog;
/// use icedtea::nav::NavStack;
/// use icedtea::pattern;
/// use icedtea::theme;
/// use icedtea::widget;
/// let tok = theme::named("dark").tokens;
/// let nav = NavStack::new("home");
/// let cat = Catalog::builtin();
/// let _: icedtea::Element<'_, ()> = pattern::navigation_view(
///     widget::label("Mail", tok, A11y::new("Mail", icedtea::a11y::Role::Header)),
///     widget::label("Inbox", tok, A11y::new("Inbox", icedtea::a11y::Role::Header)),
///     &nav,
///     1600.0,
///     (),
///     tok,
///     &cat,
/// );
/// ```
pub fn navigation_view<'a, M: Clone + 'a>(
    sidebar: Element<'a, M>,
    content: Element<'a, M>,
    nav: &NavStack,
    width: f32,
    on_back: M,
    tok: Tokens,
    cat: &Catalog,
) -> Element<'a, M> {
    if crate::layout::Breakpoint::from_width(width).sidebar_beside() {
        list_detail(sidebar, content, crate::layout::fixed(260.0), tok)
    } else {
        let top = if nav.can_back() {
            themed_button(
                cat.t("back"),
                Some(on_back),
                tok,
                Variant::Quiet,
                A11y::button(cat.t("back")),
            )
        } else {
            crate::widget::icon_svg(Icon::Menu, tok, A11y::new("menu", Role::Image))
        };
        column![top, content].spacing(8).into()
    }
}

/// Tabbed document area. Tabs plus a filling body.
///
/// ```
/// use icedtea::a11y::A11y;
/// use icedtea::collection::Tabs;
/// use icedtea::pattern;
/// use icedtea::theme;
/// use icedtea::widget;
/// let tok = theme::named("dark").tokens;
/// let tabs = Tabs::new(["Notes", "Guide"]);
/// let _: icedtea::Element<'_, ()> = pattern::tab_view(
///     &tabs,
///     widget::label("Notes", tok, A11y::new("Notes", icedtea::a11y::Role::Header)),
///     |_| (),
///     |_| (),
///     tok,
/// );
/// ```
pub fn tab_view<'a, M: Clone + 'a>(
    tabs: &Tabs,
    body: Element<'a, M>,
    on_select: impl Fn(usize) -> M + Copy + 'a,
    on_close: impl Fn(usize) -> M + Copy + 'a,
    tok: Tokens,
) -> Element<'a, M> {
    column![
        tab_bar(tabs, on_select, on_close, tok, A11y::new("tabs", Role::Tab)),
        body
    ]
    .spacing(0)
    .into()
}

/// Main window chrome: menu, toolbar, docked center, status.
///
/// ```
/// use icedtea::a11y::A11y;
/// use icedtea::action::{Action, ActionTable};
/// use icedtea::i18n::{Catalog, Direction};
/// use icedtea::pattern;
/// use icedtea::theme;
/// use icedtea::widget;
/// let tok = theme::named("dark").tokens;
/// let mut table = ActionTable::new();
/// table.insert(Action::new("file.save", "Save", ()));
/// let cat = Catalog::builtin();
/// let _: icedtea::Element<'_, ()> = pattern::main_window(
///     pattern::menu_bar(&table, tok, Direction::Ltr, &cat),
///     pattern::toolbar(table.iter(), tok, Direction::Ltr),
///     widget::label("notes.txt", tok, A11y::new("doc", icedtea::a11y::Role::Header)),
///     pattern::status_bar("ok", &table, tok, Direction::Ltr),
///     tok,
/// );
/// ```
pub fn main_window<'a, M: Clone + 'a>(
    menu: Element<'a, M>,
    toolbar: Element<'a, M>,
    center: Element<'a, M>,
    status: Element<'a, M>,
    tok: Tokens,
) -> Element<'a, M> {
    container(layout::dock(
        Some(column![menu, toolbar].into()),
        Some(status),
        None,
        None,
        center,
    ))
    .width(Length::Fill)
    .height(Length::Fill)
    .style(move |_| style::shell(tok))
    .into()
}

/// In-window modal on a dim backdrop.
///
/// ```
/// use icedtea::a11y::A11y;
/// use icedtea::pattern;
/// use icedtea::theme;
/// use icedtea::widget;
/// let tok = theme::named("dark").tokens;
/// let _: icedtea::Element<'_, ()> = pattern::modal_card(
///     widget::label(" ", tok, A11y::new("dim", icedtea::a11y::Role::Status)),
///     pattern::dialog_sheet("Save", "Overwrite?", ("Save".into(), ()), None, tok),
/// );
/// ```
pub fn modal_card<'a, M: 'a>(backdrop: Element<'a, M>, card: Element<'a, M>) -> Element<'a, M> {
    layout::overlay_center(backdrop, card)
}

/// Confirm / message / save sheet.
///
/// ```
/// use icedtea::pattern;
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let _: icedtea::Element<'_, ()> = pattern::dialog_sheet(
///     "Save",
///     "Overwrite notes.txt?",
///     ("Save".into(), ()),
///     Some(("Cancel".into(), ())),
///     tok,
/// );
/// ```
pub fn dialog_sheet<'a, M: Clone + 'a>(
    title: impl Into<String>,
    body: impl Into<String>,
    accept: (String, M),
    cancel: Option<(String, M)>,
    tok: Tokens,
) -> Element<'a, M> {
    let title = title.into();
    let body = body.into();
    let mut actions = Row::new().spacing(8);
    if let Some((t, m)) = cancel {
        actions = actions.push(themed_button(
            t.clone(),
            Some(m),
            tok,
            Variant::Quiet,
            A11y::button(t),
        ));
    }
    actions = actions.push(themed_button(
        accept.0.clone(),
        Some(accept.1),
        tok,
        Variant::Primary,
        A11y::button(accept.0),
    ));
    group_box(
        title.clone(),
        column![
            label(body.clone(), tok, A11y::new(body, Role::Status)),
            actions,
        ]
        .spacing(12)
        .into(),
        tok,
        A11y::new(title, Role::Dialog),
    )
}

/// Context menu: vertical action list.
///
/// ```
/// use icedtea::action::{Action, ActionTable};
/// use icedtea::pattern;
/// use icedtea::theme;
/// let tok = theme::named("dark").tokens;
/// let mut table = ActionTable::new();
/// table.insert(Action::new("file.save", "Save", ()));
/// let _: icedtea::Element<'_, ()> = pattern::context_menu(table.iter(), tok);
/// ```
pub fn context_menu<'a, M: Clone + 'a>(
    actions: impl IntoIterator<Item = &'a Action<M>>,
    tok: Tokens,
) -> Element<'a, M> {
    let mut col = Column::new().spacing(2).padding(6);
    for a in actions {
        col = col.push(themed_button(
            a.title.clone(),
            a.invoke(),
            tok,
            Variant::Ghost,
            A11y::new(a.title.clone(), Role::MenuItem).with_disabled(!a.enabled),
        ));
    }
    container(col)
        .style(move |_| style::raised_card(tok))
        .into()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::action::Action;
    use crate::shortcut::Shortcut;
    use crate::theme::named;

    #[test]
    fn pref_filter_and_patterns_build() {
        let prefs = [
            PrefGroup {
                title: "Appearance".into(),
                keys: vec![("theme".into(), "dark".into())],
            },
            PrefGroup {
                title: "Editor".into(),
                keys: vec![("tab".into(), "4".into())],
            },
        ];
        assert_eq!(filter_prefs(&prefs, "").len(), 2);
        assert_eq!(filter_prefs(&prefs, "theme").len(), 1);
        assert_eq!(filter_prefs(&prefs, "nope").len(), 0);
        let tok = named("dark").tokens;
        let cat = Catalog::builtin();
        let mut table = ActionTable::new();
        table.insert(
            Action::new("file.save", "Save", ()).with_shortcut(Shortcut::parse("ctrl+s").unwrap()),
        );
        table.insert(Action::new("edit.undo", "Undo", ()));
        table.insert(Action::new("file.open", "Open", ()));
        let ltr = Direction::Ltr;
        let rtl = Direction::Rtl;
        let menus = menu_groups(&table);
        assert_eq!(menus.len(), 2);
        assert_eq!(menus[0].0, "file");
        assert_eq!(menu_heading(&cat, "file"), "File");
        assert_eq!(menu_heading(&cat, "custom"), "Custom");
        assert_eq!(menu_heading(&cat, ""), "");
        let save = table.iter().find(|a| a.id.as_str() == "file.save").unwrap();
        let host = if cfg!(target_os = "macos") {
            "cmd+s"
        } else {
            "ctrl+s"
        };
        assert!(
            menu_item_label(save).contains(host),
            "Save row must show the host chord {host}, got {}",
            menu_item_label(save)
        );
        let undo = table.iter().find(|a| a.id.as_str() == "edit.undo").unwrap();
        assert_eq!(menu_item_label(undo), "Undo");
        assert_eq!(
            pick_menu_message(&[("Open".into(), 1u8), ("Save".into(), 2)], "Save"),
            2
        );
        assert_eq!(
            bind_menu_pick(vec![("Open".into(), 1u8), ("Save".into(), 2)])("Open".into()),
            1
        );
        let _: Element<'_, ()> = menu_bar(&table, tok, ltr, &cat);
        let _: Element<'_, ()> = menu_bar(&table, tok, rtl, &cat);
        let empty = ActionTable::new();
        let _: Element<'_, ()> = menu_bar(&empty, tok, ltr, &cat);
        let mut disabled = ActionTable::new();
        let mut dead = Action::new("file.dead", "Dead", ());
        dead.enabled = false;
        disabled.insert(dead);
        let _: Element<'_, ()> = menu_bar(&disabled, tok, ltr, &cat);
        let _: Element<'_, ()> = dialog_sheet(
            "Save",
            "Overwrite notes.txt?",
            ("Save".into(), ()),
            Some(("Cancel".into(), ())),
            tok,
        );
        let _: Element<'_, ()> = dialog_sheet("Note", "Hello", ("OK".into(), ()), None, tok);
        let acts: Vec<_> = table.iter().collect();
        let src = include_str!("pattern.rs");
        let palette_src = src
            .split("pub fn command_palette_view")
            .nth(1)
            .unwrap()
            .split("pub fn status_page")
            .next()
            .unwrap();
        assert!(
            !palette_src.contains("A11y::new(query"),
            "query text is state; it must not be the node name"
        );
        assert!(palette_src.contains("A11y::new(\"palette-query\""));
        let pref_src = src
            .split("pub fn preferences_page")
            .nth(1)
            .unwrap()
            .split("pub fn list_detail")
            .next()
            .unwrap();
        assert!(!pref_src.contains("A11y::new(query"));
        let _: Element<'_, ()> = toolbar(acts.iter().copied(), tok, ltr);
        let _: Element<'_, ()> = toolbar(acts.iter().copied(), tok, rtl);
        let _: Element<'_, ()> = command_bar(table.iter(), tok, rtl);
        let _: Element<'_, ()> = status_bar("ready", &table, tok, ltr);
        let _: Element<'_, ()> = status_bar("ready", &table, tok, rtl);
        let ltr_ids: Vec<_> = order(ltr, table.iter().map(|a| a.id.as_str()));
        let rtl_ids: Vec<_> = order(rtl, table.iter().map(|a| a.id.as_str()));
        assert_eq!(ltr_ids.first(), Some(&"file.save"));
        assert_eq!(rtl_ids.first(), Some(&"file.open"));
        let loc = crate::i18n::Locale::new("ar");
        assert_eq!(loc.direction, Direction::Rtl);
        let res: Vec<&Action<()>> = table.iter().collect();
        let _: Element<'_, ()> = command_palette_view("", &res, 0, |_| (), |_| (), tok);
        let dead_res: Vec<&Action<()>> = disabled.iter().collect();
        let _: Element<'_, ()> = command_palette_view("q", &dead_res, 0, |_| (), |_| (), tok);
        let _: Element<'_, ()> = status_page("Empty", "Nothing", Some(("New".into(), ())), tok);
        let _: Element<'_, ()> = status_page("Empty", "Nothing", None, tok);
        let _: Element<'_, ()> = about_page("App", "0.1.0", "MIT", "us", tok, &cat);
        let _: Element<'_, ()> = preferences_page(&prefs, "", |_| (), tok, &cat);
        let _: Element<'_, ()> = preferences_page(&prefs, "nope", |_| (), tok, &cat);
        let lab = |s: &str| crate::widget::label::<()>(s, tok, A11y::new(s, Role::Header));
        let _: Element<'_, ()> = list_detail(lab("l"), lab("d"), crate::layout::fixed(260.0), tok);
        let _: Element<'_, ()> = list_detail(lab("l"), lab("d"), crate::layout::FILL, tok);
        let nav = NavStack::new("home");
        let mut deep = nav.clone();
        deep.push("x");
        let _: Element<'_, ()> = navigation_view(lab("s"), lab("c"), &nav, 900.0, (), tok, &cat);
        let _: Element<'_, ()> = navigation_view(lab("s"), lab("c"), &deep, 400.0, (), tok, &cat);
        let _: Element<'_, ()> = navigation_view(lab("s"), lab("c"), &nav, 400.0, (), tok, &cat);
        let tabs = Tabs::new(["A"]);
        let _: Element<'_, ()> = tab_view(&tabs, lab("b"), |_| (), |_| (), tok);
        let _: Element<'_, ()> = main_window(lab("m"), lab("t"), lab("c"), lab("s"), tok);
        let _: Element<'_, ()> = modal_card(lab("b"), lab("c"));
        let _: Element<'_, ()> = context_menu(table.iter(), tok);
        fn paint(el: &mut Element<'_, ()>) {
            use iced::advanced::layout::{Layout, Limits};
            use iced::advanced::renderer::Style;
            use iced::advanced::widget::Tree;
            use iced::mouse;
            use iced::{Font, Pixels, Point, Rectangle, Size, Theme};
            let mut tree = Tree::new(el.as_widget());
            let mut renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
                Font::DEFAULT,
                Pixels::from(16u32),
            ));
            let limits = Limits::new(Size::ZERO, Size::new(640.0, 400.0));
            let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
            let layout = Layout::new(&node);
            let viewport = Rectangle::new(Point::ORIGIN, Size::new(640.0, 400.0));
            el.as_widget().draw(
                &tree,
                &mut renderer,
                &Theme::Dark,
                &Style::default(),
                layout,
                mouse::Cursor::Unavailable,
                &viewport,
            );
        }
        let mut bar = menu_bar(&table, tok, ltr, &cat);
        paint(&mut bar);
        let mut tb = toolbar(acts.iter().copied(), tok, ltr);
        paint(&mut tb);
        let mut sb = status_bar("ready", &table, tok, ltr);
        paint(&mut sb);
        let mut pal = command_palette_view("", &res, 0, |_| (), |_| (), tok);
        paint(&mut pal);
        let mut page = status_page("Empty", "Nothing", Some(("New".into(), ())), tok);
        paint(&mut page);
        let mut about = about_page("App", "0.1.0", "MIT", "us", tok, &cat);
        paint(&mut about);
        let mut prefs_el = preferences_page(&prefs, "", |_| (), tok, &cat);
        paint(&mut prefs_el);
        let mut mw = main_window(lab("m"), lab("t"), lab("c"), lab("s"), tok);
        paint(&mut mw);
        let mut cm = context_menu(table.iter(), tok);
        paint(&mut cm);
        let mut ld = list_detail(lab("l"), lab("d"), crate::layout::fixed(260.0), tok);
        paint(&mut ld);
        let mut nv = navigation_view(lab("s"), lab("c"), &nav, 900.0, (), tok, &cat);
        paint(&mut nv);
        let mut tv = tab_view(&tabs, lab("b"), |_| (), |_| (), tok);
        paint(&mut tv);
        let mut mc = modal_card(lab("b"), lab("c"));
        paint(&mut mc);
        let mut cb = command_bar(table.iter(), tok, ltr);
        paint(&mut cb);
        let mut dlg = dialog_sheet("Note", "Hello", ("OK".into(), ()), None, tok);
        paint(&mut dlg);
    }
}