feat(widget): add text function with Cow<str> input

This commit is contained in:
Michael Aaron Murphy 2023-01-23 22:48:06 +01:00 committed by Michael Murphy
parent f81a06bc4a
commit b3a3c9c29a
6 changed files with 37 additions and 14 deletions

View file

@ -89,8 +89,11 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
widget.into() widget.into()
} }
fn title_widget(&self) -> Element<'a, Message> { fn title_widget(&mut self) -> Element<'a, Message> {
widget::container(widget::text(self.title.clone())) let mut title = Cow::default();
std::mem::swap(&mut title, &mut self.title);
widget::container(super::text(title))
.center_x() .center_x()
.center_y() .center_y()
.width(Length::Fill) .width(Length::Fill)
@ -106,7 +109,7 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
super::icon(name, size) super::icon(name, size)
.force_svg(true) .force_svg(true)
.style(crate::theme::Svg::SymbolicActive) .style(crate::theme::Svg::SymbolicActive)
.apply(iced::widget::button) .apply(widget::button)
.style(theme::Button::Text) .style(theme::Button::Text)
.on_press(on_press) .on_press(on_press)
}; };

View file

@ -37,6 +37,9 @@ pub mod settings;
mod scrollable; mod scrollable;
pub use scrollable::*; pub use scrollable::*;
mod text;
pub use text::{text, Text};
pub mod separator; pub mod separator;
pub use separator::{horizontal_rule, vertical_rule}; pub use separator::{horizontal_rule, vertical_rule};

View file

@ -1,18 +1,21 @@
// Copyright 2022 System76 <info@system76.com> // Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0 // 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. /// A setting within a settings view section.
#[must_use] #[must_use]
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
pub fn item<'a, Message: 'static>( pub fn item<'a, Message: 'static>(
title: impl ToString, title: impl Into<Cow<'a, str>>,
widget: impl Into<Element<'a, Message>>, widget: impl Into<Element<'a, Message>>,
) -> iced::widget::Row<'a, Message, Renderer> { ) -> Row<'a, Message, Renderer> {
item_row(vec![ item_row(vec![
iced::widget::text(title).into(), text(title).into(),
iced::widget::horizontal_space(iced::Length::Fill).into(), horizontal_space(iced::Length::Fill).into(),
widget.into(), widget.into(),
]) ])
} }
@ -20,8 +23,8 @@ pub fn item<'a, Message: 'static>(
/// A settings item aligned in a row /// A settings item aligned in a row
#[must_use] #[must_use]
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
pub fn item_row<Message>(children: Vec<Element<Message>>) -> iced::widget::Row<Message, Renderer> { pub fn item_row<Message>(children: Vec<Element<Message>>) -> Row<Message, Renderer> {
iced::widget::row(children) row(children)
.align_items(iced::Alignment::Center) .align_items(iced::Alignment::Center)
.padding([0, 18]) .padding([0, 18])
.spacing(12) .spacing(12)

View file

@ -6,12 +6,12 @@ use std::borrow::Cow;
pub use self::model::{Message, Model}; pub use self::model::{Message, Model};
use crate::widget::icon; use crate::widget::{icon, text};
use crate::{theme, Element}; use crate::{theme, Element};
use apply::Apply; use apply::Apply;
use iced::{ use iced::{
alignment::{Horizontal, Vertical}, alignment::{Horizontal, Vertical},
widget::{button, container, row, text}, widget::{button, container, row},
Alignment, Background, Length, Alignment, Background, Length,
}; };

14
src/widget/text.rs Normal file
View file

@ -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<Cow<'a, str>>) -> Text<'a, Renderer>
where
Renderer: iced_native::text::Renderer,
Renderer::Theme: iced::widget::text::StyleSheet,
{
Text::new(text)
}

View file

@ -31,7 +31,7 @@ impl<'a, Message: 'static + Clone> Warning<'a, Message> {
} }
/// A custom button that has the desired default spacing and padding. /// 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 = let close_button =
widget::button(icon("window-close-symbolic", 16).style(theme::Svg::Default)) widget::button(icon("window-close-symbolic", 16).style(theme::Svg::Default))
.style(theme::Button::Transparent); .style(theme::Button::Transparent);
@ -44,7 +44,7 @@ impl<'a, Message: 'static + Clone> Warning<'a, Message> {
widget::container( widget::container(
widget::row(vec![ widget::row(vec![
widget::container(widget::text(self.message)) widget::container(crate::widget::text(self.message))
.width(Length::Fill) .width(Length::Fill)
.into(), .into(),
close_button.into(), close_button.into(),