Use new theme::Base::name for theme change detection
This commit is contained in:
parent
96be0b5470
commit
0495f32918
2 changed files with 61 additions and 48 deletions
|
|
@ -168,31 +168,7 @@ impl Theme {
|
||||||
|
|
||||||
impl fmt::Display for Theme {
|
impl fmt::Display for Theme {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
f.write_str(self.name())
|
||||||
Self::Light => write!(f, "Light"),
|
|
||||||
Self::Dark => write!(f, "Dark"),
|
|
||||||
Self::Dracula => write!(f, "Dracula"),
|
|
||||||
Self::Nord => write!(f, "Nord"),
|
|
||||||
Self::SolarizedLight => write!(f, "Solarized Light"),
|
|
||||||
Self::SolarizedDark => write!(f, "Solarized Dark"),
|
|
||||||
Self::GruvboxLight => write!(f, "Gruvbox Light"),
|
|
||||||
Self::GruvboxDark => write!(f, "Gruvbox Dark"),
|
|
||||||
Self::CatppuccinLatte => write!(f, "Catppuccin Latte"),
|
|
||||||
Self::CatppuccinFrappe => write!(f, "Catppuccin Frappé"),
|
|
||||||
Self::CatppuccinMacchiato => write!(f, "Catppuccin Macchiato"),
|
|
||||||
Self::CatppuccinMocha => write!(f, "Catppuccin Mocha"),
|
|
||||||
Self::TokyoNight => write!(f, "Tokyo Night"),
|
|
||||||
Self::TokyoNightStorm => write!(f, "Tokyo Night Storm"),
|
|
||||||
Self::TokyoNightLight => write!(f, "Tokyo Night Light"),
|
|
||||||
Self::KanagawaWave => write!(f, "Kanagawa Wave"),
|
|
||||||
Self::KanagawaDragon => write!(f, "Kanagawa Dragon"),
|
|
||||||
Self::KanagawaLotus => write!(f, "Kanagawa Lotus"),
|
|
||||||
Self::Moonfly => write!(f, "Moonfly"),
|
|
||||||
Self::Nightfly => write!(f, "Nightfly"),
|
|
||||||
Self::Oxocarbon => write!(f, "Oxocarbon"),
|
|
||||||
Self::Ferra => write!(f, "Ferra"),
|
|
||||||
Self::Custom(custom) => custom.fmt(f),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -270,6 +246,12 @@ pub trait Base {
|
||||||
/// debugging purposes; like displaying performance
|
/// debugging purposes; like displaying performance
|
||||||
/// metrics or devtools.
|
/// metrics or devtools.
|
||||||
fn palette(&self) -> Option<Palette>;
|
fn palette(&self) -> Option<Palette>;
|
||||||
|
|
||||||
|
/// Returns the unique name of the theme.
|
||||||
|
///
|
||||||
|
/// This name may be used to efficiently detect theme
|
||||||
|
/// changes in some widgets.
|
||||||
|
fn name(&self) -> &str;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Base for Theme {
|
impl Base for Theme {
|
||||||
|
|
@ -313,6 +295,34 @@ impl Base for Theme {
|
||||||
fn palette(&self) -> Option<Palette> {
|
fn palette(&self) -> Option<Palette> {
|
||||||
Some(self.palette())
|
Some(self.palette())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
Self::Light => "Light",
|
||||||
|
Self::Dark => "Dark",
|
||||||
|
Self::Dracula => "Dracula",
|
||||||
|
Self::Nord => "Nord",
|
||||||
|
Self::SolarizedLight => "Solarized Light",
|
||||||
|
Self::SolarizedDark => "Solarized Dark",
|
||||||
|
Self::GruvboxLight => "Gruvbox Light",
|
||||||
|
Self::GruvboxDark => "Gruvbox Dark",
|
||||||
|
Self::CatppuccinLatte => "Catppuccin Latte",
|
||||||
|
Self::CatppuccinFrappe => "Catppuccin Frappé",
|
||||||
|
Self::CatppuccinMacchiato => "Catppuccin Macchiato",
|
||||||
|
Self::CatppuccinMocha => "Catppuccin Mocha",
|
||||||
|
Self::TokyoNight => "Tokyo Night",
|
||||||
|
Self::TokyoNightStorm => "Tokyo Night Storm",
|
||||||
|
Self::TokyoNightLight => "Tokyo Night Light",
|
||||||
|
Self::KanagawaWave => "Kanagawa Wave",
|
||||||
|
Self::KanagawaDragon => "Kanagawa Dragon",
|
||||||
|
Self::KanagawaLotus => "Kanagawa Lotus",
|
||||||
|
Self::Moonfly => "Moonfly",
|
||||||
|
Self::Nightfly => "Nightfly",
|
||||||
|
Self::Oxocarbon => "Oxocarbon",
|
||||||
|
Self::Ferra => "Ferra",
|
||||||
|
Self::Custom(custom) => &custom.name,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The default [`Style`] of a built-in [`Theme`].
|
/// The default [`Style`] of a built-in [`Theme`].
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,7 @@ use crate::core::renderer;
|
||||||
use crate::core::text::editor::{Cursor, Editor as _};
|
use crate::core::text::editor::{Cursor, Editor as _};
|
||||||
use crate::core::text::highlighter::{self, Highlighter};
|
use crate::core::text::highlighter::{self, Highlighter};
|
||||||
use crate::core::text::{self, LineHeight, Text, Wrapping};
|
use crate::core::text::{self, LineHeight, Text, Wrapping};
|
||||||
|
use crate::core::theme;
|
||||||
use crate::core::time::{Duration, Instant};
|
use crate::core::time::{Duration, Instant};
|
||||||
use crate::core::widget::operation;
|
use crate::core::widget::operation;
|
||||||
use crate::core::widget::{self, Widget};
|
use crate::core::widget::{self, Widget};
|
||||||
|
|
@ -148,7 +149,7 @@ where
|
||||||
max_height: f32::INFINITY,
|
max_height: f32::INFINITY,
|
||||||
padding: Padding::new(5.0),
|
padding: Padding::new(5.0),
|
||||||
wrapping: Wrapping::default(),
|
wrapping: Wrapping::default(),
|
||||||
class: Theme::default(),
|
class: <Theme as Catalog>::default(),
|
||||||
key_binding: None,
|
key_binding: None,
|
||||||
on_edit: None,
|
on_edit: None,
|
||||||
highlighter_settings: (),
|
highlighter_settings: (),
|
||||||
|
|
@ -170,7 +171,7 @@ impl<'a, Highlighter, Message, Theme, Renderer>
|
||||||
TextEditor<'a, Highlighter, Message, Theme, Renderer>
|
TextEditor<'a, Highlighter, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Highlighter: text::Highlighter,
|
Highlighter: text::Highlighter,
|
||||||
Theme: Catalog + 'static,
|
Theme: Catalog,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
/// Sets the placeholder of the [`TextEditor`].
|
/// Sets the placeholder of the [`TextEditor`].
|
||||||
|
|
@ -335,7 +336,7 @@ where
|
||||||
|
|
||||||
fn input_method<'b>(
|
fn input_method<'b>(
|
||||||
&self,
|
&self,
|
||||||
state: &'b State<Highlighter, Theme>,
|
state: &'b State<Highlighter>,
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
) -> InputMethod<&'b str> {
|
) -> InputMethod<&'b str> {
|
||||||
|
|
@ -508,13 +509,13 @@ where
|
||||||
|
|
||||||
/// The state of a [`TextEditor`].
|
/// The state of a [`TextEditor`].
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct State<Highlighter: text::Highlighter, Theme> {
|
pub struct State<Highlighter: text::Highlighter> {
|
||||||
focus: Option<Focus>,
|
focus: Option<Focus>,
|
||||||
preedit: Option<input_method::Preedit>,
|
preedit: Option<input_method::Preedit>,
|
||||||
last_click: Option<mouse::Click>,
|
last_click: Option<mouse::Click>,
|
||||||
drag_click: Option<mouse::click::Kind>,
|
drag_click: Option<mouse::click::Kind>,
|
||||||
partial_scroll: f32,
|
partial_scroll: f32,
|
||||||
last_theme: RefCell<Option<Theme>>,
|
last_theme: RefCell<Option<String>>,
|
||||||
highlighter: RefCell<Highlighter>,
|
highlighter: RefCell<Highlighter>,
|
||||||
highlighter_settings: Highlighter::Settings,
|
highlighter_settings: Highlighter::Settings,
|
||||||
highlighter_format_address: usize,
|
highlighter_format_address: usize,
|
||||||
|
|
@ -548,15 +549,15 @@ impl Focus {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Highlighter: text::Highlighter, Theme> State<Highlighter, Theme> {
|
impl<Highlighter: text::Highlighter> State<Highlighter> {
|
||||||
/// Returns whether the [`TextEditor`] is currently focused or not.
|
/// Returns whether the [`TextEditor`] is currently focused or not.
|
||||||
pub fn is_focused(&self) -> bool {
|
pub fn is_focused(&self) -> bool {
|
||||||
self.focus.is_some()
|
self.focus.is_some()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Highlighter: text::Highlighter, Theme> operation::Focusable
|
impl<Highlighter: text::Highlighter> operation::Focusable
|
||||||
for State<Highlighter, Theme>
|
for State<Highlighter>
|
||||||
{
|
{
|
||||||
fn is_focused(&self) -> bool {
|
fn is_focused(&self) -> bool {
|
||||||
self.focus.is_some()
|
self.focus.is_some()
|
||||||
|
|
@ -575,11 +576,11 @@ impl<Highlighter, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||||
for TextEditor<'_, Highlighter, Message, Theme, Renderer>
|
for TextEditor<'_, Highlighter, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Highlighter: text::Highlighter,
|
Highlighter: text::Highlighter,
|
||||||
Theme: Catalog + 'static,
|
Theme: Catalog,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
fn tag(&self) -> widget::tree::Tag {
|
fn tag(&self) -> widget::tree::Tag {
|
||||||
widget::tree::Tag::of::<State<Highlighter, Theme>>()
|
widget::tree::Tag::of::<State<Highlighter>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn state(&self) -> widget::tree::State {
|
fn state(&self) -> widget::tree::State {
|
||||||
|
|
@ -589,7 +590,7 @@ where
|
||||||
last_click: None,
|
last_click: None,
|
||||||
drag_click: None,
|
drag_click: None,
|
||||||
partial_scroll: 0.0,
|
partial_scroll: 0.0,
|
||||||
last_theme: RefCell::<Option<Theme>>::default(),
|
last_theme: RefCell::default(),
|
||||||
highlighter: RefCell::new(Highlighter::new(
|
highlighter: RefCell::new(Highlighter::new(
|
||||||
&self.highlighter_settings,
|
&self.highlighter_settings,
|
||||||
)),
|
)),
|
||||||
|
|
@ -612,7 +613,7 @@ where
|
||||||
limits: &layout::Limits,
|
limits: &layout::Limits,
|
||||||
) -> iced_renderer::core::layout::Node {
|
) -> iced_renderer::core::layout::Node {
|
||||||
let mut internal = self.content.0.borrow_mut();
|
let mut internal = self.content.0.borrow_mut();
|
||||||
let state = tree.state.downcast_mut::<State<Highlighter, Theme>>();
|
let state = tree.state.downcast_mut::<State<Highlighter>>();
|
||||||
|
|
||||||
if state.highlighter_format_address != self.highlighter_format as usize
|
if state.highlighter_format_address != self.highlighter_format as usize
|
||||||
{
|
{
|
||||||
|
|
@ -677,7 +678,7 @@ where
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let state = tree.state.downcast_mut::<State<Highlighter, Theme>>();
|
let state = tree.state.downcast_mut::<State<Highlighter>>();
|
||||||
let is_redraw = matches!(
|
let is_redraw = matches!(
|
||||||
event,
|
event,
|
||||||
Event::Window(window::Event::RedrawRequested(_now)),
|
Event::Window(window::Event::RedrawRequested(_now)),
|
||||||
|
|
@ -789,12 +790,11 @@ where
|
||||||
fn apply_binding<
|
fn apply_binding<
|
||||||
H: text::Highlighter,
|
H: text::Highlighter,
|
||||||
R: text::Renderer,
|
R: text::Renderer,
|
||||||
T,
|
|
||||||
Message,
|
Message,
|
||||||
>(
|
>(
|
||||||
binding: Binding<Message>,
|
binding: Binding<Message>,
|
||||||
content: &Content<R>,
|
content: &Content<R>,
|
||||||
state: &mut State<H, T>,
|
state: &mut State<H>,
|
||||||
on_edit: &dyn Fn(Action) -> Message,
|
on_edit: &dyn Fn(Action) -> Message,
|
||||||
clipboard: &mut dyn Clipboard,
|
clipboard: &mut dyn Clipboard,
|
||||||
shell: &mut Shell<'_, Message>,
|
shell: &mut Shell<'_, Message>,
|
||||||
|
|
@ -937,18 +937,21 @@ where
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
|
|
||||||
let mut internal = self.content.0.borrow_mut();
|
let mut internal = self.content.0.borrow_mut();
|
||||||
let state = tree.state.downcast_ref::<State<Highlighter, Theme>>();
|
let state = tree.state.downcast_ref::<State<Highlighter>>();
|
||||||
|
|
||||||
let font = self.font.unwrap_or_else(|| renderer.default_font());
|
let font = self.font.unwrap_or_else(|| renderer.default_font());
|
||||||
|
|
||||||
|
let theme_name = theme.name();
|
||||||
|
|
||||||
if state
|
if state
|
||||||
.last_theme
|
.last_theme
|
||||||
.borrow()
|
.borrow()
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_none_or(|last_theme| last_theme != theme)
|
.is_none_or(|last_theme| last_theme != theme_name)
|
||||||
{
|
{
|
||||||
state.highlighter.borrow_mut().change_line(0);
|
state.highlighter.borrow_mut().change_line(0);
|
||||||
let _ = state.last_theme.borrow_mut().replace(theme.clone());
|
let _ =
|
||||||
|
state.last_theme.borrow_mut().replace(theme_name.to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
internal.editor.highlight(
|
internal.editor.highlight(
|
||||||
|
|
@ -1077,7 +1080,7 @@ where
|
||||||
_renderer: &Renderer,
|
_renderer: &Renderer,
|
||||||
operation: &mut dyn widget::Operation,
|
operation: &mut dyn widget::Operation,
|
||||||
) {
|
) {
|
||||||
let state = tree.state.downcast_mut::<State<Highlighter, Theme>>();
|
let state = tree.state.downcast_mut::<State<Highlighter>>();
|
||||||
|
|
||||||
operation.focusable(self.id.as_ref(), layout.bounds(), state);
|
operation.focusable(self.id.as_ref(), layout.bounds(), state);
|
||||||
}
|
}
|
||||||
|
|
@ -1089,7 +1092,7 @@ impl<'a, Highlighter, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Highlighter: text::Highlighter,
|
Highlighter: text::Highlighter,
|
||||||
Message: 'a,
|
Message: 'a,
|
||||||
Theme: Catalog + 'static,
|
Theme: Catalog + 'a,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
fn from(
|
fn from(
|
||||||
|
|
@ -1245,9 +1248,9 @@ enum Ime {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message> Update<Message> {
|
impl<Message> Update<Message> {
|
||||||
fn from_event<H: Highlighter, T>(
|
fn from_event<H: Highlighter>(
|
||||||
event: &Event,
|
event: &Event,
|
||||||
state: &State<H, T>,
|
state: &State<H>,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
cursor: mouse::Cursor,
|
cursor: mouse::Cursor,
|
||||||
|
|
@ -1404,7 +1407,7 @@ pub struct Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The theme catalog of a [`TextEditor`].
|
/// The theme catalog of a [`TextEditor`].
|
||||||
pub trait Catalog: PartialEq + Clone {
|
pub trait Catalog: theme::Base {
|
||||||
/// The item class of the [`Catalog`].
|
/// The item class of the [`Catalog`].
|
||||||
type Class<'a>;
|
type Class<'a>;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue