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`].
///
/// [`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)

View file

@ -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
}
}