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 vertical 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-03 19:35:34 +01:00
|
|
|
|
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-04 05:37:20 +01:00
|
|
|
/// A type marker defining the vertical variant of a [`SegmentedButton`].
|
|
|
|
|
pub struct Vertical;
|
|
|
|
|
|
|
|
|
|
/// Vertical [`SegmentedButton`].
|
2023-09-01 07:29:19 +02:00
|
|
|
pub type VerticalSegmentedButton<'a, SelectionMode, Message> =
|
|
|
|
|
SegmentedButton<'a, Vertical, SelectionMode, Message>;
|
2023-01-04 05:37:20 +01:00
|
|
|
|
|
|
|
|
/// Vertical implementation of the [`SegmentedButton`].
|
2023-01-17 18:49:40 +01:00
|
|
|
///
|
|
|
|
|
/// 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 vertical<SelectionMode, Message>(
|
2023-01-17 18:49:40 +01:00
|
|
|
model: &Model<SelectionMode>,
|
2023-09-01 07:29:19 +02:00
|
|
|
) -> SegmentedButton<Vertical, 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-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, Vertical, 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.vertical(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> {
|
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);
|
|
|
|
|
bounds.height = (bounds.height - (num as f32 * spacing) + spacing) / num as f32;
|
2023-01-03 19:35:34 +01:00
|
|
|
|
2023-01-04 05:37:20 +01:00
|
|
|
if nth != 0 {
|
|
|
|
|
bounds.y += (nth as f32 * bounds.height) + (nth as f32 * 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 (width, mut 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 {
|
|
|
|
|
height = (num as f32 * height) + (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)
|
|
|
|
|
}
|
|
|
|
|
}
|