libcosmic/src/widget/mod.rs

400 lines
9.7 KiB
Rust
Raw Normal View History

// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! The COSMIC widget library
//!
//! This module contains a wide variety of widgets used throughout the COSMIC app ecosystem.
//!
//! # Overview
//!
//! Add widgets to your application view by calling the modules and functions below.
//! Widgets are constructed by chaining their property methods using a functional paradigm.
//! Modules may contain additional functions for constructing different variations of a widget.
//! Each module will typically have one widget with the same name as the module, which will be re-exported here.
//!
//! ```no_run,ignore
//! use cosmic::prelude::*;
//! use cosmic::{cosmic_theme, theme, widget};
//!
//! const REPOSITORY: &str = "https://github.com/pop-os/libcosmic";
//!
//! let cosmic_theme::Spacing { space_xxs, .. } = theme::spacing();
//!
//! let link = widget::button::link(REPOSITORY)
//! .on_press(Message::LaunchUrl(REPOSITORY))
//! .padding(0);
//!
//! let content = widget::column()
//! .push(widget::icon::from_name("my-app-icon"))
//! .push(widget::text::title3("My App Name"))
//! .push(link)
//! .align_items(Alignment::Center)
//! .spacing(space_xxs);
//! ```
//!
//! Widgets may borrow data from your application struct, and should do so to avoid allocating.
//!
//! ```no_run,ignore
//! let text = widget::text::body(&self.cached_text);
//! ```
//!
//! Use the [`cosmic::Apply`](crate::Apply) trait to embed widgets into other widgets which accept them.
//!
//! ```no_run,ignore
//! let button = widget::icon::from_name("printer-symbolic")
//! .apply(widget::button::icon)
//! .on_press(Message::Print);
//! ```
// Re-exports from Iced
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{Canvas, canvas};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{Checkbox, checkbox};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{ComboBox, combo_box};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{Container, container};
#[doc(inline)]
2026-02-10 15:37:41 -05:00
pub use iced::widget::{Space, space};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{Image, image};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{Lazy, lazy};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{MouseArea, mouse_area};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{PaneGrid, pane_grid};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{ProgressBar, progress_bar};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{Responsive, responsive};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{Slider, VerticalSlider, slider, vertical_slider};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{Svg, svg};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use iced::widget::{TextEditor, text_editor};
#[doc(inline)]
pub use iced_core::widget::{Id, Operation, Widget};
2023-01-26 22:16:13 +01:00
pub mod aspect_ratio;
2024-10-16 20:36:46 -04:00
#[cfg(feature = "autosize")]
pub mod autosize;
pub(crate) mod responsive_container;
#[cfg(feature = "surface-message")]
mod responsive_menu_bar;
#[cfg(feature = "surface-message")]
#[doc(inline)]
pub use responsive_menu_bar::{ResponsiveMenuBar, responsive_menu_bar};
pub mod button;
#[doc(inline)]
pub use button::{Button, IconButton, LinkButton, TextButton};
pub(crate) mod common;
2024-03-14 16:16:51 +01:00
pub mod calendar;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use calendar::{Calendar, calendar};
2024-03-14 16:16:51 +01:00
2023-07-17 11:44:57 -04:00
pub mod card;
#[doc(inline)]
2023-07-17 11:44:57 -04:00
pub use card::*;
2023-09-29 16:14:03 -04:00
pub mod color_picker;
#[doc(inline)]
2023-10-12 13:21:38 +02:00
pub use color_picker::{ColorPicker, ColorPickerModel};
2023-09-29 16:14:03 -04:00
2024-08-28 22:48:31 +02:00
#[cfg(feature = "qr_code")]
#[doc(inline)]
pub use iced::widget::qr_code;
2026-02-19 18:15:22 -05:00
mod cards;
#[doc(inline)]
pub use cards::cards;
pub mod context_drawer;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use context_drawer::{ContextDrawer, context_drawer};
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use column::{Column, column};
pub mod column {
//! A container which aligns its children in a column.
2024-01-30 22:14:00 -05:00
pub type Column<'a, Message> = iced::widget::Column<'a, Message, crate::Theme, crate::Renderer>;
#[must_use]
/// A container which aligns its children in a column.
pub fn column<'a, Message>() -> Column<'a, Message> {
Column::new()
}
#[must_use]
/// A pre-allocated [`column`].
pub fn with_capacity<'a, Message>(capacity: usize) -> Column<'a, Message> {
Column::with_capacity(capacity)
}
#[must_use]
/// A [`column`] that will be assigned an [`Iterator`] of children.
pub fn with_children<'a, Message>(
children: impl IntoIterator<Item = crate::Element<'a, Message>>,
) -> Column<'a, Message> {
Column::with_children(children)
}
}
pub mod layer_container;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use layer_container::{LayerContainer, layer_container};
pub mod context_menu;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use context_menu::{ContextMenu, context_menu};
pub mod dialog;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use dialog::{Dialog, dialog};
/// An element to distinguish a boundary between two elements.
pub mod divider {
/// Horizontal variant of a divider.
pub mod horizontal {
2026-02-10 15:37:41 -05:00
use iced::{widget::Rule, widget::rule};
/// Horizontal divider with default thickness
#[must_use]
2024-10-16 20:36:46 -04:00
pub fn default<'a>() -> Rule<'a, crate::Theme> {
2026-02-10 15:37:41 -05:00
rule::horizontal(1).class(crate::theme::Rule::Default)
}
/// Horizontal divider with light thickness
#[must_use]
2024-10-16 20:36:46 -04:00
pub fn light<'a>() -> Rule<'a, crate::Theme> {
2026-02-10 15:37:41 -05:00
rule::horizontal(1).class(crate::theme::Rule::LightDivider)
}
/// Horizontal divider with heavy thickness.
#[must_use]
2024-10-16 20:36:46 -04:00
pub fn heavy<'a>() -> Rule<'a, crate::Theme> {
2026-02-10 15:37:41 -05:00
rule::horizontal(4).class(crate::theme::Rule::HeavyDivider)
}
}
/// Vertical variant of a divider.
pub mod vertical {
2026-02-10 15:37:41 -05:00
use iced::widget::{Rule, rule};
/// Vertical divider with default thickness
#[must_use]
2024-10-16 20:36:46 -04:00
pub fn default<'a>() -> Rule<'a, crate::Theme> {
2026-02-10 15:37:41 -05:00
rule::vertical(1).class(crate::theme::Rule::Default)
}
/// Vertical divider with light thickness
#[must_use]
2024-10-16 20:36:46 -04:00
pub fn light<'a>() -> Rule<'a, crate::Theme> {
2026-02-10 15:37:41 -05:00
rule::vertical(4).class(crate::theme::Rule::LightDivider)
}
/// Vertical divider with heavy thickness.
#[must_use]
2024-10-16 20:36:46 -04:00
pub fn heavy<'a>() -> Rule<'a, crate::Theme> {
2026-02-10 15:37:41 -05:00
rule::vertical(10).class(crate::theme::Rule::HeavyDivider)
}
}
}
pub mod dnd_destination;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use dnd_destination::{DndDestination, dnd_destination};
pub mod dnd_source;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use dnd_source::{DndSource, dnd_source};
pub mod dropdown;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use dropdown::{Dropdown, dropdown};
2023-06-23 23:39:48 +02:00
pub mod flex_row;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use flex_row::{FlexRow, flex_row};
2023-06-23 23:39:48 +02:00
pub mod grid;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use grid::{Grid, grid};
2022-10-09 02:35:03 -07:00
mod header_bar;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use header_bar::{HeaderBar, header_bar};
2022-10-07 05:23:26 +02:00
pub mod icon;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use icon::{Icon, icon};
2024-05-30 15:48:42 -04:00
pub mod id_container;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use id_container::{IdContainer, id_container};
2024-05-30 15:48:42 -04:00
2023-07-26 16:32:23 -04:00
#[cfg(feature = "animated-image")]
pub mod frames;
2026-03-11 17:25:02 +01:00
pub use taffy::{JustifyContent, JustifyItems};
pub mod list;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use list::{ListColumn, list_column};
2023-11-16 08:00:11 -07:00
pub mod menu;
pub mod nav_bar;
#[doc(inline)]
pub use nav_bar::{nav_bar, nav_bar_dnd};
pub mod nav_bar_toggle;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use nav_bar_toggle::{NavBarToggle, nav_bar_toggle};
pub mod popover;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use popover::{Popover, popover};
2024-08-28 15:22:26 +02:00
pub mod radio;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use radio::{Radio, radio};
2024-08-28 15:22:26 +02:00
2023-01-26 22:16:13 +01:00
pub mod rectangle_tracker;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use rectangle_tracker::{RectangleTracker, rectangle_tracking_container};
2023-01-26 22:16:13 +01:00
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use row::{Row, row};
pub mod row {
//! A container which aligns its children in a row.
2024-01-30 22:14:00 -05:00
pub type Row<'a, Message> = iced::widget::Row<'a, Message, crate::Theme, crate::Renderer>;
#[must_use]
/// A container which aligns its children in a row.
pub fn row<'a, Message>() -> Row<'a, Message> {
Row::new()
}
#[must_use]
/// A pre-allocated [`row`].
pub fn with_capacity<'a, Message>(capacity: usize) -> Row<'a, Message> {
Row::with_capacity(capacity)
}
#[must_use]
/// A [`row`] that will be assigned an [`Iterator`] of children.
pub fn with_children<'a, Message>(
children: impl IntoIterator<Item = crate::Element<'a, Message>>,
) -> Row<'a, Message> {
Row::with_children(children)
}
}
pub mod scrollable;
#[doc(inline)]
pub use scrollable::scrollable;
2022-12-28 12:42:28 +01:00
pub mod segmented_button;
pub mod segmented_control;
2022-12-28 12:42:28 +01:00
pub mod settings;
pub mod spin_button;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use spin_button::{SpinButton, spin_button, vertical as vertical_spin_button};
2022-12-14 16:51:22 -05:00
pub mod tab_bar;
2025-03-24 16:48:20 +00:00
pub mod table;
#[doc(inline)]
pub use table::{compact_table, table};
pub mod text;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use text::{Text, text};
2022-12-17 18:17:13 -05:00
pub mod text_input;
#[doc(inline)]
pub use text_input::{
2025-03-21 13:33:07 +01:00
TextInput, editable_input, inline_input, search_input, secure_input, text_input,
};
pub mod toaster;
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use toaster::{Toast, ToastId, Toasts, toaster};
2023-01-26 22:16:13 +01:00
mod toggler;
#[doc(inline)]
2026-02-25 11:10:34 -05:00
pub use toggler::{Toggler, toggler};
2023-01-16 12:04:39 -07:00
#[doc(inline)]
2025-03-21 13:33:07 +01:00
pub use tooltip::{Tooltip, tooltip};
#[cfg(all(feature = "wayland", feature = "winit"))]
pub mod wayland;
2023-09-13 15:34:47 +02:00
pub mod tooltip {
use crate::Element;
pub use iced::widget::tooltip::Position;
2024-01-30 22:14:00 -05:00
pub type Tooltip<'a, Message> =
iced::widget::Tooltip<'a, Message, crate::Theme, crate::Renderer>;
2023-09-13 15:34:47 +02:00
pub fn tooltip<'a, Message>(
content: impl Into<Element<'a, Message>>,
2024-10-16 20:36:46 -04:00
tooltip: impl Into<Element<'a, Message>>,
2023-09-13 15:34:47 +02:00
position: Position,
) -> Tooltip<'a, Message> {
let xxs = crate::theme::spacing().space_xxs;
2023-09-13 15:34:47 +02:00
Tooltip::new(content, tooltip, position)
2024-10-16 20:36:46 -04:00
.class(crate::theme::Container::Tooltip)
2023-09-13 15:34:47 +02:00
.padding(xxs)
.gap(1)
}
}
2023-01-16 12:04:39 -07:00
pub mod warning;
#[doc(inline)]
2023-01-16 12:04:39 -07:00
pub use warning::*;
2024-11-07 01:15:04 +01:00
pub mod wrapper;
#[doc(inline)]
pub use wrapper::*;
2024-11-07 01:15:04 +01:00
#[cfg(feature = "markdown")]
#[doc(inline)]
pub use iced::widget::markdown;
2024-11-10 02:42:16 +01:00
2024-11-11 11:11:49 -07:00
#[cfg(feature = "about")]
2024-11-10 02:42:16 +01:00
pub mod about;
2024-11-11 11:11:49 -07:00
#[cfg(feature = "about")]
2024-11-10 02:42:16 +01:00
#[doc(inline)]
pub use about::about;