Avoid reblinking cursor when clicking a focused TextInput

This commit is contained in:
Héctor Ramón Jiménez 2023-01-12 03:21:15 +01:00
parent a980024bbf
commit 178bd2d83c
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -429,11 +429,10 @@ where
let is_clicked = layout.bounds().contains(cursor_position); let is_clicked = layout.bounds().contains(cursor_position);
state.is_focused = if is_clicked { state.is_focused = if is_clicked {
let now = Instant::now(); state.is_focused.or_else(|| {
let now = Instant::now();
Some(Focus { Some(Focus { at: now, now })
at: now,
last_draw: now,
}) })
} else { } else {
None None
@ -785,7 +784,7 @@ where
let state = state(); let state = state();
if let Some(focus) = &mut state.is_focused { if let Some(focus) = &mut state.is_focused {
focus.last_draw = now; focus.now = now;
let millis_until_redraw = CURSOR_BLINK_INTERVAL_MILLIS let millis_until_redraw = CURSOR_BLINK_INTERVAL_MILLIS
- (now - focus.at).as_millis() - (now - focus.at).as_millis()
@ -864,8 +863,7 @@ pub fn draw<Renderer>(
font.clone(), font.clone(),
); );
let is_cursor_visible = ((focus.last_draw - focus.at) let is_cursor_visible = ((focus.now - focus.at).as_millis()
.as_millis()
/ CURSOR_BLINK_INTERVAL_MILLIS) / CURSOR_BLINK_INTERVAL_MILLIS)
% 2 % 2
== 0; == 0;
@ -1010,7 +1008,7 @@ pub struct State {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
struct Focus { struct Focus {
at: Instant, at: Instant,
last_draw: Instant, now: Instant,
} }
impl State { impl State {
@ -1045,10 +1043,7 @@ impl State {
pub fn focus(&mut self) { pub fn focus(&mut self) {
let now = Instant::now(); let now = Instant::now();
self.is_focused = Some(Focus { self.is_focused = Some(Focus { at: now, now: now });
at: now,
last_draw: now,
});
self.move_cursor_to_end(); self.move_cursor_to_end();
} }