Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IME support, extended actions, customize appearance #178

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ modit = { version = "0.1.4", optional = true }
rangemap = "1.4.0"
rustc-hash = { version = "1.1.0", default-features = false }
rustybuzz = { version = "0.12.0", default-features = false, features = ["libm"] }
itertools = "0.11.0"
self_cell = "1.0.1"
swash = { version = "0.1.12", optional = true }
syntect = { version = "5.1.0", optional = true }
Expand Down
12 changes: 10 additions & 2 deletions examples/editor-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn redraw(
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let cursor_color = Color::rgb(0xFF, 0xFF, 0xFF);
let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33);
let selected_text_color = Color::rgb(0xF0, 0xF0, 0xFF);

editor.shape_as_needed(true);
if editor.redraw() {
Expand All @@ -29,6 +30,7 @@ fn redraw(
font_color,
cursor_color,
selection_color,
selected_text_color,
|x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
},
Expand Down Expand Up @@ -114,7 +116,10 @@ fn main() {
// Test delete of EGC
{
let cursor = editor.cursor();
editor.action(Action::Motion(Motion::Previous));
editor.action(Action::Motion {
motion: Motion::Previous,
select: false,
});
editor.action(Action::Delete);
for c in grapheme.chars() {
editor.action(Action::Insert(c));
Expand All @@ -138,7 +143,10 @@ fn main() {
{
let cursor = editor.cursor();
editor.action(Action::Enter);
editor.action(Action::Motion(Motion::Previous));
editor.action(Action::Motion {
motion: Motion::Previous,
select: false,
});
editor.action(Action::Delete);
assert_eq!(cursor, editor.cursor());
}
Expand Down
147 changes: 123 additions & 24 deletions examples/editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ use cosmic_text::{
Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, Motion, SwashCache, SyntaxEditor,
SyntaxSystem,
};
use std::{env, num::NonZeroU32, rc::Rc, slice};
use std::{
env,
num::NonZeroU32,
rc::Rc,
slice,
time::{Duration, Instant},
};
use tiny_skia::{Paint, PixmapMut, Rect, Transform};
use winit::{
dpi::PhysicalPosition,
event::{ElementState, Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent},
dpi::{PhysicalPosition, PhysicalSize},
event::{ElementState, Event, Ime, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::{Key, NamedKey},
window::WindowBuilder,
Expand All @@ -28,6 +34,7 @@ fn main() {
let mut swash_cache = SwashCache::new();

let mut display_scale = window.scale_factor() as f32;
window.set_ime_allowed(true);

let scrollbar_width = 12.0;
let font_sizes = [
Expand Down Expand Up @@ -62,11 +69,16 @@ fn main() {
}

let mut ctrl_pressed = false;
let mut shift_pressed = false;
let mut mouse_x = 0.0;
let mut mouse_y = 0.0;
let mut mouse_left = ElementState::Released;
let mut unapplied_scroll_delta = 0.0;

let mut num_clicks = 0;
let mut last_click_instant = Instant::now();
const DOUBLE_CLICK_TIMEOUT: Duration = Duration::from_millis(300);

event_loop
.run(|event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);
Expand Down Expand Up @@ -130,6 +142,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 Expand Up @@ -168,7 +186,8 @@ fn main() {
surface_buffer.present().unwrap();
}
WindowEvent::ModifiersChanged(modifiers) => {
ctrl_pressed = modifiers.state().control_key()
ctrl_pressed = modifiers.state().control_key();
shift_pressed = modifiers.state().shift_key();
}
WindowEvent::KeyboardInput { event, .. } => {
let KeyEvent {
Expand All @@ -178,35 +197,79 @@ fn main() {
if state.is_pressed() {
match logical_key {
Key::Named(NamedKey::ArrowLeft) => {
editor.action(Action::Motion(Motion::Left))
editor.action(Action::Motion {
motion: if ctrl_pressed {
Motion::PreviousWord
} else {
Motion::Left
},
select: shift_pressed,
})
}
Key::Named(NamedKey::ArrowRight) => {
editor.action(Action::Motion(Motion::Right))
editor.action(Action::Motion {
motion: if ctrl_pressed {
Motion::NextWord
} else {
Motion::Right
},
select: shift_pressed,
})
}
Key::Named(NamedKey::ArrowUp) => {
editor.action(Action::Motion(Motion::Up))
editor.action(Action::Motion {
motion: Motion::Up,
select: shift_pressed,
})
}
Key::Named(NamedKey::ArrowDown) => {
editor.action(Action::Motion(Motion::Down))
}
Key::Named(NamedKey::Home) => {
editor.action(Action::Motion(Motion::Home))
}
Key::Named(NamedKey::End) => {
editor.action(Action::Motion(Motion::End))
}
Key::Named(NamedKey::PageUp) => {
editor.action(Action::Motion(Motion::PageUp))
editor.action(Action::Motion {
motion: Motion::Down,
select: shift_pressed,
})
}
Key::Named(NamedKey::Home) => editor.action(Action::Motion {
motion: if ctrl_pressed {
Motion::DocumentStart
} else {
Motion::Home
},
select: shift_pressed,
}),
Key::Named(NamedKey::End) => editor.action(Action::Motion {
motion: if ctrl_pressed {
Motion::DocumentEnd
} else {
Motion::End
},
select: shift_pressed,
}),
Key::Named(NamedKey::PageUp) => editor.action(Action::Motion {
motion: Motion::PageUp,
select: shift_pressed,
}),
Key::Named(NamedKey::PageDown) => {
editor.action(Action::Motion(Motion::PageDown))
editor.action(Action::Motion {
motion: Motion::PageDown,
select: shift_pressed,
})
}
Key::Named(NamedKey::Escape) => editor.action(Action::Escape),
Key::Named(NamedKey::Enter) => editor.action(Action::Enter),
Key::Named(NamedKey::Backspace) => {
editor.action(Action::Backspace)
editor.action(if ctrl_pressed {
Action::DeleteStartOfWord
} else {
Action::Backspace
})
}
Key::Named(NamedKey::Delete) => {
editor.action(if ctrl_pressed {
Action::DeleteEndOfWord
} else {
Action::Delete
})
}
Key::Named(NamedKey::Delete) => editor.action(Action::Delete),
Key::Named(key) => {
if let Some(text) = key.to_text() {
for c in text.chars() {
Expand Down Expand Up @@ -248,6 +311,9 @@ fn main() {
});
}
}
"a" => {
editor.action(Action::SelectAll);
}
_ => {}
}
} else {
Expand All @@ -261,6 +327,21 @@ fn main() {
window.request_redraw();
}
}
WindowEvent::Ime(ime) => match ime {
Ime::Enabled | Ime::Disabled => {}
Ime::Preedit(preedit, cursor) => {
editor.action(Action::SetPreedit {
preedit,
cursor,
attrs: None,
});
window.request_redraw();
}
Ime::Commit(text) => {
editor.insert_string(&text, None);
window.request_redraw();
}
},
WindowEvent::CursorMoved {
device_id: _,
position,
Expand Down Expand Up @@ -296,10 +377,28 @@ fn main() {
if state == ElementState::Pressed
&& mouse_left == ElementState::Released
{
editor.action(Action::Click {
x: mouse_x as i32,
y: mouse_y as i32,
});
if last_click_instant.elapsed() > DOUBLE_CLICK_TIMEOUT {
num_clicks = 1;
} else {
num_clicks += 1;
}
last_click_instant = Instant::now();
let x = mouse_x as i32;
let y = mouse_y as i32;
match num_clicks {
1 => editor.action(Action::Click {
x,
y,
select: shift_pressed,
}),
2 => editor.action(Action::DoubleClick { x, y }),
3 => editor.action(Action::TripleClick { x, y }),
_ => {}
};
if num_clicks >= 3 {
num_clicks = 0;
}

window.request_redraw();
}
mouse_left = state;
Expand Down
53 changes: 38 additions & 15 deletions examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ fn main() {
editor.with_buffer_mut(|buffer| set_buffer_text(buffer));

let mut ctrl_pressed = false;
let mut shift_pressed = false;
let mut mouse_x = 0.0;
let mut mouse_y = 0.0;
let mut mouse_left = ElementState::Released;
Expand All @@ -130,6 +131,7 @@ fn main() {
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let cursor_color = Color::rgb(0xFF, 0xFF, 0xFF);
let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33);
let selected_text_color = Color::rgb(0xA0, 0xA0, 0xFF);

event_loop
.run(|event, elwt| {
Expand Down Expand Up @@ -185,6 +187,7 @@ fn main() {
font_color,
cursor_color,
selection_color,
selected_text_color,
|x, y, w, h, color| {
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
Expand All @@ -207,7 +210,8 @@ fn main() {
surface_buffer.present().unwrap();
}
WindowEvent::ModifiersChanged(modifiers) => {
ctrl_pressed = modifiers.state().control_key()
ctrl_pressed = modifiers.state().control_key();
shift_pressed = modifiers.state().shift_key();
}
WindowEvent::KeyboardInput { event, .. } => {
let KeyEvent {
Expand All @@ -217,28 +221,46 @@ fn main() {
if state.is_pressed() {
match logical_key {
Key::Named(NamedKey::ArrowLeft) => {
editor.action(Action::Motion(Motion::Left))
editor.action(Action::Motion {
motion: Motion::Left,
select: shift_pressed,
})
}
Key::Named(NamedKey::ArrowRight) => {
editor.action(Action::Motion(Motion::Right))
editor.action(Action::Motion {
motion: Motion::Right,
select: shift_pressed,
})
}
Key::Named(NamedKey::ArrowUp) => {
editor.action(Action::Motion(Motion::Up))
editor.action(Action::Motion {
motion: Motion::Up,
select: shift_pressed,
})
}
Key::Named(NamedKey::ArrowDown) => {
editor.action(Action::Motion(Motion::Down))
}
Key::Named(NamedKey::Home) => {
editor.action(Action::Motion(Motion::Home))
}
Key::Named(NamedKey::End) => {
editor.action(Action::Motion(Motion::End))
}
Key::Named(NamedKey::PageUp) => {
editor.action(Action::Motion(Motion::PageUp))
editor.action(Action::Motion {
motion: Motion::Down,
select: shift_pressed,
})
}
Key::Named(NamedKey::Home) => editor.action(Action::Motion {
motion: Motion::Home,
select: shift_pressed,
}),
Key::Named(NamedKey::End) => editor.action(Action::Motion {
motion: Motion::End,
select: shift_pressed,
}),
Key::Named(NamedKey::PageUp) => editor.action(Action::Motion {
motion: Motion::PageUp,
select: shift_pressed,
}),
Key::Named(NamedKey::PageDown) => {
editor.action(Action::Motion(Motion::PageDown))
editor.action(Action::Motion {
motion: Motion::PageDown,
select: shift_pressed,
})
}
Key::Named(NamedKey::Escape) => editor.action(Action::Escape),
Key::Named(NamedKey::Enter) => editor.action(Action::Enter),
Expand Down Expand Up @@ -303,6 +325,7 @@ fn main() {
editor.action(Action::Click {
x: mouse_x /*- line_x*/ as i32,
y: mouse_y as i32,
select: shift_pressed,
});
window.request_redraw();
}
Expand Down
Loading