Skip to content

Commit

Permalink
Add Edit::cursor_position
Browse files Browse the repository at this point in the history
  • Loading branch information
Riateche authored and jackpot51 committed Jun 10, 2024
1 parent 320b034 commit a3a6262
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 65 deletions.
8 changes: 7 additions & 1 deletion examples/editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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
{
Expand Down
141 changes: 77 additions & 64 deletions src/edit/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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>> {
Expand Down
3 changes: 3 additions & 0 deletions src/edit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
4 changes: 4 additions & 0 deletions src/edit/syntect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions src/edit/vi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit a3a6262

Please sign in to comment.