[text] show solid text cursor (less repainting)

This commit is contained in:
Emil Ernerfeldt 2020-08-05 14:03:12 +02:00
parent 81d642b1f1
commit efedb09b9f
2 changed files with 10 additions and 5 deletions

View file

@ -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<f32>,
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(),

View file

@ -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);