Caret: skip 16 ms blink-fade repaints when the caret does not animate (#8451) (#8680)

This commit is contained in:
Lucas Batista Vargas
2026-12-06 08:15:10 +00:00
committed by GitHub
parent c28b01dc27
commit 9de73dda6d
3 changed files with 63 additions and 3 deletions
+2 -2
View File
@@ -18,7 +18,7 @@ use wordcraft_layout::DocLayout;
use super::cursor::{caret_color_at, preedit_cursor_chars};
use super::story::dims_body;
use super::{WordApp, preview_sel};
use crate::caret_glide::{Bar, Look, LookRequest, blink_fading, look};
use crate::caret_glide::{Bar, Look, LookRequest, animates, blink_fading, look};
/// Where the caret stands on screen in this pass's placement, and what the painters need of it.
struct Placement {
@@ -212,7 +212,7 @@ pub(crate) fn paint_caret(app: &mut WordApp, ui: &mut Ui, pass: &ToolPass, body:
}
if look.moving {
ctx.request_repaint();
} else if shown && blink_fading(since_ms / 1000.0, app.canvas.glide.secs()) {
} else if shown && animates(app, motion) && blink_fading(since_ms / 1000.0, app.canvas.glide.secs()) {
ctx.request_repaint_after(std::time::Duration::from_millis(16));
} else if active {
ctx.request_repaint_after(std::time::Duration::from_millis(530 - (since_ms as u64 % 530)));
+55
View File
@@ -3193,6 +3193,61 @@ fn the_caret_blinks_only_while_the_window_is_active() {
assert!(next_frame(&h) <= blink, "active again, the caret blinks again");
}
/// #8451: when the caret does not animate (software drawing under System, or the caret animation
/// option off), a blink change schedules only the 530 ms blink timer, never 16 ms fade frames.
#[test]
fn non_animating_caret_does_not_request_fade_frames_on_blink() {
use std::time::Duration;
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Default::default());
a.run("text.insert", json!({"text": "Some text"})).unwrap();
let mut h = crate::test_harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(
|ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
a,
);
// kittest zeroes egui's animation time, which drops the framework's motion mode to Off; restore
// the default so software_drawing and ui.caret_animation control the caret's motion here.
h.ctx.all_styles_mut(|style| style.animation_time = egui::Style::default().animation_time);
let ctx = h.ctx.clone();
h.state_mut().canvas.focus_page(&ctx);
for _ in 0..6 {
h.app_frame();
}
assert!(h.state().canvas.focused(&ctx), "the page has the keyboard focus");
let blink = Duration::from_millis(530);
let fade_frame = Duration::from_millis(16);
let next_frame = |h: &H| h.output().viewport_output.get(&egui::ViewportId::ROOT).map_or(Duration::MAX, |viewport| viewport.repaint_delay);
let delay_during_blink_fade = |h: &mut H| {
h.state_mut().canvas.glide.restart(craft::platform::clock::monotonic_ms() - 535.0);
h.app_frame();
next_frame(h)
};
let delay = delay_during_blink_fade(&mut h);
assert!(delay <= fade_frame, "with animations on, a blink fade asks for 16 ms frames: {delay:?}");
h.state_mut().set_software_drawing(true);
h.app_frame();
let delay = delay_during_blink_fade(&mut h);
assert!(delay > fade_frame && delay <= blink, "software drawing under System skips 16 ms fade frames and waits for the 530 ms blink: {delay:?}");
h.state_mut().ui.animations = crate::theme::Animations::On;
h.app_frame();
let delay = delay_during_blink_fade(&mut h);
assert!(delay <= fade_frame, "explicit Animations::On still fades in software: {delay:?}");
h.state_mut().set_software_drawing(false);
h.state_mut().ui.caret_animation = false;
h.app_frame();
let delay = delay_during_blink_fade(&mut h);
assert!(delay > fade_frame && delay <= blink, "caret_animation off skips 16 ms fade frames and waits for the 530 ms blink: {delay:?}");
}
#[test]
fn word_pages_reuses_doc_snapshot_across_unchanged_frames_and_invalidates_on_edit() {
let mut app = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("hello")), Default::default());
+6 -1
View File
@@ -368,11 +368,16 @@ fn cause_key(app: &WordApp) -> u64 {
h.finish()
}
/// Whether the caret glides and fades: only when the option is on and motion is full.
pub(crate) fn animates(app: &WordApp, motion: MotionMode) -> bool {
app.ui.caret_animation && matches!(motion, MotionMode::Full)
}
/// Steps `glide` for this pass: where and how strongly to draw the caret. With the caret
/// animation off, or any motion mode but the full one, the caret jumps and blinks on and off.
pub fn look(glide: &mut CaretGlide, app: &WordApp, request: LookRequest) -> Look {
let LookRequest { content_origin, target, shown, since_ms, on, now, frame_nr, viewport, keys, pointer, motion, view } = request;
if !app.ui.caret_animation || !matches!(motion, MotionMode::Full) {
if !animates(app, motion) {
glide.reset();
return Look { bar: target, opacity: if on { 1.0 } else { 0.0 }, moving: false };
}