cosmic-text/examples/editor-orbclient/src/main.rs

267 lines
9.5 KiB
Rust
Raw Normal View History

2022-10-24 08:56:48 -06:00
// SPDX-License-Identifier: MIT OR Apache-2.0
2022-10-27 09:16:21 -06:00
use cosmic_text::{
Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, Motion, SwashCache, SyntaxEditor,
SyntaxSystem,
2022-10-27 09:16:21 -06:00
};
2022-10-27 09:07:47 -06:00
use orbclient::{EventOption, Renderer, Window, WindowFlag};
2023-01-04 20:03:03 -07:00
use std::{
env, thread,
time::{Duration, Instant},
};
2022-10-18 12:07:22 -06:00
fn main() {
env_logger::init();
let path = if let Some(arg) = env::args().nth(1) {
2023-02-28 19:42:53 +01:00
arg
2022-10-27 09:16:21 -06:00
} else {
String::new()
2022-10-27 09:16:21 -06:00
};
2022-10-18 12:07:22 -06:00
let display_scale = match orbclient::get_display_size() {
Ok((w, h)) => {
log::info!("Display size: {}, {}", w, h);
2023-11-07 15:56:43 -07:00
(h / 1600) as f32 + 1.0
2022-10-18 12:07:22 -06:00
}
Err(err) => {
log::warn!("Failed to get display size: {}", err);
1.0
2022-10-18 12:07:22 -06:00
}
};
let mut window = Window::new_flags(
-1,
-1,
1024 * display_scale as u32,
768 * display_scale as u32,
2023-02-28 19:42:53 +01:00
&format!("COSMIC Text - {path}"),
2022-10-25 09:31:51 -06:00
&[WindowFlag::Resizable],
2022-10-18 12:07:22 -06:00
)
.unwrap();
let mut font_system = FontSystem::new();
2022-10-27 09:16:21 -06:00
let syntax_system = SyntaxSystem::new();
2022-10-18 12:07:22 -06:00
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
2022-10-18 12:07:22 -06:00
];
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(
2023-03-12 10:30:20 +01:00
Buffer::new(&mut font_system, font_sizes[font_size_i]),
&syntax_system,
2023-01-04 20:03:03 -07:00
"base16-eighties.dark",
)
.unwrap();
2022-10-27 09:16:21 -06:00
#[cfg(feature = "vi")]
let mut editor = cosmic_text::ViEditor::new(editor);
let mut editor = editor.borrow_with(&mut font_system);
2023-01-04 20:03:03 -07:00
editor
.buffer_mut()
.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32);
2022-10-27 09:16:21 -06:00
2023-03-30 08:04:52 -06:00
let attrs = Attrs::new().family(Family::Monospace);
match editor.load_text(&path, attrs) {
Ok(()) => (),
Err(err) => {
log::error!("failed to load {:?}: {}", path, err);
}
}
2022-10-18 12:07:22 -06:00
2023-03-01 22:41:59 +01:00
let mut swash_cache = SwashCache::new();
2022-10-27 09:16:21 -06:00
2022-10-18 12:07:22 -06:00
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.buffer().redraw() {
2022-10-18 12:07:22 -06:00
let instant = Instant::now();
let bg = editor.background_color();
window.set(orbclient::Color::rgb(bg.r(), bg.g(), bg.b()));
2022-10-18 12:07:22 -06:00
let fg = editor.foreground_color();
editor.draw(&mut swash_cache, fg, |x, y, w, h, color| {
window.rect(
line_x as i32 + x,
y,
w,
h,
orbclient::Color { data: color.0 },
)
});
2022-10-18 12:07:22 -06:00
// Draw scrollbar
{
let mut start_line_opt = None;
let mut end_line = 0;
for run in editor.buffer().layout_runs() {
end_line = run.line_i;
2023-02-28 19:42:53 +01:00
if start_line_opt.is_none() {
start_line_opt = Some(end_line);
}
}
2022-10-18 12:07:22 -06:00
let start_line = start_line_opt.unwrap_or(end_line);
let lines = editor.buffer().lines.len();
2022-10-27 09:18:27 -06:00
let start_y = (start_line * window.height() as usize) / lines;
let end_y = (end_line * window.height() as usize) / lines;
2022-10-18 12:07:22 -06:00
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,
2022-10-27 09:18:27 -06:00
orbclient::Color::rgba(0xFF, 0xFF, 0xFF, 0x40),
2022-10-18 12:07:22 -06:00
);
}
}
window.sync();
editor.buffer_mut().set_redraw(false);
2022-10-18 12:07:22 -06:00
log::debug!("redraw: {:?}", instant.elapsed());
2022-10-18 12:07:22 -06:00
}
2022-10-25 09:03:39 -06:00
let mut found_event = false;
2022-10-27 09:16:21 -06:00
let mut force_drag = true;
let mut window_async = false;
2022-10-18 12:07:22 -06:00
for event in window.events() {
2022-10-25 09:03:39 -06:00
found_event = true;
2022-10-18 12:07:22 -06:00
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),
2022-10-18 12:07:22 -06:00
orbclient::K_0 if event.pressed && ctrl_pressed => {
font_size_i = font_size_default;
editor.buffer_mut().set_metrics(font_sizes[font_size_i]);
}
2022-10-18 12:07:22 -06:00
orbclient::K_MINUS if event.pressed && ctrl_pressed => {
if font_size_i > 0 {
font_size_i -= 1;
editor.buffer_mut().set_metrics(font_sizes[font_size_i]);
2022-10-18 12:07:22 -06:00
}
}
2022-10-18 12:07:22 -06:00
orbclient::K_EQUALS if event.pressed && ctrl_pressed => {
if font_size_i + 1 < font_sizes.len() {
font_size_i += 1;
editor.buffer_mut().set_metrics(font_sizes[font_size_i]);
2022-10-18 12:07:22 -06:00
}
}
2022-10-18 12:07:22 -06:00
_ => (),
},
EventOption::TextInput(event) if !ctrl_pressed => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Insert(event.character));
2022-10-18 12:07:22 -06:00
}
EventOption::Mouse(event) => {
mouse_x = event.x;
mouse_y = event.y;
if mouse_left {
2022-10-31 11:24:36 -06:00
editor.action(Action::Drag {
x: mouse_x - line_x as i32,
y: mouse_y,
});
if mouse_y <= 5 {
2022-10-31 11:24:36 -06:00
editor.action(Action::Scroll { lines: -3 });
window_async = true;
} else if mouse_y + 5 >= window.height() as i32 {
2022-10-31 11:24:36 -06:00
editor.action(Action::Scroll { lines: 3 });
window_async = true;
}
force_drag = false;
2022-10-18 12:07:22 -06:00
}
}
EventOption::Button(event) => {
if event.left != mouse_left {
mouse_left = event.left;
if mouse_left {
2022-10-31 11:24:36 -06:00
editor.action(Action::Click {
x: mouse_x - line_x as i32,
y: mouse_y,
});
2022-10-18 12:07:22 -06:00
}
force_drag = false;
2022-10-18 12:07:22 -06:00
}
}
EventOption::Resize(event) => {
2023-01-04 20:03:03 -07:00
editor
.buffer_mut()
.set_size(event.width as f32 - line_x * 2.0, event.height as f32);
2022-10-18 12:07:22 -06:00
}
EventOption::Scroll(event) => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Scroll {
2022-10-19 08:34:34 -06:00
lines: -event.y * 3,
});
2022-10-18 12:07:22 -06:00
}
EventOption::Quit(_) => return,
_ => (),
}
}
if mouse_left && force_drag {
2022-10-31 11:24:36 -06:00
editor.action(Action::Drag {
x: mouse_x - line_x as i32,
y: mouse_y,
});
if mouse_y <= 5 {
2022-10-31 11:24:36 -06:00
editor.action(Action::Scroll { lines: -3 });
window_async = true;
} else if mouse_y + 5 >= window.height() as i32 {
2022-10-31 11:24:36 -06:00
editor.action(Action::Scroll { lines: 3 });
window_async = true;
}
}
2022-10-25 09:03:39 -06:00
if window_async != window.is_async() {
window.set_async(window_async);
}
2023-01-04 20:03:03 -07:00
if window_async && !found_event {
// In async mode and no event found, sleep
2022-10-25 09:03:39 -06:00
thread::sleep(Duration::from_millis(5));
}
2022-10-18 12:07:22 -06:00
}
}