Skip to content

Commit

Permalink
WIP: implement IME primitives for PlainEditor.
Browse files Browse the repository at this point in the history
  • Loading branch information
xorgy committed Oct 11, 2024
1 parent 352df19 commit d400210
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 44 deletions.
12 changes: 11 additions & 1 deletion examples/vello_editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use vello::util::{RenderContext, RenderSurface};
use vello::wgpu;
use vello::{AaConfig, Renderer, RendererOptions, Scene};
use winit::application::ApplicationHandler;
use winit::dpi::LogicalSize;
use winit::dpi::{LogicalPosition, LogicalSize};
use winit::event::*;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::Window;
Expand Down Expand Up @@ -84,6 +84,8 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
self.renderers[surface.dev_id]
.get_or_insert_with(|| create_vello_renderer(&self.context, &surface));

window.set_ime_allowed(true);

// Save the Window and Surface to a state variable
self.state = RenderState::Active(ActiveRenderState { window, surface });

Expand Down Expand Up @@ -114,6 +116,14 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
};

self.editor.handle_event(event.clone());
if let Some(parley::Rect { x0, y0, x1, y1 }) = self.editor.preedit_area() {
println!("{x0} : {y0}");
println!("{x1} : {y1}");
render_state.window.set_ime_cursor_area(
LogicalPosition::new(x0, y0),
LogicalSize::new(x1 - x0, y1 - y0),
);
}
render_state.window.request_redraw();
// render_state
// .window
Expand Down
44 changes: 42 additions & 2 deletions examples/vello_editor/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use peniko::{kurbo::Affine, Color, Fill};
use std::time::Instant;
use vello::Scene;
use winit::{
event::{Modifiers, WindowEvent},
event::{Ime, Modifiers, WindowEvent},
keyboard::{Key, NamedKey},
};

Expand All @@ -15,7 +15,7 @@ use alloc::{sync::Arc, vec};

use core::{default::Default, iter::IntoIterator};

use parley::{FontContext, LayoutContext, PlainEditor, PlainEditorOp};
use parley::{FontContext, LayoutContext, PlainEditor, PlainEditorOp, Rect};

pub const INSET: f32 = 32.0;

Expand All @@ -41,6 +41,17 @@ impl Editor {
self.editor.text()
}

pub fn preedit_area(&self) -> Option<Rect> {
self.editor.preedit_area().map(|r| {
Rect::new(
r.x0 + INSET as f64,
r.y0 + INSET as f64,
r.x1 + INSET as f64,
r.y1 + INSET as f64,
)
})
}

pub fn handle_event(&mut self, event: WindowEvent) {
match event {
WindowEvent::Resized(size) => {
Expand All @@ -55,6 +66,32 @@ impl Editor {
WindowEvent::ModifiersChanged(modifiers) => {
self.modifiers = Some(modifiers);
}
WindowEvent::Ime(ime) => {
self.editor.transact(
&mut self.font_cx,
&mut self.layout_cx,
match ime {
Ime::Enabled => vec![],
Ime::Disabled => vec![
PlainEditorOp::SetCompose("".into(), None),
PlainEditorOp::CommitCompose,
],
// Winit on some platforms delivers an empty Preedit after Commit
// so don't lock into compose when preedit is empty.
Ime::Preedit(text, None) if text.is_empty() => vec![
PlainEditorOp::SetCompose("".into(), None),
PlainEditorOp::CommitCompose,
],
Ime::Preedit(text, sel) => {
vec![PlainEditorOp::SetCompose(text.into(), sel)]
}
Ime::Commit(text) => vec![
PlainEditorOp::SetCompose(text.into(), None),
PlainEditorOp::CommitCompose,
],
},
);
}
WindowEvent::KeyboardInput { event, .. } => {
if !event.state.is_pressed() {
return;
Expand Down Expand Up @@ -267,6 +304,9 @@ impl Editor {
if let Some(cursor) = self.editor.selection_weak_geometry(1.5) {
scene.fill(Fill::NonZero, transform, Color::LIGHT_GRAY, None, &cursor);
};
for rect in self.editor.preedit_underline_geometry(1.5).iter() {
scene.fill(Fill::NonZero, transform, Color::WHITE, None, &rect);
}
for line in self.editor.lines() {
for item in line.items() {
let PositionedLayoutItem::GlyphRun(glyph_run) = item else {
Expand Down
6 changes: 5 additions & 1 deletion parley/src/layout/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,11 @@ impl Selection {
self.maybe_extend(self.focus.previous_word(layout), extend)
}

fn maybe_extend(&self, focus: Cursor, extend: bool) -> Self {
/// Returns a new selection with the focus moved to a cursor.
///
/// If `extend` is `true` then the current anchor will be retained,
/// otherwise the new selection will be collapsed.
pub fn maybe_extend(&self, focus: Cursor, extend: bool) -> Self {
if extend {
Self {
anchor: self.anchor,
Expand Down
Loading

0 comments on commit d400210

Please sign in to comment.