2023-09-01 07:29:19 +02:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2024-05-20 20:01:47 +02:00
|
|
|
//! Button widgets for COSMIC applications.
|
|
|
|
|
|
2023-09-13 15:47:32 +02:00
|
|
|
pub use crate::theme::Button as Style;
|
|
|
|
|
|
|
|
|
|
pub mod link;
|
2023-09-18 15:44:44 +02:00
|
|
|
use derive_setters::Setters;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-09-13 15:47:32 +02:00
|
|
|
pub use link::link;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-09-13 15:47:32 +02:00
|
|
|
pub use link::Button as LinkButton;
|
2023-09-01 07:29:19 +02:00
|
|
|
|
|
|
|
|
mod icon;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub use icon::icon;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub use icon::Button as IconButton;
|
|
|
|
|
|
2023-10-30 16:32:10 +01:00
|
|
|
mod image;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-10-30 16:32:10 +01:00
|
|
|
pub use image::image;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-10-30 16:32:10 +01:00
|
|
|
pub use image::Button as ImageButton;
|
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
mod style;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub use style::{Appearance, StyleSheet};
|
|
|
|
|
|
|
|
|
|
mod text;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub use text::Button as TextButton;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub use text::{destructive, standard, suggested, text};
|
|
|
|
|
|
|
|
|
|
mod widget;
|
2024-05-20 20:01:47 +02:00
|
|
|
#[doc(inline)]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub use widget::{draw, focus, layout, mouse_interaction, Button};
|
|
|
|
|
|
|
|
|
|
use iced_core::font::Weight;
|
|
|
|
|
use iced_core::widget::Id;
|
|
|
|
|
use iced_core::{Length, Padding};
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
2024-05-20 20:01:47 +02:00
|
|
|
/// A button with the default style, which may contain any widget as its content.
|
2024-05-17 19:08:43 +02:00
|
|
|
pub fn button<'a, Message>(content: impl Into<crate::Element<'a, Message>>) -> Button<'a, Message> {
|
2023-09-01 07:29:19 +02:00
|
|
|
Button::new(content)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 20:01:47 +02:00
|
|
|
/// An image button which may contain any widget as its content.
|
2024-05-17 19:08:43 +02:00
|
|
|
pub fn custom_image_button<'a, Message>(
|
|
|
|
|
content: impl Into<crate::Element<'a, Message>>,
|
2023-11-15 16:09:17 +01:00
|
|
|
on_remove: Option<Message>,
|
2024-05-17 19:08:43 +02:00
|
|
|
) -> Button<'a, Message> {
|
2023-11-15 16:09:17 +01:00
|
|
|
Button::new_image(content, on_remove)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 20:01:47 +02:00
|
|
|
/// A builder for constructing a custom [`Button`].
|
2023-09-01 07:29:19 +02:00
|
|
|
#[must_use]
|
2023-09-18 15:44:44 +02:00
|
|
|
#[derive(Setters)]
|
2023-09-01 07:29:19 +02:00
|
|
|
pub struct Builder<'a, Message, Variant> {
|
2023-09-18 15:44:44 +02:00
|
|
|
/// Sets the [`Id`] of the button.
|
2023-09-01 07:29:19 +02:00
|
|
|
id: Id,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// The label to display within the button.
|
|
|
|
|
#[setters(into)]
|
2023-09-01 07:29:19 +02:00
|
|
|
label: Cow<'a, str>,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
// Adds a tooltip to the button.
|
|
|
|
|
#[setters(into)]
|
2023-09-13 15:47:32 +02:00
|
|
|
tooltip: Cow<'a, str>,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// Sets the message that will be produced when the button is pressed.
|
|
|
|
|
///
|
|
|
|
|
/// If `None`, the button will be disabled.
|
|
|
|
|
#[setters(strip_option)]
|
2023-09-01 07:29:19 +02:00
|
|
|
on_press: Option<Message>,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// Sets the preferred width of the button.
|
2023-09-19 16:39:25 +02:00
|
|
|
#[setters(into)]
|
2023-09-01 07:29:19 +02:00
|
|
|
width: Length,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// Sets the preferred height of the button.
|
2023-09-19 16:39:25 +02:00
|
|
|
#[setters(into)]
|
2023-09-01 07:29:19 +02:00
|
|
|
height: Length,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// Sets the preferred padding of the button.
|
|
|
|
|
#[setters(into)]
|
2023-09-01 07:29:19 +02:00
|
|
|
padding: Padding,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// Sets the preferred spacing between elements in the button.
|
2023-09-01 07:29:19 +02:00
|
|
|
spacing: u16,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// Sets the preferred size of icons.
|
2023-09-01 07:29:19 +02:00
|
|
|
icon_size: u16,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// Sets the prefered font line height.
|
2023-09-01 07:29:19 +02:00
|
|
|
line_height: u16,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// Sets the preferred font size.
|
2023-09-01 07:29:19 +02:00
|
|
|
font_size: u16,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
/// Sets the preferred font weight.
|
2023-09-01 07:29:19 +02:00
|
|
|
font_weight: Weight,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
2023-10-30 16:32:10 +01:00
|
|
|
/// The preferred style of the button.
|
2023-09-01 07:29:19 +02:00
|
|
|
style: Style,
|
2023-09-18 15:44:44 +02:00
|
|
|
|
|
|
|
|
#[setters(skip)]
|
2023-09-01 07:29:19 +02:00
|
|
|
variant: Variant,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message, Variant> Builder<'a, Message, Variant> {
|
2023-09-18 15:44:44 +02:00
|
|
|
/// Set the value of [`on_press`] as either `Some` or `None`.
|
2023-09-01 07:29:19 +02:00
|
|
|
pub fn on_press_maybe(mut self, on_press: Option<Message>) -> Self {
|
|
|
|
|
self.on_press = on_press;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|