Fix click/drag offset when using line numbers

This commit is contained in:
Jeremy Soller 2023-11-30 14:44:14 -07:00
parent 664e6abd64
commit 8e7dbaa51d
No known key found for this signature in database
GPG key ID: DCFCA852D3906975

View file

@ -357,9 +357,7 @@ where
(gutter, gutter_foreground) (gutter, gutter_foreground)
}; };
let mut line_number_width = 0.0; let editor_offset_x = if self.line_numbers {
if self.line_numbers {
// Ensure fill with gutter color // Ensure fill with gutter color
//TODO: optimize to only fill gutter //TODO: optimize to only fill gutter
draw_rect(buffer, image_w, image_h, 0, 0, image_w, image_h, gutter); draw_rect(buffer, image_w, image_h, 0, 0, image_w, image_h, gutter);
@ -376,6 +374,7 @@ where
// Draw line numbers // Draw line numbers
//TODO: move to cosmic-text? //TODO: move to cosmic-text?
let mut line_number_width = 0.0;
{ {
let mut line_number_cache = LINE_NUMBER_CACHE.lock().unwrap(); let mut line_number_cache = LINE_NUMBER_CACHE.lock().unwrap();
for run in editor.buffer().layout_runs() { for run in editor.buffer().layout_runs() {
@ -428,10 +427,15 @@ where
} }
} }
} }
}
(line_number_width + 8.0).ceil() as i32
} else {
0
};
state.editor_offset_x.set(editor_offset_x);
// Draw editor // Draw editor
let editor_offset_x = (line_number_width + 8.0).ceil() as i32;
editor.draw( editor.draw(
&mut font_system, &mut font_system,
&mut swash_cache, &mut swash_cache,
@ -628,7 +632,7 @@ where
if let Button::Left = button { if let Button::Left = button {
let x_logical = p.x - self.padding.left; let x_logical = p.x - self.padding.left;
let y_logical = p.y - self.padding.top; let y_logical = p.y - self.padding.top;
let x = x_logical * scale_factor; let x = x_logical * scale_factor - state.editor_offset_x.get() as f32;
let y = y_logical * scale_factor; let y = y_logical * scale_factor;
if x >= 0.0 && x < buffer_size.0 && y >= 0.0 && y < buffer_size.1 { if x >= 0.0 && x < buffer_size.0 && y >= 0.0 && y < buffer_size.1 {
editor.action(Action::Click { editor.action(Action::Click {
@ -676,8 +680,10 @@ where
Event::Mouse(MouseEvent::CursorMoved { .. }) => { Event::Mouse(MouseEvent::CursorMoved { .. }) => {
if let Some(dragging) = &state.dragging { if let Some(dragging) = &state.dragging {
if let Some(p) = cursor_position.position() { if let Some(p) = cursor_position.position() {
let x = ((p.x - layout.bounds().x) - self.padding.left) * scale_factor; let x_logical = (p.x - layout.bounds().x) - self.padding.left;
let y = ((p.y - layout.bounds().y) - self.padding.top) * scale_factor; let y_logical = (p.y - layout.bounds().y) - self.padding.top;
let x = x_logical * scale_factor - state.editor_offset_x.get() as f32;
let y = y_logical * scale_factor;
match dragging { match dragging {
Dragging::Buffer => { Dragging::Buffer => {
editor.action(Action::Drag { editor.action(Action::Drag {
@ -765,6 +771,7 @@ enum Dragging {
pub struct State { pub struct State {
modifiers: Modifiers, modifiers: Modifiers,
dragging: Option<Dragging>, dragging: Option<Dragging>,
editor_offset_x: Cell<i32>,
scale_factor: Cell<f32>, scale_factor: Cell<f32>,
scroll_pixels: f32, scroll_pixels: f32,
scrollbar_rect: Cell<Rectangle<f32>>, scrollbar_rect: Cell<Rectangle<f32>>,
@ -777,6 +784,7 @@ impl State {
State { State {
modifiers: Modifiers::empty(), modifiers: Modifiers::empty(),
dragging: None, dragging: None,
editor_offset_x: Cell::new(0),
scale_factor: Cell::new(1.0), scale_factor: Cell::new(1.0),
scroll_pixels: 0.0, scroll_pixels: 0.0,
scrollbar_rect: Cell::new(Rectangle::default()), scrollbar_rect: Cell::new(Rectangle::default()),