2023-02-10 07:42:05 -07:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
2023-02-07 13:00:49 -07:00
|
|
|
|
|
|
|
|
use cosmic::{
|
2023-08-18 09:39:37 -06:00
|
|
|
iced::{
|
2023-02-07 13:00:49 -07:00
|
|
|
event::{Event, Status},
|
2023-11-01 09:43:39 -06:00
|
|
|
keyboard::{Event as KeyEvent, KeyCode, Modifiers},
|
2023-02-07 13:00:49 -07:00
|
|
|
mouse::{self, Button, Event as MouseEvent, ScrollDelta},
|
2023-11-10 11:20:55 -07:00
|
|
|
Color, Element, Length, Padding, Point, Rectangle, Size, Vector,
|
2023-08-18 09:39:37 -06:00
|
|
|
},
|
|
|
|
|
iced_core::{
|
|
|
|
|
clipboard::Clipboard,
|
|
|
|
|
image,
|
|
|
|
|
layout::{self, Layout},
|
2023-11-10 11:20:55 -07:00
|
|
|
renderer::{self, Quad},
|
2023-02-07 13:00:49 -07:00
|
|
|
widget::{self, tree, Widget},
|
2023-08-18 09:39:37 -06:00
|
|
|
Shell,
|
2023-02-07 13:00:49 -07:00
|
|
|
},
|
2023-11-02 12:56:59 -06:00
|
|
|
theme::Theme,
|
2023-02-07 13:00:49 -07:00
|
|
|
};
|
2023-11-02 12:56:59 -06:00
|
|
|
use cosmic_text::{Action, Edit, Metrics, ViEditor};
|
2023-10-31 20:41:52 -06:00
|
|
|
use std::{cell::Cell, cmp, sync::Mutex, time::Instant};
|
2023-02-07 13:00:49 -07:00
|
|
|
|
2023-10-30 10:23:59 -06:00
|
|
|
use crate::{FONT_SYSTEM, SWASH_CACHE};
|
2023-03-17 18:48:56 -06:00
|
|
|
|
2023-02-07 13:00:49 -07:00
|
|
|
pub struct Appearance {
|
2023-11-02 12:56:59 -06:00
|
|
|
pub background_color: Option<Color>,
|
|
|
|
|
pub text_color: Color,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Appearance {
|
|
|
|
|
pub fn dark() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)),
|
|
|
|
|
text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn light() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
background_color: Some(Color::from_rgb8(0xFC, 0xFC, 0xFC)),
|
|
|
|
|
text_color: Color::from_rgb8(0x00, 0x00, 0x00),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait StyleSheet {
|
|
|
|
|
fn appearance(&self) -> Appearance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl StyleSheet for Theme {
|
|
|
|
|
fn appearance(&self) -> Appearance {
|
2023-11-02 12:56:59 -06:00
|
|
|
if self.theme_type.is_dark() {
|
|
|
|
|
Appearance::dark()
|
|
|
|
|
} else {
|
|
|
|
|
Appearance::light()
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 12:56:59 -06:00
|
|
|
pub struct TextBox<'a> {
|
|
|
|
|
editor: &'a Mutex<ViEditor<'static>>,
|
2023-11-03 15:58:26 -06:00
|
|
|
metrics: Metrics,
|
2023-02-07 13:00:49 -07:00
|
|
|
padding: Padding,
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 12:56:59 -06:00
|
|
|
impl<'a> TextBox<'a> {
|
2023-11-03 15:58:26 -06:00
|
|
|
pub fn new(editor: &'a Mutex<ViEditor<'static>>, metrics: Metrics) -> Self {
|
2023-02-07 13:00:49 -07:00
|
|
|
Self {
|
|
|
|
|
editor,
|
2023-11-03 15:58:26 -06:00
|
|
|
metrics,
|
2023-08-18 09:39:37 -06:00
|
|
|
padding: Padding::new(0.0),
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
|
|
|
|
|
self.padding = padding.into();
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-03 15:58:26 -06:00
|
|
|
pub fn text_box<'a>(editor: &'a Mutex<ViEditor<'static>>, metrics: Metrics) -> TextBox<'a> {
|
|
|
|
|
TextBox::new(editor, metrics)
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-10-11 15:52:46 -06:00
|
|
|
//TODO: improve performance
|
|
|
|
|
fn draw_rect(
|
|
|
|
|
buffer: &mut [u32],
|
|
|
|
|
image_w: i32,
|
|
|
|
|
image_h: i32,
|
|
|
|
|
start_x: i32,
|
|
|
|
|
start_y: i32,
|
|
|
|
|
w: i32,
|
|
|
|
|
h: i32,
|
|
|
|
|
color: u32,
|
|
|
|
|
) {
|
|
|
|
|
let alpha = (color >> 24) & 0xFF;
|
|
|
|
|
if alpha == 0 {
|
|
|
|
|
// Do not draw if alpha is zero
|
|
|
|
|
return;
|
|
|
|
|
} else if alpha >= 255 {
|
|
|
|
|
// Handle overwrite
|
|
|
|
|
for y in start_y..start_y + h {
|
|
|
|
|
if y < 0 || y >= image_h {
|
|
|
|
|
// Skip if y out of bounds
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
|
2023-10-11 15:52:46 -06:00
|
|
|
let line_offset = y as usize * image_w as usize;
|
|
|
|
|
for x in start_x..start_x + w {
|
|
|
|
|
if x < 0 || x >= image_w {
|
|
|
|
|
// Skip if x out of bounds
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let offset = line_offset + x as usize;
|
|
|
|
|
buffer[offset] = color;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
} else {
|
|
|
|
|
let n_alpha = 255 - alpha;
|
2023-10-11 15:52:46 -06:00
|
|
|
for y in start_y..start_y + h {
|
|
|
|
|
if y < 0 || y >= image_h {
|
|
|
|
|
// Skip if y out of bounds
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let line_offset = y as usize * image_w as usize;
|
|
|
|
|
for x in start_x..start_x + w {
|
|
|
|
|
if x < 0 || x >= image_w {
|
|
|
|
|
// Skip if x out of bounds
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
|
2023-10-11 15:52:46 -06:00
|
|
|
// Alpha blend with current value
|
|
|
|
|
let offset = line_offset + x as usize;
|
|
|
|
|
let current = buffer[offset];
|
2023-11-02 12:56:59 -06:00
|
|
|
if current & 0xFF000000 == 0 {
|
|
|
|
|
// Overwrite if buffer empty
|
|
|
|
|
buffer[offset] = color;
|
|
|
|
|
} else {
|
|
|
|
|
let rb =
|
|
|
|
|
((n_alpha * (current & 0x00FF00FF)) + (alpha * (color & 0x00FF00FF))) >> 8;
|
|
|
|
|
let ag = (n_alpha * ((current & 0xFF00FF00) >> 8))
|
|
|
|
|
+ (alpha * (0x01000000 | ((color & 0x0000FF00) >> 8)));
|
|
|
|
|
buffer[offset] = (rb & 0x00FF00FF) | (ag & 0xFF00FF00);
|
|
|
|
|
}
|
2023-10-11 15:52:46 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-02 12:56:59 -06:00
|
|
|
impl<'a, 'editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a>
|
2023-02-07 13:00:49 -07:00
|
|
|
where
|
|
|
|
|
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
|
|
|
|
|
Renderer::Theme: StyleSheet,
|
|
|
|
|
{
|
|
|
|
|
fn tag(&self) -> tree::Tag {
|
|
|
|
|
tree::Tag::of::<State>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state(&self) -> tree::State {
|
|
|
|
|
tree::State::new(State::new())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn width(&self) -> Length {
|
|
|
|
|
Length::Fill
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn height(&self) -> Length {
|
|
|
|
|
Length::Fill
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn layout(&self, _renderer: &Renderer, limits: &layout::Limits) -> layout::Node {
|
|
|
|
|
let limits = limits.width(Length::Fill).height(Length::Fill);
|
|
|
|
|
|
|
|
|
|
let mut editor = self.editor.lock().unwrap();
|
2023-11-02 10:02:07 -06:00
|
|
|
//TODO: set size?
|
2023-03-17 18:48:56 -06:00
|
|
|
editor
|
|
|
|
|
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
|
2023-11-02 10:02:07 -06:00
|
|
|
.shape_as_needed();
|
2023-02-07 13:00:49 -07:00
|
|
|
|
|
|
|
|
let mut layout_lines = 0;
|
|
|
|
|
for line in editor.buffer().lines.iter() {
|
|
|
|
|
match line.layout_opt() {
|
|
|
|
|
Some(layout) => layout_lines += layout.len(),
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 18:48:56 -06:00
|
|
|
let height = layout_lines as f32 * editor.buffer().metrics().line_height;
|
2023-02-07 13:00:49 -07:00
|
|
|
let size = Size::new(limits.max().width, height);
|
|
|
|
|
|
|
|
|
|
layout::Node::new(limits.resolve(size))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mouse_interaction(
|
|
|
|
|
&self,
|
2023-11-10 11:20:55 -07:00
|
|
|
tree: &widget::Tree,
|
2023-02-07 13:00:49 -07:00
|
|
|
layout: Layout<'_>,
|
2023-08-18 09:39:37 -06:00
|
|
|
cursor_position: mouse::Cursor,
|
2023-02-07 13:00:49 -07:00
|
|
|
_viewport: &Rectangle,
|
|
|
|
|
_renderer: &Renderer,
|
|
|
|
|
) -> mouse::Interaction {
|
2023-11-10 11:20:55 -07:00
|
|
|
let state = tree.state.downcast_ref::<State>();
|
|
|
|
|
|
|
|
|
|
match &state.dragging {
|
|
|
|
|
Some(Dragging::Scrollbar { .. }) => return mouse::Interaction::Idle,
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(p) = cursor_position.position_in(layout.bounds()) {
|
|
|
|
|
let scale_factor = state.scale_factor.get();
|
|
|
|
|
let editor = self.editor.lock().unwrap();
|
|
|
|
|
let buffer_size = editor.buffer().size();
|
|
|
|
|
|
|
|
|
|
let x = (p.x - self.padding.left) * scale_factor;
|
|
|
|
|
let y = (p.y - self.padding.top) * scale_factor;
|
|
|
|
|
if x >= 0.0 && x < buffer_size.0 && y >= 0.0 && y < buffer_size.1 {
|
|
|
|
|
return mouse::Interaction::Text;
|
|
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
2023-11-10 11:20:55 -07:00
|
|
|
|
|
|
|
|
mouse::Interaction::Idle
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &widget::Tree,
|
|
|
|
|
renderer: &mut Renderer,
|
|
|
|
|
theme: &Renderer::Theme,
|
2023-03-24 07:55:07 -06:00
|
|
|
style: &renderer::Style,
|
2023-02-07 13:00:49 -07:00
|
|
|
layout: Layout<'_>,
|
2023-08-18 09:39:37 -06:00
|
|
|
_cursor_position: mouse::Cursor,
|
2023-02-07 13:00:49 -07:00
|
|
|
viewport: &Rectangle,
|
|
|
|
|
) {
|
2023-03-24 07:55:07 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
2023-02-07 13:00:49 -07:00
|
|
|
let state = tree.state.downcast_ref::<State>();
|
|
|
|
|
|
|
|
|
|
let appearance = theme.appearance();
|
|
|
|
|
|
|
|
|
|
if let Some(background_color) = appearance.background_color {
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds: layout.bounds(),
|
|
|
|
|
border_radius: 0.0.into(),
|
|
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
background_color,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 editor = self.editor.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32)
|
|
|
|
|
- self.padding.horizontal() as i32;
|
|
|
|
|
let view_h = cmp::min(viewport.height as i32, layout.bounds().height as i32)
|
|
|
|
|
- self.padding.vertical() as i32;
|
2023-03-17 18:48:56 -06:00
|
|
|
|
2023-11-10 11:20:55 -07:00
|
|
|
let scale_factor = style.scale_factor as f32;
|
2023-08-18 09:39:37 -06:00
|
|
|
|
2023-11-10 11:20:55 -07:00
|
|
|
let image_w = (view_w as f32 * scale_factor) as i32;
|
|
|
|
|
let image_h = (view_h as f32 * scale_factor) as i32;
|
|
|
|
|
|
|
|
|
|
//TODO: make this configurable and do not repeat
|
2023-10-31 09:34:34 -06:00
|
|
|
let scrollbar_w = (8.0 * scale_factor) as i32;
|
2023-10-20 13:12:26 -06:00
|
|
|
|
2023-10-31 09:34:34 -06:00
|
|
|
if image_w <= scrollbar_w || image_h <= 0 {
|
2023-10-11 19:32:04 -06:00
|
|
|
// Zero sized image
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-24 07:55:07 -06:00
|
|
|
|
2023-11-10 11:20:55 -07:00
|
|
|
// Adjust image width by scrollbar width
|
|
|
|
|
let image_w = image_w - scrollbar_w;
|
|
|
|
|
|
2023-03-17 18:48:56 -06:00
|
|
|
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
|
|
|
|
let mut editor = editor.borrow_with(&mut font_system);
|
|
|
|
|
|
2023-10-31 20:41:52 -06:00
|
|
|
// Set metrics and size
|
|
|
|
|
editor.buffer_mut().set_metrics_and_size(
|
2023-11-10 11:20:55 -07:00
|
|
|
self.metrics.scale(scale_factor),
|
2023-10-31 20:41:52 -06:00
|
|
|
image_w as f32,
|
|
|
|
|
image_h as f32,
|
|
|
|
|
);
|
2023-02-07 13:00:49 -07:00
|
|
|
|
2023-10-31 20:41:52 -06:00
|
|
|
// Shape and layout as needed
|
2023-03-24 07:55:07 -06:00
|
|
|
editor.shape_as_needed();
|
2023-02-07 13:00:49 -07:00
|
|
|
|
2023-10-11 18:54:29 -06:00
|
|
|
if editor.buffer().redraw() {
|
|
|
|
|
// Draw to pixel buffer
|
|
|
|
|
let mut pixels = vec![0; image_w as usize * image_h as usize * 4];
|
|
|
|
|
{
|
|
|
|
|
let buffer = unsafe {
|
|
|
|
|
std::slice::from_raw_parts_mut(
|
|
|
|
|
pixels.as_mut_ptr() as *mut u32,
|
|
|
|
|
pixels.len() / 4,
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
editor.draw(
|
2023-10-30 10:23:59 -06:00
|
|
|
&mut SWASH_CACHE.lock().unwrap(),
|
2023-10-11 18:54:29 -06:00
|
|
|
text_color,
|
|
|
|
|
|x, y, w, h, color| {
|
|
|
|
|
draw_rect(buffer, image_w, image_h, x, y, w as i32, h as i32, color.0);
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-10-31 09:34:34 -06:00
|
|
|
|
2023-11-10 11:20:55 -07:00
|
|
|
// Calculate scrollbar
|
2023-10-31 09:34:34 -06:00
|
|
|
{
|
|
|
|
|
let mut start_line_opt = None;
|
|
|
|
|
let mut end_line = 0;
|
|
|
|
|
for run in editor.buffer().layout_runs() {
|
|
|
|
|
end_line = run.line_i;
|
|
|
|
|
if start_line_opt.is_none() {
|
|
|
|
|
start_line_opt = Some(end_line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let start_line = start_line_opt.unwrap_or(end_line);
|
|
|
|
|
let lines = editor.buffer().lines.len();
|
|
|
|
|
let start_y = (start_line * image_h as usize) / lines;
|
2023-11-02 10:02:07 -06:00
|
|
|
let end_y = ((end_line * image_h as usize) / lines).max(start_y + 1);
|
2023-11-10 11:20:55 -07:00
|
|
|
|
|
|
|
|
let rect = Rectangle::new(
|
|
|
|
|
[image_w as f32 / scale_factor, start_y as f32 / scale_factor].into(),
|
|
|
|
|
Size::new(
|
|
|
|
|
scrollbar_w as f32 / scale_factor,
|
|
|
|
|
(end_y as f32 - start_y as f32) / scale_factor,
|
|
|
|
|
),
|
2023-11-02 10:02:07 -06:00
|
|
|
);
|
2023-11-10 11:20:55 -07:00
|
|
|
state.scrollbar_rect.set(rect);
|
2023-10-31 09:34:34 -06:00
|
|
|
}
|
2023-10-11 18:54:29 -06:00
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
|
2023-10-11 18:54:29 -06:00
|
|
|
// Clear redraw flag
|
|
|
|
|
editor.buffer_mut().set_redraw(false);
|
|
|
|
|
|
2023-10-31 20:41:52 -06:00
|
|
|
state.scale_factor.set(scale_factor);
|
2023-10-11 18:54:29 -06:00
|
|
|
*state.handle.lock().unwrap() =
|
|
|
|
|
image::Handle::from_pixels(image_w as u32, image_h as u32, pixels);
|
|
|
|
|
}
|
2023-03-24 07:55:07 -06:00
|
|
|
|
2023-10-11 18:54:29 -06:00
|
|
|
let handle = state.handle.lock().unwrap().clone();
|
2023-11-10 11:20:55 -07:00
|
|
|
let image_position =
|
|
|
|
|
layout.position() + [self.padding.left as f32, self.padding.top as f32].into();
|
|
|
|
|
let image_size = image::Renderer::dimensions(renderer, &handle);
|
2023-02-07 13:00:49 -07:00
|
|
|
image::Renderer::draw(
|
|
|
|
|
renderer,
|
|
|
|
|
handle,
|
|
|
|
|
Rectangle::new(
|
2023-11-10 11:20:55 -07:00
|
|
|
image_position,
|
2023-11-12 18:42:32 -07:00
|
|
|
Size::new(
|
|
|
|
|
image_size.width as f32 / scale_factor,
|
|
|
|
|
image_size.height as f32 / scale_factor,
|
|
|
|
|
),
|
2023-02-07 13:00:49 -07:00
|
|
|
),
|
2023-11-03 15:58:26 -06:00
|
|
|
[0.0; 4],
|
2023-02-07 13:00:49 -07:00
|
|
|
);
|
|
|
|
|
|
2023-11-10 11:20:55 -07:00
|
|
|
// Draw scrollbar
|
|
|
|
|
let scrollbar_alpha = match &state.dragging {
|
|
|
|
|
Some(Dragging::Scrollbar { .. }) => 0.5,
|
|
|
|
|
_ => 0.25,
|
|
|
|
|
};
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
Quad {
|
|
|
|
|
bounds: state.scrollbar_rect.get()
|
|
|
|
|
+ Vector::new(image_position.x, image_position.y),
|
|
|
|
|
border_radius: 0.0.into(),
|
|
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
Color::new(1.0, 1.0, 1.0, scrollbar_alpha),
|
|
|
|
|
);
|
|
|
|
|
|
2023-02-07 13:00:49 -07:00
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
log::debug!("redraw {}, {}: {:?}", view_w, view_h, duration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_event(
|
|
|
|
|
&mut self,
|
|
|
|
|
tree: &mut widget::Tree,
|
|
|
|
|
event: Event,
|
|
|
|
|
layout: Layout<'_>,
|
2023-08-18 09:39:37 -06:00
|
|
|
cursor_position: mouse::Cursor,
|
2023-02-07 13:00:49 -07:00
|
|
|
_renderer: &Renderer,
|
|
|
|
|
_clipboard: &mut dyn Clipboard,
|
|
|
|
|
_shell: &mut Shell<'_, Message>,
|
2023-10-11 14:15:46 -06:00
|
|
|
_viewport: &Rectangle<f32>,
|
2023-02-07 13:00:49 -07:00
|
|
|
) -> Status {
|
|
|
|
|
let state = tree.state.downcast_mut::<State>();
|
2023-11-10 11:20:55 -07:00
|
|
|
let scale_factor = state.scale_factor.get();
|
|
|
|
|
let scrollbar_rect = state.scrollbar_rect.get();
|
2023-02-07 13:00:49 -07:00
|
|
|
let mut editor = self.editor.lock().unwrap();
|
2023-11-10 11:20:55 -07:00
|
|
|
let buffer_size = editor.buffer().size();
|
2023-03-17 18:48:56 -06:00
|
|
|
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
|
|
|
|
let mut editor = editor.borrow_with(&mut font_system);
|
2023-02-07 13:00:49 -07:00
|
|
|
|
|
|
|
|
let mut status = Status::Ignored;
|
|
|
|
|
match event {
|
|
|
|
|
Event::Keyboard(KeyEvent::KeyPressed {
|
|
|
|
|
key_code,
|
|
|
|
|
modifiers,
|
|
|
|
|
}) => match key_code {
|
|
|
|
|
KeyCode::Left => {
|
|
|
|
|
editor.action(Action::Left);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::Right => {
|
|
|
|
|
editor.action(Action::Right);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::Up => {
|
|
|
|
|
editor.action(Action::Up);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::Down => {
|
|
|
|
|
editor.action(Action::Down);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::Home => {
|
|
|
|
|
editor.action(Action::Home);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::End => {
|
|
|
|
|
editor.action(Action::End);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::PageUp => {
|
|
|
|
|
editor.action(Action::PageUp);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::PageDown => {
|
|
|
|
|
editor.action(Action::PageDown);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::Escape => {
|
|
|
|
|
editor.action(Action::Escape);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::Enter => {
|
|
|
|
|
editor.action(Action::Enter);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::Backspace => {
|
|
|
|
|
editor.action(Action::Backspace);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::Delete => {
|
|
|
|
|
editor.action(Action::Delete);
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
2023-11-01 13:33:10 -06:00
|
|
|
KeyCode::Tab => {
|
|
|
|
|
if modifiers.shift() {
|
|
|
|
|
editor.action(Action::Unindent);
|
|
|
|
|
} else {
|
|
|
|
|
editor.action(Action::Indent);
|
|
|
|
|
}
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
_ => (),
|
|
|
|
|
},
|
2023-11-01 09:43:39 -06:00
|
|
|
Event::Keyboard(KeyEvent::ModifiersChanged(modifiers)) => {
|
|
|
|
|
state.modifiers = modifiers;
|
|
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
Event::Keyboard(KeyEvent::CharacterReceived(character)) => {
|
2023-11-01 09:43:39 -06:00
|
|
|
// Only parse keys when Super, Ctrl, and Alt are not pressed
|
|
|
|
|
if !state.modifiers.logo() && !state.modifiers.control() && !state.modifiers.alt() {
|
2023-11-08 11:45:46 -07:00
|
|
|
if !character.is_control() {
|
|
|
|
|
editor.action(Action::Insert(character));
|
2023-10-20 13:12:26 -06:00
|
|
|
}
|
2023-11-01 09:43:39 -06:00
|
|
|
status = Status::Captured;
|
2023-10-20 13:12:26 -06:00
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => {
|
2023-08-18 09:39:37 -06:00
|
|
|
if let Some(p) = cursor_position.position_in(layout.bounds()) {
|
2023-11-10 11:20:55 -07:00
|
|
|
let x = (p.x - self.padding.left) * scale_factor;
|
|
|
|
|
let y = (p.y - self.padding.top) * scale_factor;
|
|
|
|
|
if x >= 0.0 && x < buffer_size.0 && y >= 0.0 && y < buffer_size.1 {
|
|
|
|
|
editor.action(Action::Click {
|
|
|
|
|
x: x as i32,
|
|
|
|
|
y: y as i32,
|
|
|
|
|
});
|
|
|
|
|
state.dragging = Some(Dragging::Buffer);
|
|
|
|
|
} else if scrollbar_rect.contains(Point::new(x, y)) {
|
|
|
|
|
state.dragging = Some(Dragging::Scrollbar {
|
|
|
|
|
start_y: y,
|
|
|
|
|
start_scroll: editor.buffer().scroll(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
//TODO: support clicking below or above scrollbar
|
2023-02-07 13:00:49 -07:00
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Event::Mouse(MouseEvent::ButtonReleased(Button::Left)) => {
|
2023-11-10 11:20:55 -07:00
|
|
|
state.dragging = None;
|
2023-02-07 13:00:49 -07:00
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
Event::Mouse(MouseEvent::CursorMoved { .. }) => {
|
2023-11-10 11:20:55 -07:00
|
|
|
if let Some(dragging) = &state.dragging {
|
2023-08-18 09:39:37 -06:00
|
|
|
if let Some(p) = cursor_position.position() {
|
2023-11-10 11:20:55 -07:00
|
|
|
let x = ((p.x - layout.bounds().x) - self.padding.left) * scale_factor;
|
|
|
|
|
let y = ((p.y - layout.bounds().y) - self.padding.top) * scale_factor;
|
|
|
|
|
match dragging {
|
|
|
|
|
Dragging::Buffer => {
|
|
|
|
|
editor.action(Action::Drag {
|
|
|
|
|
x: x as i32,
|
|
|
|
|
y: y as i32,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
Dragging::Scrollbar {
|
|
|
|
|
start_y,
|
|
|
|
|
start_scroll,
|
|
|
|
|
} => {
|
|
|
|
|
let mut buffer = editor.buffer_mut();
|
|
|
|
|
let scroll_offset = (((y - start_y) / buffer.size().1)
|
|
|
|
|
* buffer.lines.len() as f32)
|
|
|
|
|
as i32;
|
|
|
|
|
buffer.set_scroll(start_scroll + scroll_offset);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-18 09:39:37 -06:00
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Event::Mouse(MouseEvent::WheelScrolled { delta }) => match delta {
|
|
|
|
|
ScrollDelta::Lines { x, y } => {
|
2023-11-13 09:49:52 -07:00
|
|
|
//TODO: this adjustment is just a guess!
|
|
|
|
|
state.scroll_pixels = 0.0;
|
|
|
|
|
let lines = (-y * 6.0) as i32;
|
|
|
|
|
if lines != 0 {
|
|
|
|
|
editor.action(Action::Scroll { lines });
|
|
|
|
|
}
|
|
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
ScrollDelta::Pixels { x, y } => {
|
|
|
|
|
//TODO: this adjustment is just a guess!
|
|
|
|
|
state.scroll_pixels -= y * 6.0;
|
|
|
|
|
let mut lines = 0;
|
|
|
|
|
let metrics = editor.buffer().metrics();
|
|
|
|
|
while state.scroll_pixels <= -metrics.line_height {
|
|
|
|
|
lines -= 1;
|
|
|
|
|
state.scroll_pixels += metrics.line_height;
|
|
|
|
|
}
|
|
|
|
|
while state.scroll_pixels >= metrics.line_height {
|
|
|
|
|
lines += 1;
|
|
|
|
|
state.scroll_pixels -= metrics.line_height;
|
|
|
|
|
}
|
|
|
|
|
if lines != 0 {
|
|
|
|
|
editor.action(Action::Scroll { lines });
|
|
|
|
|
}
|
2023-02-07 13:00:49 -07:00
|
|
|
status = Status::Captured;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 12:56:59 -06:00
|
|
|
impl<'a, 'editor, Message, Renderer> From<TextBox<'a>> for Element<'a, Message, Renderer>
|
2023-02-07 13:00:49 -07:00
|
|
|
where
|
|
|
|
|
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
|
|
|
|
|
Renderer::Theme: StyleSheet,
|
|
|
|
|
{
|
2023-11-02 12:56:59 -06:00
|
|
|
fn from(text_box: TextBox<'a>) -> Self {
|
2023-02-07 13:00:49 -07:00
|
|
|
Self::new(text_box)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 11:20:55 -07:00
|
|
|
enum Dragging {
|
|
|
|
|
Buffer,
|
|
|
|
|
Scrollbar { start_y: f32, start_scroll: i32 },
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 13:00:49 -07:00
|
|
|
pub struct State {
|
2023-11-01 09:43:39 -06:00
|
|
|
modifiers: Modifiers,
|
2023-11-10 11:20:55 -07:00
|
|
|
dragging: Option<Dragging>,
|
|
|
|
|
scale_factor: Cell<f32>,
|
2023-11-13 09:49:52 -07:00
|
|
|
scroll_pixels: f32,
|
2023-11-10 11:20:55 -07:00
|
|
|
scrollbar_rect: Cell<Rectangle<f32>>,
|
2023-10-11 18:54:29 -06:00
|
|
|
handle: Mutex<image::Handle>,
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
|
/// Creates a new [`State`].
|
|
|
|
|
pub fn new() -> State {
|
|
|
|
|
State {
|
2023-11-01 09:43:39 -06:00
|
|
|
modifiers: Modifiers::empty(),
|
2023-11-10 11:20:55 -07:00
|
|
|
dragging: None,
|
2023-10-31 20:41:52 -06:00
|
|
|
scale_factor: Cell::new(1.0),
|
2023-11-13 09:49:52 -07:00
|
|
|
scroll_pixels: 0.0,
|
2023-11-10 11:20:55 -07:00
|
|
|
scrollbar_rect: Cell::new(Rectangle::default()),
|
2023-10-11 18:54:29 -06:00
|
|
|
//TODO: make option!
|
|
|
|
|
handle: Mutex::new(image::Handle::from_pixels(1, 1, vec![0, 0, 0, 0])),
|
2023-02-07 13:00:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|