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
use crate::widgets::DEFAULT_CORNER_RADIUS;
use crate::{ButtonBuilder, EventCtx, Panel, Widget};
use geom::CornerRadii;

struct Tab {
    tab_id: String,
    bar_item: ButtonBuilder<'static, 'static>,
    content: Widget,
}

impl Tab {
    fn new(tab_id: String, bar_item: ButtonBuilder<'static, 'static>, content: Widget) -> Self {
        Self {
            tab_id,
            bar_item,
            content,
        }
    }

    fn build_bar_item_widget(&self, ctx: &EventCtx, active: bool) -> Widget {
        self.bar_item
            .clone()
            .corner_rounding(CornerRadii {
                top_left: DEFAULT_CORNER_RADIUS,
                top_right: DEFAULT_CORNER_RADIUS,
                bottom_left: 0.0,
                bottom_right: 0.0,
            })
            .disabled(active)
            .build_widget(ctx, &self.tab_id)
    }
}

pub struct TabController {
    id: String,
    tabs: Vec<Tab>,
    active_tab_idx: usize,
}

impl TabController {
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            tabs: vec![],
            active_tab_idx: 0,
        }
    }

    /// Add a new tab.
    ///
    /// `bar_item`: The button shown in the tab bar
    /// `content`: The content shown when this tab's `bar_item` is clicked
    pub fn push_tab(&mut self, bar_item: ButtonBuilder<'static, 'static>, content: Widget) {
        let tab_id = self.tab_id(self.tabs.len() + 1);
        let tab = Tab::new(tab_id, bar_item, content);
        self.tabs.push(tab);
    }

    /// A widget containing the tab bar and a content pane with the currently active tab.
    // TODO: Clarify that this can only be called once - maybe `TabBuilder.into_tab_controller()`
    pub fn build_widget(&mut self, ctx: &EventCtx) -> Widget {
        Widget::custom_col(vec![
            self.build_bar_items(ctx),
            self.pop_active_content()
                .container()
                .tab_body(ctx)
                .named(self.active_content_id()),
        ])
    }

    pub fn handle_action(&mut self, ctx: &EventCtx, action: &str, panel: &mut Panel) -> bool {
        if !action.starts_with(&self.id) {
            return false;
        }

        let tab_idx = self
            .tabs
            .iter()
            .enumerate()
            .find(|(_idx, tab)| tab.tab_id == action)
            .unwrap_or_else(|| panic!("invalid tab id: {}", action))
            .0;
        self.activate_tab(ctx, tab_idx, panel);
        true
    }

    pub fn active_tab_idx(&self) -> usize {
        self.active_tab_idx
    }

    fn active_content_id(&self) -> String {
        format!("{}_active_content", self.id)
    }

    fn bar_items_id(&self) -> String {
        format!("{}_bar_items", self.id)
    }

    fn tab_id(&self, tab_index: usize) -> String {
        format!("{}_tab_{}", self.id, tab_index)
    }

    fn pop_active_content(&mut self) -> Widget {
        let mut tmp = Widget::nothing();
        assert!(
            self.tabs.get(self.active_tab_idx).is_some(),
            "must add at least one tab before rendering"
        );
        std::mem::swap(&mut self.tabs[self.active_tab_idx].content, &mut tmp);
        tmp
    }

    fn build_bar_items(&self, ctx: &EventCtx) -> Widget {
        let bar_items = self
            .tabs
            .iter()
            .enumerate()
            .map(|(idx, tab)| tab.build_bar_item_widget(ctx, idx == self.active_tab_idx))
            .collect();

        Widget::row(bar_items)
            .container()
            .named(self.bar_items_id())
    }

    fn activate_tab(&mut self, ctx: &EventCtx, tab_idx: usize, panel: &mut Panel) {
        let old_idx = self.active_tab_idx;
        self.active_tab_idx = tab_idx;

        let mut bar_items = self.build_bar_items(ctx);
        panel.swap_inner_content(ctx, &self.bar_items_id(), &mut bar_items);

        let mut content = self.pop_active_content();
        panel.swap_inner_content(ctx, &self.active_content_id(), &mut content);
        self.tabs[old_idx].content = content;
    }
}