feat: only rehighlight TextEditor when Theme changes
This commit is contained in:
parent
4406681221
commit
e20fee7956
1 changed files with 31 additions and 16 deletions
|
|
@ -170,7 +170,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,
|
Theme: Catalog + 'static + PartialEq,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
/// Sets the placeholder of the [`TextEditor`].
|
/// Sets the placeholder of the [`TextEditor`].
|
||||||
|
|
@ -335,7 +335,7 @@ where
|
||||||
|
|
||||||
fn input_method<'b>(
|
fn input_method<'b>(
|
||||||
&self,
|
&self,
|
||||||
state: &'b State<Highlighter>,
|
state: &'b State<Highlighter, Theme>,
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
) -> InputMethod<&'b str> {
|
) -> InputMethod<&'b str> {
|
||||||
|
|
@ -508,12 +508,13 @@ where
|
||||||
|
|
||||||
/// The state of a [`TextEditor`].
|
/// The state of a [`TextEditor`].
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct State<Highlighter: text::Highlighter> {
|
pub struct State<Highlighter: text::Highlighter, Theme: PartialEq> {
|
||||||
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>>,
|
||||||
highlighter: RefCell<Highlighter>,
|
highlighter: RefCell<Highlighter>,
|
||||||
highlighter_settings: Highlighter::Settings,
|
highlighter_settings: Highlighter::Settings,
|
||||||
highlighter_format_address: usize,
|
highlighter_format_address: usize,
|
||||||
|
|
@ -547,15 +548,17 @@ impl Focus {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Highlighter: text::Highlighter> State<Highlighter> {
|
impl<Highlighter: text::Highlighter, Theme: PartialEq + 'static>
|
||||||
|
State<Highlighter, Theme>
|
||||||
|
{
|
||||||
/// 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> operation::Focusable
|
impl<Highlighter: text::Highlighter, Theme: PartialEq + 'static>
|
||||||
for State<Highlighter>
|
operation::Focusable for State<Highlighter, Theme>
|
||||||
{
|
{
|
||||||
fn is_focused(&self) -> bool {
|
fn is_focused(&self) -> bool {
|
||||||
self.focus.is_some()
|
self.focus.is_some()
|
||||||
|
|
@ -574,11 +577,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,
|
Theme: Catalog + 'static + PartialEq + Clone,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
fn tag(&self) -> widget::tree::Tag {
|
fn tag(&self) -> widget::tree::Tag {
|
||||||
widget::tree::Tag::of::<State<Highlighter>>()
|
widget::tree::Tag::of::<State<Highlighter, Theme>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn state(&self) -> widget::tree::State {
|
fn state(&self) -> widget::tree::State {
|
||||||
|
|
@ -588,6 +591,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(),
|
||||||
highlighter: RefCell::new(Highlighter::new(
|
highlighter: RefCell::new(Highlighter::new(
|
||||||
&self.highlighter_settings,
|
&self.highlighter_settings,
|
||||||
)),
|
)),
|
||||||
|
|
@ -610,7 +614,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>>();
|
let state = tree.state.downcast_mut::<State<Highlighter, Theme>>();
|
||||||
|
|
||||||
if state.highlighter_format_address != self.highlighter_format as usize
|
if state.highlighter_format_address != self.highlighter_format as usize
|
||||||
{
|
{
|
||||||
|
|
@ -675,7 +679,7 @@ where
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let state = tree.state.downcast_mut::<State<Highlighter>>();
|
let state = tree.state.downcast_mut::<State<Highlighter, Theme>>();
|
||||||
let is_redraw = matches!(
|
let is_redraw = matches!(
|
||||||
event,
|
event,
|
||||||
Event::Window(window::Event::RedrawRequested(_now)),
|
Event::Window(window::Event::RedrawRequested(_now)),
|
||||||
|
|
@ -785,13 +789,14 @@ where
|
||||||
},
|
},
|
||||||
Update::Binding(binding) => {
|
Update::Binding(binding) => {
|
||||||
fn apply_binding<
|
fn apply_binding<
|
||||||
|
T: PartialEq + 'static,
|
||||||
H: text::Highlighter,
|
H: text::Highlighter,
|
||||||
R: text::Renderer,
|
R: text::Renderer,
|
||||||
Message,
|
Message,
|
||||||
>(
|
>(
|
||||||
binding: Binding<Message>,
|
binding: Binding<Message>,
|
||||||
content: &Content<R>,
|
content: &Content<R>,
|
||||||
state: &mut State<H>,
|
state: &mut State<H, T>,
|
||||||
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>,
|
||||||
|
|
@ -934,10 +939,20 @@ 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>>();
|
let state = tree.state.downcast_ref::<State<Highlighter, Theme>>();
|
||||||
|
|
||||||
let font = self.font.unwrap_or_else(|| renderer.default_font());
|
let font = self.font.unwrap_or_else(|| renderer.default_font());
|
||||||
|
|
||||||
|
if state
|
||||||
|
.last_theme
|
||||||
|
.borrow()
|
||||||
|
.as_ref()
|
||||||
|
.is_none_or(|last_theme| last_theme != theme)
|
||||||
|
{
|
||||||
|
state.highlighter.borrow_mut().change_line(0);
|
||||||
|
let _ = state.last_theme.borrow_mut().replace(theme.clone());
|
||||||
|
}
|
||||||
|
|
||||||
internal.editor.highlight(
|
internal.editor.highlight(
|
||||||
font,
|
font,
|
||||||
state.highlighter.borrow_mut().deref_mut(),
|
state.highlighter.borrow_mut().deref_mut(),
|
||||||
|
|
@ -1064,7 +1079,7 @@ where
|
||||||
_renderer: &Renderer,
|
_renderer: &Renderer,
|
||||||
operation: &mut dyn widget::Operation,
|
operation: &mut dyn widget::Operation,
|
||||||
) {
|
) {
|
||||||
let state = tree.state.downcast_mut::<State<Highlighter>>();
|
let state = tree.state.downcast_mut::<State<Highlighter, Theme>>();
|
||||||
|
|
||||||
operation.focusable(self.id.as_ref(), layout.bounds(), state);
|
operation.focusable(self.id.as_ref(), layout.bounds(), state);
|
||||||
}
|
}
|
||||||
|
|
@ -1076,7 +1091,7 @@ impl<'a, Highlighter, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Highlighter: text::Highlighter,
|
Highlighter: text::Highlighter,
|
||||||
Message: 'a,
|
Message: 'a,
|
||||||
Theme: Catalog + 'a,
|
Theme: Catalog + 'static + PartialEq + Clone,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
fn from(
|
fn from(
|
||||||
|
|
@ -1232,9 +1247,9 @@ enum Ime {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message> Update<Message> {
|
impl<Message> Update<Message> {
|
||||||
fn from_event<H: Highlighter>(
|
fn from_event<H: Highlighter, T: PartialEq + 'static>(
|
||||||
event: &Event,
|
event: &Event,
|
||||||
state: &State<H>,
|
state: &State<H, T>,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
cursor: mouse::Cursor,
|
cursor: mouse::Cursor,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue