Skip to content

Visual Studio Code, rebuilt in Rust

Rust

fix(input): time the caret blink on the executor's clock

rokartur149129740e13
diff --git a/native/crates/app/src/workbench/input_box_view.rs b/native/crates/app/src/workbench/input_box_view.rs
index 37d391a420b..0301bf6941a 100644
--- a/native/crates/app/src/workbench/input_box_view.rs
+++ b/native/crates/app/src/workbench/input_box_view.rs
@@ -98,6 +98,7 @@ pub struct InputView {
/// The IME's marked text, a UTF-16 range of the value.
pub marked: Option<Range<usize>>,
selecting: bool,
+ /// On the executor's clock, which the blink timer runs on: a test's clock in tests.
caret_moved: Instant,
blink: Option<Task<()>>,
/// The shaped text, its window position, and the scroll that keeps the caret visible.
@@ -350,7 +351,7 @@ impl Workbench {
/// Runs `edit` on the element's value and restarts the caret blink.
pub(super) fn edit_input_box(&mut self, id: InputId, window: &mut Window, cx: &mut Context<Self>, edit: impl FnOnce(&mut InputBox)) -> Outcome {
- self.input_view_mut(id).caret_moved = Instant::now();
+ self.input_view_mut(id).caret_moved = cx.background_executor().now();
match id {
InputId::QuickInput => {
let before = self.quick_inputs.current().map(|input| input.input_box.value.clone());
@@ -507,7 +508,7 @@ impl Workbench {
/// The caret blinks while the input has focus; a key restarts the blink.
pub(super) fn blink_caret(&mut self, id: InputId, window: &mut Window, cx: &mut Context<Self>) {
- self.input_view_mut(id).caret_moved = Instant::now();
+ self.input_view_mut(id).caret_moved = cx.background_executor().now();
self.input_view_mut(id).blink = Some(cx.spawn_in(window, async move |workbench, cx| {
loop {
cx.background_executor().timer(Duration::from_millis(500)).await;
@@ -711,7 +712,7 @@ impl Workbench {
let placeholder_color = self.color("input.placeholderForeground");
let selection_color = self.color(if look.focused { "selection.background" } else { "editor.inactiveSelectionBackground" });
let selection_color = if self.theme.color("selection.background").is_some() { selection_color } else { self.color("editor.selectionBackground") };
- let caret_on = look.focused && self.input_caret_on(id, view.caret_moved.elapsed());
+ let caret_on = look.focused && self.input_caret_on(id, cx.background_executor().now().saturating_duration_since(view.caret_moved));
// A Monaco cursor is `lineHeight` tall; these inputs' elements are one editor line.
let full_height_caret = matches!(id, InputId::SettingsSearch | InputId::DebugConsole);
let layout = view.text.clone();