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
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0

//! Navigation side panel for switching between views.
//!
//! For details on the model, see the [`segmented_button`] module for more details.

use apply::Apply;
use iced::{
    clipboard::{dnd::DndAction, mime::AllowedMimeTypes},
    Background, Length,
};
use iced_core::{Border, Color, Shadow};

use crate::widget::{container, menu, scrollable, segmented_button, Container, Icon};
use crate::{theme, Theme};

use super::dnd_destination::DragId;

pub type Id = segmented_button::Entity;
pub type Model = segmented_button::SingleSelectModel;

/// Navigation side panel for switching between views.
///
/// For details on the model, see the [`segmented_button`] module for more details.
pub fn nav_bar<Message: Clone + 'static>(
    model: &segmented_button::SingleSelectModel,
    on_activate: fn(segmented_button::Entity) -> Message,
) -> NavBar<Message> {
    NavBar {
        segmented_button: segmented_button::vertical(model).on_activate(on_activate),
    }
}

/// Navigation side panel for switching between views.
/// Can receive drag and drop events.
pub fn nav_bar_dnd<Message, D: AllowedMimeTypes>(
    model: &segmented_button::SingleSelectModel,
    on_activate: fn(segmented_button::Entity) -> Message,
    on_dnd_enter: impl Fn(segmented_button::Entity, Vec<String>) -> Message + 'static,
    on_dnd_leave: impl Fn(segmented_button::Entity) -> Message + 'static,
    on_dnd_drop: impl Fn(segmented_button::Entity, Option<D>, DndAction) -> Message + 'static,
    id: DragId,
) -> NavBar<Message>
where
    Message: Clone + 'static,
{
    NavBar {
        segmented_button: segmented_button::vertical(model)
            .on_activate(on_activate)
            .on_dnd_enter(on_dnd_enter)
            .on_dnd_leave(on_dnd_leave)
            .on_dnd_drop(on_dnd_drop)
            .drag_id(id),
    }
}

#[must_use]
pub struct NavBar<'a, Message> {
    segmented_button:
        segmented_button::VerticalSegmentedButton<'a, segmented_button::SingleSelect, Message>,
}

impl<'a, Message: Clone + 'static> NavBar<'a, Message> {
    pub fn close_icon(mut self, close_icon: Icon) -> Self {
        self.segmented_button = self.segmented_button.close_icon(close_icon);
        self
    }

    pub fn context_menu(mut self, context_menu: Option<Vec<menu::Tree<'a, Message>>>) -> Self {
        self.segmented_button = self.segmented_button.context_menu(context_menu);
        self
    }

    pub fn drag_id(mut self, id: DragId) -> Self {
        self.segmented_button = self.segmented_button.drag_id(id);
        self
    }

    /// Pre-convert this widget into the [`Container`] widget that it becomes.
    #[must_use]
    pub fn into_container(self) -> Container<'a, Message, crate::Theme, crate::Renderer> {
        Container::from(self)
    }

    /// Emitted when a tab close button is pressed.
    pub fn on_close<T>(mut self, on_close: T) -> Self
    where
        T: Fn(Id) -> Message + 'static,
    {
        self.segmented_button = self.segmented_button.on_close(on_close);
        self
    }

    /// Emitted when a button is right-clicked.
    pub fn on_context<T>(mut self, on_context: T) -> Self
    where
        T: Fn(Id) -> Message + 'static,
    {
        self.segmented_button = self.segmented_button.on_context(on_context);
        self
    }

    /// Emitted when the middle mouse button is pressed on a button.
    pub fn on_middle_press<T>(mut self, on_middle_press: T) -> Self
    where
        T: Fn(Id) -> Message + 'static,
    {
        self.segmented_button = self.segmented_button.on_middle_press(on_middle_press);
        self
    }

    /// Handle the dnd drop event.
    pub fn on_dnd_drop<D: AllowedMimeTypes>(
        mut self,
        handler: impl Fn(Id, Option<D>, DndAction) -> Message + 'static,
    ) -> Self {
        self.segmented_button = self.segmented_button.on_dnd_drop(handler);
        self
    }

    /// Handle the dnd enter event.
    pub fn on_dnd_enter(mut self, handler: impl Fn(Id, Vec<String>) -> Message + 'static) -> Self {
        self.segmented_button = self.segmented_button.on_dnd_enter(handler);
        self
    }

    /// Handle the dnd leave event.
    pub fn on_dnd_leave(mut self, handler: impl Fn(Id) -> Message + 'static) -> Self {
        self.segmented_button = self.segmented_button.on_dnd_leave(handler);
        self
    }
}

impl<'a, Message: Clone + 'static> From<NavBar<'a, Message>>
    for Container<'a, Message, crate::Theme, crate::Renderer>
{
    fn from(this: NavBar<'a, Message>) -> Self {
        let theme = crate::theme::active();
        let space_s = theme.cosmic().space_s();
        let space_xxs = theme.cosmic().space_xxs();

        this.segmented_button
            .button_height(32)
            .button_padding([space_s, space_xxs, space_s, space_xxs])
            .button_spacing(space_xxs)
            .spacing(space_xxs)
            .style(crate::theme::SegmentedButton::TabBar)
            .apply(container)
            .padding(space_xxs)
            .apply(scrollable)
            .class(crate::style::iced::Scrollable::Minimal)
            .height(Length::Fill)
            .apply(container)
            .height(Length::Fill)
            .class(theme::Container::custom(nav_bar_style))
    }
}

impl<'a, Message: Clone + 'static> From<NavBar<'a, Message>> for crate::Element<'a, Message> {
    fn from(this: NavBar<'a, Message>) -> Self {
        Container::from(this).into()
    }
}

#[must_use]
pub fn nav_bar_style(theme: &Theme) -> iced_widget::container::Style {
    let cosmic = &theme.cosmic();
    iced_widget::container::Style {
        icon_color: Some(cosmic.on_bg_color().into()),
        text_color: Some(cosmic.on_bg_color().into()),
        background: Some(Background::Color(cosmic.primary.base.into())),
        border: Border {
            width: 0.0,
            color: Color::TRANSPARENT,
            radius: cosmic.corner_radii.radius_s.into(),
        },
        shadow: Shadow::default(),
    }
}