Fix scrolling offset for Cursor::Selection

This commit is contained in:
Héctor Ramón Jiménez 2023-09-14 00:41:15 +02:00
parent c829b4b04e
commit ab020383b9
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -1,6 +1,6 @@
use crate::core::text::editor::{self, Action, Cursor, Motion}; use crate::core::text::editor::{self, Action, Cursor, Motion};
use crate::core::text::LineHeight; use crate::core::text::LineHeight;
use crate::core::{Font, Pixels, Point, Rectangle, Size, Vector}; use crate::core::{Font, Pixels, Point, Rectangle, Size};
use crate::text; use crate::text;
use cosmic_text::Edit; use cosmic_text::Edit;
@ -78,29 +78,18 @@ impl editor::Editor for Editor {
match internal.editor.select_opt() { match internal.editor.select_opt() {
Some(selection) => { Some(selection) => {
let line_height = buffer.metrics().line_height;
let scroll_offset = buffer.scroll() as f32 * line_height;
let (start, end) = if cursor < selection { let (start, end) = if cursor < selection {
(cursor, selection) (cursor, selection)
} else { } else {
(selection, cursor) (selection, cursor)
}; };
let visual_lines_before_start: usize = buffer let line_height = buffer.metrics().line_height;
.lines
.iter()
.take(start.line)
.map(|line| {
line.layout_opt()
.as_ref()
.expect("Line layout should be cached")
.len()
})
.sum();
let selected_lines = end.line - start.line + 1; let selected_lines = end.line - start.line + 1;
let visual_lines_offset =
visual_lines_offset(start.line, buffer);
let regions = buffer let regions = buffer
.lines .lines
.iter() .iter()
@ -124,37 +113,24 @@ impl editor::Editor for Editor {
Some(Rectangle { Some(Rectangle {
x, x,
width, width,
y: visual_line as f32 * line_height, y: (visual_line as i32 + visual_lines_offset)
as f32
* line_height,
height: line_height, height: line_height,
}) })
} else { } else {
None None
} }
}) })
.map(|region| {
region
+ Vector::new(
0.0,
visual_lines_before_start as f32 * line_height
+ scroll_offset,
)
})
.collect(); .collect();
Cursor::Selection(regions) Cursor::Selection(regions)
} }
_ => { _ => {
let lines_before_cursor: usize = buffer let line_height = buffer.metrics().line_height;
.lines
.iter() let visual_lines_offset =
.take(cursor.line) visual_lines_offset(cursor.line, buffer);
.map(|line| {
line.layout_opt()
.as_ref()
.expect("Line layout should be cached")
.len()
})
.sum();
let line = buffer let line = buffer
.lines .lines
@ -168,7 +144,7 @@ impl editor::Editor for Editor {
let mut lines = layout.iter().enumerate(); let mut lines = layout.iter().enumerate();
let (subline, offset) = lines let (visual_line, offset) = lines
.find_map(|(i, line)| { .find_map(|(i, line)| {
let start = line let start = line
.glyphs .glyphs
@ -208,14 +184,10 @@ impl editor::Editor for Editor {
layout.last().map(|line| line.w).unwrap_or(0.0), layout.last().map(|line| line.w).unwrap_or(0.0),
)); ));
let line_height = buffer.metrics().line_height;
let scroll_offset = buffer.scroll() as f32 * line_height;
Cursor::Caret(Point::new( Cursor::Caret(Point::new(
offset, offset,
(lines_before_cursor + subline) as f32 * line_height (visual_lines_offset + visual_line as i32) as f32
- scroll_offset, * line_height,
)) ))
} }
} }
@ -511,3 +483,19 @@ fn highlight_line(
} }
}) })
} }
fn visual_lines_offset(line: usize, buffer: &cosmic_text::Buffer) -> i32 {
let visual_lines_before_start: usize = buffer
.lines
.iter()
.take(line)
.map(|line| {
line.layout_opt()
.as_ref()
.expect("Line layout should be cached")
.len()
})
.sum();
visual_lines_before_start as i32 - buffer.scroll()
}