2023-01-06 01:39:09 +01:00
|
|
|
// Copyright 2022 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-01-17 18:49:40 +01:00
|
|
|
use super::model::{Entity, Model, Selectable};
|
2023-12-13 12:59:26 -05:00
|
|
|
use crate::theme::{SegmentedButton as Style, THEME};
|
2023-09-01 07:29:19 +02:00
|
|
|
use crate::widget::{icon, Icon};
|
|
|
|
|
use crate::{Element, Renderer};
|
2023-01-04 05:37:20 +01:00
|
|
|
use derive_setters::Setters;
|
|
|
|
|
use iced::{
|
2023-09-01 07:29:19 +02:00
|
|
|
alignment, event, keyboard, mouse, touch, Background, Color, Command, Event, Length, Rectangle,
|
|
|
|
|
Size,
|
2023-01-04 05:37:20 +01:00
|
|
|
};
|
2024-01-22 15:57:59 +01:00
|
|
|
use iced_core::mouse::ScrollDelta;
|
2023-11-30 14:01:42 -05:00
|
|
|
use iced_core::text::{LineHeight, Paragraph, Renderer as TextRenderer, Shaping};
|
2023-05-30 12:03:15 -04:00
|
|
|
use iced_core::widget::{self, operation, tree};
|
|
|
|
|
use iced_core::{layout, renderer, widget::Tree, Clipboard, Layout, Shell, Widget};
|
2024-01-22 14:06:41 +01:00
|
|
|
use iced_core::{Point, Renderer as IcedRenderer, Text};
|
2024-01-22 15:57:59 +01:00
|
|
|
use slotmap::{Key, SecondaryMap};
|
2023-02-13 15:57:30 +01:00
|
|
|
use std::marker::PhantomData;
|
2024-01-22 15:57:59 +01:00
|
|
|
use std::time::{Duration, Instant};
|
2023-01-04 05:37:20 +01:00
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
/// A command that focuses a segmented item stored in a widget.
|
|
|
|
|
pub fn focus<Message: 'static>(id: Id) -> Command<Message> {
|
|
|
|
|
Command::widget(operation::focusable::focus(id.0))
|
2023-01-09 16:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
/// Isolates variant-specific behaviors from [`SegmentedButton`].
|
|
|
|
|
pub trait SegmentedVariant {
|
|
|
|
|
/// Get the appearance for this variant of the widget.
|
|
|
|
|
fn variant_appearance(
|
2023-09-01 07:29:19 +02:00
|
|
|
theme: &crate::Theme,
|
|
|
|
|
style: &crate::theme::SegmentedButton,
|
|
|
|
|
) -> super::Appearance;
|
2023-01-04 05:37:20 +01:00
|
|
|
|
|
|
|
|
/// Calculates the bounds for the given button by its position.
|
2024-01-23 14:31:37 +01:00
|
|
|
fn variant_button_bounds(
|
|
|
|
|
&self,
|
|
|
|
|
state: &LocalState,
|
|
|
|
|
bounds: Rectangle,
|
|
|
|
|
position: usize,
|
|
|
|
|
) -> Option<Rectangle>;
|
2023-01-04 05:37:20 +01:00
|
|
|
|
|
|
|
|
/// Calculates the layout of this variant.
|
2023-11-30 14:01:42 -05:00
|
|
|
fn variant_layout(
|
|
|
|
|
&self,
|
|
|
|
|
state: &mut LocalState,
|
|
|
|
|
renderer: &crate::Renderer,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
) -> layout::Node;
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-17 18:49:40 +01:00
|
|
|
/// A conjoined group of items that function together as a button.
|
2023-01-04 05:37:20 +01:00
|
|
|
#[derive(Setters)]
|
2024-01-22 14:06:41 +01:00
|
|
|
#[must_use]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub struct SegmentedButton<'a, Variant, SelectionMode, Message>
|
2023-01-04 05:37:20 +01:00
|
|
|
where
|
2023-01-17 18:49:40 +01:00
|
|
|
Model<SelectionMode>: Selectable,
|
|
|
|
|
SelectionMode: Default,
|
2023-01-04 05:37:20 +01:00
|
|
|
{
|
2023-01-09 16:18:02 +01:00
|
|
|
/// The model borrowed from the application create this widget.
|
2023-01-04 05:37:20 +01:00
|
|
|
#[setters(skip)]
|
2023-01-17 18:49:40 +01:00
|
|
|
pub(super) model: &'a Model<SelectionMode>,
|
|
|
|
|
/// iced widget ID
|
2023-01-09 16:18:02 +01:00
|
|
|
pub(super) id: Option<Id>,
|
2023-02-13 15:57:30 +01:00
|
|
|
/// The icon used for the close button.
|
2023-09-01 07:29:19 +02:00
|
|
|
pub(super) close_icon: Icon,
|
2023-02-13 15:57:30 +01:00
|
|
|
/// Show the close icon only when item is hovered.
|
|
|
|
|
pub(super) show_close_icon_on_hover: bool,
|
2023-01-06 01:39:09 +01:00
|
|
|
/// Padding around a button.
|
|
|
|
|
pub(super) button_padding: [u16; 4],
|
|
|
|
|
/// Desired height of a button.
|
|
|
|
|
pub(super) button_height: u16,
|
|
|
|
|
/// Spacing between icon and text in button.
|
|
|
|
|
pub(super) button_spacing: u16,
|
2024-01-23 14:31:37 +01:00
|
|
|
/// Minimum width of a button.
|
|
|
|
|
pub(super) minimum_button_width: u16,
|
2023-11-16 08:00:33 -07:00
|
|
|
/// Spacing for each indent.
|
|
|
|
|
pub(super) indent_spacing: u16,
|
2023-01-04 05:37:20 +01:00
|
|
|
/// Desired font for active tabs.
|
2023-09-01 07:29:19 +02:00
|
|
|
pub(super) font_active: Option<crate::font::Font>,
|
2023-01-04 05:37:20 +01:00
|
|
|
/// Desired font for hovered tabs.
|
2023-09-01 07:29:19 +02:00
|
|
|
pub(super) font_hovered: Option<crate::font::Font>,
|
2023-01-04 05:37:20 +01:00
|
|
|
/// Desired font for inactive tabs.
|
2023-09-01 07:29:19 +02:00
|
|
|
pub(super) font_inactive: Option<crate::font::Font>,
|
2023-01-27 04:44:25 +01:00
|
|
|
/// Size of the font.
|
2023-05-30 12:03:15 -04:00
|
|
|
pub(super) font_size: f32,
|
2023-01-04 05:37:20 +01:00
|
|
|
/// Desired width of the widget.
|
|
|
|
|
pub(super) width: Length,
|
|
|
|
|
/// Desired height of the widget.
|
|
|
|
|
pub(super) height: Length,
|
2023-01-09 16:18:02 +01:00
|
|
|
/// Desired spacing between items.
|
2023-01-04 05:37:20 +01:00
|
|
|
pub(super) spacing: u16,
|
2023-05-30 12:03:15 -04:00
|
|
|
/// LineHeight of the font.
|
|
|
|
|
pub(super) line_height: LineHeight,
|
2023-01-04 05:37:20 +01:00
|
|
|
/// Style to draw the widget in.
|
|
|
|
|
#[setters(into)]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub(super) style: Style,
|
2023-02-13 15:57:30 +01:00
|
|
|
/// Emits the ID of the item that was activated.
|
2023-09-29 16:14:03 -04:00
|
|
|
#[setters(skip)]
|
|
|
|
|
pub(super) on_activate: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
|
|
|
|
|
#[setters(skip)]
|
|
|
|
|
pub(super) on_close: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
|
2023-01-04 05:37:20 +01:00
|
|
|
#[setters(skip)]
|
|
|
|
|
/// Defines the implementation of this struct
|
|
|
|
|
variant: PhantomData<Variant>,
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
impl<'a, Variant, SelectionMode, Message> SegmentedButton<'a, Variant, SelectionMode, Message>
|
2023-01-04 05:37:20 +01:00
|
|
|
where
|
2023-09-01 07:29:19 +02:00
|
|
|
Self: SegmentedVariant,
|
2023-01-17 18:49:40 +01:00
|
|
|
Model<SelectionMode>: Selectable,
|
|
|
|
|
SelectionMode: Default,
|
2023-01-04 05:37:20 +01:00
|
|
|
{
|
2023-01-17 18:49:40 +01:00
|
|
|
pub fn new(model: &'a Model<SelectionMode>) -> Self {
|
2023-01-04 05:37:20 +01:00
|
|
|
Self {
|
2023-01-17 18:49:40 +01:00
|
|
|
model,
|
2023-01-09 16:18:02 +01:00
|
|
|
id: None,
|
2023-09-13 15:47:32 +02:00
|
|
|
close_icon: icon::from_name("window-close-symbolic").size(16).icon(),
|
2023-02-13 15:57:30 +01:00
|
|
|
show_close_icon_on_hover: false,
|
2023-01-06 01:39:09 +01:00
|
|
|
button_padding: [4, 4, 4, 4],
|
|
|
|
|
button_height: 32,
|
|
|
|
|
button_spacing: 4,
|
2024-01-23 14:31:37 +01:00
|
|
|
minimum_button_width: 150,
|
2023-11-16 08:00:33 -07:00
|
|
|
indent_spacing: 16,
|
2023-05-30 12:03:15 -04:00
|
|
|
font_active: None,
|
|
|
|
|
font_hovered: None,
|
|
|
|
|
font_inactive: None,
|
2023-05-30 23:46:49 +02:00
|
|
|
font_size: 14.0,
|
2023-01-04 05:37:20 +01:00
|
|
|
height: Length::Shrink,
|
|
|
|
|
width: Length::Fill,
|
|
|
|
|
spacing: 0,
|
2023-05-30 12:03:15 -04:00
|
|
|
line_height: LineHeight::default(),
|
2023-09-01 07:29:19 +02:00
|
|
|
style: Style::default(),
|
2023-01-04 05:37:20 +01:00
|
|
|
on_activate: None,
|
2023-02-13 15:57:30 +01:00
|
|
|
on_close: None,
|
2023-01-04 05:37:20 +01:00
|
|
|
variant: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-29 16:14:03 -04:00
|
|
|
pub fn on_activate<T>(mut self, on_activate: T) -> Self
|
|
|
|
|
where
|
|
|
|
|
T: Fn(Entity) -> Message + 'static,
|
|
|
|
|
{
|
|
|
|
|
self.on_activate = Some(Box::new(on_activate));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn on_close<T>(mut self, on_close: T) -> Self
|
|
|
|
|
where
|
|
|
|
|
T: Fn(Entity) -> Message + 'static,
|
|
|
|
|
{
|
|
|
|
|
self.on_close = Some(Box::new(on_close));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 18:18:54 +01:00
|
|
|
/// Check if an item is enabled.
|
2023-01-17 18:49:40 +01:00
|
|
|
fn is_enabled(&self, key: Entity) -> bool {
|
2023-01-09 18:18:54 +01:00
|
|
|
self.model.items.get(key).map_or(false, |item| item.enabled)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Focus the previous item in the widget.
|
2023-01-09 16:18:02 +01:00
|
|
|
fn focus_previous(&mut self, state: &mut LocalState) -> event::Status {
|
2024-01-23 14:31:37 +01:00
|
|
|
match state.focused_item {
|
|
|
|
|
Focus::Tab(entity) => {
|
|
|
|
|
let mut keys = self.iterate_visible_tabs(state).rev();
|
|
|
|
|
|
|
|
|
|
while let Some(key) = keys.next() {
|
|
|
|
|
if key == entity {
|
|
|
|
|
for key in keys {
|
|
|
|
|
// Skip disabled buttons.
|
|
|
|
|
if !self.is_enabled(key) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.focused_item = Focus::Tab(key);
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2023-01-09 19:26:31 +01:00
|
|
|
}
|
2024-01-23 14:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.prev_tab_sensitive(state) {
|
|
|
|
|
state.focused_item = Focus::PrevButton;
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 19:26:31 +01:00
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
Focus::NextButton => {
|
|
|
|
|
if let Some(last) = self.last_tab(state) {
|
|
|
|
|
state.focused_item = Focus::Tab(last);
|
2023-01-09 18:18:54 +01:00
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
2024-01-23 14:31:37 +01:00
|
|
|
}
|
2023-01-09 18:18:54 +01:00
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
Focus::None => {
|
|
|
|
|
if self.next_tab_sensitive(state) {
|
|
|
|
|
state.focused_item = Focus::NextButton;
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
} else if let Some(last) = self.last_tab(state) {
|
|
|
|
|
state.focused_item = Focus::Tab(last);
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
2023-01-09 16:18:02 +01:00
|
|
|
}
|
2024-01-23 14:31:37 +01:00
|
|
|
|
|
|
|
|
Focus::PrevButton | Focus::Set => (),
|
2023-01-09 16:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
state.focused_item = Focus::None;
|
2023-01-09 16:18:02 +01:00
|
|
|
event::Status::Ignored
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 18:18:54 +01:00
|
|
|
/// Focus the next item in the widget.
|
2023-01-09 16:18:02 +01:00
|
|
|
fn focus_next(&mut self, state: &mut LocalState) -> event::Status {
|
2024-01-23 14:31:37 +01:00
|
|
|
match state.focused_item {
|
|
|
|
|
Focus::Tab(entity) => {
|
|
|
|
|
let mut keys = self.iterate_visible_tabs(state);
|
|
|
|
|
while let Some(key) = keys.next() {
|
|
|
|
|
if key == entity {
|
|
|
|
|
for key in keys {
|
|
|
|
|
// Skip disabled buttons.
|
|
|
|
|
if !self.is_enabled(key) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.focused_item = Focus::Tab(key);
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2023-01-09 16:18:02 +01:00
|
|
|
}
|
2024-01-23 14:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.next_tab_sensitive(state) {
|
|
|
|
|
state.focused_item = Focus::NextButton;
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 18:18:54 +01:00
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
Focus::PrevButton => {
|
|
|
|
|
if let Some(first) = self.first_tab(state) {
|
|
|
|
|
state.focused_item = Focus::Tab(first);
|
2023-01-09 18:18:54 +01:00
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
2024-01-23 14:31:37 +01:00
|
|
|
}
|
2023-01-09 18:18:54 +01:00
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
Focus::None => {
|
|
|
|
|
if self.prev_tab_sensitive(state) {
|
|
|
|
|
state.focused_item = Focus::PrevButton;
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
} else if let Some(first) = self.first_tab(state) {
|
|
|
|
|
state.focused_item = Focus::Tab(first);
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
2023-01-09 16:18:02 +01:00
|
|
|
}
|
2024-01-23 14:31:37 +01:00
|
|
|
|
|
|
|
|
Focus::NextButton | Focus::Set => (),
|
2023-01-09 16:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
state.focused_item = Focus::None;
|
2023-01-09 16:18:02 +01:00
|
|
|
event::Status::Ignored
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
fn iterate_visible_tabs<'b>(
|
|
|
|
|
&'b self,
|
|
|
|
|
state: &LocalState,
|
|
|
|
|
) -> impl DoubleEndedIterator<Item = Entity> + 'b {
|
|
|
|
|
self.model
|
|
|
|
|
.order
|
|
|
|
|
.iter()
|
|
|
|
|
.copied()
|
|
|
|
|
.skip(state.buttons_offset)
|
|
|
|
|
.take(state.buttons_visible)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn first_tab(&self, state: &LocalState) -> Option<Entity> {
|
|
|
|
|
self.model.order.get(state.buttons_offset).copied()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn last_tab(&self, state: &LocalState) -> Option<Entity> {
|
|
|
|
|
self.model
|
|
|
|
|
.order
|
|
|
|
|
.get(state.buttons_offset + state.buttons_visible)
|
|
|
|
|
.copied()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(clippy::unused_self)]
|
|
|
|
|
fn prev_tab_sensitive(&self, state: &LocalState) -> bool {
|
|
|
|
|
state.buttons_offset > 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn next_tab_sensitive(&self, state: &LocalState) -> bool {
|
|
|
|
|
state.buttons_offset < self.model.order.len() - state.buttons_visible
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 14:01:42 -05:00
|
|
|
pub(super) fn max_button_dimensions(
|
|
|
|
|
&self,
|
|
|
|
|
state: &mut LocalState,
|
|
|
|
|
renderer: &Renderer,
|
2023-12-04 10:14:50 -05:00
|
|
|
_bounds: Size,
|
2023-11-30 14:01:42 -05:00
|
|
|
) -> (f32, f32) {
|
2023-01-04 05:37:20 +01:00
|
|
|
let mut width = 0.0f32;
|
|
|
|
|
let mut height = 0.0f32;
|
2023-05-30 12:03:15 -04:00
|
|
|
let font = renderer.default_font();
|
2023-01-04 05:37:20 +01:00
|
|
|
|
2023-01-17 18:49:40 +01:00
|
|
|
for key in self.model.order.iter().copied() {
|
2023-01-06 01:39:09 +01:00
|
|
|
let mut button_width = 0.0f32;
|
|
|
|
|
let mut button_height = 0.0f32;
|
|
|
|
|
|
|
|
|
|
// Add text to measurement if text was given.
|
2023-11-30 14:01:42 -05:00
|
|
|
if let Some((text, entry)) = self.model.text.get(key).zip(state.paragraphs.entry(key)) {
|
|
|
|
|
let paragraph = entry.or_insert_with(|| {
|
|
|
|
|
crate::Paragraph::with_text(Text {
|
|
|
|
|
content: text,
|
|
|
|
|
size: iced::Pixels(self.font_size),
|
|
|
|
|
bounds: Size::INFINITY,
|
|
|
|
|
font,
|
|
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
|
vertical_alignment: alignment::Vertical::Center,
|
|
|
|
|
shaping: Shaping::Advanced,
|
|
|
|
|
line_height: self.line_height,
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let Size { width, height } = paragraph.min_bounds();
|
2023-01-06 01:39:09 +01:00
|
|
|
|
2023-08-21 11:52:19 -04:00
|
|
|
button_width = width;
|
|
|
|
|
button_height = height;
|
2023-01-06 01:39:09 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 08:00:33 -07:00
|
|
|
// Add indent to measurement if found.
|
|
|
|
|
if let Some(indent) = self.model.indent(key) {
|
2024-01-23 14:31:37 +01:00
|
|
|
button_width =
|
|
|
|
|
f32::from(indent).mul_add(f32::from(self.indent_spacing), button_width);
|
2023-11-16 08:00:33 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-06 01:39:09 +01:00
|
|
|
// Add icon to measurement if icon was given.
|
2023-09-01 07:29:19 +02:00
|
|
|
if let Some(icon) = self.model.icon(key) {
|
|
|
|
|
button_height = button_height.max(f32::from(icon.size));
|
|
|
|
|
button_width += f32::from(icon.size) + f32::from(self.button_spacing);
|
2023-02-13 15:57:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add close button to measurement if found.
|
|
|
|
|
if self.model.is_closable(key) {
|
2023-09-01 07:29:19 +02:00
|
|
|
button_height = button_height.max(f32::from(self.close_icon.size));
|
|
|
|
|
button_width +=
|
|
|
|
|
f32::from(self.close_icon.size) + f32::from(self.button_spacing) + 8.0;
|
2023-01-06 01:39:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
height = height.max(button_height);
|
|
|
|
|
width = width.max(button_width);
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-06 01:39:09 +01:00
|
|
|
// Add button padding to the max size found
|
|
|
|
|
width += f32::from(self.button_padding[0]) + f32::from(self.button_padding[2]);
|
|
|
|
|
height += f32::from(self.button_padding[1]) + f32::from(self.button_padding[3]);
|
|
|
|
|
height = height.max(f32::from(self.button_height));
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
(width, height)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
impl<'a, Variant, SelectionMode, Message> Widget<Message, Renderer>
|
|
|
|
|
for SegmentedButton<'a, Variant, SelectionMode, Message>
|
2023-01-04 05:37:20 +01:00
|
|
|
where
|
2023-09-01 07:29:19 +02:00
|
|
|
Self: SegmentedVariant,
|
2023-01-17 18:49:40 +01:00
|
|
|
Model<SelectionMode>: Selectable,
|
|
|
|
|
SelectionMode: Default,
|
2023-01-04 05:37:20 +01:00
|
|
|
Message: 'static + Clone,
|
|
|
|
|
{
|
|
|
|
|
fn tag(&self) -> tree::Tag {
|
2023-01-09 16:18:02 +01:00
|
|
|
tree::Tag::of::<LocalState>()
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state(&self) -> tree::State {
|
2023-11-30 14:01:42 -05:00
|
|
|
// update the paragraphs for the model
|
2023-01-09 16:18:02 +01:00
|
|
|
tree::State::new(LocalState {
|
2023-01-09 19:26:31 +01:00
|
|
|
first: self.model.order.iter().copied().next().unwrap_or_default(),
|
2023-11-30 14:01:42 -05:00
|
|
|
paragraphs: SecondaryMap::new(),
|
2023-01-09 16:18:02 +01:00
|
|
|
..LocalState::default()
|
|
|
|
|
})
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-30 14:01:42 -05:00
|
|
|
fn diff(&mut self, tree: &mut Tree) {
|
|
|
|
|
for e in self.model.order.iter().copied() {
|
|
|
|
|
if let Some(text) = self.model.text.get(e) {
|
|
|
|
|
let text = Text {
|
|
|
|
|
content: text,
|
|
|
|
|
size: iced::Pixels(self.font_size),
|
|
|
|
|
bounds: Size::INFINITY,
|
|
|
|
|
font: self.font_active.unwrap_or(crate::font::FONT),
|
|
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
|
vertical_alignment: alignment::Vertical::Center,
|
|
|
|
|
shaping: Shaping::Advanced,
|
|
|
|
|
line_height: self.line_height,
|
|
|
|
|
};
|
|
|
|
|
if let Some(paragraph) = tree
|
|
|
|
|
.state
|
|
|
|
|
.downcast_mut::<LocalState>()
|
|
|
|
|
.paragraphs
|
|
|
|
|
.get_mut(e)
|
|
|
|
|
{
|
|
|
|
|
paragraph.update(text);
|
|
|
|
|
} else {
|
|
|
|
|
tree.state
|
|
|
|
|
.downcast_mut::<LocalState>()
|
|
|
|
|
.paragraphs
|
|
|
|
|
.insert(e, crate::Paragraph::with_text(text));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
fn width(&self) -> Length {
|
|
|
|
|
self.width
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn height(&self) -> Length {
|
|
|
|
|
self.height
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 14:01:42 -05:00
|
|
|
fn layout(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &mut Tree,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
) -> layout::Node {
|
|
|
|
|
self.variant_layout(tree.state.downcast_mut::<LocalState>(), renderer, limits)
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2023-01-04 05:37:20 +01:00
|
|
|
fn on_event(
|
|
|
|
|
&mut self,
|
|
|
|
|
tree: &mut Tree,
|
|
|
|
|
event: Event,
|
|
|
|
|
layout: Layout<'_>,
|
2023-06-15 11:16:32 -04:00
|
|
|
cursor_position: mouse::Cursor,
|
2023-01-04 05:37:20 +01:00
|
|
|
_renderer: &Renderer,
|
|
|
|
|
_clipboard: &mut dyn Clipboard,
|
|
|
|
|
shell: &mut Shell<'_, Message>,
|
2023-08-21 11:52:19 -04:00
|
|
|
_viewport: &iced::Rectangle,
|
2023-01-04 05:37:20 +01:00
|
|
|
) -> event::Status {
|
|
|
|
|
let bounds = layout.bounds();
|
2023-01-09 16:18:02 +01:00
|
|
|
let state = tree.state.downcast_mut::<LocalState>();
|
2023-01-04 05:37:20 +01:00
|
|
|
|
2023-06-15 11:16:32 -04:00
|
|
|
if cursor_position.is_over(bounds) {
|
2024-01-23 14:31:37 +01:00
|
|
|
// Check for clicks on the previous and next tab buttons, when tabs are collapsed.
|
|
|
|
|
if state.collapsed {
|
|
|
|
|
// Check if the prev tab button was clicked.
|
|
|
|
|
if cursor_position.is_over(Rectangle {
|
|
|
|
|
y: bounds.y + 8.0,
|
|
|
|
|
width: 16.0,
|
|
|
|
|
..bounds
|
|
|
|
|
}) {
|
|
|
|
|
if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
|
|
|
|
| Event::Touch(touch::Event::FingerLifted { .. }) = event
|
|
|
|
|
{
|
|
|
|
|
if self.prev_tab_sensitive(state) {
|
|
|
|
|
state.buttons_offset -= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Check if the next tab button was clicked.
|
|
|
|
|
if cursor_position.is_over(Rectangle {
|
|
|
|
|
x: bounds.width,
|
|
|
|
|
y: bounds.y + 8.0,
|
|
|
|
|
width: 16.0,
|
|
|
|
|
..bounds
|
|
|
|
|
}) {
|
|
|
|
|
if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
|
|
|
|
| Event::Touch(touch::Event::FingerLifted { .. }) = event
|
|
|
|
|
{
|
|
|
|
|
if self.next_tab_sensitive(state) {
|
|
|
|
|
state.buttons_offset += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (nth, key) in self
|
|
|
|
|
.model
|
|
|
|
|
.order
|
|
|
|
|
.iter()
|
|
|
|
|
.copied()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.skip(state.buttons_offset)
|
|
|
|
|
.take(state.buttons_visible)
|
|
|
|
|
{
|
|
|
|
|
let Some(bounds) = self.variant_button_bounds(state, bounds, nth) else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-15 11:16:32 -04:00
|
|
|
if cursor_position.is_over(bounds) {
|
2023-01-09 19:26:31 +01:00
|
|
|
if self.model.items[key].enabled {
|
2023-01-19 22:32:58 +01:00
|
|
|
// Record that the mouse is hovering over this button.
|
|
|
|
|
state.hovered = key;
|
|
|
|
|
|
2023-02-13 15:57:30 +01:00
|
|
|
// If marked as closable, show a close icon.
|
|
|
|
|
if self.model.items[key].closable {
|
2024-01-22 14:06:41 +01:00
|
|
|
// Emit close message if the close button is pressed.
|
2023-02-13 15:57:30 +01:00
|
|
|
if let Some(on_close) = self.on_close.as_ref() {
|
2023-06-15 11:16:32 -04:00
|
|
|
if cursor_position.is_over(close_bounds(
|
2023-02-13 15:57:30 +01:00
|
|
|
bounds,
|
2023-09-01 07:29:19 +02:00
|
|
|
f32::from(self.close_icon.size),
|
2023-02-13 15:57:30 +01:00
|
|
|
self.button_padding,
|
2023-06-15 11:16:32 -04:00
|
|
|
)) {
|
2023-02-13 15:57:30 +01:00
|
|
|
if let Event::Mouse(mouse::Event::ButtonReleased(
|
|
|
|
|
mouse::Button::Left,
|
|
|
|
|
))
|
|
|
|
|
| Event::Touch(touch::Event::FingerLifted { .. }) = event
|
|
|
|
|
{
|
|
|
|
|
shell.publish(on_close(key));
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-22 14:06:41 +01:00
|
|
|
|
|
|
|
|
// Emit close message if the tab is middle clicked.
|
|
|
|
|
if let Event::Mouse(mouse::Event::ButtonReleased(
|
|
|
|
|
mouse::Button::Middle,
|
|
|
|
|
)) = event
|
|
|
|
|
{
|
|
|
|
|
shell.publish(on_close(key));
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
2023-02-13 15:57:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 16:18:02 +01:00
|
|
|
if let Some(on_activate) = self.on_activate.as_ref() {
|
|
|
|
|
if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
|
|
|
|
| Event::Touch(touch::Event::FingerLifted { .. }) = event
|
|
|
|
|
{
|
|
|
|
|
shell.publish(on_activate(key));
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 16:18:02 +01:00
|
|
|
|
|
|
|
|
break;
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-22 15:57:59 +01:00
|
|
|
|
|
|
|
|
if let Some(on_activate) = self.on_activate.as_ref() {
|
|
|
|
|
if let Event::Mouse(mouse::Event::WheelScrolled { delta }) = event {
|
|
|
|
|
let current = Instant::now();
|
|
|
|
|
|
|
|
|
|
// Permit successive scroll wheel events only after a given delay.
|
|
|
|
|
if state.wheel_timestamp.map_or(true, |previous| {
|
|
|
|
|
current.duration_since(previous) > Duration::from_millis(250)
|
|
|
|
|
}) {
|
|
|
|
|
state.wheel_timestamp = Some(current);
|
|
|
|
|
|
|
|
|
|
match delta {
|
|
|
|
|
ScrollDelta::Lines { y, .. } | ScrollDelta::Pixels { y, .. } => {
|
|
|
|
|
let mut activate_key = None;
|
|
|
|
|
|
|
|
|
|
if y < 0.0 {
|
|
|
|
|
let mut prev_key = Entity::null();
|
|
|
|
|
|
|
|
|
|
for key in self.model.order.iter().copied() {
|
|
|
|
|
if self.model.is_active(key) && !prev_key.is_null() {
|
|
|
|
|
activate_key = Some(prev_key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.model.is_enabled(key) {
|
|
|
|
|
prev_key = key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if y > 0.0 {
|
|
|
|
|
let mut buttons = self.model.order.iter().copied();
|
|
|
|
|
while let Some(key) = buttons.next() {
|
|
|
|
|
if self.model.is_active(key) {
|
|
|
|
|
for key in buttons {
|
|
|
|
|
if self.model.is_enabled(key) {
|
|
|
|
|
activate_key = Some(key);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(key) = activate_key {
|
|
|
|
|
shell.publish(on_activate(key));
|
|
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-04 05:37:20 +01:00
|
|
|
} else {
|
2023-01-17 18:49:40 +01:00
|
|
|
state.hovered = Entity::default();
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-09 16:18:02 +01:00
|
|
|
if state.focused {
|
|
|
|
|
if let Event::Keyboard(keyboard::Event::KeyPressed {
|
|
|
|
|
key_code: keyboard::KeyCode::Tab,
|
|
|
|
|
modifiers,
|
|
|
|
|
..
|
|
|
|
|
}) = event
|
|
|
|
|
{
|
|
|
|
|
return if modifiers.shift() {
|
|
|
|
|
self.focus_previous(state)
|
|
|
|
|
} else {
|
|
|
|
|
self.focus_next(state)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(on_activate) = self.on_activate.as_ref() {
|
|
|
|
|
if let Event::Keyboard(keyboard::Event::KeyReleased {
|
|
|
|
|
key_code: keyboard::KeyCode::Enter,
|
|
|
|
|
..
|
|
|
|
|
}) = event
|
|
|
|
|
{
|
2024-01-23 14:31:37 +01:00
|
|
|
match state.focused_item {
|
|
|
|
|
Focus::Tab(entity) => {
|
|
|
|
|
shell.publish(on_activate(entity));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Focus::PrevButton => {
|
|
|
|
|
if self.prev_tab_sensitive(state) {
|
|
|
|
|
state.buttons_offset -= 1;
|
|
|
|
|
|
|
|
|
|
// If the change would cause it to be insensitive, focus the first tab.
|
|
|
|
|
if !self.prev_tab_sensitive(state) {
|
|
|
|
|
if let Some(first) = self.first_tab(state) {
|
|
|
|
|
state.focused_item = Focus::Tab(first);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Focus::NextButton => {
|
|
|
|
|
if self.next_tab_sensitive(state) {
|
|
|
|
|
state.buttons_offset += 1;
|
|
|
|
|
|
|
|
|
|
// If the change would cause it to be insensitive, focus the last tab.
|
|
|
|
|
if !self.next_tab_sensitive(state) {
|
|
|
|
|
if let Some(last) = self.last_tab(state) {
|
|
|
|
|
state.focused_item = Focus::Tab(last);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Focus::None | Focus::Set => (),
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 16:18:02 +01:00
|
|
|
return event::Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
event::Status::Ignored
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 16:18:02 +01:00
|
|
|
fn operate(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &mut Tree,
|
|
|
|
|
_layout: Layout<'_>,
|
2023-05-30 12:03:15 -04:00
|
|
|
_renderer: &Renderer,
|
|
|
|
|
operation: &mut dyn iced_core::widget::Operation<
|
|
|
|
|
iced_core::widget::OperationOutputWrapper<Message>,
|
|
|
|
|
>,
|
2023-01-09 16:18:02 +01:00
|
|
|
) {
|
|
|
|
|
let state = tree.state.downcast_mut::<LocalState>();
|
|
|
|
|
operation.focusable(state, self.id.as_ref().map(|id| &id.0));
|
2024-01-23 14:31:37 +01:00
|
|
|
|
|
|
|
|
if let Focus::Set = state.focused_item {
|
|
|
|
|
if self.prev_tab_sensitive(state) {
|
|
|
|
|
state.focused_item = Focus::PrevButton;
|
|
|
|
|
} else if let Some(first) = self.first_tab(state) {
|
|
|
|
|
state.focused_item = Focus::Tab(first);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 16:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
2024-01-23 14:31:37 +01:00
|
|
|
tree: &Tree,
|
2023-01-04 05:37:20 +01:00
|
|
|
layout: Layout<'_>,
|
2023-06-15 11:16:32 -04:00
|
|
|
cursor_position: mouse::Cursor,
|
2023-01-04 05:37:20 +01:00
|
|
|
_viewport: &iced::Rectangle,
|
|
|
|
|
_renderer: &Renderer,
|
2023-05-30 12:03:15 -04:00
|
|
|
) -> iced_core::mouse::Interaction {
|
2024-01-23 14:31:37 +01:00
|
|
|
let state = tree.state.downcast_ref::<LocalState>();
|
2023-01-04 05:37:20 +01:00
|
|
|
let bounds = layout.bounds();
|
2023-01-09 16:18:02 +01:00
|
|
|
|
2023-06-15 11:16:32 -04:00
|
|
|
if cursor_position.is_over(bounds) {
|
2024-01-23 14:31:37 +01:00
|
|
|
for (nth, key) in self
|
|
|
|
|
.model
|
|
|
|
|
.order
|
|
|
|
|
.iter()
|
|
|
|
|
.copied()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.skip(state.buttons_offset)
|
|
|
|
|
.take(state.buttons_visible)
|
|
|
|
|
{
|
|
|
|
|
let Some(bounds) = self.variant_button_bounds(state, bounds, nth) else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if cursor_position.is_over(bounds) {
|
2023-01-09 19:26:31 +01:00
|
|
|
return if self.model.items[key].enabled {
|
2023-05-30 12:03:15 -04:00
|
|
|
iced_core::mouse::Interaction::Pointer
|
2023-01-09 16:18:02 +01:00
|
|
|
} else {
|
2023-05-30 12:03:15 -04:00
|
|
|
iced_core::mouse::Interaction::Idle
|
2023-01-09 16:18:02 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
2023-01-09 16:18:02 +01:00
|
|
|
|
2023-05-30 12:03:15 -04:00
|
|
|
iced_core::mouse::Interaction::Idle
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-06 01:39:09 +01:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2023-01-04 05:37:20 +01:00
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &Tree,
|
|
|
|
|
renderer: &mut Renderer,
|
2023-09-01 07:29:19 +02:00
|
|
|
theme: &crate::Theme,
|
|
|
|
|
style: &renderer::Style,
|
2023-01-04 05:37:20 +01:00
|
|
|
layout: Layout<'_>,
|
2023-09-01 07:29:19 +02:00
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
viewport: &iced::Rectangle,
|
2023-01-04 05:37:20 +01:00
|
|
|
) {
|
2023-01-09 16:18:02 +01:00
|
|
|
let state = tree.state.downcast_ref::<LocalState>();
|
2023-01-04 05:37:20 +01:00
|
|
|
let appearance = Self::variant_appearance(theme, &self.style);
|
|
|
|
|
let bounds = layout.bounds();
|
2023-01-09 16:18:02 +01:00
|
|
|
let button_amount = self.model.items.len();
|
2023-01-04 05:37:20 +01:00
|
|
|
|
|
|
|
|
// Draw the background, if a background was defined.
|
|
|
|
|
if let Some(background) = appearance.background {
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds,
|
|
|
|
|
border_radius: appearance.border_radius,
|
|
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
background,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
// Draw previous and next tab buttons if there is a need to paginate tabs.
|
|
|
|
|
if state.collapsed {
|
|
|
|
|
// Previous tab button
|
|
|
|
|
let prev_bounds = Rectangle {
|
|
|
|
|
y: bounds.y + 8.0,
|
|
|
|
|
width: 16.0,
|
|
|
|
|
..bounds
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Focus::PrevButton = state.focused_item {
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds: prev_bounds,
|
|
|
|
|
border_radius: appearance.focus.first.border_radius,
|
|
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
appearance
|
|
|
|
|
.focus
|
|
|
|
|
.background
|
|
|
|
|
.unwrap_or(Background::Color(Color::TRANSPARENT)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw_icon::<Message>(
|
|
|
|
|
renderer,
|
|
|
|
|
theme,
|
|
|
|
|
style,
|
|
|
|
|
cursor,
|
|
|
|
|
viewport,
|
|
|
|
|
if state.buttons_offset == 0 {
|
|
|
|
|
appearance.inactive.text_color
|
|
|
|
|
} else if let Focus::PrevButton = state.focused_item {
|
|
|
|
|
appearance.focus.text_color
|
|
|
|
|
} else {
|
|
|
|
|
appearance.active.text_color
|
|
|
|
|
},
|
|
|
|
|
prev_bounds,
|
|
|
|
|
icon::from_name("go-previous-symbolic").size(16).icon(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Next tab button
|
|
|
|
|
let next_bounds = Rectangle {
|
|
|
|
|
x: bounds.width,
|
|
|
|
|
y: bounds.y + 8.0,
|
|
|
|
|
width: 16.0,
|
|
|
|
|
..bounds
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Focus::NextButton = state.focused_item {
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds: next_bounds,
|
|
|
|
|
border_radius: appearance.focus.last.border_radius,
|
|
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
appearance
|
|
|
|
|
.focus
|
|
|
|
|
.background
|
|
|
|
|
.unwrap_or(Background::Color(Color::TRANSPARENT)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw_icon::<Message>(
|
|
|
|
|
renderer,
|
|
|
|
|
theme,
|
|
|
|
|
style,
|
|
|
|
|
cursor,
|
|
|
|
|
viewport,
|
|
|
|
|
if self.next_tab_sensitive(state) {
|
|
|
|
|
appearance.active.text_color
|
|
|
|
|
} else if let Focus::NextButton = state.focused_item {
|
|
|
|
|
appearance.focus.text_color
|
|
|
|
|
} else {
|
|
|
|
|
appearance.inactive.text_color
|
|
|
|
|
},
|
|
|
|
|
next_bounds,
|
|
|
|
|
icon::from_name("go-next-symbolic").size(16).icon(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 16:18:02 +01:00
|
|
|
// Draw each of the items in the widget.
|
2024-01-23 14:31:37 +01:00
|
|
|
for (nth, key) in self
|
|
|
|
|
.model
|
|
|
|
|
.order
|
|
|
|
|
.iter()
|
|
|
|
|
.copied()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.skip(state.buttons_offset)
|
|
|
|
|
.take(state.buttons_visible)
|
|
|
|
|
{
|
|
|
|
|
let Some(mut bounds) = self.variant_button_bounds(state, bounds, nth) else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
2023-01-04 05:37:20 +01:00
|
|
|
|
2023-02-13 15:57:30 +01:00
|
|
|
let key_is_active = self.model.is_active(key);
|
|
|
|
|
let key_is_hovered = state.hovered == key;
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
let (status_appearance, font) = if Focus::Tab(key) == state.focused_item {
|
2023-01-09 16:18:02 +01:00
|
|
|
(appearance.focus, &self.font_active)
|
2023-02-13 15:57:30 +01:00
|
|
|
} else if key_is_active {
|
2023-01-04 05:37:20 +01:00
|
|
|
(appearance.active, &self.font_active)
|
2023-02-13 15:57:30 +01:00
|
|
|
} else if key_is_hovered {
|
2023-01-04 05:37:20 +01:00
|
|
|
(appearance.hover, &self.font_hovered)
|
|
|
|
|
} else {
|
|
|
|
|
(appearance.inactive, &self.font_inactive)
|
|
|
|
|
};
|
2023-05-30 12:03:15 -04:00
|
|
|
let font = font.unwrap_or_else(|| renderer.default_font());
|
2023-01-04 05:37:20 +01:00
|
|
|
|
|
|
|
|
let button_appearance = if nth == 0 {
|
|
|
|
|
status_appearance.first
|
|
|
|
|
} else if nth + 1 == button_amount {
|
|
|
|
|
status_appearance.last
|
|
|
|
|
} else {
|
|
|
|
|
status_appearance.middle
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Render the background of the button.
|
|
|
|
|
if status_appearance.background.is_some() {
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds,
|
|
|
|
|
border_radius: button_appearance.border_radius,
|
|
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
status_appearance
|
|
|
|
|
.background
|
|
|
|
|
.unwrap_or(Background::Color(Color::TRANSPARENT)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw the bottom border defined for this button.
|
|
|
|
|
if let Some((width, background)) = button_appearance.border_bottom {
|
|
|
|
|
let mut bounds = bounds;
|
|
|
|
|
bounds.y = bounds.y + bounds.height - width;
|
|
|
|
|
bounds.height = width;
|
|
|
|
|
|
2023-12-13 12:59:26 -05:00
|
|
|
let rad_0 = THEME.with(|t| t.borrow().cosmic().corner_radii.radius_0);
|
2023-01-04 05:37:20 +01:00
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds,
|
2023-12-13 12:59:26 -05:00
|
|
|
border_radius: rad_0.into(),
|
2023-01-04 05:37:20 +01:00
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
background,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 15:57:30 +01:00
|
|
|
let original_bounds = bounds;
|
|
|
|
|
|
2023-01-06 01:39:09 +01:00
|
|
|
let y = bounds.center_y();
|
|
|
|
|
|
2023-11-16 08:00:33 -07:00
|
|
|
// Adjust bounds by indent
|
|
|
|
|
if let Some(indent) = self.model.indent(key) {
|
|
|
|
|
let adjustment = f32::from(indent) * f32::from(self.indent_spacing);
|
|
|
|
|
bounds.x += adjustment;
|
|
|
|
|
bounds.width -= adjustment;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 01:39:09 +01:00
|
|
|
// Draw the image beside the text.
|
2023-01-17 18:49:40 +01:00
|
|
|
let horizontal_alignment = if let Some(icon) = self.model.icon(key) {
|
2023-01-06 01:39:09 +01:00
|
|
|
bounds.x += f32::from(self.button_padding[0]);
|
|
|
|
|
|
2024-01-23 22:40:12 +01:00
|
|
|
let mut image_bounds = bounds;
|
2023-09-01 07:29:19 +02:00
|
|
|
let width = f32::from(icon.size);
|
2023-01-06 01:39:09 +01:00
|
|
|
let offset = width + f32::from(self.button_spacing);
|
2024-01-23 22:40:12 +01:00
|
|
|
image_bounds.y += f32::from(self.button_padding[1]);
|
|
|
|
|
image_bounds.y = y - width / 2.0;
|
2023-01-06 01:39:09 +01:00
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
draw_icon::<Message>(
|
2023-09-01 07:29:19 +02:00
|
|
|
renderer,
|
|
|
|
|
theme,
|
2024-01-23 14:31:37 +01:00
|
|
|
style,
|
2023-09-01 07:29:19 +02:00
|
|
|
cursor,
|
|
|
|
|
viewport,
|
2024-01-23 14:31:37 +01:00
|
|
|
status_appearance.text_color,
|
|
|
|
|
Rectangle {
|
|
|
|
|
width,
|
|
|
|
|
height: width,
|
2024-01-23 22:40:12 +01:00
|
|
|
..image_bounds
|
2024-01-23 14:31:37 +01:00
|
|
|
},
|
|
|
|
|
icon.clone(),
|
2023-09-01 07:29:19 +02:00
|
|
|
);
|
2023-01-06 01:39:09 +01:00
|
|
|
|
2023-09-13 16:47:42 +02:00
|
|
|
bounds.x += offset;
|
|
|
|
|
bounds.width -= offset;
|
|
|
|
|
|
2023-01-06 01:39:09 +01:00
|
|
|
alignment::Horizontal::Left
|
|
|
|
|
} else {
|
|
|
|
|
bounds.x = bounds.center_x();
|
|
|
|
|
alignment::Horizontal::Center
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-23 02:36:05 +01:00
|
|
|
// Whether to show the close button on this tab.
|
|
|
|
|
let show_close_button =
|
|
|
|
|
(key_is_active || !self.show_close_icon_on_hover || key_is_hovered)
|
|
|
|
|
&& self.model.is_closable(key);
|
|
|
|
|
|
|
|
|
|
// Width of the icon used by the close button, which we will subtract from the text bounds.
|
|
|
|
|
let close_icon_width = if show_close_button {
|
|
|
|
|
f32::from(self.close_icon.size)
|
|
|
|
|
} else {
|
|
|
|
|
0.0
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-17 18:49:40 +01:00
|
|
|
if let Some(text) = self.model.text(key) {
|
2023-01-06 01:39:09 +01:00
|
|
|
bounds.y = y;
|
|
|
|
|
|
2024-01-23 02:36:05 +01:00
|
|
|
// Draw the text for this segmented button or tab.
|
2023-11-30 14:01:42 -05:00
|
|
|
renderer.fill_text(
|
|
|
|
|
iced_core::text::Text {
|
|
|
|
|
content: text,
|
|
|
|
|
size: iced::Pixels(self.font_size),
|
|
|
|
|
bounds: bounds.size(),
|
|
|
|
|
font,
|
|
|
|
|
horizontal_alignment,
|
|
|
|
|
vertical_alignment: alignment::Vertical::Center,
|
|
|
|
|
shaping: Shaping::Advanced,
|
|
|
|
|
line_height: self.line_height,
|
|
|
|
|
},
|
|
|
|
|
bounds.position(),
|
|
|
|
|
status_appearance.text_color,
|
2024-01-23 02:36:05 +01:00
|
|
|
Rectangle {
|
2024-01-23 22:40:12 +01:00
|
|
|
width: bounds.width - close_icon_width,
|
2024-01-23 02:36:05 +01:00
|
|
|
..original_bounds
|
|
|
|
|
},
|
2023-11-30 14:01:42 -05:00
|
|
|
);
|
2023-01-06 01:39:09 +01:00
|
|
|
}
|
2023-02-13 15:57:30 +01:00
|
|
|
|
2024-01-23 02:36:05 +01:00
|
|
|
// Draw a close button if set.
|
2023-02-13 15:57:30 +01:00
|
|
|
if show_close_button {
|
2024-01-23 02:36:05 +01:00
|
|
|
let close_button_bounds =
|
|
|
|
|
close_bounds(original_bounds, close_icon_width, self.button_padding);
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
draw_icon::<Message>(
|
2023-09-01 07:29:19 +02:00
|
|
|
renderer,
|
|
|
|
|
theme,
|
2024-01-23 14:31:37 +01:00
|
|
|
style,
|
2023-09-01 07:29:19 +02:00
|
|
|
cursor,
|
|
|
|
|
viewport,
|
2024-01-23 14:31:37 +01:00
|
|
|
status_appearance.text_color,
|
|
|
|
|
close_button_bounds,
|
|
|
|
|
self.close_icon.clone(),
|
2023-09-01 07:29:19 +02:00
|
|
|
);
|
2023-02-13 15:57:30 +01:00
|
|
|
}
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlay<'b>(
|
2023-05-30 12:03:15 -04:00
|
|
|
&'b mut self,
|
2023-01-04 05:37:20 +01:00
|
|
|
_tree: &'b mut Tree,
|
2023-05-30 12:03:15 -04:00
|
|
|
_layout: iced_core::Layout<'_>,
|
2023-01-04 05:37:20 +01:00
|
|
|
_renderer: &Renderer,
|
2023-05-30 12:03:15 -04:00
|
|
|
) -> Option<iced_core::overlay::Element<'b, Message, Renderer>> {
|
2023-01-04 05:37:20 +01:00
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
impl<'a, Variant, SelectionMode, Message> From<SegmentedButton<'a, Variant, SelectionMode, Message>>
|
|
|
|
|
for Element<'a, Message>
|
2023-01-04 05:37:20 +01:00
|
|
|
where
|
2023-09-01 07:29:19 +02:00
|
|
|
SegmentedButton<'a, Variant, SelectionMode, Message>: SegmentedVariant,
|
2023-01-04 05:37:20 +01:00
|
|
|
Variant: 'static,
|
2023-01-17 18:49:40 +01:00
|
|
|
Model<SelectionMode>: Selectable,
|
|
|
|
|
SelectionMode: Default,
|
2023-01-04 05:37:20 +01:00
|
|
|
Message: 'static + Clone,
|
|
|
|
|
{
|
2023-09-01 07:29:19 +02:00
|
|
|
fn from(mut widget: SegmentedButton<'a, Variant, SelectionMode, Message>) -> Self {
|
2023-01-09 16:18:02 +01:00
|
|
|
if widget.model.items.is_empty() {
|
2023-01-04 05:37:20 +01:00
|
|
|
widget.spacing = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Self::new(widget)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
/// State that is maintained by each individual widget.
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct LocalState {
|
|
|
|
|
/// Whether buttons need to be collapsed to preserve minimum width
|
|
|
|
|
pub(super) collapsed: bool,
|
|
|
|
|
/// Defines how many buttons to show at a time.
|
|
|
|
|
pub(super) buttons_visible: usize,
|
|
|
|
|
/// Button visibility offset, when collapsed.
|
|
|
|
|
pub(super) buttons_offset: usize,
|
|
|
|
|
/// The first focusable key.
|
|
|
|
|
first: Entity,
|
|
|
|
|
/// If the widget is focused or not.
|
|
|
|
|
focused: bool,
|
|
|
|
|
/// The key inside the widget that is currently focused.
|
|
|
|
|
focused_item: Focus,
|
|
|
|
|
/// The ID of the button that is being hovered. Defaults to null.
|
|
|
|
|
hovered: Entity,
|
|
|
|
|
/// The paragraphs for each text.
|
|
|
|
|
paragraphs: SecondaryMap<Entity, crate::Paragraph>,
|
|
|
|
|
/// Time since last tab activation from wheel movements.
|
|
|
|
|
wheel_timestamp: Option<Instant>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Default, PartialEq)]
|
|
|
|
|
enum Focus {
|
|
|
|
|
NextButton,
|
|
|
|
|
#[default]
|
|
|
|
|
None,
|
|
|
|
|
PrevButton,
|
|
|
|
|
Set,
|
|
|
|
|
Tab(Entity),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl operation::Focusable for LocalState {
|
|
|
|
|
fn is_focused(&self) -> bool {
|
|
|
|
|
self.focused
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn focus(&mut self) {
|
|
|
|
|
self.focused = true;
|
|
|
|
|
self.focused_item = Focus::Set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn unfocus(&mut self) {
|
|
|
|
|
self.focused = false;
|
|
|
|
|
self.focused_item = Focus::None;
|
|
|
|
|
}
|
2023-01-09 16:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-17 18:49:40 +01:00
|
|
|
/// The iced identifier of a segmented button.
|
2023-09-29 16:14:03 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2023-01-09 16:18:02 +01:00
|
|
|
pub struct Id(widget::Id);
|
|
|
|
|
|
|
|
|
|
impl Id {
|
|
|
|
|
/// Creates a custom [`Id`].
|
|
|
|
|
pub fn new(id: impl Into<std::borrow::Cow<'static, str>>) -> Self {
|
|
|
|
|
Self(widget::Id::new(id))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a unique [`Id`].
|
|
|
|
|
///
|
|
|
|
|
/// This function produces a different [`Id`] every time it is called.
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn unique() -> Self {
|
|
|
|
|
Self(widget::Id::unique())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Id> for widget::Id {
|
|
|
|
|
fn from(id: Id) -> Self {
|
|
|
|
|
id.0
|
|
|
|
|
}
|
2023-01-04 05:37:20 +01:00
|
|
|
}
|
2023-02-13 15:57:30 +01:00
|
|
|
|
|
|
|
|
/// Calculates the bounds of the close button within the area of an item.
|
|
|
|
|
fn close_bounds(area: Rectangle<f32>, icon_size: f32, button_padding: [u16; 4]) -> Rectangle<f32> {
|
2023-02-13 17:38:23 +01:00
|
|
|
let unpadded_height = area.height - f32::from(button_padding[1]) - f32::from(button_padding[3]);
|
2023-02-13 15:57:30 +01:00
|
|
|
|
|
|
|
|
Rectangle {
|
2023-02-13 17:38:23 +01:00
|
|
|
x: area.x + area.width - icon_size - 8.0,
|
|
|
|
|
y: area.y + (unpadded_height / 2.0) - (icon_size / 2.0),
|
2023-02-13 15:57:30 +01:00
|
|
|
width: icon_size,
|
|
|
|
|
height: icon_size,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-23 14:31:37 +01:00
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
|
fn draw_icon<Message: 'static>(
|
|
|
|
|
renderer: &mut Renderer,
|
|
|
|
|
theme: &crate::Theme,
|
|
|
|
|
style: &renderer::Style,
|
|
|
|
|
cursor: mouse::Cursor,
|
|
|
|
|
viewport: &Rectangle,
|
|
|
|
|
color: Color,
|
|
|
|
|
bounds: Rectangle,
|
|
|
|
|
icon: Icon,
|
|
|
|
|
) {
|
|
|
|
|
let mut layout_node = layout::Node::new(Size {
|
|
|
|
|
width: bounds.width,
|
|
|
|
|
height: bounds.width,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
layout_node.move_to(Point {
|
|
|
|
|
x: bounds.x,
|
|
|
|
|
y: bounds.y,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Widget::<Message, Renderer>::draw(
|
|
|
|
|
Element::<Message>::from(icon.clone()).as_widget(),
|
|
|
|
|
&Tree::empty(),
|
|
|
|
|
renderer,
|
|
|
|
|
theme,
|
|
|
|
|
&renderer::Style {
|
|
|
|
|
icon_color: color,
|
|
|
|
|
text_color: color,
|
|
|
|
|
scale_factor: style.scale_factor,
|
|
|
|
|
},
|
|
|
|
|
Layout::new(&layout_node),
|
|
|
|
|
cursor,
|
|
|
|
|
viewport,
|
|
|
|
|
);
|
|
|
|
|
}
|