Use closures for TextEditor::style

This commit is contained in:
Héctor Ramón Jiménez 2024-03-12 15:47:37 +01:00
parent 7a5f5b0be7
commit 3e190b9ee0
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 18 additions and 15 deletions

View file

@ -218,12 +218,12 @@ where
/// Creates a new [`TextEditor`]. /// Creates a new [`TextEditor`].
/// ///
/// [`TextEditor`]: crate::TextEditor /// [`TextEditor`]: crate::TextEditor
pub fn text_editor<Message, Theme, Renderer>( pub fn text_editor<'a, Message, Theme, Renderer>(
content: &text_editor::Content<Renderer>, content: &'a text_editor::Content<Renderer>,
) -> TextEditor<'_, core::text::highlighter::PlainText, Message, Theme, Renderer> ) -> TextEditor<'a, core::text::highlighter::PlainText, Message, Theme, Renderer>
where where
Message: Clone, Message: Clone,
Theme: text_editor::DefaultStyle, Theme: text_editor::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
TextEditor::new(content) TextEditor::new(content)

View file

@ -41,7 +41,7 @@ pub struct TextEditor<
width: Length, width: Length,
height: Length, height: Length,
padding: Padding, padding: Padding,
style: Style<Theme>, style: Style<'a, Theme>,
on_edit: Option<Box<dyn Fn(Action) -> Message + 'a>>, on_edit: Option<Box<dyn Fn(Action) -> Message + 'a>>,
highlighter_settings: Highlighter::Settings, highlighter_settings: Highlighter::Settings,
highlighter_format: fn( highlighter_format: fn(
@ -58,7 +58,7 @@ where
/// Creates new [`TextEditor`] with the given [`Content`]. /// Creates new [`TextEditor`] with the given [`Content`].
pub fn new(content: &'a Content<Renderer>) -> Self pub fn new(content: &'a Content<Renderer>) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Self { Self {
content, content,
@ -68,7 +68,7 @@ where
width: Length::Fill, width: Length::Fill,
height: Length::Shrink, height: Length::Shrink,
padding: Padding::new(5.0), padding: Padding::new(5.0),
style: Theme::default_style(), style: Box::new(Theme::default_style),
on_edit: None, on_edit: None,
highlighter_settings: (), highlighter_settings: (),
highlighter_format: |_highlight, _theme| { highlighter_format: |_highlight, _theme| {
@ -142,8 +142,11 @@ where
} }
/// Sets the style of the [`TextEditor`]. /// Sets the style of the [`TextEditor`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style.into(); mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
} }
@ -809,23 +812,23 @@ pub struct Appearance {
} }
/// The style of a [`TextEditor`]. /// 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`]. /// The default style of a [`TextEditor`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`TextEditor`]. /// Returns the default style of a [`TextEditor`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
default default(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }