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
|
|
|
}
|
|
|
|
|
|
2025-03-14 11:56:21 -04:00
|
|
|
impl<SelectionMode, Message> SegmentedVariant
|
|
|
|
|
for 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
|
|
|
{
|
2025-08-12 17:15:18 +02:00
|
|
|
const VERTICAL: bool = true;
|
|
|
|
|
|
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-09-11 12:51:19 -06:00
|
|
|
.flat_map(move |(nth, key)| {
|
|
|
|
|
let mut divider = None;
|
|
|
|
|
if self.model.divider_above(key).unwrap_or(false) && nth > 0 {
|
|
|
|
|
let mut divider_bounds = bounds;
|
|
|
|
|
divider_bounds.height = 1.0;
|
|
|
|
|
divider_bounds.x += f32::from(self.button_padding[0]);
|
|
|
|
|
divider_bounds.width -= f32::from(self.button_padding[0]);
|
|
|
|
|
divider_bounds.width -= f32::from(self.button_padding[2]);
|
2024-09-11 14:29:36 -06:00
|
|
|
divider = Some(ItemBounds::Divider(divider_bounds, false));
|
2024-09-11 12:51:19 -06:00
|
|
|
|
|
|
|
|
bounds.y += divider_bounds.height + spacing;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-23 16:05:55 +01:00
|
|
|
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;
|
|
|
|
|
|
2024-09-11 12:51:19 -06:00
|
|
|
std::iter::once(ItemBounds::Button(key, layout_bounds)).chain(divider)
|
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
|
|
|
|
2024-09-12 12:47:18 -06:00
|
|
|
let (width, item_height) = self.max_button_dimensions(state, renderer);
|
2024-02-23 16:05:55 +01:00
|
|
|
|
|
|
|
|
for (size, actual) in &mut state.internal_layout {
|
|
|
|
|
size.width = width;
|
2024-09-12 12:47:18 -06:00
|
|
|
actual.width = item_height;
|
2024-02-23 16:05:55 +01:00
|
|
|
}
|
2023-01-04 05:37:20 +01:00
|
|
|
|
|
|
|
|
let spacing = f32::from(self.spacing);
|
2024-09-12 12:47:18 -06:00
|
|
|
let mut height = 0.0;
|
|
|
|
|
for (nth, key) in self.model.order.iter().copied().enumerate() {
|
|
|
|
|
if nth > 0 {
|
|
|
|
|
height += spacing;
|
|
|
|
|
if self.model.divider_above(key).unwrap_or(false) {
|
|
|
|
|
height += 1.0 + spacing;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
height += item_height;
|
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
|
|
|
}
|
|
|
|
|
}
|