cosmic-text/examples/editor-libcosmic/src/text_box.rs

345 lines
11 KiB
Rust
Raw Normal View History

2022-10-24 08:56:48 -06:00
// SPDX-License-Identifier: MIT OR Apache-2.0
2022-11-09 08:03:13 -07:00
use cosmic::{
iced_native::{
{Color, Element, Length, Point, Rectangle, Shell, Size},
clipboard::Clipboard,
event::{Event, Status},
image,
keyboard::{Event as KeyEvent, KeyCode},
layout::{self, Layout},
mouse::{self, Button, Event as MouseEvent, ScrollDelta},
renderer,
widget::{self, tree, Widget},
},
theme::Theme,
2022-10-18 12:07:22 -06:00
};
use cosmic_text::{
2022-10-31 11:24:36 -06:00
Action,
Editor,
2022-10-25 11:40:10 -06:00
SwashCache,
2022-10-18 12:07:22 -06:00
};
use std::{
cmp,
sync::Mutex,
2022-10-18 12:07:22 -06:00
time::Instant,
};
2022-11-01 11:54:53 -07:00
use super::text;
2022-10-18 12:07:22 -06:00
2022-10-18 14:35:16 -06:00
pub struct Appearance {
background_color: Option<Color>,
text_color: Color,
2022-10-18 14:35:16 -06:00
}
pub trait StyleSheet {
fn appearance(&self) -> Appearance;
}
impl StyleSheet for Theme {
fn appearance(&self) -> Appearance {
match self {
Theme::Dark => Appearance {
background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)),
text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF),
2022-10-18 14:35:16 -06:00
},
Theme::Light => Appearance {
background_color: Some(Color::from_rgb8(0xFC, 0xFC, 0xFC)),
text_color: Color::from_rgb8(0x00, 0x00, 0x00),
2022-10-18 14:35:16 -06:00
},
}
}
}
2022-10-18 12:07:22 -06:00
pub struct TextBox<'a> {
2022-10-31 11:24:36 -06:00
editor: &'a Mutex<Editor<'static>>,
2022-10-18 12:07:22 -06:00
}
impl<'a> TextBox<'a> {
2022-10-31 11:24:36 -06:00
pub fn new(editor: &'a Mutex<Editor<'static>>) -> Self {
Self {
2022-10-31 11:24:36 -06:00
editor,
}
2022-10-18 12:07:22 -06:00
}
}
2022-10-31 11:24:36 -06:00
pub fn text_box<'a>(editor: &'a Mutex<Editor<'static>>) -> TextBox<'a> {
TextBox::new(editor)
2022-10-18 12:07:22 -06:00
}
impl<'a, Message, Renderer> Widget<Message, Renderer> for TextBox<'a>
where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
2022-10-18 14:35:16 -06:00
Renderer::Theme: StyleSheet,
2022-10-18 12:07:22 -06:00
{
2022-10-19 10:12:52 -06:00
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
}
fn state(&self) -> tree::State {
tree::State::new(State::new())
}
2022-10-18 12:07:22 -06:00
fn width(&self) -> Length {
2022-10-18 13:20:13 -06:00
Length::Fill
2022-10-18 12:07:22 -06:00
}
fn height(&self) -> Length {
2022-10-18 13:20:13 -06:00
Length::Fill
2022-10-18 12:07:22 -06:00
}
fn layout(
&self,
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
2022-11-01 11:23:21 -07:00
let limits = limits.width(Length::Fill).height(Length::Fill);
2022-11-01 09:09:36 -06:00
//TODO: allow lazy shape
let mut editor = self.editor.lock().unwrap();
editor.buffer.shape_until(i32::max_value());
let mut layout_lines = 0;
for line in editor.buffer.lines.iter() {
match line.layout_opt() {
Some(layout) => layout_lines += layout.len(),
None => (),
}
}
let height = layout_lines as f32 * editor.buffer.metrics().line_height as f32;
let size = Size::new(limits.max().width, height);
log::info!("size {:?}", size);
2022-11-01 11:23:21 -07:00
layout::Node::new(limits.resolve(size))
2022-10-18 12:07:22 -06:00
}
2022-10-19 09:26:43 -06:00
fn mouse_interaction(
&self,
_tree: &widget::Tree,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
_renderer: &Renderer,
) -> mouse::Interaction {
if layout.bounds().contains(cursor_position) {
mouse::Interaction::Text
} else {
mouse::Interaction::Idle
}
}
2022-10-18 12:07:22 -06:00
fn draw(
&self,
2022-10-27 15:17:52 -06:00
tree: &widget::Tree,
2022-10-18 12:07:22 -06:00
renderer: &mut Renderer,
2022-10-18 14:35:16 -06:00
theme: &Renderer::Theme,
2022-10-18 12:07:22 -06:00
_style: &renderer::Style,
layout: Layout<'_>,
_cursor_position: Point,
2022-11-01 09:09:36 -06:00
viewport: &Rectangle,
2022-10-18 12:07:22 -06:00
) {
2022-10-27 15:17:52 -06:00
let state = tree.state.downcast_ref::<State>();
let appearance = theme.appearance();
if let Some(background_color) = appearance.background_color {
2022-10-18 14:35:16 -06:00
renderer.fill_quad(
renderer::Quad {
bounds: layout.bounds(),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
background_color
);
}
2022-10-18 12:07:22 -06:00
let text_color = cosmic_text::Color::rgba(
cmp::max(0, cmp::min(255, (appearance.text_color.r * 255.0) as i32)) as u8,
cmp::max(0, cmp::min(255, (appearance.text_color.g * 255.0) as i32)) as u8,
cmp::max(0, cmp::min(255, (appearance.text_color.b * 255.0) as i32)) as u8,
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
);
2022-10-31 11:24:36 -06:00
let mut editor = self.editor.lock().unwrap();
2022-11-01 09:09:36 -06:00
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32);
let view_h = cmp::min(viewport.height as i32, layout.bounds().height as i32);
editor.buffer.set_size(view_w, view_h);
2022-10-31 11:24:36 -06:00
editor.shape_as_needed();
2022-11-01 09:09:36 -06:00
let instant = Instant::now();
let mut pixels = vec![0; view_w as usize * view_h as usize * 4];
editor.draw(&mut state.cache.lock().unwrap(), text_color, |x, y, w, h, color| {
if w <= 0 || h <= 0 {
// Do not draw invalid sized rectangles
return;
}
if w > 1 || h > 1 {
// Draw rectangles with optimized quad renderer
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle::new(
Point::new(layout.position().x + x as f32, layout.position().y + y as f32),
Size::new(w as f32, h as f32)
),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
Color::from_rgba8(
color.r(),
color.g(),
color.b(),
(color.a() as f32) / 255.0
)
);
} else {
2022-11-01 11:54:53 -07:00
text::draw_pixel(&mut pixels, view_w, view_h, x, y, color);
2022-11-01 09:09:36 -06:00
}
});
let handle = image::Handle::from_pixels(view_w as u32, view_h as u32, pixels);
image::Renderer::draw(renderer, handle, Rectangle::new(
layout.position(),
Size::new(view_w as f32, view_h as f32)
));
let duration = instant.elapsed();
log::debug!("redraw {}, {}: {:?}", view_w, view_h, duration);
2022-10-18 12:07:22 -06:00
}
fn on_event(
&mut self,
2022-10-19 10:12:52 -06:00
tree: &mut widget::Tree,
2022-10-18 12:07:22 -06:00
event: Event,
layout: Layout<'_>,
cursor_position: Point,
2022-10-18 12:07:22 -06:00
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
_shell: &mut Shell<'_, Message>,
) -> Status {
2022-10-19 10:12:52 -06:00
let state = tree.state.downcast_mut::<State>();
2022-10-31 11:24:36 -06:00
let mut editor = self.editor.lock().unwrap();
2022-10-18 12:07:22 -06:00
let mut status = Status::Ignored;
2022-10-18 12:07:22 -06:00
match event {
2022-10-19 10:12:52 -06:00
Event::Keyboard(KeyEvent::KeyPressed { key_code, modifiers }) => match key_code {
KeyCode::Left => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Left);
status = Status::Captured;
2022-10-18 12:07:22 -06:00
},
2022-10-19 10:12:52 -06:00
KeyCode::Right => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Right);
status = Status::Captured;
2022-10-18 12:07:22 -06:00
},
2022-10-19 10:12:52 -06:00
KeyCode::Up => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Up);
status = Status::Captured;
},
2022-10-19 10:12:52 -06:00
KeyCode::Down => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Down);
status = Status::Captured;
2022-10-19 10:12:52 -06:00
},
2022-10-19 11:08:15 -06:00
KeyCode::Home => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Home);
status = Status::Captured;
2022-10-19 11:08:15 -06:00
},
KeyCode::End => {
2022-10-31 11:24:36 -06:00
editor.action(Action::End);
status = Status::Captured;
2022-10-19 11:08:15 -06:00
},
2022-10-19 10:12:52 -06:00
KeyCode::PageUp => {
2022-10-31 11:24:36 -06:00
editor.action(Action::PageUp);
status = Status::Captured;
2022-10-19 10:12:52 -06:00
},
KeyCode::PageDown => {
2022-10-31 11:24:36 -06:00
editor.action(Action::PageDown);
status = Status::Captured;
2022-10-19 10:12:52 -06:00
},
2022-10-19 11:33:35 -06:00
KeyCode::Enter => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Enter);
status = Status::Captured;
2022-10-19 11:33:35 -06:00
},
KeyCode::Backspace => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Backspace);
status = Status::Captured;
2022-10-19 11:33:35 -06:00
},
KeyCode::Delete => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Delete);
status = Status::Captured;
2022-10-19 11:33:35 -06:00
},
2022-10-19 10:12:52 -06:00
_ => ()
},
Event::Keyboard(KeyEvent::CharacterReceived(character)) => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Insert(character));
status = Status::Captured;
2022-10-19 10:12:52 -06:00
},
Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => {
if layout.bounds().contains(cursor_position) {
2022-10-31 11:24:36 -06:00
editor.action(Action::Click {
2022-10-19 10:12:52 -06:00
x: (cursor_position.x - layout.bounds().x) as i32,
y: (cursor_position.y - layout.bounds().y) as i32,
});
state.is_dragging = true;
status = Status::Captured;
2022-10-19 10:12:52 -06:00
}
},
Event::Mouse(MouseEvent::ButtonReleased(Button::Left)) => {
state.is_dragging = false;
status = Status::Captured;
2022-10-19 10:12:52 -06:00
},
Event::Mouse(MouseEvent::CursorMoved { .. }) => {
if state.is_dragging {
2022-10-31 11:24:36 -06:00
editor.action(Action::Drag {
2022-10-19 10:12:52 -06:00
x: (cursor_position.x - layout.bounds().x) as i32,
y: (cursor_position.y - layout.bounds().y) as i32,
});
status = Status::Captured;
2022-10-18 13:27:58 -06:00
}
},
2022-10-19 10:12:52 -06:00
Event::Mouse(MouseEvent::WheelScrolled { delta }) => match delta {
ScrollDelta::Lines { x, y } => {
2022-10-31 11:24:36 -06:00
editor.action(Action::Scroll {
2022-10-19 10:12:52 -06:00
lines: (-y * 6.0) as i32,
});
status = Status::Captured;
2022-10-19 10:12:52 -06:00
},
_ => (),
},
_ => ()
2022-10-18 12:07:22 -06:00
}
2022-10-19 10:12:52 -06:00
status
2022-10-18 12:07:22 -06:00
}
}
impl<'a, Message, Renderer> From<TextBox<'a>> for Element<'a, Message, Renderer>
where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
2022-10-18 14:35:16 -06:00
Renderer::Theme: StyleSheet,
2022-10-18 12:07:22 -06:00
{
fn from(text_box: TextBox<'a>) -> Self {
Self::new(text_box)
}
}
2022-10-19 10:12:52 -06:00
pub struct State {
is_dragging: bool,
2022-10-27 20:31:05 -06:00
cache: Mutex<SwashCache<'static>>,
2022-10-19 10:12:52 -06:00
}
impl State {
/// Creates a new [`State`].
pub fn new() -> State {
State {
is_dragging: false,
2022-10-27 20:31:05 -06:00
cache: Mutex::new(SwashCache::new(&crate::FONT_SYSTEM)),
}
2022-10-19 10:12:52 -06:00
}
}