cursor blink

This commit is contained in:
Dongdong Zhou 2022-05-25 10:41:30 +01:00
parent c8884c41da
commit 17b3edc945
4 changed files with 59 additions and 3 deletions

View File

@ -5,6 +5,7 @@
rc::Rc,
sync::Arc,
thread,
time::Instant,
};
use anyhow::Result;
@ -3121,6 +3122,7 @@ pub struct LapceEditorData {
pub code_lens: bool,
pub scroll_offset: Vec2,
pub new_cursor: Cursor,
pub last_cursor_instant: Rc<RefCell<Instant>>,
pub size: Rc<RefCell<Size>>,
pub window_origin: Rc<RefCell<Point>>,
pub snippet: Option<Vec<(usize, (usize, usize))>>,
@ -3159,6 +3161,7 @@ pub fn new(
} else {
Cursor::new(CursorMode::Insert(Selection::caret(0)), None, None)
},
last_cursor_instant: Rc::new(RefCell::new(Instant::now())),
content,
size: Rc::new(RefCell::new(Size::ZERO)),
compare: None,

View File

@ -1854,6 +1854,16 @@ fn layout(
fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) {
let is_focused = data.focus == self.view_id;
let data = data.editor_view_content(self.view_id);
let is_focused = is_focused
&& (data
.editor
.last_cursor_instant
.borrow()
.elapsed()
.as_millis()
/ 500)
% 2
== 0;
self.paint_content(&data, ctx, is_focused, env);
}
}

View File

@ -1,9 +1,15 @@
use std::{iter::Iterator, ops::Sub, sync::Arc};
use std::{
iter::Iterator,
ops::Sub,
sync::Arc,
time::{Duration, Instant},
};
use druid::{
piet::PietText, BoxConstraints, Command, Data, Env, Event, EventCtx, LayoutCtx,
LifeCycle, LifeCycleCtx, Modifiers, PaintCtx, Point, Rect, RenderContext,
SingleUse, Size, Target, Vec2, Widget, WidgetExt, WidgetId, WidgetPod,
SingleUse, Size, Target, TimerToken, Vec2, Widget, WidgetExt, WidgetId,
WidgetPod,
};
use lapce_core::command::{EditCommand, FocusCommand};
use lapce_data::{
@ -34,6 +40,7 @@ pub struct LapceEditorView {
pub header: WidgetPod<LapceTabData, LapceEditorHeader>,
pub editor: WidgetPod<LapceTabData, LapceEditorContainer>,
pub find: Option<WidgetPod<LapceTabData, Box<dyn Widget<LapceTabData>>>>,
cursor_blink_timer: TimerToken,
}
pub fn editor_tab_child_widget(
@ -67,6 +74,7 @@ pub fn new(
header: WidgetPod::new(header),
editor: WidgetPod::new(editor),
find,
cursor_blink_timer: TimerToken::INVALID,
}
}
@ -531,6 +539,16 @@ fn event(
);
}
}
Event::Timer(id) if self.cursor_blink_timer == *id => {
ctx.set_handled();
if data.focus == self.view_id {
ctx.request_paint();
self.cursor_blink_timer =
ctx.request_timer(Duration::from_millis(500), None);
} else {
self.cursor_blink_timer = TimerToken::INVALID;
}
}
_ => (),
}
@ -679,6 +697,28 @@ fn update(
let old_editor_data = old_data.editor_view_content(self.view_id);
let editor_data = data.editor_view_content(self.view_id);
if data.focus == self.view_id {
let reset = if old_data.focus != self.view_id {
true
} else {
let offset = editor_data.editor.new_cursor.offset();
let old_offset = old_editor_data.editor.new_cursor.offset();
let (line, col) =
editor_data.doc.buffer().offset_to_line_col(offset);
let (old_line, old_col) =
old_editor_data.doc.buffer().offset_to_line_col(old_offset);
line != old_line || col != old_col
};
if reset {
self.cursor_blink_timer =
ctx.request_timer(Duration::from_millis(500), None);
*editor_data.editor.last_cursor_instant.borrow_mut() =
Instant::now();
ctx.request_paint();
}
}
if old_data.config.lapce.modal != data.config.lapce.modal
&& !editor_data.doc.content().is_input()
{

View File

@ -885,7 +885,7 @@ fn update(
ctx: &mut UpdateCtx,
old_data: &LapceTabData,
data: &LapceTabData,
_env: &Env,
env: &Env,
) {
if data.config.id != old_data.config.id {
self.clear_text_layout_cache();
@ -921,6 +921,9 @@ fn update(
}
}
}
if let Some(input) = self.input_widget.as_mut() {
input.update(ctx, data, env);
}
}
fn layout(