2023-01-06 01:39:09 +01:00
|
|
|
// Copyright 2022 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-01-09 16:18:02 +01:00
|
|
|
//! Implementation details for the horizontal layout of a segmented button.
|
|
|
|
|
|
2023-01-17 18:49:40 +01:00
|
|
|
use super::model::{Model, Selectable};
|
2023-01-03 19:35:34 +01:00
|
|
|
use super::style::StyleSheet;
|
2023-11-30 14:01:42 -05:00
|
|
|
use super::widget::{LocalState, SegmentedButton, SegmentedVariant};
|
2023-01-04 05:37:20 +01:00
|
|
|
|
|
|
|
|
use iced::{Length, Rectangle, Size};
|
2023-05-30 12:03:15 -04:00
|
|
|
use iced_core::layout;
|
2023-01-03 19:35:34 +01:00
|
|
|
|
2023-01-09 16:18:02 +01:00
|
|
|
/// Horizontal [`SegmentedButton`].
|
2023-09-01 07:29:19 +02:00
|
|
|
pub type HorizontalSegmentedButton<'a, SelectionMode, Message> =
|
|
|
|
|
SegmentedButton<'a, Horizontal, SelectionMode, Message>;
|
2023-01-09 16:18:02 +01:00
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
/// A type marker defining the horizontal variant of a [`SegmentedButton`].
|
|
|
|
|
pub struct Horizontal;
|
2023-01-03 19:35:34 +01:00
|
|
|
|
2023-01-17 18:49:40 +01:00
|
|
|
/// Horizontal implementation of the [`SegmentedButton`].
|
|
|
|
|
///
|
|
|
|
|
/// For details on the model, see the [`segmented_button`](super) module for more details.
|
2023-01-03 19:35:34 +01:00
|
|
|
#[must_use]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub fn horizontal<SelectionMode: Default, Message>(
|
2023-01-17 18:49:40 +01:00
|
|
|
model: &Model<SelectionMode>,
|
2023-09-01 07:29:19 +02:00
|
|
|
) -> SegmentedButton<Horizontal, SelectionMode, Message>
|
2023-01-03 19:35:34 +01:00
|
|
|
where
|
2023-01-17 18:49:40 +01:00
|
|
|
Model<SelectionMode>: Selectable,
|
2023-01-03 19:35:34 +01:00
|
|
|
{
|
2023-01-09 16:18:02 +01:00
|
|
|
SegmentedButton::new(model)
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
impl<'a, SelectionMode, Message> SegmentedVariant
|
|
|
|
|
for SegmentedButton<'a, Horizontal, SelectionMode, Message>
|
2023-01-03 19:35:34 +01:00
|
|
|
where
|
2023-01-17 18:49:40 +01:00
|
|
|
Model<SelectionMode>: Selectable,
|
|
|
|
|
SelectionMode: Default,
|
2023-01-03 19:35:34 +01:00
|
|
|
{
|
2023-01-04 05:37:20 +01:00
|
|
|
fn variant_appearance(
|
2023-09-01 07:29:19 +02:00
|
|
|
theme: &crate::Theme,
|
|
|
|
|
style: &crate::theme::SegmentedButton,
|
2023-01-04 05:37:20 +01:00
|
|
|
) -> super::Appearance {
|
|
|
|
|
theme.horizontal(style)
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
#[allow(clippy::cast_precision_loss)]
|
2024-01-23 14:31:37 +01:00
|
|
|
fn variant_button_bounds(
|
|
|
|
|
&self,
|
|
|
|
|
state: &LocalState,
|
|
|
|
|
mut bounds: Rectangle,
|
|
|
|
|
nth: usize,
|
|
|
|
|
) -> Option<Rectangle> {
|
|
|
|
|
let num = state.buttons_visible;
|
|
|
|
|
|
|
|
|
|
// Do not display tabs that are currently hidden due to width constraints.
|
|
|
|
|
if state.collapsed && nth < state.buttons_offset {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
if num != 0 {
|
2024-01-23 14:31:37 +01:00
|
|
|
let offset_width;
|
|
|
|
|
(bounds.x, offset_width) = if state.collapsed {
|
|
|
|
|
(bounds.x + 16.0, 32.0)
|
|
|
|
|
} else {
|
|
|
|
|
(bounds.x, 0.0)
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
let spacing = f32::from(self.spacing);
|
2024-01-23 14:31:37 +01:00
|
|
|
bounds.width = ((num as f32).mul_add(-spacing, bounds.width - offset_width) + spacing)
|
|
|
|
|
/ num as f32;
|
2023-01-03 19:35:34 +01:00
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
if nth != state.buttons_offset {
|
|
|
|
|
let pos = (nth - state.buttons_offset) as f32;
|
|
|
|
|
bounds.x += pos.mul_add(bounds.width, pos * spacing);
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
Some(bounds)
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
#[allow(clippy::cast_precision_loss)]
|
|
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
|
|
|
|
#[allow(clippy::cast_sign_loss)]
|
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-03 19:35:34 +01:00
|
|
|
let limits = limits.width(self.width);
|
2023-11-30 14:01:42 -05:00
|
|
|
let (mut width, height) = self.max_button_dimensions(state, renderer, limits.max());
|
2023-01-04 05:37:20 +01:00
|
|
|
|
2023-01-09 16:18:02 +01:00
|
|
|
let num = self.model.items.len();
|
2023-01-04 05:37:20 +01:00
|
|
|
let spacing = f32::from(self.spacing);
|
|
|
|
|
|
|
|
|
|
if num != 0 {
|
2023-11-28 19:02:08 +00:00
|
|
|
width = (num as f32).mul_add(width, num as f32 * spacing) - spacing;
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let size = limits
|
2023-05-30 12:03:15 -04:00
|
|
|
.height(Length::Fixed(height))
|
2023-01-03 19:35:34 +01:00
|
|
|
.resolve(Size::new(width, height));
|
|
|
|
|
|
2024-01-23 14:31:37 +01:00
|
|
|
let actual_width = size.width as usize;
|
|
|
|
|
let minimum_width = self.minimum_button_width as usize * self.model.items.len();
|
|
|
|
|
|
|
|
|
|
state.buttons_visible = num;
|
|
|
|
|
state.collapsed = actual_width < minimum_width;
|
|
|
|
|
if state.collapsed {
|
|
|
|
|
state.buttons_visible = (actual_width / self.minimum_button_width as usize).min(num);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 19:35:34 +01:00
|
|
|
layout::Node::new(size)
|
|
|
|
|
}
|
|
|
|
|
}
|