Skip to content

Commit 0495f32

Browse files
committed
Use new theme::Base::name for theme change detection
1 parent 96be0b5 commit 0495f32

2 files changed

Lines changed: 61 additions & 48 deletions

File tree

core/src/theme.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,31 +168,7 @@ impl Theme {
168168

169169
impl fmt::Display for Theme {
170170
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171-
match self {
172-
Self::Light => write!(f, "Light"),
173-
Self::Dark => write!(f, "Dark"),
174-
Self::Dracula => write!(f, "Dracula"),
175-
Self::Nord => write!(f, "Nord"),
176-
Self::SolarizedLight => write!(f, "Solarized Light"),
177-
Self::SolarizedDark => write!(f, "Solarized Dark"),
178-
Self::GruvboxLight => write!(f, "Gruvbox Light"),
179-
Self::GruvboxDark => write!(f, "Gruvbox Dark"),
180-
Self::CatppuccinLatte => write!(f, "Catppuccin Latte"),
181-
Self::CatppuccinFrappe => write!(f, "Catppuccin Frappé"),
182-
Self::CatppuccinMacchiato => write!(f, "Catppuccin Macchiato"),
183-
Self::CatppuccinMocha => write!(f, "Catppuccin Mocha"),
184-
Self::TokyoNight => write!(f, "Tokyo Night"),
185-
Self::TokyoNightStorm => write!(f, "Tokyo Night Storm"),
186-
Self::TokyoNightLight => write!(f, "Tokyo Night Light"),
187-
Self::KanagawaWave => write!(f, "Kanagawa Wave"),
188-
Self::KanagawaDragon => write!(f, "Kanagawa Dragon"),
189-
Self::KanagawaLotus => write!(f, "Kanagawa Lotus"),
190-
Self::Moonfly => write!(f, "Moonfly"),
191-
Self::Nightfly => write!(f, "Nightfly"),
192-
Self::Oxocarbon => write!(f, "Oxocarbon"),
193-
Self::Ferra => write!(f, "Ferra"),
194-
Self::Custom(custom) => custom.fmt(f),
195-
}
171+
f.write_str(self.name())
196172
}
197173
}
198174

@@ -270,6 +246,12 @@ pub trait Base {
270246
/// debugging purposes; like displaying performance
271247
/// metrics or devtools.
272248
fn palette(&self) -> Option<Palette>;
249+
250+
/// Returns the unique name of the theme.
251+
///
252+
/// This name may be used to efficiently detect theme
253+
/// changes in some widgets.
254+
fn name(&self) -> &str;
273255
}
274256

