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
use crate::{
    core::{
        pool::Handle,
        color::Color,
    },
    UserInterface,
    widget::{
        Widget,
        WidgetBuilder,
    },
    Control,
    UINode,
    border::BorderBuilder,
    button::ButtonBuilder,
    grid::{
        GridBuilder,
        Column,
        Row,
    },
    brush::Brush,
    message::{
        ButtonMessage,
        UiMessage,
        UiMessageData,
        WidgetMessage
    },
    NodeHandleMapping,
    BuildContext,
};
use std::ops::{Deref, DerefMut};

pub struct Tab<M: 'static, C: 'static + Control<M, C>> {
    header_button: Handle<UINode<M, C>>,
    content: Handle<UINode<M, C>>,
}

pub struct TabControl<M: 'static, C: 'static + Control<M, C>> {
    widget: Widget<M, C>,
    tabs: Vec<Tab<M, C>>,
}

impl<M: 'static, C: 'static + Control<M, C>> Deref for TabControl<M, C> {
    type Target = Widget<M, C>;

    fn deref(&self) -> &Self::Target {
        &self.widget
    }
}

impl<M: 'static, C: 'static + Control<M, C>> DerefMut for TabControl<M, C> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.widget
    }
}

impl<M: 'static, C: 'static + Control<M, C>> Control<M, C> for TabControl<M, C> {
    fn raw_copy(&self) -> UINode<M, C> {
        UINode::TabControl(Self {
            widget: self.widget.raw_copy(),
            tabs: Default::default(),
        })
    }

    fn resolve(&mut self, node_map: &NodeHandleMapping<M, C>) {
        for tab in self.tabs.iter_mut() {
            tab.header_button = *node_map.get(&tab.header_button).unwrap();
            tab.content = *node_map.get(&tab.content).unwrap();
        }
    }

    fn handle_routed_message(&mut self, ui: &mut UserInterface<M, C>, message: &mut UiMessage<M, C>) {
        self.widget.handle_routed_message(ui, message);

        if let UiMessageData::Button(msg) = &message.data {
            if let ButtonMessage::Click = msg {
                for (i, tab) in self.tabs.iter().enumerate() {
                    if message.destination == tab.header_button && tab.header_button.is_some() && tab.content.is_some() {
                        for (j, other_tab) in self.tabs.iter().enumerate() {
                            ui.send_message(WidgetMessage::visibility(other_tab.content, j == i));
                        }
                        break;
                    }
                }
            }
        }
    }

    fn remove_ref(&mut self, handle: Handle<UINode<M, C>>) {
        for tab in self.tabs.iter_mut() {
            if tab.content == handle {
                tab.content = Handle::NONE;
            }
            if tab.header_button == handle {
                tab.header_button = Handle::NONE;
            }
        }
    }
}

pub struct TabControlBuilder<M: 'static, C: 'static + Control<M, C>> {
    widget_builder: WidgetBuilder<M, C>,
    tabs: Vec<TabDefinition<M, C>>,
}

pub struct TabDefinition<M: 'static, C: 'static + Control<M, C>> {
    pub header: Handle<UINode<M, C>>,
    pub content: Handle<UINode<M, C>>,
}

impl<M: 'static, C: 'static + Control<M, C>> TabControlBuilder<M, C> {
    pub fn new(widget_builder: WidgetBuilder<M, C>) -> Self {
        Self {
            widget_builder,
            tabs: Default::default(),
        }
    }

    pub fn with_tab(mut self, tab: TabDefinition<M, C>) -> Self {
        self.tabs.push(tab);
        self
    }

    pub fn build(self, ctx: &mut BuildContext<M, C>) -> Handle<UINode<M, C>> {
        let mut headers = Vec::new();
        let mut content = Vec::new();
        let tab_count = self.tabs.len();
        for (i, tab) in self.tabs.into_iter().enumerate() {
            headers.push(tab.header);
            // Hide everything but first tab content.
            if i > 0 {
                ctx[tab.content].set_visibility(false);
            }
            content.push(tab.content);
        }

        let tab_buttons = headers
            .into_iter()
            .enumerate()
            .map(|(i, header)| {
                ButtonBuilder::new(WidgetBuilder::new()
                    .on_column(i))
                    .with_content(header)
                    .build(ctx)
            }).collect::<Vec<Handle<UINode<M, C>>>>();

        let headers_grid = GridBuilder::new(WidgetBuilder::new()
            .with_children(&tab_buttons)
            .on_row(0))
            .add_row(Row::auto())
            .add_columns((0..tab_count)
                .map(|_| Column::auto())
                .collect())
            .build(ctx);

        let content_grid = GridBuilder::new(WidgetBuilder::new()
            .with_children(&content)
            .on_row(1))
            .build(ctx);

        let grid = GridBuilder::new(WidgetBuilder::new()
            .with_child(headers_grid)
            .with_child(content_grid))
            .add_column(Column::auto())
            .add_row(Row::strict(30.0))
            .add_row(Row::auto())
            .build(ctx);

        let tc = TabControl {
            widget: self.widget_builder
                .with_child(BorderBuilder::new(WidgetBuilder::new()
                    .with_background(Brush::Solid(Color::from_rgba(0, 0, 0, 0)))
                    .with_child(grid))
                    .build(ctx))
                .build(),
            tabs: tab_buttons.iter()
                .zip(content)
                .map(|(tab_button, content)| {
                    Tab {
                        header_button: *tab_button,
                        content,
                    }
                })
                .collect(),
        };

        ctx.add_node(UINode::TabControl(tc))
    }
}