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

392 lines
13 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::{
Attrs,
AttrsList,
2022-10-31 11:24:36 -06:00
Buffer,
2022-10-27 09:16:21 -06:00
Color,
2022-10-31 11:24:36 -06:00
Editor,
2022-10-27 09:16:21 -06:00
Family,
FontSystem,
2022-10-31 11:24:36 -06:00
Metrics,
2022-10-27 09:16:21 -06:00
Style,
SwashCache,
2022-10-31 11:24:36 -06:00
Action,
2022-10-27 09:16:21 -06:00
Weight
};
2022-10-27 09:07:47 -06:00
use orbclient::{EventOption, Renderer, Window, WindowFlag};
2022-10-25 09:03:39 -06:00
use std::{env, fs, thread, time::{Duration, Instant}};
2022-10-27 09:16:21 -06:00
use syntect::highlighting::{
FontStyle,
Highlighter,
HighlightState,
RangedHighlightIterator,
ThemeSet,
};
use syntect::parsing::{
ParseState,
ScopeStack,
SyntaxSet,
};
2022-10-18 12:07:22 -06:00
fn main() {
env_logger::init();
2022-10-27 09:16:21 -06:00
let (path, text) = if let Some(arg) = env::args().nth(1) {
(
arg.clone(),
fs::read_to_string(&arg).expect("failed to open file")
)
} else {
(
String::new(),
String::new()
)
};
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);
(h as i32 / 1600) + 1
}
Err(err) => {
log::warn!("Failed to get display size: {}", err);
1
}
};
let mut window = Window::new_flags(
-1,
-1,
1024 * display_scale as u32,
768 * display_scale as u32,
2022-10-27 09:16:21 -06:00
&format!("COSMIC Text - {}", path),
2022-10-25 09:31:51 -06:00
&[WindowFlag::Resizable],
2022-10-18 12:07:22 -06:00
)
.unwrap();
2022-10-27 09:16:21 -06:00
let font_system = FontSystem::new();
2022-10-18 12:07:22 -06:00
let font_sizes = [
2022-10-31 11:24:36 -06:00
Metrics::new(10, 14).scale(display_scale), // Caption
Metrics::new(14, 20).scale(display_scale), // Body
Metrics::new(20, 28).scale(display_scale), // Title 4
Metrics::new(24, 32).scale(display_scale), // Title 3
Metrics::new(28, 36).scale(display_scale), // Title 2
Metrics::new(32, 44).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 * display_scale;
2022-10-31 11:24:36 -06:00
let mut editor = Editor::new(Buffer::new(
2022-10-25 16:13:07 -06:00
&font_system,
2022-10-18 17:04:22 -06:00
font_sizes[font_size_i]
2022-10-31 11:24:36 -06:00
));
2022-10-27 09:16:21 -06:00
2022-10-31 11:24:36 -06:00
editor.buffer.set_size(
2022-10-18 12:07:22 -06:00
window.width() as i32 - line_x * 2,
2022-10-18 12:42:37 -06:00
window.height() as i32
2022-10-18 12:07:22 -06:00
);
2022-10-27 09:16:21 -06:00
let attrs = Attrs::new()
.monospaced(true)
.family(Family::Monospace);
2022-10-31 11:24:36 -06:00
editor.buffer.set_text(&text, attrs);
2022-10-18 12:07:22 -06:00
2022-10-27 09:16:21 -06:00
let mut bg_color = orbclient::Color::rgb(0x00, 0x00, 0x00);
let mut font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let now = Instant::now();
//TODO: store newlines in buffer
let ps = SyntaxSet::load_defaults_nonewlines();
let ts = ThemeSet::load_defaults();
let theme = &ts.themes["base16-eighties.dark"];
let highlighter = Highlighter::new(theme);
if let Some(background) = theme.settings.background {
bg_color = orbclient::Color::rgba(
background.r,
background.g,
background.b,
background.a,
);
}
if let Some(foreground) = theme.settings.foreground {
font_color = Color::rgba(
foreground.r,
foreground.g,
foreground.b,
foreground.a,
);
}
let syntax = match ps.find_syntax_for_file(&path) {
Ok(Some(some)) => some,
Ok(None) => {
log::warn!("no syntax found for {:?}", path);
ps.find_syntax_plain_text()
}
Err(err) => {
log::warn!("failed to determine syntax for {:?}: {:?}", path, err);
ps.find_syntax_plain_text()
}
};
log::info!("using syntax {:?}, loaded in {:?}", syntax.name, now.elapsed());
let mut swash_cache = SwashCache::new(&font_system);
let mut syntax_cache = Vec::<(ParseState, HighlightState)>::new();
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;
2022-10-27 09:16:21 -06:00
let mut rehighlight = true;
2022-10-18 12:07:22 -06:00
loop {
2022-10-27 09:16:21 -06:00
if rehighlight {
let now = Instant::now();
2022-10-31 11:24:36 -06:00
for line_i in 0..editor.buffer.lines.len() {
let line = &mut editor.buffer.lines[line_i];
2022-10-27 09:16:21 -06:00
if ! line.is_reset() && line_i < syntax_cache.len() {
continue;
}
let (mut parse_state, mut highlight_state) = if line_i > 0 && line_i <= syntax_cache.len() {
syntax_cache[line_i - 1].clone()
} else {
(
ParseState::new(syntax),
HighlightState::new(&highlighter, ScopeStack::new())
)
};
let ops = parse_state.parse_line(line.text(), &ps).unwrap();
let ranges = RangedHighlightIterator::new(
&mut highlight_state,
&ops,
line.text(),
&highlighter,
);
let mut attrs_list = AttrsList::new(attrs);
for (style, _, range) in ranges {
attrs_list.add_span(
range,
2022-10-27 09:16:21 -06:00
attrs
.color(Color::rgba(
style.foreground.r,
style.foreground.g,
style.foreground.b,
style.foreground.a,
))
//TODO: background
.style(if style.font_style.contains(FontStyle::ITALIC) {
Style::Italic
} else {
Style::Normal
})
.weight(if style.font_style.contains(FontStyle::BOLD) {
Weight::BOLD
} else {
Weight::NORMAL
})
//TODO: underline
);
}
// Update line attributes. This operation only resets if the line changes
line.set_attrs_list(attrs_list);
line.set_wrap_simple(true);
//TODO: efficiently do syntax highlighting without having to shape whole buffer
line.shape(&font_system);
let cache_item = (parse_state.clone(), highlight_state.clone());
if line_i < syntax_cache.len() {
if syntax_cache[line_i] != cache_item {
syntax_cache[line_i] = cache_item;
2022-10-31 11:24:36 -06:00
if line_i + 1 < editor.buffer.lines.len() {
editor.buffer.lines[line_i + 1].reset();
2022-10-27 09:16:21 -06:00
}
}
} else {
syntax_cache.push(cache_item);
}
}
2022-10-31 11:24:36 -06:00
editor.buffer.redraw = true;
2022-10-27 09:16:21 -06:00
rehighlight = false;
log::info!("Syntax highlighted in {:?}", now.elapsed());
}
2022-10-31 11:24:36 -06:00
editor.shape_as_needed();
if editor.buffer.redraw {
2022-10-18 12:07:22 -06:00
let instant = Instant::now();
window.set(bg_color);
2022-10-31 11:24:36 -06:00
editor.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
2022-10-27 09:07:47 -06:00
window.rect(line_x + 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;
2022-10-31 11:24:36 -06:00
for run in editor.buffer.layout_runs() {
end_line = run.line_i;
if start_line_opt == 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);
2022-10-31 11:24:36 -06:00
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();
2022-10-31 11:24:36 -06:00
editor.buffer.redraw = false;
2022-10-18 12:07:22 -06:00
let duration = instant.elapsed();
log::debug!("redraw: {:?}", duration);
}
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,
2022-10-31 11:24:36 -06:00
orbclient::K_LEFT if event.pressed => editor.action(Action::Left),
orbclient::K_RIGHT if event.pressed => editor.action(Action::Right),
orbclient::K_UP if event.pressed => editor.action(Action::Up),
orbclient::K_DOWN if event.pressed => editor.action(Action::Down),
orbclient::K_HOME if event.pressed => editor.action(Action::Home),
orbclient::K_END if event.pressed => editor.action(Action::End),
orbclient::K_PGUP if event.pressed => editor.action(Action::PageUp),
orbclient::K_PGDN if event.pressed => editor.action(Action::PageDown),
2022-10-27 09:16:21 -06:00
orbclient::K_ENTER if event.pressed => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Enter);
2022-10-27 09:16:21 -06:00
rehighlight = true;
},
orbclient::K_BKSP if event.pressed => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Backspace);
2022-10-27 09:16:21 -06:00
rehighlight = true;
},
orbclient::K_DEL if event.pressed => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Delete);
2022-10-27 09:16:21 -06:00
rehighlight = true;
},
2022-10-18 12:07:22 -06:00
orbclient::K_0 if event.pressed && ctrl_pressed => {
font_size_i = font_size_default;
2022-10-31 11:24:36 -06:00
editor.buffer.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;
2022-10-31 11:24:36 -06:00
editor.buffer.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;
2022-10-31 11:24:36 -06:00
editor.buffer.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-27 09:16:21 -06:00
rehighlight = true;
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,
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,
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) => {
2022-10-31 11:24:36 -06:00
editor.buffer.set_size(event.width as i32 - line_x * 2, event.height as i32);
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,
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);
}
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
}
}