275257
impl Base for Theme {
@@ -313,6 +295,34 @@ impl Base for Theme {
313295
fn palette(&self) -> Option<Palette> {
314296
Some(self.palette())
315297
}
298+
299+
fn name(&self) -> &str {
300+
match self {
301+
Self::Light => "Light",
302+
Self::Dark => "Dark",
303+
Self::Dracula => "Dracula",
304+
Self::Nord => "Nord",
305+
Self::SolarizedLight => "Solarized Light",
306+
Self::SolarizedDark => "Solarized Dark",
307+
Self::GruvboxLight => "Gruvbox Light",
308+
Self::GruvboxDark => "Gruvbox Dark",
309+
Self::CatppuccinLatte => "Catppuccin Latte",
310+
Self::CatppuccinFrappe => "Catppuccin Frappé",
311+
Self::CatppuccinMacchiato => "Catppuccin Macchiato",
312+
Self::CatppuccinMocha => "Catppuccin Mocha",
313+
Self::TokyoNight => "Tokyo Night",
314+
Self::TokyoNightStorm => "Tokyo Night Storm",
315+
Self::TokyoNightLight => "Tokyo Night Light",
316+
Self::KanagawaWave => "Kanagawa Wave",
317+
Self::KanagawaDragon => "Kanagawa Dragon",
318+
Self::KanagawaLotus => "Kanagawa Lotus",
319+
Self::Moonfly => "Moonfly",
320+
Self::Nightfly => "Nightfly",
321+
Self::Oxocarbon => "Oxocarbon",
322+
Self::Ferra => "Ferra",
323+
Self::Custom(custom) => &custom.name,
324+
}
325+
}
316326
}
317327

318328
/// The default [`Style`] of a built-in [`Theme`].

widget/src/text_editor.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use crate::core::renderer;
4242
use crate::core::text::editor::{Cursor, Editor as _};
4343
use crate::core::text::highlighter::{self, Highlighter};
4444
use crate::core::text::{self, LineHeight, Text, Wrapping};
45+
use crate::core::theme;
4546
use crate::core::time::{Duration, Instant};
4647
use crate::core::widget::operation;
4748
use crate::core::widget::{self, Widget};
@@ -148,7 +149,7 @@ where
148149
max_height: f32::INFINITY,
149150
padding: Padding::new(5.0),
150151
wrapping: Wrapping::default(),
151-
class: Theme::default(),
152+
class: <Theme as Catalog>::default(),
152153
key_binding: None,
153154
on_edit: None,
154155
highlighter_settings: (),
@@ -170,7 +171,7 @@ impl<'a, Highlighter, Message, Theme, Renderer>
170171
TextEditor<'a, Highlighter, Message, Theme, Renderer>
171172
where
172173
Highlighter: text::Highlighter,
173-
Theme: Catalog + 'static,
174+
Theme: Catalog,
174175
Renderer: text::Renderer,
175176
{
176177
/// Sets the placeholder of the [`TextEditor`].
@@ -335,7 +336,7 @@ where
335336

336337
fn input_method<'b>(
337338
&self,
338-
state: &'b State<Highlighter, Theme>,
339+
state: &'b State<Highlighter>,
339340
renderer: &Renderer,
340341
layout: Layout<'_>,
341342
) -> InputMethod<&'b str> {
@@ -508,13 +509,13 @@ where
508509

509510
/// The state of a [`TextEditor`].
510511
#[derive(Debug)]
511-
pub struct State<Highlighter: text::Highlighter, Theme> {
512+
pub struct State<Highlighter: text::Highlighter> {
512513
focus: Option<Focus>,
513514
preedit: Option<input_method::Preedit>,
514515
last_click: Option<mouse::Click>,
515516
drag_click: Option<mouse::click::Kind>,
516517
partial_scroll: f32,
517-
last_theme: RefCell<Option<Theme>>,
518+
last_theme: RefCell<Option<String>>,
518519
highlighter: RefCell<Highlighter>,
519520
highlighter_settings: Highlighter::Settings,
520521
highlighter_format_address: usize,
@@ -548,15 +549,15 @@ impl Focus {
548549
}
549550
}
550551

551-
impl<Highlighter: text::Highlighter, Theme> State<Highlighter, Theme> {
552+
impl<Highlighter: text::Highlighter> State<Highlighter> {
552553
/// Returns whether the [`TextEditor`] is currently focused or not.
553554
pub fn is_focused(&self) -> bool {
554555
self.focus.is_some()
555556
}
556557
}
557558

558-
impl<Highlighter: text::Highlighter, Theme> operation::Focusable
559-
for State<Highlighter, Theme>
559+
impl<Highlighter: text::Highlighter> operation::Focusable
560+
for State<Highlighter>
560561
{
561562
fn is_focused(&self) -> bool {
562563
self.focus.is_some()
@@ -575,11 +576,11 @@ impl<Highlighter, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
575576
for TextEditor<'_, Highlighter, Message, Theme, Renderer>
576577
where
577578
Highlighter: text::Highlighter,
578-
Theme: Catalog + 'static,
579+
Theme: Catalog,
579580
Renderer: text::Renderer,
580581
{
581582
fn tag(&self) -> widget::tree::Tag {
582-
widget::tree::Tag::of::<State<Highlighter, Theme>>()
583+
widget::tree::Tag::of::<State<Highlighter>>()
583584
}
584585

585586
fn state(&self) -> widget::tree::State {
@@ -589,7 +590,7 @@ where
589590
last_click: None,
590591
drag_click: None,
591592
partial_scroll: 0.0,
592-
last_theme: RefCell::<Option<Theme>>::default(),
593+
last_theme: RefCell::default(),
593594
highlighter: RefCell::new(Highlighter::new(
594595
&self.highlighter_settings,
595596
)),
@@ -612,7 +613,7 @@ where
612613
limits: &layout::Limits,
613614
) -> iced_renderer::core::layout::Node {
614615
let mut internal = self.content.0.borrow_mut();
615-
let state = tree.state.downcast_mut::<State<Highlighter, Theme>>();
616+
let state = tree.state.downcast_mut::<State<Highlighter>>();
616617

617618
if state.highlighter_format_address != self.highlighter_format as usize
618619
{
@@ -677,7 +678,7 @@ where
677678
return;
678679
};
679680

680-
let state = tree.state.downcast_mut::<State<Highlighter, Theme>>();
681+
let state = tree.state.downcast_mut::<State<Highlighter>>();
681682
let is_redraw = matches!(
682683
event,
683684
Event::Window(window::Event::RedrawRequested(_now)),
@@ -789,12 +790,11 @@ where
789790
fn apply_binding<
790791
H: text::Highlighter,
791792
R: text::Renderer,
792-
T,
793793
Message,
794794
>(
795795
binding: Binding<Message>,
796796
content: &Content<R>,
797-
state: &mut State<H, T>,
797+
state: &mut State<H>,
798798
on_edit: &dyn Fn(Action) -> Message,
799799
clipboard: &mut dyn Clipboard,
800800
shell: &mut Shell<'_, Message>,
@@ -937,18 +937,21 @@ where
937937
let bounds = layout.bounds();
938938

939939
let mut internal = self.content.0.borrow_mut();
940-
let state = tree.state.downcast_ref::<State<Highlighter, Theme>>();
940+
let state = tree.state.downcast_ref::<State<Highlighter>>();
941941

942942
let font = self.font.unwrap_or_else(|| renderer.default_font());
943943

944+
let theme_name = theme.name();
945+
944946
if state
945947
.last_theme
946948
.borrow()
947949
.as_ref()
948-
.is_none_or(|last_theme| last_theme != theme)
950+
.is_none_or(|last_theme| last_theme != theme_name)
949951
{
950952
state.highlighter.borrow_mut().change_line(0);
951-
let _ = state.last_theme.borrow_mut().replace(theme.clone());
953+
let _ =
954+
state.last_theme.borrow_mut().replace(theme_name.to_owned());
952955
}
953956

954957
internal.editor.highlight(
@@ -1077,7 +1080,7 @@ where
10771080
_renderer: &Renderer,
10781081
operation: &mut dyn widget::Operation,
10791082
) {
1080-
let state = tree.state.downcast_mut::<State<Highlighter, Theme>>();
1083+
let state = tree.state.downcast_mut::<State<Highlighter>>();
10811084

10821085
operation.focusable(self.id.as_ref(), layout.bounds(), state);
10831086
}
@@ -1089,7 +1092,7 @@ impl<'a, Highlighter, Message, Theme, Renderer>
10891092
where
10901093
Highlighter: text::Highlighter,
10911094
Message: 'a,
1092-
Theme: Catalog + 'static,
1095+
Theme: Catalog + 'a,
10931096
Renderer: text::Renderer,
10941097
{
10951098
fn from(
@@ -1245,9 +1248,9 @@ enum Ime {
12451248
}
12461249

12471250
impl<Message> Update<Message> {
1248-
fn from_event<H: Highlighter, T>(
1251+
fn from_event<H: Highlighter>(
12491252
event: &Event,
1250-
state: &State<H, T>,
1253+
state: &State<H>,
12511254
bounds: Rectangle,
12521255
padding: Padding,
12531256
cursor: mouse::Cursor,
@@ -1404,7 +1407,7 @@ pub struct Style {
14041407
}
14051408

14061409
/// The theme catalog of a [`TextEditor`].
1407-
pub trait Catalog: PartialEq + Clone {
1410+
pub trait Catalog: theme::Base {
14081411
/// The item class of the [`Catalog`].
14091412
type Class<'a>;
14101413

0 commit comments

Comments
 (0)