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

358 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-10-18 12:07:22 -06:00
use cosmic::iced_native::{
{Color, Element, Length, Point, Rectangle, Shell, Theme},
2022-10-18 12:07:22 -06:00
clipboard::Clipboard,
2022-10-27 17:39:47 -06:00
event::{Event, Status},
image,
2022-10-18 12:07:22 -06:00
keyboard::{Event as KeyEvent, KeyCode},
layout::{self, Layout},
2022-10-19 09:26:43 -06:00
mouse::{self, Button, Event as MouseEvent, ScrollDelta},
2022-10-18 12:07:22 -06:00
renderer,
2022-10-19 10:12:52 -06:00
widget::{self, tree, Widget},
2022-10-18 12:07:22 -06:00
};
use cosmic_text::{
2022-10-25 11:40:10 -06:00
SwashCache,
2022-10-18 12:07:22 -06:00
TextAction,
TextBuffer,
};
use std::{
cmp,
sync::Mutex,
2022-10-18 12:07:22 -06:00
time::Instant,
};
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> {
buffer: &'a Mutex<TextBuffer<'static>>,
2022-10-18 12:07:22 -06:00
}
impl<'a> TextBox<'a> {
2022-10-27 20:31:05 -06:00
pub fn new(buffer: &'a Mutex<TextBuffer<'static>>) -> Self {
Self {
buffer,
}
2022-10-18 12:07:22 -06:00
}
}
2022-10-27 20:31:05 -06:00
pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>) -> TextBox<'a> {
TextBox::new(buffer)
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 {
layout::Node::new(limits.max())
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,
_viewport: &Rectangle,
) {
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,
);
let mut pixels_opt = state.pixels_opt.lock().unwrap();
let mut buffer = self.buffer.lock().unwrap();
let layout_w = layout.bounds().width as i32;
let layout_h = layout.bounds().height as i32;
buffer.set_size(layout_w, layout_h);
if buffer.cursor_moved {
buffer.shape_until_cursor();
buffer.cursor_moved = false;
} else {
buffer.shape_until_scroll();
}
//TODO: redraw on color change
if buffer.redraw || pixels_opt.is_none() {
// Redraw buffer to image
let instant = Instant::now();
let mut pixels = vec![0; layout_w as usize * layout_h as usize * 4];
2022-10-27 20:31:05 -06:00
buffer.draw(&mut state.cache.lock().unwrap(), text_color, |start_x, start_y, w, h, color| {
let alpha = (color.0 >> 24) & 0xFF;
if alpha == 0 {
// Do not draw if alpha is zero
return;
}
for y in start_y..start_y + h as i32{
if y < 0 || y >= layout_h {
// Skip if y out of bounds
continue;
}
let offset_y = y as usize * layout_w as usize * 4;
for x in start_x..start_x + w as i32 {
if x < 0 || x >= layout_w {
// Skip if x out of bounds
continue;
}
let offset = offset_y + x as usize * 4;
let mut current =
pixels[offset] as u32 |
(pixels[offset + 1] as u32) << 8 |
(pixels[offset + 2] as u32) << 16 |
(pixels[offset + 3] as u32) << 24;
if alpha >= 255 || current == 0 {
// Alpha is 100% or current is null, replace with no blending
current = color.0;
} else {
// Alpha blend with current value
let n_alpha = 255 - alpha;
let rb = ((n_alpha * (current & 0x00FF00FF)) + (alpha * (color.0 & 0x00FF00FF))) >> 8;
let ag = (n_alpha * ((current & 0xFF00FF00) >> 8))
+ (alpha * (0x01000000 | ((color.0 & 0x0000FF00) >> 8)));
current = (rb & 0x00FF00FF) | (ag & 0xFF00FF00);
}
pixels[offset] = current as u8;
pixels[offset + 1] = (current >> 8) as u8;
pixels[offset + 2] = (current >> 16) as u8;
pixels[offset + 3] = (current >> 24) as u8;
}
}
});
*pixels_opt = Some((layout_w as u32, layout_h as u32, pixels));
buffer.redraw = false;
let duration = instant.elapsed();
log::debug!("redraw: {:?}", duration);
}
if let Some((w, h, pixels)) = &*pixels_opt {
let handle = image::Handle::from_pixels(*w, *h, pixels.clone());
image::Renderer::draw(renderer, handle, layout.bounds());
2022-10-25 20:49:15 -06:00
}
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-18 13:12:25 -06:00
let mut buffer = self.buffer.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 => {
buffer.action(TextAction::Left);
status = Status::Captured;
2022-10-18 12:07:22 -06:00
},
2022-10-19 10:12:52 -06:00
KeyCode::Right => {
buffer.action(TextAction::Right);
status = Status::Captured;
2022-10-18 12:07:22 -06:00
},
2022-10-19 10:12:52 -06:00
KeyCode::Up => {
buffer.action(TextAction::Up);
status = Status::Captured;
},
2022-10-19 10:12:52 -06:00
KeyCode::Down => {
buffer.action(TextAction::Down);
status = Status::Captured;
2022-10-19 10:12:52 -06:00
},
2022-10-19 11:08:15 -06:00
KeyCode::Home => {
buffer.action(TextAction::Home);
status = Status::Captured;
2022-10-19 11:08:15 -06:00
},
KeyCode::End => {
buffer.action(TextAction::End);
status = Status::Captured;
2022-10-19 11:08:15 -06:00
},
2022-10-19 10:12:52 -06:00
KeyCode::PageUp => {
buffer.action(TextAction::PageUp);
status = Status::Captured;
2022-10-19 10:12:52 -06:00
},
KeyCode::PageDown => {
buffer.action(TextAction::PageDown);
status = Status::Captured;
2022-10-19 10:12:52 -06:00
},
2022-10-19 11:33:35 -06:00
KeyCode::Enter => {
buffer.action(TextAction::Enter);
status = Status::Captured;
2022-10-19 11:33:35 -06:00
},
KeyCode::Backspace => {
buffer.action(TextAction::Backspace);
status = Status::Captured;
2022-10-19 11:33:35 -06:00
},
KeyCode::Delete => {
buffer.action(TextAction::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)) => {
buffer.action(TextAction::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) {
buffer.action(TextAction::Click {
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 {
buffer.action(TextAction::Drag {
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 } => {
buffer.action(TextAction::Scroll {
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>>,
pixels_opt: Mutex<Option<(u32, u32, Vec<u8>)>>,
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)),
pixels_opt: Mutex::new(None),
}
2022-10-19 10:12:52 -06:00
}
}