Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ where
}

fn state(&self) -> tree::State {
tree::State::new(State::new())
tree::State::new(State::new(self.id.clone()))
}

fn diff(&mut self, tree: &mut Tree) {
Expand All @@ -396,6 +396,10 @@ where
if self.direction.vertical().is_none() {
self.height = self.height.enclose(size.height);
}

if tree.state.downcast_mut::<State>().last_id != self.id {
tree.state = self.state();
}
}

fn size(&self) -> Size<Length> {
Expand Down Expand Up @@ -1489,14 +1493,15 @@ fn notify_viewport<Message>(
true
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
struct State {
offset_y: Offset,
offset_x: Offset,
interaction: Interaction,
keyboard_modifiers: keyboard::Modifiers,
last_notified: Option<Viewport>,
last_scrolled: Option<Instant>,
last_id: Option<widget::Id>,
is_scrollbar_visible: bool,
}

Expand All @@ -1513,20 +1518,6 @@ enum Interaction {
},
}

impl Default for State {
fn default() -> Self {
Self {
offset_y: Offset::Absolute(0.0),
offset_x: Offset::Absolute(0.0),
interaction: Interaction::None,
keyboard_modifiers: keyboard::Modifiers::default(),
last_notified: None,
last_scrolled: None,
is_scrollbar_visible: true,
}
}
}

impl operation::Scrollable for State {
fn snap_to(&mut self, offset: RelativeOffset<Option<f32>>) {
State::snap_to(self, offset);
Expand Down Expand Up @@ -1623,8 +1614,17 @@ impl Viewport {
}

impl State {
fn new() -> Self {
State::default()
fn new(last_id: Option<widget::Id>) -> Self {
Self {
offset_y: Offset::Absolute(0.0),
offset_x: Offset::Absolute(0.0),
interaction: Interaction::None,
keyboard_modifiers: keyboard::Modifiers::default(),
last_notified: None,
last_scrolled: None,
last_id,
is_scrollbar_visible: true,
}
}

fn scroll(&mut self, delta: Vector<f32>, bounds: Rectangle, content_bounds: Rectangle) {
Expand Down
8 changes: 8 additions & 0 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ pub struct State<Highlighter: text::Highlighter> {
preedit: Option<input_method::Preedit>,
last_click: Option<mouse::Click>,
drag_click: Option<mouse::click::Kind>,
last_id: Option<widget::Id>,
partial_scroll: f32,
last_theme: RefCell<Option<String>>,
highlighter: RefCell<Highlighter>,
Expand Down Expand Up @@ -568,6 +569,7 @@ where
preedit: None,
last_click: None,
drag_click: None,
last_id: self.id.clone(),
partial_scroll: 0.0,
last_theme: RefCell::default(),
highlighter: RefCell::new(Highlighter::new(&self.highlighter_settings)),
Expand All @@ -576,6 +578,12 @@ where
})
}

fn diff(&mut self, tree: &mut widget::Tree) {
if tree.state.downcast_mut::<State<Highlighter>>().last_id != self.id {
tree.state = self.state();
}
}

fn size(&self) -> Size<Length> {
Size {
width: self.width,
Expand Down
14 changes: 11 additions & 3 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ where
}

fn state(&self) -> tree::State {
tree::State::new(State::<Renderer::Paragraph>::new())
tree::State::new(State::<Renderer::Paragraph>::new(self.id.clone()))
}

fn diff(&mut self, tree: &mut Tree) {
Expand All @@ -610,6 +610,10 @@ where
if self.on_input.is_none() {
state.is_pasting = None;
}

if state.last_id != self.id {
tree.state = self.state();
}
}

fn size(&self) -> Size<Length> {
Expand Down Expand Up @@ -1371,6 +1375,7 @@ pub struct State<P: text::Paragraph> {
last_click: Option<mouse::Click>,
cursor: Cursor,
keyboard_modifiers: keyboard::Modifiers,
last_id: Option<widget::Id>,
// TODO: Add stateful horizontal scrolling offset
}

Expand Down Expand Up @@ -1399,8 +1404,11 @@ enum Paste {

impl<P: text::Paragraph> State<P> {
/// Creates a new [`State`], representing an unfocused [`TextInput`].
pub fn new() -> Self {
Self::default()
pub fn new(last_id: Option<widget::Id>) -> Self {
Self {
last_id,
..Self::default()
}
}

/// Returns whether the [`TextInput`] is currently focused or not.
Expand Down
Loading