From efedb09b9f5861413c9748622595af208a86403d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 5 Aug 2020 14:03:12 +0200 Subject: [PATCH] [text] show solid text cursor (less repainting) --- egui/src/style.rs | 5 +++-- egui/src/widgets/text_edit.rs | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/egui/src/style.rs b/egui/src/style.rs index 414471f6..10dba49c 100644 --- a/egui/src/style.rs +++ b/egui/src/style.rs @@ -50,7 +50,8 @@ pub struct Style { /// e.g. the background of the slider or text edit pub dark_bg_color: Color, - pub cursor_blink_hz: f32, + /// Blink text cursor by this frequency. If None, always show the cursor. + pub cursor_blink_hz: Option, pub text_cursor_width: f32, // TODO: add ability to disable animations! @@ -87,7 +88,7 @@ impl Default for Style { thin_outline: LineStyle::new(0.5, GRAY), background_fill: gray(32, 250), dark_bg_color: gray(0, 140), - cursor_blink_hz: 1.0, + cursor_blink_hz: None, // Some(1.0) text_cursor_width: 2.0, animation_time: 1.0 / 15.0, window: Window::default(), diff --git a/egui/src/widgets/text_edit.rs b/egui/src/widgets/text_edit.rs index 5a0e6888..54cf542f 100644 --- a/egui/src/widgets/text_edit.rs +++ b/egui/src/widgets/text_edit.rs @@ -146,8 +146,13 @@ impl<'t> Widget for TextEdit<'t> { if ui.memory().has_kb_focus(id) { let cursor_blink_hz = ui.style().cursor_blink_hz; - let show_cursor = - (ui.input().time * cursor_blink_hz as f64 * 3.0).floor() as i64 % 3 != 0; + let show_cursor = if let Some(cursor_blink_hz) = cursor_blink_hz { + ui.ctx().request_repaint(); // TODO: only when cursor blinks on or off + (ui.input().time * cursor_blink_hz as f64 * 3.0).floor() as i64 % 3 != 0 + } else { + true + }; + if show_cursor { if let Some(cursor) = state.cursor { let cursor_pos = interact.rect.min + galley.char_start_pos(cursor); @@ -158,7 +163,6 @@ impl<'t> Widget for TextEdit<'t> { )); } } - ui.ctx().request_repaint(); // TODO: only when cursor blinks on or off } let text_color = text_color.unwrap_or_else(|| ui.style().text_color);