mirror of https://github.com/lapce/lapce.git
hover mouse move improvement
This commit is contained in:
parent
3af0028aff
commit
14c1fe0f8a
|
@ -910,14 +910,18 @@ pub fn offset_of_point(
|
|||
point: Point,
|
||||
font_size: usize,
|
||||
config: &Config,
|
||||
) -> usize {
|
||||
) -> (usize, bool) {
|
||||
let last_line = self.buffer.last_line();
|
||||
let line = ((point.y / config.editor.line_height as f64).floor() as usize)
|
||||
.min(last_line);
|
||||
let text_layout = self.get_text_layout(text, line, font_size, config);
|
||||
let col = text_layout.hit_test_point(Point::new(point.x, 0.0)).idx;
|
||||
let hit_point = text_layout.hit_test_point(Point::new(point.x, 0.0));
|
||||
let col = hit_point.idx;
|
||||
let max_col = self.buffer.line_end_col(line, mode != Mode::Normal);
|
||||
self.buffer.offset_of_line_col(line, col.min(max_col))
|
||||
(
|
||||
self.buffer.offset_of_line_col(line, col.min(max_col)),
|
||||
hit_point.is_inside,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn point_of_offset(
|
||||
|
|
|
@ -456,6 +456,33 @@ fn update_completion(&mut self, ctx: &mut EventCtx) {
|
|||
}
|
||||
}
|
||||
|
||||
/// return true if there's existing hover and it's not changed
|
||||
pub fn update_hover_new(
|
||||
&mut self,
|
||||
_ctx: &mut EventCtx,
|
||||
offset: usize,
|
||||
is_inside: bool,
|
||||
) -> bool {
|
||||
let hover = Arc::make_mut(&mut self.hover);
|
||||
|
||||
if hover.status != HoverStatus::Inactive {
|
||||
if !is_inside {
|
||||
hover.cancel();
|
||||
return false;
|
||||
}
|
||||
|
||||
let start_offset = self.doc.buffer().prev_code_boundary(offset);
|
||||
if self.doc.id() == hover.buffer_id && start_offset == hover.offset {
|
||||
return true;
|
||||
}
|
||||
|
||||
hover.cancel();
|
||||
return false;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn update_hover(&mut self, ctx: &mut EventCtx, offset: usize) {
|
||||
if !self.doc.loaded() {
|
||||
return;
|
||||
|
@ -938,7 +965,7 @@ pub fn single_click(
|
|||
mouse_event: &MouseEvent,
|
||||
config: &Config,
|
||||
) {
|
||||
let new_offset = self.doc.offset_of_point(
|
||||
let (new_offset, _) = self.doc.offset_of_point(
|
||||
ctx.text(),
|
||||
self.get_mode(),
|
||||
mouse_event.pos,
|
||||
|
@ -983,7 +1010,7 @@ pub fn double_click(
|
|||
config: &Config,
|
||||
) {
|
||||
ctx.set_active(true);
|
||||
let mouse_offset = self.doc.offset_of_point(
|
||||
let (mouse_offset, _) = self.doc.offset_of_point(
|
||||
ctx.text(),
|
||||
self.get_mode(),
|
||||
mouse_event.pos,
|
||||
|
@ -1007,7 +1034,7 @@ pub fn triple_click(
|
|||
config: &Config,
|
||||
) {
|
||||
ctx.set_active(true);
|
||||
let mouse_offset = self.doc.offset_of_point(
|
||||
let (mouse_offset, _) = self.doc.offset_of_point(
|
||||
ctx.text(),
|
||||
self.get_mode(),
|
||||
mouse_event.pos,
|
||||
|
|
|
@ -82,6 +82,44 @@ pub fn new(view_id: WidgetId) -> Self {
|
|||
}
|
||||
}
|
||||
|
||||
fn mouse_move(
|
||||
&mut self,
|
||||
ctx: &mut EventCtx,
|
||||
mouse_event: &MouseEvent,
|
||||
editor_data: &mut LapceEditorBufferData,
|
||||
config: &Config,
|
||||
) {
|
||||
self.mouse_pos = mouse_event.pos;
|
||||
self.mouse_hover_timer = TimerToken::INVALID;
|
||||
|
||||
if ctx.is_active() {
|
||||
let (new_offset, _) = editor_data.doc.offset_of_point(
|
||||
ctx.text(),
|
||||
editor_data.get_mode(),
|
||||
mouse_event.pos,
|
||||
config.editor.font_size,
|
||||
config,
|
||||
);
|
||||
let editor = Arc::make_mut(&mut editor_data.editor);
|
||||
editor
|
||||
.new_cursor
|
||||
.set_offset(new_offset, true, mouse_event.mods.alt());
|
||||
return;
|
||||
}
|
||||
|
||||
let (offset, is_inside) = editor_data.doc.offset_of_point(
|
||||
ctx.text(),
|
||||
Mode::Insert,
|
||||
mouse_event.pos,
|
||||
config.editor.font_size,
|
||||
config,
|
||||
);
|
||||
if !editor_data.update_hover_new(ctx, offset, is_inside) && is_inside {
|
||||
self.mouse_hover_timer =
|
||||
ctx.request_timer(Duration::from_millis(config.editor.hover_delay));
|
||||
}
|
||||
}
|
||||
|
||||
fn mouse_down(
|
||||
&mut self,
|
||||
ctx: &mut EventCtx,
|
||||
|
@ -1595,32 +1633,12 @@ fn event(
|
|||
match event {
|
||||
Event::MouseMove(mouse_event) => {
|
||||
ctx.set_cursor(&druid::Cursor::IBeam);
|
||||
if mouse_event.pos != self.mouse_pos {
|
||||
self.mouse_pos = mouse_event.pos;
|
||||
// Get a new hover timer, overwriting the old one that will just be ignored
|
||||
// when it is received
|
||||
self.mouse_hover_timer = ctx.request_timer(
|
||||
Duration::from_millis(data.config.editor.hover_delay),
|
||||
);
|
||||
if ctx.is_active() {
|
||||
let editor_data = data.editor_view_content(self.view_id);
|
||||
let new_offset = editor_data.doc.offset_of_point(
|
||||
ctx.text(),
|
||||
editor_data.get_mode(),
|
||||
mouse_event.pos,
|
||||
data.config.editor.font_size,
|
||||
&data.config,
|
||||
);
|
||||
let editor =
|
||||
data.main_split.editors.get_mut(&self.view_id).unwrap();
|
||||
let editor = Arc::make_mut(editor);
|
||||
editor.new_cursor.set_offset(
|
||||
new_offset,
|
||||
true,
|
||||
mouse_event.mods.alt(),
|
||||
);
|
||||
}
|
||||
}
|
||||
let doc = data.main_split.editor_doc(self.view_id);
|
||||
let editor =
|
||||
data.main_split.editors.get(&self.view_id).unwrap().clone();
|
||||
let mut editor_data = data.editor_view_content(self.view_id);
|
||||
self.mouse_move(ctx, mouse_event, &mut editor_data, &data.config);
|
||||
data.update_from_editor_buffer_data(editor_data, &editor, &doc);
|
||||
}
|
||||
Event::MouseUp(_mouse_event) => {
|
||||
ctx.set_active(false);
|
||||
|
@ -1639,7 +1657,7 @@ fn event(
|
|||
data.main_split.editors.get(&self.view_id).unwrap().clone();
|
||||
let mut editor_data = data.editor_view_content(self.view_id);
|
||||
let doc = editor_data.doc.clone();
|
||||
let offset = doc.offset_of_point(
|
||||
let (offset, _) = doc.offset_of_point(
|
||||
ctx.text(),
|
||||
editor.new_cursor.get_mode(),
|
||||
self.mouse_pos,
|
||||
|
|
|
@ -204,11 +204,14 @@ fn new() -> Self {
|
|||
impl Widget<LapceTabData> for Hover {
|
||||
fn event(
|
||||
&mut self,
|
||||
_ctx: &mut EventCtx,
|
||||
_event: &Event,
|
||||
ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
_data: &mut LapceTabData,
|
||||
_env: &Env,
|
||||
) {
|
||||
if let Event::MouseMove(_) = event {
|
||||
ctx.set_handled();
|
||||
}
|
||||
}
|
||||
|
||||
fn lifecycle(
|
||||
|
|
Loading…
Reference in New Issue