Add Edit::cursor_position

This commit is contained in:
Pavel Strakhov 2024-06-08 13:16:28 +01:00 committed by Jeremy Soller
parent 320b034f5b
commit a3a6262e5d
5 changed files with 95 additions and 65 deletions

View file

@ -7,7 +7,7 @@ use cosmic_text::{
use std::{env, fs, num::NonZeroU32, rc::Rc, slice};
use tiny_skia::{Paint, PixmapMut, Rect, Transform};
use winit::{
dpi::PhysicalPosition,
dpi::{PhysicalPosition, PhysicalSize},
event::{ElementState, Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::{Key, NamedKey},
@ -130,6 +130,12 @@ fn main() {
None,
);
});
if let Some((x, y)) = editor.cursor_position() {
window.set_ime_cursor_area(
PhysicalPosition::new(x, y),
PhysicalSize::new(20, 20),
);
}
// Draw scrollbar
{

View file

@ -12,7 +12,7 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::Color;
use crate::{
Action, Attrs, AttrsList, BorrowedWithFontSystem, BufferLine, BufferRef, Change, ChangeItem,
Cursor, Edit, FontSystem, Selection, Shaping,
Cursor, Edit, FontSystem, LayoutRun, Selection, Shaping,
};
/// A wrapper of [`Buffer`] for easy editing
@ -27,6 +27,72 @@ pub struct Editor<'buffer> {
change: Option<Change>,
}
fn cursor_glyph_opt(cursor: &Cursor, run: &LayoutRun) -> Option<(usize, f32)> {
if cursor.line == run.line_i {
for (glyph_i, glyph) in run.glyphs.iter().enumerate() {
if cursor.index == glyph.start {
return Some((glyph_i, 0.0));
} else if cursor.index > glyph.start && cursor.index < glyph.end {
// Guess x offset based on characters
let mut before = 0;
let mut total = 0;
let cluster = &run.text[glyph.start..glyph.end];
for (i, _) in cluster.grapheme_indices(true) {
if glyph.start + i < cursor.index {
before += 1;
}
total += 1;
}
let offset = glyph.w * (before as f32) / (total as f32);
return Some((glyph_i, offset));
}
}
match run.glyphs.last() {
Some(glyph) => {
if cursor.index == glyph.end {
return Some((run.glyphs.len(), 0.0));
}
}
None => {
return Some((0, 0.0));
}
}
}
None
}
fn cursor_position(cursor: &Cursor, run: &LayoutRun) -> Option<(i32, i32)> {
let (cursor_glyph, cursor_glyph_offset) = cursor_glyph_opt(cursor, run)?;
let x = match run.glyphs.get(cursor_glyph) {
Some(glyph) => {
// Start of detected glyph
if glyph.level.is_rtl() {
(glyph.x + glyph.w - cursor_glyph_offset) as i32
} else {
(glyph.x + cursor_glyph_offset) as i32
}
}
None => match run.glyphs.last() {
Some(glyph) => {
// End of last glyph
if glyph.level.is_rtl() {
glyph.x as i32
} else {
(glyph.x + glyph.w) as i32
}
}
None => {
// Start of empty line
0
}
},
};
Some((x, run.line_top as i32))
}
impl<'buffer> Editor<'buffer> {
/// Create a new [`Editor`] with the provided [`Buffer`]
pub fn new(buffer: impl Into<BufferRef<'buffer>>) -> Self {
@ -63,42 +129,6 @@ impl<'buffer> Editor<'buffer> {
let line_top = run.line_top;
let line_height = run.line_height;
let cursor_glyph_opt = |cursor: &Cursor| -> Option<(usize, f32)> {
if cursor.line == line_i {
for (glyph_i, glyph) in run.glyphs.iter().enumerate() {
if cursor.index == glyph.start {
return Some((glyph_i, 0.0));
} else if cursor.index > glyph.start && cursor.index < glyph.end {
// Guess x offset based on characters
let mut before = 0;
let mut total = 0;
let cluster = &run.text[glyph.start..glyph.end];
for (i, _) in cluster.grapheme_indices(true) {
if glyph.start + i < cursor.index {
before += 1;
}
total += 1;
}
let offset = glyph.w * (before as f32) / (total as f32);
return Some((glyph_i, offset));
}
}
match run.glyphs.last() {
Some(glyph) => {
if cursor.index == glyph.end {
return Some((run.glyphs.len(), 0.0));
}
}
None => {
return Some((0, 0.0));
}
}
}
None
};
// Highlight selection
if let Some((start, end)) = selection_bounds {
if line_i >= start.line && line_i <= end.line {
@ -161,33 +191,8 @@ impl<'buffer> Editor<'buffer> {
}
// Draw cursor
if let Some((cursor_glyph, cursor_glyph_offset)) = cursor_glyph_opt(&self.cursor) {
let x = match run.glyphs.get(cursor_glyph) {
Some(glyph) => {
// Start of detected glyph
if glyph.level.is_rtl() {
(glyph.x + glyph.w - cursor_glyph_offset) as i32
} else {
(glyph.x + cursor_glyph_offset) as i32
}
}
None => match run.glyphs.last() {
Some(glyph) => {
// End of last glyph
if glyph.level.is_rtl() {
glyph.x as i32
} else {
(glyph.x + glyph.w) as i32
}
}
None => {
// Start of empty line
0
}
},
};
f(x, line_top as i32, 1, line_height as u32, cursor_color);
if let Some((x, y)) = cursor_position(&self.cursor, &run) {
f(x, y, 1, line_height as u32, cursor_color);
}
for glyph in run.glyphs.iter() {
@ -883,6 +888,14 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
*/
}
}
fn cursor_position(&self) -> Option<(i32, i32)> {
self.with_buffer(|buffer| {
buffer
.layout_runs()
.find_map(|run| cursor_position(&self.cursor, &run))
})
}
}
impl<'font_system, 'buffer> BorrowedWithFontSystem<'font_system, Editor<'buffer>> {

View file

@ -319,6 +319,9 @@ pub trait Edit<'buffer> {
/// Perform an [Action] on the editor
fn action(&mut self, font_system: &mut FontSystem, action: Action);
/// Get X and Y position of the top left corner of the cursor
fn cursor_position(&self) -> Option<(i32, i32)>;
}
impl<'font_system, 'buffer, E: Edit<'buffer>> BorrowedWithFontSystem<'font_system, E> {

View file

@ -413,6 +413,10 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for SyntaxEditor<'syntax_system, 'bu
fn action(&mut self, font_system: &mut FontSystem, action: Action) {
self.editor.action(font_system, action);
}
fn cursor_position(&self) -> Option<(i32, i32)> {
self.editor.cursor_position()
}
}
impl<'font_system, 'syntax_system, 'buffer>

View file

@ -1157,6 +1157,10 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer
editor.action(font_system, action);
});
}
fn cursor_position(&self) -> Option<(i32, i32)> {
self.editor.cursor_position()
}
}
impl<'font_system, 'syntax_system, 'buffer>