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-01-04 05:37:20 +01:00
|
|
|
use super::widget::{SegmentedButton, SegmentedVariant};
|
|
|
|
|
|
|
|
|
|
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)]
|
|
|
|
|
fn variant_button_bounds(&self, mut bounds: Rectangle, nth: usize) -> Rectangle {
|
2023-01-09 16:18:02 +01:00
|
|
|
let num = self.model.items.len();
|
2023-01-04 05:37:20 +01:00
|
|
|
if num != 0 {
|
|
|
|
|
let spacing = f32::from(self.spacing);
|
2023-11-28 19:02:08 +00:00
|
|
|
bounds.width = ((num as f32).mul_add(-spacing, bounds.width) + spacing) / num as f32;
|
2023-01-03 19:35:34 +01:00
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
if nth != 0 {
|
2023-11-28 19:02:08 +00:00
|
|
|
bounds.x += (nth as f32).mul_add(bounds.width, nth as f32 * spacing);
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
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-09-01 07:29:19 +02:00
|
|
|
fn variant_layout(&self, renderer: &crate::Renderer, limits: &layout::Limits) -> layout::Node {
|
2023-01-03 19:35:34 +01:00
|
|
|
let limits = limits.width(self.width);
|
2023-01-27 04:44:25 +01:00
|
|
|
let (mut width, height) = self.max_button_dimensions(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));
|
|
|
|
|
|
|
|
|
|
layout::Node::new(size)
|
|
|
|
|
}
|
|
|
|
|
}
|