Skip to main content

iced_core/text/
highlighter.rs

1//! Highlight text.
2use crate::Color;
3use crate::font;
4
5/// A type that describes how to highlight an `Input`
6/// with some [`Style`].
7pub trait Highlighter<Input, Theme = crate::Theme> {
8    /// A unique identifier for the highlighter.
9    fn id(&self) -> &str;
10
11    /// Returns the [`Style`] of the given `Input`.
12    fn highlight(&self, input: Input, theme: &Theme) -> Style;
13}
14
15/// The style of some highlighted text.
16#[derive(Debug, Clone, Copy, PartialEq, Default)]
17pub struct Style {
18    /// The [`Color`] of the text.
19    pub color: Option<Color>,
20    /// The [`font::Style`] of the text.
21    pub style: Option<font::Style>,
22}
23
24impl<T, Input, Theme> Highlighter<Input, Theme> for T
25where
26    T: Fn(Input, &Theme) -> Style,
27{
28    fn id(&self) -> &str {
29        std::any::type_name_of_val(self) // Hack: Best effort
30    }
31
32    fn highlight(&self, input: Input, theme: &Theme) -> Style {
33        (self)(input, theme)
34    }
35}