libcosmic/src/widget/segmented_button/vertical.rs

127 lines
4 KiB
Rust
Raw Normal View History

// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Implementation details for the vertical layout of a segmented button.
use super::model::{Model, Selectable};
use super::style::StyleSheet;
use super::widget::{ItemBounds, LocalState, SegmentedButton, SegmentedVariant};
use iced::{Length, Rectangle, Size};
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
use iced_core::layout;
/// A type marker defining the vertical variant of a [`SegmentedButton`].
pub struct Vertical;
/// Vertical [`SegmentedButton`].
pub type VerticalSegmentedButton<'a, SelectionMode, Message> =
SegmentedButton<'a, Vertical, SelectionMode, Message>;
/// Vertical implementation of the [`SegmentedButton`].
///
/// For details on the model, see the [`segmented_button`](super) module for more details.
pub fn vertical<SelectionMode, Message>(
model: &Model<SelectionMode>,
) -> SegmentedButton<Vertical, SelectionMode, Message>
where
Model<SelectionMode>: Selectable,
SelectionMode: Default,
{
SegmentedButton::new(model)
}
impl<SelectionMode, Message> SegmentedVariant
for SegmentedButton<'_, Vertical, SelectionMode, Message>
where
Model<SelectionMode>: Selectable,
SelectionMode: Default,
{
const VERTICAL: bool = true;
fn variant_appearance(
theme: &crate::Theme,
style: &crate::theme::SegmentedButton,
) -> super::Appearance {
theme.vertical(style)
}
#[allow(clippy::cast_precision_loss)]
fn variant_bounds<'b>(
2024-01-29 09:43:06 -07:00
&'b self,
state: &'b LocalState,
mut bounds: Rectangle,
) -> Box<dyn Iterator<Item = ItemBounds> + 'b> {
let spacing = f32::from(self.spacing);
2024-01-29 09:43:06 -07:00
Box::new(
self.model
.order
.iter()
.copied()
.enumerate()
.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]);
divider = Some(ItemBounds::Divider(divider_bounds, false));
bounds.y += divider_bounds.height + spacing;
}
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;
std::iter::once(ItemBounds::Button(key, layout_bounds)).chain(divider)
2024-01-29 09:43:06 -07: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,
) -> Size {
state.internal_layout.clear();
state.buttons_visible = self.model.order.len();
let limits = limits.width(self.width);
let (width, item_height) = self.max_button_dimensions(state, renderer);
for (size, actual) in &mut state.internal_layout {
size.width = width;
actual.width = item_height;
}
let spacing = f32::from(self.spacing);
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;
}
limits.height(Length::Fixed(height)).resolve(
2024-02-01 17:29:33 -05:00
self.width,
self.height,
Size::new(width, height),
)
}
}