Convert editor example to winit
Swap color channels Remove unused code in rich-text-winit example Port editor example to winit WIP Implement scroll logic + add shape_as_needed Handle text input using named keys request redraw on click cargo fmt Implement dragging Refactor winit event handlers to avoid duplication Re-enable text size keyboard shortcuts Remove unused imports Fix updating scale factor Fix unused variable warnings Remove logging Remove commented code Delete rich-text-winit example Rename editor-winit example to editor
This commit is contained in:
parent
22e61965aa
commit
43d3620e44
3 changed files with 341 additions and 274 deletions
|
|
@ -1,272 +0,0 @@
|
||||||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
||||||
|
|
||||||
use cosmic_text::{
|
|
||||||
Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, Motion, SwashCache, SyntaxEditor,
|
|
||||||
SyntaxSystem,
|
|
||||||
};
|
|
||||||
use orbclient::{EventOption, Renderer, Window, WindowFlag};
|
|
||||||
use std::{
|
|
||||||
env, thread,
|
|
||||||
time::{Duration, Instant},
|
|
||||||
};
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
env_logger::init();
|
|
||||||
|
|
||||||
let path = if let Some(arg) = env::args().nth(1) {
|
|
||||||
arg
|
|
||||||
} else {
|
|
||||||
String::new()
|
|
||||||
};
|
|
||||||
|
|
||||||
let display_scale = match orbclient::get_display_size() {
|
|
||||||
Ok((w, h)) => {
|
|
||||||
log::info!("Display size: {}, {}", w, h);
|
|
||||||
(h / 1600) as f32 + 1.0
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
log::warn!("Failed to get display size: {}", err);
|
|
||||||
1.0
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut window = Window::new_flags(
|
|
||||||
-1,
|
|
||||||
-1,
|
|
||||||
1024 * display_scale as u32,
|
|
||||||
768 * display_scale as u32,
|
|
||||||
&format!("COSMIC Text - {path}"),
|
|
||||||
&[WindowFlag::Resizable],
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut font_system = FontSystem::new();
|
|
||||||
|
|
||||||
let syntax_system = SyntaxSystem::new();
|
|
||||||
|
|
||||||
let font_sizes = [
|
|
||||||
Metrics::new(10.0, 14.0).scale(display_scale), // Caption
|
|
||||||
Metrics::new(14.0, 20.0).scale(display_scale), // Body
|
|
||||||
Metrics::new(20.0, 28.0).scale(display_scale), // Title 4
|
|
||||||
Metrics::new(24.0, 32.0).scale(display_scale), // Title 3
|
|
||||||
Metrics::new(28.0, 36.0).scale(display_scale), // Title 2
|
|
||||||
Metrics::new(32.0, 44.0).scale(display_scale), // Title 1
|
|
||||||
];
|
|
||||||
let font_size_default = 1; // Body
|
|
||||||
let mut font_size_i = font_size_default;
|
|
||||||
|
|
||||||
let line_x = 8.0 * display_scale;
|
|
||||||
|
|
||||||
let mut editor = SyntaxEditor::new(
|
|
||||||
Buffer::new(&mut font_system, font_sizes[font_size_i]),
|
|
||||||
&syntax_system,
|
|
||||||
"base16-eighties.dark",
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
#[cfg(feature = "vi")]
|
|
||||||
let mut editor = cosmic_text::ViEditor::new(editor);
|
|
||||||
|
|
||||||
let mut editor = editor.borrow_with(&mut font_system);
|
|
||||||
|
|
||||||
editor.with_buffer_mut(|buffer| {
|
|
||||||
buffer.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32)
|
|
||||||
});
|
|
||||||
|
|
||||||
let attrs = Attrs::new().family(Family::Monospace);
|
|
||||||
match editor.load_text(&path, attrs) {
|
|
||||||
Ok(()) => (),
|
|
||||||
Err(err) => {
|
|
||||||
log::error!("failed to load {:?}: {}", path, err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut swash_cache = SwashCache::new();
|
|
||||||
|
|
||||||
let mut ctrl_pressed = false;
|
|
||||||
let mut mouse_x = -1;
|
|
||||||
let mut mouse_y = -1;
|
|
||||||
let mut mouse_left = false;
|
|
||||||
loop {
|
|
||||||
editor.shape_as_needed(true);
|
|
||||||
if editor.redraw() {
|
|
||||||
let instant = Instant::now();
|
|
||||||
|
|
||||||
let bg = editor.background_color();
|
|
||||||
window.set(orbclient::Color::rgb(bg.r(), bg.g(), bg.b()));
|
|
||||||
|
|
||||||
editor.draw(&mut swash_cache, |x, y, w, h, color| {
|
|
||||||
window.rect(
|
|
||||||
line_x as i32 + x,
|
|
||||||
y,
|
|
||||||
w,
|
|
||||||
h,
|
|
||||||
orbclient::Color { data: color.0 },
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
// Draw scrollbar
|
|
||||||
{
|
|
||||||
let mut start_line_opt = None;
|
|
||||||
let mut end_line = 0;
|
|
||||||
editor.with_buffer(|buffer| {
|
|
||||||
for run in buffer.layout_runs() {
|
|
||||||
end_line = run.line_i;
|
|
||||||
if start_line_opt.is_none() {
|
|
||||||
start_line_opt = Some(end_line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let start_line = start_line_opt.unwrap_or(end_line);
|
|
||||||
let lines = editor.with_buffer(|buffer| buffer.lines.len());
|
|
||||||
let start_y = (start_line * window.height() as usize) / lines;
|
|
||||||
let end_y = (end_line * window.height() as usize) / lines;
|
|
||||||
if end_y > start_y {
|
|
||||||
window.rect(
|
|
||||||
window.width() as i32 - line_x as i32,
|
|
||||||
start_y as i32,
|
|
||||||
line_x as u32,
|
|
||||||
(end_y - start_y) as u32,
|
|
||||||
orbclient::Color::rgba(0xFF, 0xFF, 0xFF, 0x40),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
window.sync();
|
|
||||||
|
|
||||||
editor.set_redraw(false);
|
|
||||||
|
|
||||||
log::debug!("redraw: {:?}", instant.elapsed());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut found_event = false;
|
|
||||||
let mut force_drag = true;
|
|
||||||
let mut window_async = false;
|
|
||||||
for event in window.events() {
|
|
||||||
found_event = true;
|
|
||||||
match event.to_option() {
|
|
||||||
EventOption::Key(event) => match event.scancode {
|
|
||||||
orbclient::K_CTRL => ctrl_pressed = event.pressed,
|
|
||||||
orbclient::K_LEFT if event.pressed => {
|
|
||||||
editor.action(Action::Motion(Motion::Left))
|
|
||||||
}
|
|
||||||
orbclient::K_RIGHT if event.pressed => {
|
|
||||||
editor.action(Action::Motion(Motion::Right))
|
|
||||||
}
|
|
||||||
orbclient::K_UP if event.pressed => editor.action(Action::Motion(Motion::Up)),
|
|
||||||
orbclient::K_DOWN if event.pressed => {
|
|
||||||
editor.action(Action::Motion(Motion::Down))
|
|
||||||
}
|
|
||||||
orbclient::K_HOME if event.pressed => {
|
|
||||||
editor.action(Action::Motion(Motion::Home))
|
|
||||||
}
|
|
||||||
orbclient::K_END if event.pressed => editor.action(Action::Motion(Motion::End)),
|
|
||||||
orbclient::K_PGUP if event.pressed => {
|
|
||||||
editor.action(Action::Motion(Motion::PageUp))
|
|
||||||
}
|
|
||||||
orbclient::K_PGDN if event.pressed => {
|
|
||||||
editor.action(Action::Motion(Motion::PageDown))
|
|
||||||
}
|
|
||||||
orbclient::K_ESC if event.pressed => editor.action(Action::Escape),
|
|
||||||
orbclient::K_ENTER if event.pressed => editor.action(Action::Enter),
|
|
||||||
orbclient::K_BKSP if event.pressed => editor.action(Action::Backspace),
|
|
||||||
orbclient::K_DEL if event.pressed => editor.action(Action::Delete),
|
|
||||||
orbclient::K_0 if event.pressed && ctrl_pressed => {
|
|
||||||
font_size_i = font_size_default;
|
|
||||||
editor
|
|
||||||
.with_buffer_mut(|buffer| buffer.set_metrics(font_sizes[font_size_i]));
|
|
||||||
}
|
|
||||||
orbclient::K_MINUS if event.pressed && ctrl_pressed => {
|
|
||||||
if font_size_i > 0 {
|
|
||||||
font_size_i -= 1;
|
|
||||||
editor.with_buffer_mut(|buffer| {
|
|
||||||
buffer.set_metrics(font_sizes[font_size_i])
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
orbclient::K_EQUALS if event.pressed && ctrl_pressed => {
|
|
||||||
if font_size_i + 1 < font_sizes.len() {
|
|
||||||
font_size_i += 1;
|
|
||||||
editor.with_buffer_mut(|buffer| {
|
|
||||||
buffer.set_metrics(font_sizes[font_size_i])
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
},
|
|
||||||
EventOption::TextInput(event) if !ctrl_pressed => {
|
|
||||||
editor.action(Action::Insert(event.character));
|
|
||||||
}
|
|
||||||
EventOption::Mouse(event) => {
|
|
||||||
mouse_x = event.x;
|
|
||||||
mouse_y = event.y;
|
|
||||||
if mouse_left {
|
|
||||||
editor.action(Action::Drag {
|
|
||||||
x: mouse_x - line_x as i32,
|
|
||||||
y: mouse_y,
|
|
||||||
});
|
|
||||||
|
|
||||||
if mouse_y <= 5 {
|
|
||||||
editor.action(Action::Scroll { lines: -3 });
|
|
||||||
window_async = true;
|
|
||||||
} else if mouse_y + 5 >= window.height() as i32 {
|
|
||||||
editor.action(Action::Scroll { lines: 3 });
|
|
||||||
window_async = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
force_drag = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EventOption::Button(event) => {
|
|
||||||
if event.left != mouse_left {
|
|
||||||
mouse_left = event.left;
|
|
||||||
if mouse_left {
|
|
||||||
editor.action(Action::Click {
|
|
||||||
x: mouse_x - line_x as i32,
|
|
||||||
y: mouse_y,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
force_drag = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EventOption::Resize(event) => {
|
|
||||||
editor.with_buffer_mut(|buffer| {
|
|
||||||
buffer.set_size(event.width as f32 - line_x * 2.0, event.height as f32);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
EventOption::Scroll(event) => {
|
|
||||||
editor.action(Action::Scroll {
|
|
||||||
lines: -event.y * 3,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
EventOption::Quit(_) => return,
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if mouse_left && force_drag {
|
|
||||||
editor.action(Action::Drag {
|
|
||||||
x: mouse_x - line_x as i32,
|
|
||||||
y: mouse_y,
|
|
||||||
});
|
|
||||||
|
|
||||||
if mouse_y <= 5 {
|
|
||||||
editor.action(Action::Scroll { lines: -3 });
|
|
||||||
window_async = true;
|
|
||||||
} else if mouse_y + 5 >= window.height() as i32 {
|
|
||||||
editor.action(Action::Scroll { lines: 3 });
|
|
||||||
window_async = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if window_async != window.is_async() {
|
|
||||||
window.set_async(window_async);
|
|
||||||
}
|
|
||||||
|
|
||||||
if window_async && !found_event {
|
|
||||||
// In async mode and no event found, sleep
|
|
||||||
thread::sleep(Duration::from_millis(5));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "editor-orbclient"
|
name = "editor"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Jeremy Soller <jeremy@system76.com>"]
|
authors = ["Jeremy Soller <jeremy@system76.com>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
@ -11,8 +11,10 @@ cosmic-text = { path = "../..", features = ["syntect"] }
|
||||||
env_logger = "0.10"
|
env_logger = "0.10"
|
||||||
fontdb = "0.13"
|
fontdb = "0.13"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
orbclient = "0.3.35"
|
softbuffer = "0.4"
|
||||||
|
tiny-skia = "0.11"
|
||||||
unicode-segmentation = "1.7"
|
unicode-segmentation = "1.7"
|
||||||
|
winit = "0.29"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
337
examples/editor/src/main.rs
Normal file
337
examples/editor/src/main.rs
Normal file
|
|
@ -0,0 +1,337 @@
|
||||||
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
|
||||||
|
use cosmic_text::{
|
||||||
|
Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, Motion, SwashCache, SyntaxEditor,
|
||||||
|
SyntaxSystem,
|
||||||
|
};
|
||||||
|
use std::{env, num::NonZeroU32, rc::Rc, slice};
|
||||||
|
use tiny_skia::{Paint, PixmapMut, Rect, Transform};
|
||||||
|
use winit::{
|
||||||
|
dpi::PhysicalPosition,
|
||||||
|
event::{ElementState, Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent},
|
||||||
|
event_loop::{ControlFlow, EventLoop},
|
||||||
|
keyboard::{Key, NamedKey},
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
env_logger::init();
|
||||||
|
|
||||||
|
let path = env::args().nth(1).unwrap_or(String::new());
|
||||||
|
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
|
||||||
|
let context = softbuffer::Context::new(window.clone()).unwrap();
|
||||||
|
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
|
||||||
|
let mut font_system = FontSystem::new();
|
||||||
|
let syntax_system = SyntaxSystem::new();
|
||||||
|
let mut swash_cache = SwashCache::new();
|
||||||
|
|
||||||
|
let mut display_scale = window.scale_factor() as f32;
|
||||||
|
|
||||||
|
let font_sizes = [
|
||||||
|
Metrics::new(10.0, 14.0), // Caption
|
||||||
|
Metrics::new(14.0, 20.0), // Body
|
||||||
|
Metrics::new(20.0, 28.0), // Title 4
|
||||||
|
Metrics::new(24.0, 32.0), // Title 3
|
||||||
|
Metrics::new(28.0, 36.0), // Title 2
|
||||||
|
Metrics::new(32.0, 44.0), // Title 1
|
||||||
|
];
|
||||||
|
let font_size_default = 1; // Body
|
||||||
|
let mut font_size_i = font_size_default;
|
||||||
|
|
||||||
|
let line_x = 8.0 * (window.scale_factor() as f32);
|
||||||
|
|
||||||
|
let mut editor = SyntaxEditor::new(
|
||||||
|
Buffer::new(
|
||||||
|
&mut font_system,
|
||||||
|
font_sizes[font_size_i].scale(display_scale),
|
||||||
|
),
|
||||||
|
&syntax_system,
|
||||||
|
"base16-eighties.dark",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let mut editor = editor.borrow_with(&mut font_system);
|
||||||
|
|
||||||
|
let attrs = Attrs::new().family(Family::Monospace);
|
||||||
|
|
||||||
|
match editor.load_text(&path, attrs) {
|
||||||
|
Ok(()) => (),
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("failed to load {:?}: {}", path, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut ctrl_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;
|
||||||
|
|
||||||
|
event_loop
|
||||||
|
.run(|event, elwt| {
|
||||||
|
elwt.set_control_flow(ControlFlow::Wait);
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent { window_id, event } => {
|
||||||
|
match event {
|
||||||
|
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
|
||||||
|
log::info!("Updated scale factor for {window_id:?}");
|
||||||
|
|
||||||
|
display_scale = scale_factor as f32;
|
||||||
|
editor.with_buffer_mut(|buffer| {
|
||||||
|
buffer.set_metrics(font_sizes[font_size_i].scale(display_scale))
|
||||||
|
});
|
||||||
|
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
let (width, height) = {
|
||||||
|
let size = window.inner_size();
|
||||||
|
(size.width, size.height)
|
||||||
|
};
|
||||||
|
|
||||||
|
surface
|
||||||
|
.resize(
|
||||||
|
NonZeroU32::new(width).unwrap(),
|
||||||
|
NonZeroU32::new(height).unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut surface_buffer = surface.buffer_mut().unwrap();
|
||||||
|
let surface_buffer_u8 = unsafe {
|
||||||
|
slice::from_raw_parts_mut(
|
||||||
|
surface_buffer.as_mut_ptr() as *mut u8,
|
||||||
|
surface_buffer.len() * 4,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
let mut pixmap =
|
||||||
|
PixmapMut::from_bytes(surface_buffer_u8, width, height).unwrap();
|
||||||
|
pixmap.fill(tiny_skia::Color::from_rgba8(0, 0, 0, 0xFF));
|
||||||
|
|
||||||
|
editor.with_buffer_mut(|buffer| {
|
||||||
|
buffer
|
||||||
|
.set_size(width as f32 - line_x * display_scale, height as f32)
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut paint = Paint::default();
|
||||||
|
paint.anti_alias = false;
|
||||||
|
editor.shape_as_needed(true);
|
||||||
|
editor.draw(&mut swash_cache, |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
|
||||||
|
paint.set_color_rgba8(color.b(), color.g(), color.r(), color.a());
|
||||||
|
pixmap.fill_rect(
|
||||||
|
Rect::from_xywh(x as f32, y as f32, w as f32, h as f32)
|
||||||
|
.unwrap(),
|
||||||
|
&paint,
|
||||||
|
Transform::identity(),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Draw scrollbar
|
||||||
|
{
|
||||||
|
let mut start_line_opt = None;
|
||||||
|
let mut end_line = 0;
|
||||||
|
editor.with_buffer(|buffer| {
|
||||||
|
for run in buffer.layout_runs() {
|
||||||
|
end_line = run.line_i;
|
||||||
|
if start_line_opt.is_none() {
|
||||||
|
start_line_opt = Some(end_line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let start_line = start_line_opt.unwrap_or(end_line);
|
||||||
|
let lines = editor.with_buffer(|buffer| buffer.lines.len());
|
||||||
|
let start_y = (start_line * height as usize) / lines;
|
||||||
|
let end_y = (end_line * height as usize) / lines;
|
||||||
|
paint.set_color_rgba8(0xFF, 0xFF, 0xFF, 0x40);
|
||||||
|
if end_y > start_y {
|
||||||
|
pixmap.fill_rect(
|
||||||
|
Rect::from_xywh(
|
||||||
|
width as f32 - line_x * display_scale,
|
||||||
|
start_y as f32,
|
||||||
|
line_x * display_scale,
|
||||||
|
(end_y - start_y) as f32,
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
&paint,
|
||||||
|
Transform::identity(),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
surface_buffer.present().unwrap();
|
||||||
|
}
|
||||||
|
WindowEvent::ModifiersChanged(modifiers) => {
|
||||||
|
ctrl_pressed = modifiers.state().control_key()
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput { event, .. } => {
|
||||||
|
let KeyEvent {
|
||||||
|
logical_key, state, ..
|
||||||
|
} = event;
|
||||||
|
|
||||||
|
if state.is_pressed() {
|
||||||
|
match logical_key {
|
||||||
|
Key::Named(NamedKey::ArrowLeft) => {
|
||||||
|
editor.action(Action::Motion(Motion::Left))
|
||||||
|
}
|
||||||
|
Key::Named(NamedKey::ArrowRight) => {
|
||||||
|
editor.action(Action::Motion(Motion::Right))
|
||||||
|
}
|
||||||
|
Key::Named(NamedKey::ArrowUp) => {
|
||||||
|
editor.action(Action::Motion(Motion::Up))
|
||||||
|
}
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
Key::Named(NamedKey::PageDown) => {
|
||||||
|
editor.action(Action::Motion(Motion::PageDown))
|
||||||
|
}
|
||||||
|
Key::Named(NamedKey::Escape) => editor.action(Action::Escape),
|
||||||
|
Key::Named(NamedKey::Enter) => editor.action(Action::Enter),
|
||||||
|
Key::Named(NamedKey::Backspace) => {
|
||||||
|
editor.action(Action::Backspace)
|
||||||
|
}
|
||||||
|
Key::Named(NamedKey::Delete) => editor.action(Action::Delete),
|
||||||
|
Key::Named(key) => {
|
||||||
|
if let Some(text) = key.to_text() {
|
||||||
|
for c in text.chars() {
|
||||||
|
editor.action(Action::Insert(c));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Key::Character(text) => {
|
||||||
|
if ctrl_pressed {
|
||||||
|
match &*text {
|
||||||
|
"0" => {
|
||||||
|
font_size_i = font_size_default;
|
||||||
|
editor.with_buffer_mut(|buffer| {
|
||||||
|
buffer.set_metrics(
|
||||||
|
font_sizes[font_size_i]
|
||||||
|
.scale(display_scale),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
"-" => {
|
||||||
|
if font_size_i > 0 {
|
||||||
|
font_size_i -= 1;
|
||||||
|
editor.with_buffer_mut(|buffer| {
|
||||||
|
buffer.set_metrics(
|
||||||
|
font_sizes[font_size_i]
|
||||||
|
.scale(display_scale),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"=" => {
|
||||||
|
if font_size_i + 1 < font_sizes.len() {
|
||||||
|
font_size_i += 1;
|
||||||
|
editor.with_buffer_mut(|buffer| {
|
||||||
|
buffer.set_metrics(
|
||||||
|
font_sizes[font_size_i]
|
||||||
|
.scale(display_scale),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for c in text.chars() {
|
||||||
|
editor.action(Action::Insert(c));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::CursorMoved {
|
||||||
|
device_id: _,
|
||||||
|
position,
|
||||||
|
} => {
|
||||||
|
// Update saved mouse position for use when handling click events
|
||||||
|
mouse_x = position.x;
|
||||||
|
mouse_y = position.y;
|
||||||
|
|
||||||
|
// Implement dragging
|
||||||
|
if mouse_left.is_pressed() {
|
||||||
|
// Execute Drag editor action (update selection)
|
||||||
|
editor.action(Action::Drag {
|
||||||
|
x: position.x as i32,
|
||||||
|
y: position.y as i32,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Scroll if cursor is near edge of window while dragging
|
||||||
|
if mouse_y <= 5.0 {
|
||||||
|
editor.action(Action::Scroll { lines: -1 });
|
||||||
|
} else if mouse_y - 5.0 >= window.inner_size().height as f64 {
|
||||||
|
editor.action(Action::Scroll { lines: 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::MouseInput {
|
||||||
|
device_id: _,
|
||||||
|
state,
|
||||||
|
button,
|
||||||
|
} => {
|
||||||
|
if button == MouseButton::Left {
|
||||||
|
if state == ElementState::Pressed
|
||||||
|
&& mouse_left == ElementState::Released
|
||||||
|
{
|
||||||
|
editor.action(Action::Click {
|
||||||
|
x: mouse_x /*- line_x*/ as i32,
|
||||||
|
y: mouse_y as i32,
|
||||||
|
});
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
mouse_left = state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::MouseWheel {
|
||||||
|
device_id: _,
|
||||||
|
delta,
|
||||||
|
phase: _,
|
||||||
|
} => {
|
||||||
|
let line_delta = match delta {
|
||||||
|
MouseScrollDelta::LineDelta(_x, y) => y as i32,
|
||||||
|
MouseScrollDelta::PixelDelta(PhysicalPosition { x: _, y }) => {
|
||||||
|
unapplied_scroll_delta += y;
|
||||||
|
let line_delta = (unapplied_scroll_delta / 20.0).floor();
|
||||||
|
unapplied_scroll_delta -= line_delta * 20.0;
|
||||||
|
line_delta as i32
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if line_delta != 0 {
|
||||||
|
editor.action(Action::Scroll { lines: -line_delta });
|
||||||
|
}
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
WindowEvent::CloseRequested => {
|
||||||
|
//TODO: just close one window
|
||||||
|
elwt.exit();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue