From b3a3c9c29aaa003e8e4357ebffe343298fc1b05b Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Mon, 23 Jan 2023 22:48:06 +0100 Subject: [PATCH] feat(widget): add text function with Cow input --- src/widget/header_bar.rs | 9 ++++++--- src/widget/mod.rs | 3 +++ src/widget/settings/item.rs | 17 ++++++++++------- src/widget/spin_button/mod.rs | 4 ++-- src/widget/text.rs | 14 ++++++++++++++ src/widget/warning.rs | 4 ++-- 6 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 src/widget/text.rs diff --git a/src/widget/header_bar.rs b/src/widget/header_bar.rs index b3e714a1..2d1f610d 100644 --- a/src/widget/header_bar.rs +++ b/src/widget/header_bar.rs @@ -89,8 +89,11 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> { widget.into() } - fn title_widget(&self) -> Element<'a, Message> { - widget::container(widget::text(self.title.clone())) + fn title_widget(&mut self) -> Element<'a, Message> { + let mut title = Cow::default(); + std::mem::swap(&mut title, &mut self.title); + + widget::container(super::text(title)) .center_x() .center_y() .width(Length::Fill) @@ -106,7 +109,7 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> { super::icon(name, size) .force_svg(true) .style(crate::theme::Svg::SymbolicActive) - .apply(iced::widget::button) + .apply(widget::button) .style(theme::Button::Text) .on_press(on_press) }; diff --git a/src/widget/mod.rs b/src/widget/mod.rs index e8964f4e..d6bf8b82 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -37,6 +37,9 @@ pub mod settings; mod scrollable; pub use scrollable::*; +mod text; +pub use text::{text, Text}; + pub mod separator; pub use separator::{horizontal_rule, vertical_rule}; diff --git a/src/widget/settings/item.rs b/src/widget/settings/item.rs index 2cb0c99b..4e5b56af 100644 --- a/src/widget/settings/item.rs +++ b/src/widget/settings/item.rs @@ -1,18 +1,21 @@ // Copyright 2022 System76 // SPDX-License-Identifier: MPL-2.0 -use crate::{Element, Renderer}; +use std::borrow::Cow; + +use crate::{widget::text, Element, Renderer}; +use iced::widget::{horizontal_space, row, Row}; /// A setting within a settings view section. #[must_use] #[allow(clippy::module_name_repetitions)] pub fn item<'a, Message: 'static>( - title: impl ToString, + title: impl Into>, widget: impl Into>, -) -> iced::widget::Row<'a, Message, Renderer> { +) -> Row<'a, Message, Renderer> { item_row(vec![ - iced::widget::text(title).into(), - iced::widget::horizontal_space(iced::Length::Fill).into(), + text(title).into(), + horizontal_space(iced::Length::Fill).into(), widget.into(), ]) } @@ -20,8 +23,8 @@ pub fn item<'a, Message: 'static>( /// A settings item aligned in a row #[must_use] #[allow(clippy::module_name_repetitions)] -pub fn item_row(children: Vec>) -> iced::widget::Row { - iced::widget::row(children) +pub fn item_row(children: Vec>) -> Row { + row(children) .align_items(iced::Alignment::Center) .padding([0, 18]) .spacing(12) diff --git a/src/widget/spin_button/mod.rs b/src/widget/spin_button/mod.rs index bf51549e..1115dc56 100644 --- a/src/widget/spin_button/mod.rs +++ b/src/widget/spin_button/mod.rs @@ -6,12 +6,12 @@ use std::borrow::Cow; pub use self::model::{Message, Model}; -use crate::widget::icon; +use crate::widget::{icon, text}; use crate::{theme, Element}; use apply::Apply; use iced::{ alignment::{Horizontal, Vertical}, - widget::{button, container, row, text}, + widget::{button, container, row}, Alignment, Background, Length, }; diff --git a/src/widget/text.rs b/src/widget/text.rs new file mode 100644 index 00000000..c868f9a6 --- /dev/null +++ b/src/widget/text.rs @@ -0,0 +1,14 @@ +use std::borrow::Cow; + +pub use iced::widget::Text; + +/// Creates a new [`Text`] widget with the provided content. +/// +/// [`Text`]: widget::Text +pub fn text<'a, Renderer>(text: impl Into>) -> Text<'a, Renderer> +where + Renderer: iced_native::text::Renderer, + Renderer::Theme: iced::widget::text::StyleSheet, +{ + Text::new(text) +} diff --git a/src/widget/warning.rs b/src/widget/warning.rs index 764a87b6..4bc5d497 100644 --- a/src/widget/warning.rs +++ b/src/widget/warning.rs @@ -31,7 +31,7 @@ impl<'a, Message: 'static + Clone> Warning<'a, Message> { } /// A custom button that has the desired default spacing and padding. - pub fn into_widget(self) -> widget::Container<'static, Message, Renderer> { + pub fn into_widget(self) -> widget::Container<'a, Message, Renderer> { let close_button = widget::button(icon("window-close-symbolic", 16).style(theme::Svg::Default)) .style(theme::Button::Transparent); @@ -44,7 +44,7 @@ impl<'a, Message: 'static + Clone> Warning<'a, Message> { widget::container( widget::row(vec![ - widget::container(widget::text(self.message)) + widget::container(crate::widget::text(self.message)) .width(Length::Fill) .into(), close_button.into(),