summaryrefslogtreecommitdiffstats
path: root/widget
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-12 15:47:37 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-12 15:47:37 +0100
commit3e190b9ee0d95d73008dab23b18117e0a21223d8 (patch)
tree4c4adec4da60205c0dbfb4a16eb1642ae5735af6 /widget
parent7a5f5b0be779e5128de05fa779693fe65e81c1e6 (diff)
downloadiced-3e190b9ee0d95d73008dab23b18117e0a21223d8.tar.gz
iced-3e190b9ee0d95d73008dab23b18117e0a21223d8.tar.bz2
iced-3e190b9ee0d95d73008dab23b18117e0a21223d8.zip
Use closures for `TextEditor::style`
Diffstat (limited to 'widget')
-rw-r--r--widget/src/helpers.rs8
-rw-r--r--widget/src/text_editor.rs25
2 files changed, 18 insertions, 15 deletions
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs
index b23e3cb9..4863e550 100644
--- a/widget/src/helpers.rs
+++ b/widget/src/helpers.rs
@@ -218,12 +218,12 @@ where
/// Creates a new [`TextEditor`].
///
/// [`TextEditor`]: crate::TextEditor
-pub fn text_editor<Message, Theme, Renderer>(
- content: &text_editor::Content<Renderer>,
-) -> TextEditor<'_, core::text::highlighter::PlainText, Message, Theme, Renderer>
+pub fn text_editor<'a, Message, Theme, Renderer>(
+ content: &'a text_editor::Content<Renderer>,
+) -> TextEditor<'a, core::text::highlighter::PlainText, Message, Theme, Renderer>
where
Message: Clone,
- Theme: text_editor::DefaultStyle,
+ Theme: text_editor::DefaultStyle + 'a,
Renderer: core::text::Renderer,
{
TextEditor::new(content)
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs
index c7d62d92..5b8f6a1b 100644
--- a/widget/src/text_editor.rs
+++ b/widget/src/text_editor.rs
@@ -41,7 +41,7 @@ pub struct TextEditor<
width: Length,
height: Length,
padding: Padding,
- style: Style<Theme>,
+ style: Style<'a, Theme>,
on_edit: Option<Box<dyn Fn(Action) -> Message + 'a>>,
highlighter_settings: Highlighter::Settings,
highlighter_format: fn(
@@ -58,7 +58,7 @@ where
/// Creates new [`TextEditor`] with the given [`Content`].
pub fn new(content: &'a Content<Renderer>) -> Self
where
- Theme: DefaultStyle,
+ Theme: DefaultStyle + 'a,
{
Self {
content,
@@ -68,7 +68,7 @@ where
width: Length::Fill,
height: Length::Shrink,
padding: Padding::new(5.0),
- style: Theme::default_style(),
+ style: Box::new(Theme::default_style),
on_edit: None,
highlighter_settings: (),
highlighter_format: |_highlight, _theme| {
@@ -142,8 +142,11 @@ where
}
/// Sets the style of the [`TextEditor`].
- pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
- self.style = style.into();
+ pub fn style(
+ mut self,
+ style: impl Fn(&Theme, Status) -> Appearance + 'a,
+ ) -> Self {
+ self.style = Box::new(style);
self
}
}
@@ -809,23 +812,23 @@ pub struct Appearance {
}
/// The style of a [`TextEditor`].
-pub type Style<Theme> = fn(&Theme, Status) -> Appearance;
+pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>;
/// The default style of a [`TextEditor`].
pub trait DefaultStyle {
/// Returns the default style of a [`TextEditor`].
- fn default_style() -> Style<Self>;
+ fn default_style(&self, status: Status) -> Appearance;
}
impl DefaultStyle for Theme {
- fn default_style() -> Style<Self> {
- default
+ fn default_style(&self, status: Status) -> Appearance {
+ default(self, status)
}
}
impl DefaultStyle for Appearance {
- fn default_style() -> Style<Self> {
- |appearance, _status| *appearance
+ fn default_style(&self, _status: Status) -> Appearance {
+ *self
}
}