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

330 lines
9.7 KiB
Rust
Raw Normal View History

2022-10-18 12:07:22 -06:00
use cosmic::iced_native::{
2022-10-18 14:35:16 -06:00
{Color, Element, Length, Point, Rectangle, Size, Shell, Theme},
2022-10-18 12:07:22 -06:00
clipboard::Clipboard,
event::{
Event,
Status,
},
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::{
TextAction,
TextBuffer,
};
use std::{
2022-10-18 14:35:16 -06:00
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,
}
impl Appearance {
fn text_color_u32(&self) -> u32 {
let channel = |f: f32, shift: i32| -> u32 {
(cmp::max(0, cmp::min(255, (f * 255.0) as i32)) << shift) as u32
};
channel(self.text_color.b, 0) |
channel(self.text_color.g, 8) |
channel(self.text_color.r, 16) |
channel(self.text_color.a, 24)
}
}
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),
},
Theme::Light => Appearance {
background_color: Some(Color::from_rgb8(0xFC, 0xFC, 0xFC)),
text_color: Color::from_rgb8(0x00, 0x00, 0x00),
},
}
}
}
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> {
pub fn new(buffer: &'a Mutex<TextBuffer<'static>>) -> Self {
2022-10-18 12:07:22 -06:00
Self { buffer }
}
}
pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>) -> TextBox<'a> {
2022-10-18 12:07:22 -06:00
TextBox::new(buffer)
}
impl<'a, Message, Renderer> Widget<Message, Renderer> for TextBox<'a>
where
Renderer: renderer::Renderer,
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 {
println!("{:?}", limits);
let size = limits.max();
{
2022-10-18 13:12:25 -06:00
let mut buffer = self.buffer.lock().unwrap();
2022-10-18 12:07:22 -06:00
2022-10-18 12:42:37 -06:00
buffer.set_size(size.width as i32, size.height as i32);
2022-10-18 12:07:22 -06:00
}
layout::Node::new(size)
}
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,
_state: &widget::Tree,
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-18 14:35:16 -06:00
let appearance = theme.appearance();
let text_color_u32 = appearance.text_color_u32();
2022-10-18 13:12:25 -06:00
let buffer = self.buffer.lock().unwrap();
2022-10-18 12:07:22 -06:00
let instant = Instant::now();
2022-10-18 14:35:16 -06:00
if let Some(background_color) = appearance.background_color {
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
2022-10-21 12:31:02 -06:00
buffer.shape_until_cursor();
let buffer_x = layout.bounds().x;
let buffer_y = layout.bounds().y;
buffer.draw(text_color_u32, |x, y, w, h, color| {
let a = (color >> 24) as u8;
if a > 0 {
let r = (color >> 16) as u8;
let g = (color >> 8) as u8;
let b = color as u8;
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle::new(
Point::new(buffer_x + x as f32, buffer_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(r, g, b, a as f32 / 255.0),
);
2022-10-18 12:07:22 -06:00
}
});
2022-10-18 12:07:22 -06:00
/*
// Draw scrollbar
{
let start_line = start_line_opt.unwrap_or(end_line);
let lines = buffer.text_lines().len();
let start_y = (start_line.get() * window.height() as usize) / lines;
let end_y = (end_line.get() * 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,
Color::from_rgba8(0xFF, 0xFF, 0xFF, 0.25),
);
}
}
buffer.redraw = false;
*/
let duration = instant.elapsed();
2022-10-19 13:29:50 -06:00
log::trace!("redraw: {:?}", 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-18 13:12:25 -06:00
let mut buffer = self.buffer.lock().unwrap();
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);
return Status::Captured;
2022-10-18 12:07:22 -06:00
},
2022-10-19 10:12:52 -06:00
KeyCode::Right => {
buffer.action(TextAction::Right);
return Status::Captured;
2022-10-18 12:07:22 -06:00
},
2022-10-19 10:12:52 -06:00
KeyCode::Up => {
buffer.action(TextAction::Up);
return Status::Captured;
},
2022-10-19 10:12:52 -06:00
KeyCode::Down => {
buffer.action(TextAction::Down);
return Status::Captured;
},
2022-10-19 11:08:15 -06:00
KeyCode::Home => {
buffer.action(TextAction::Home);
return Status::Captured;
},
KeyCode::End => {
buffer.action(TextAction::End);
return Status::Captured;
},
2022-10-19 10:12:52 -06:00
KeyCode::PageUp => {
buffer.action(TextAction::PageUp);
return Status::Captured;
},
KeyCode::PageDown => {
buffer.action(TextAction::PageDown);
return Status::Captured;
},
2022-10-19 11:33:35 -06:00
KeyCode::Enter => {
buffer.action(TextAction::Enter);
return Status::Captured;
},
KeyCode::Backspace => {
buffer.action(TextAction::Backspace);
return Status::Captured;
},
KeyCode::Delete => {
buffer.action(TextAction::Delete);
return Status::Captured;
},
2022-10-19 10:12:52 -06:00
_ => ()
},
Event::Keyboard(KeyEvent::CharacterReceived(character)) => {
buffer.action(TextAction::Insert(character));
return Status::Captured;
},
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;
return Status::Captured;
}
},
Event::Mouse(MouseEvent::ButtonReleased(Button::Left)) => {
state.is_dragging = false;
return Status::Captured;
},
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,
});
return 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,
});
return Status::Captured;
},
_ => (),
},
_ => ()
2022-10-18 12:07:22 -06:00
}
2022-10-19 10:12:52 -06:00
Status::Ignored
2022-10-18 12:07:22 -06:00
}
}
impl<'a, Message, Renderer> From<TextBox<'a>> for Element<'a, Message, Renderer>
where
Renderer: renderer::Renderer,
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
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct State {
is_dragging: bool,
}
impl State {
/// Creates a new [`State`].
pub fn new() -> State {
State::default()
}
}