Skip to content

Commit

Permalink
vello_editor: Stop blinking and hide cursor when window not focused.
Browse files Browse the repository at this point in the history
  • Loading branch information
xorgy committed Nov 11, 2024
1 parent d676a86 commit 3fd415a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 13 additions & 0 deletions examples/vello_editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ impl ApplicationHandler<accesskit_winit::Event> for SimpleVelloApp<'_> {
_ => {}
}
}

fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
Expand Down Expand Up @@ -239,6 +240,18 @@ impl ApplicationHandler<accesskit_winit::Event> for SimpleVelloApp<'_> {
render_state.window.request_redraw();
}

// Don't blink the cursor when we're not focused.
WindowEvent::Focused(false) => {
self.editor.disable_blink();
self.editor.cursor_blink();
self.last_drawn_generation = text::Generation::default();
if let RenderState::Active(state) = &self.state {
state.window.request_redraw();
}
}
// Make sure cursor is visible when we regain focus.
WindowEvent::Focused(true) => self.editor.cursor_reset(),

// This is where all the rendering happens
WindowEvent::RedrawRequested => {
// Send an accessibility update if accessibility is active.
Expand Down
10 changes: 7 additions & 3 deletions examples/vello_editor/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ impl Editor {
self.cursor_visible = true;
}

pub fn disable_blink(&mut self) {
self.start_time = None;
}

pub fn next_blink_time(&self) -> Option<Instant> {
self.start_time.map(|start_time| {
let phase = Instant::now().duration_since(start_time);
Expand All @@ -64,10 +68,10 @@ impl Editor {
}

pub fn cursor_blink(&mut self) {
if let Some(start_time) = self.start_time {
self.cursor_visible = self.start_time.map_or(false, |start_time| {
let elapsed = Instant::now().duration_since(start_time);
self.cursor_visible = (elapsed.as_millis() / self.blink_period.as_millis()) % 2 == 0;
}
(elapsed.as_millis() / self.blink_period.as_millis()) % 2 == 0
});
}

pub fn handle_event(&mut self, event: WindowEvent) {
Expand Down

0 comments on commit 3fd415a

Please sign in to comment.