2022-12-28 23:18:31 +01:00
|
|
|
// Copyright 2022 System76 <info@system76.com>
|
2022-12-28 12:42:28 +01:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2022-12-28 23:18:31 +01:00
|
|
|
|
2023-01-17 18:49:40 +01:00
|
|
|
//! A widget providing a conjoined set of linear items that function in conjunction as a single button.
|
2022-12-28 23:18:31 +01:00
|
|
|
//!
|
|
|
|
|
//! ## Example
|
|
|
|
|
//!
|
2023-01-17 18:49:40 +01:00
|
|
|
//! Add the model and a message variant in your application for handling selections.
|
2022-12-28 23:18:31 +01:00
|
|
|
//!
|
2023-01-04 01:26:33 +01:00
|
|
|
//! ```ignore
|
2022-12-28 23:18:31 +01:00
|
|
|
//! use iced_core::Length;
|
|
|
|
|
//! use cosmic::theme;
|
|
|
|
|
//! use cosmic::widget::segmented_button;
|
|
|
|
|
//!
|
|
|
|
|
//! enum AppMessage {
|
|
|
|
|
//! Selected(segmented_button::Key)
|
|
|
|
|
//! }
|
|
|
|
|
//!
|
|
|
|
|
//! struct App {
|
2023-01-17 18:49:40 +01:00
|
|
|
//! model: segmented_button::SingleSelectModel,
|
2022-12-28 23:18:31 +01:00
|
|
|
//! }
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
2023-01-17 18:49:40 +01:00
|
|
|
//! Then add choices to the model, while activating the first.
|
2022-12-28 23:18:31 +01:00
|
|
|
//!
|
2023-01-04 01:26:33 +01:00
|
|
|
//! ```ignore
|
2023-01-17 18:49:40 +01:00
|
|
|
//! application.model = segmented_button::Model::builder()
|
|
|
|
|
//! .insert(|b| b.text("Choice A").data(0u16))
|
|
|
|
|
//! .insert(|b| b.text("Choice B").data(1u16))
|
|
|
|
|
//! .insert(|b| b.text("Choice C").data(2u16))
|
2023-01-09 16:18:02 +01:00
|
|
|
//! .build();
|
2022-12-28 23:18:31 +01:00
|
|
|
//! ```
|
|
|
|
|
//!
|
2023-01-17 18:49:40 +01:00
|
|
|
//! Or incrementally insert items with
|
|
|
|
|
//!
|
|
|
|
|
//! ```ignore
|
|
|
|
|
//! let id = application.model.insert()
|
|
|
|
|
//! .text("Choice C")
|
|
|
|
|
//! .icon("custom-icon")
|
|
|
|
|
//! .data(3u16)
|
|
|
|
|
//! .data("custom-meta")
|
|
|
|
|
//! .id();
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
2022-12-28 23:18:31 +01:00
|
|
|
//! Then use it in the view method to create segmented button widgets.
|
|
|
|
|
//!
|
2023-01-04 01:26:33 +01:00
|
|
|
//! ```ignore
|
2023-01-17 18:49:40 +01:00
|
|
|
//! let widget = segmented_button::horizontal(&application.model)
|
2023-01-09 16:18:02 +01:00
|
|
|
//! .style(theme::SegmentedButton::ViewSeitcher)
|
|
|
|
|
//! .button_height(32)
|
|
|
|
|
//! .button_padding([16, 10, 16, 10])
|
|
|
|
|
//! .button_spacing(8)
|
|
|
|
|
//! .icon_size(16)
|
|
|
|
|
//! .spacing(8)
|
2022-12-28 23:18:31 +01:00
|
|
|
//! .on_activate(AppMessage::Selected);
|
|
|
|
|
//! ```
|
2023-01-17 18:49:40 +01:00
|
|
|
//!
|
|
|
|
|
//! And respond to events like so:
|
|
|
|
|
//!
|
|
|
|
|
//! ```ignore
|
|
|
|
|
//! match message {
|
|
|
|
|
//! AppMessage::Selected(id) => {
|
|
|
|
|
//! application.model.activate(id);
|
|
|
|
|
//!
|
|
|
|
|
//! if let Some(number) = application.model.data::<u16>(id) {
|
|
|
|
|
//! println!("activated item with number {number}");
|
|
|
|
|
//! }
|
|
|
|
|
//!
|
|
|
|
|
//! if let Some(text) = application.text(id) {
|
|
|
|
|
//! println!("activated button with text {text}");
|
|
|
|
|
//! }
|
|
|
|
|
//! }
|
|
|
|
|
//! }
|
|
|
|
|
//! ```
|
2022-12-28 23:18:31 +01:00
|
|
|
|
2023-01-03 19:35:34 +01:00
|
|
|
mod horizontal;
|
2023-01-09 16:18:02 +01:00
|
|
|
mod model;
|
2022-12-28 12:42:28 +01:00
|
|
|
mod style;
|
2023-01-03 19:35:34 +01:00
|
|
|
mod vertical;
|
2023-01-04 05:37:20 +01:00
|
|
|
mod widget;
|
2022-12-28 12:42:28 +01:00
|
|
|
|
2023-01-17 18:49:40 +01:00
|
|
|
pub use self::horizontal::{horizontal, HorizontalSegmentedButton};
|
|
|
|
|
pub use self::model::{
|
|
|
|
|
BuilderEntity, Entity, EntityMut, Model, ModelBuilder, MultiSelect, MultiSelectEntityMut,
|
|
|
|
|
MultiSelectModel, Selectable, SingleSelect, SingleSelectEntityMut, SingleSelectModel,
|
|
|
|
|
};
|
|
|
|
|
pub use self::style::{Appearance, ItemAppearance, ItemStatusAppearance, StyleSheet};
|
|
|
|
|
pub use self::vertical::{vertical, VerticalSegmentedButton};
|
2023-01-09 16:18:02 +01:00
|
|
|
pub use self::widget::{focus, Id, SegmentedButton, SegmentedVariant};
|
2023-01-17 18:49:40 +01:00
|
|
|
|
|
|
|
|
/// Associates extra data with an external secondary map.
|
|
|
|
|
///
|
|
|
|
|
/// The secondary map internally uses a `Vec`, so should only be used for data that
|
|
|
|
|
pub type SecondaryMap<T> = slotmap::SecondaryMap<Entity, T>;
|
|
|
|
|
|
|
|
|
|
/// Associates extra data with an external sparse secondary map.
|
|
|
|
|
///
|
|
|
|
|
/// Sparse maps internally use a `HashMap`, for data that is sparsely associated.
|
|
|
|
|
pub type SparseSecondaryMap<T> = slotmap::SparseSecondaryMap<Entity, T>;
|