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.
|
|
|
|
|
|
2024-02-27 18:31:28 +01:00
|
|
|
use super::model::{Model, Selectable};
|
2023-01-03 19:35:34 +01:00
|
|
|
use super::style::StyleSheet;
|
2024-02-23 16:05:55 +01:00
|
|
|
use super::widget::{ItemBounds, LocalState, SegmentedButton, SegmentedVariant};
|
2023-01-03 19:35:34 +01:00
|
|
|
|
2024-02-27 18:31:28 +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-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-02-23 16:05:55 +01:00
|
|
|
fn variant_bounds<'b>(
|
2024-01-29 09:43:06 -07:00
|
|
|
&'b self,
|
|
|
|
|
state: &'b LocalState,
|
2024-01-23 14:31:37 +01:00
|
|
|
mut bounds: Rectangle,
|
2024-02-23 16:05:55 +01:00
|
|
|
) -> Box<dyn Iterator<Item = ItemBounds> + 'b> {
|
2024-01-24 17:32:50 +01:00
|
|
|
let spacing = f32::from(self.spacing);
|
2023-01-03 19:35:34 +01:00
|
|
|
|
2024-01-29 09:43:06 -07:00
|
|
|
Box::new(
|
|
|
|
|
self.model
|
|
|
|
|
.order
|
|
|
|
|
.iter()
|
|
|
|
|
.copied()
|
|
|
|
|
.enumerate()
|
2024-02-23 16:05:55 +01:00
|
|
|
.map(move |(nth, key)| {
|
|
|
|
|
let mut layout_bounds = bounds;
|
|
|
|
|
|
|
|
|
|
let layout_size = state.internal_layout[nth].0;
|
|
|
|
|
|
|
|
|
|
layout_bounds.height = layout_size.height;
|
|
|
|
|
|
|
|
|
|
bounds.y += layout_bounds.height + spacing;
|
|
|
|
|
|
|
|
|
|
ItemBounds::Button(key, layout_bounds)
|
2024-01-29 09:43:06 -07:00
|
|
|
}),
|
|
|
|
|
)
|
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,
|
2024-02-23 16:05:55 +01:00
|
|
|
) -> Size {
|
2024-01-24 17:32:50 +01:00
|
|
|
state.internal_layout.clear();
|
2024-06-07 11:14:52 +02:00
|
|
|
state.buttons_visible = self.model.order.len();
|
2023-01-03 19:35:34 +01:00
|
|
|
let limits = limits.width(self.width);
|
2024-02-23 16:05:55 +01:00
|
|
|
|
|
|
|
|
let (width, mut height) = self.max_button_dimensions(state, renderer);
|
|
|
|
|
|
|
|
|
|
for (size, actual) in &mut state.internal_layout {
|
|
|
|
|
size.width = width;
|
|
|
|
|
actual.width = height;
|
|
|
|
|
}
|
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
|
|
|
}
|
2024-02-23 16:05:55 +01:00
|
|
|
|
|
|
|
|
limits.height(Length::Fixed(height)).resolve(
|
2024-02-01 17:29:33 -05:00
|
|
|
self.width,
|
|
|
|
|
self.height,
|
|
|
|
|
Size::new(width, height),
|
2024-02-23 16:05:55 +01:00
|
|
|
)
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
}
|