Initialize with code from cosmic-text
This commit is contained in:
parent
6571739f77
commit
5cae802775
5 changed files with 4904 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
||||||
4276
Cargo.lock
generated
Normal file
4276
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
31
Cargo.toml
Normal file
31
Cargo.toml
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
[package]
|
||||||
|
name = "cosmic-text-editor"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Jeremy Soller <jeremy@system76.com>"]
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
env_logger = "0.9"
|
||||||
|
fontdb = "0.9"
|
||||||
|
lazy_static = "1.4"
|
||||||
|
log = "0.4"
|
||||||
|
|
||||||
|
[dependencies.cosmic-text]
|
||||||
|
version = "0.7"
|
||||||
|
features = ["syntect"]
|
||||||
|
|
||||||
|
[dependencies.libcosmic]
|
||||||
|
git = "https://github.com/pop-os/libcosmic"
|
||||||
|
rev = "843919e44f0a00c33c29358359be5b4bfa41ab00"
|
||||||
|
default-features = false
|
||||||
|
features = ["winit_softbuffer"]
|
||||||
|
|
||||||
|
[dependencies.rfd]
|
||||||
|
version = "0.10"
|
||||||
|
#TODO: iced portal
|
||||||
|
#default-features = false
|
||||||
|
#features = ["xdg-portal"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
vi = ["cosmic-text/vi"]
|
||||||
185
src/main.rs
Normal file
185
src/main.rs
Normal file
|
|
@ -0,0 +1,185 @@
|
||||||
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
|
||||||
|
use cosmic::{
|
||||||
|
iced::{
|
||||||
|
self,
|
||||||
|
widget::{column, horizontal_space, pick_list, row},
|
||||||
|
Alignment, Application, Color, Command, Length,
|
||||||
|
},
|
||||||
|
settings,
|
||||||
|
theme::{self, Theme},
|
||||||
|
widget::{button, toggler},
|
||||||
|
Element,
|
||||||
|
};
|
||||||
|
use cosmic_text::{
|
||||||
|
Attrs, AttrsList, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem, Wrap,
|
||||||
|
};
|
||||||
|
use std::{env, fs, path::PathBuf, sync::Mutex};
|
||||||
|
|
||||||
|
use self::text_box::text_box;
|
||||||
|
mod text_box;
|
||||||
|
|
||||||
|
lazy_static::lazy_static! {
|
||||||
|
static ref FONT_SYSTEM: FontSystem = FontSystem::new();
|
||||||
|
static ref SYNTAX_SYSTEM: SyntaxSystem = SyntaxSystem::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
static FONT_SIZES: &'static [Metrics] = &[
|
||||||
|
Metrics::new(10, 14), // Caption
|
||||||
|
Metrics::new(14, 20), // Body
|
||||||
|
Metrics::new(20, 28), // Title 4
|
||||||
|
Metrics::new(24, 32), // Title 3
|
||||||
|
Metrics::new(28, 36), // Title 2
|
||||||
|
Metrics::new(32, 44), // Title 1
|
||||||
|
];
|
||||||
|
|
||||||
|
fn main() -> cosmic::iced::Result {
|
||||||
|
env_logger::init();
|
||||||
|
|
||||||
|
let mut settings = settings();
|
||||||
|
settings.window.min_size = Some((400, 100));
|
||||||
|
Window::run(settings)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Window {
|
||||||
|
theme: Theme,
|
||||||
|
path_opt: Option<PathBuf>,
|
||||||
|
attrs: Attrs<'static>,
|
||||||
|
#[cfg(not(feature = "vi"))]
|
||||||
|
editor: Mutex<SyntaxEditor<'static>>,
|
||||||
|
#[cfg(feature = "vi")]
|
||||||
|
editor: Mutex<cosmic_text::ViEditor<'static>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum Message {
|
||||||
|
Open,
|
||||||
|
Save,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Window {
|
||||||
|
pub fn open(&mut self, path: PathBuf) {
|
||||||
|
let mut editor = self.editor.lock().unwrap();
|
||||||
|
match editor.load_text(&path, self.attrs) {
|
||||||
|
Ok(()) => {
|
||||||
|
log::info!("opened '{}'", path.display());
|
||||||
|
self.path_opt = Some(path);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("failed to open '{}': {}", path.display(), err);
|
||||||
|
self.path_opt = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Application for Window {
|
||||||
|
type Executor = iced::executor::Default;
|
||||||
|
type Flags = ();
|
||||||
|
type Message = Message;
|
||||||
|
type Theme = Theme;
|
||||||
|
|
||||||
|
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
|
||||||
|
let attrs = cosmic_text::Attrs::new()
|
||||||
|
.monospaced(true)
|
||||||
|
.family(cosmic_text::Family::Monospace);
|
||||||
|
|
||||||
|
let mut editor = SyntaxEditor::new(
|
||||||
|
Buffer::new(&FONT_SYSTEM, FONT_SIZES[1 /* Body */]),
|
||||||
|
&SYNTAX_SYSTEM,
|
||||||
|
"base16-eighties.dark",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
#[cfg(feature = "vi")]
|
||||||
|
let mut editor = cosmic_text::ViEditor::new(editor);
|
||||||
|
|
||||||
|
update_attrs(&mut editor, attrs);
|
||||||
|
|
||||||
|
let mut window = Window {
|
||||||
|
theme: Theme::Dark,
|
||||||
|
path_opt: None,
|
||||||
|
attrs,
|
||||||
|
editor: Mutex::new(editor),
|
||||||
|
};
|
||||||
|
if let Some(arg) = env::args().nth(1) {
|
||||||
|
window.open(PathBuf::from(arg));
|
||||||
|
}
|
||||||
|
(window, Command::none())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn theme(&self) -> Theme {
|
||||||
|
self.theme
|
||||||
|
}
|
||||||
|
|
||||||
|
fn title(&self) -> String {
|
||||||
|
if let Some(path) = &self.path_opt {
|
||||||
|
format!(
|
||||||
|
"COSMIC Text - {} - {}",
|
||||||
|
FONT_SYSTEM.locale(),
|
||||||
|
path.display()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!("COSMIC Text - {}", FONT_SYSTEM.locale())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, message: Message) -> iced::Command<Self::Message> {
|
||||||
|
match message {
|
||||||
|
Message::Open => {
|
||||||
|
if let Some(path) = rfd::FileDialog::new().pick_file() {
|
||||||
|
self.open(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Message::Save => {
|
||||||
|
if let Some(path) = &self.path_opt {
|
||||||
|
let editor = self.editor.lock().unwrap();
|
||||||
|
let mut text = String::new();
|
||||||
|
for line in editor.buffer().lines.iter() {
|
||||||
|
text.push_str(line.text());
|
||||||
|
text.push('\n');
|
||||||
|
}
|
||||||
|
match fs::write(path, text) {
|
||||||
|
Ok(()) => {
|
||||||
|
log::info!("saved '{}'", path.display());
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("failed to save '{}': {}", path.display(), err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::none()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&self) -> Element<Message> {
|
||||||
|
let content: Element<_> = column![
|
||||||
|
row![
|
||||||
|
button(theme::Button::Secondary)
|
||||||
|
.text("Open")
|
||||||
|
.on_press(Message::Open),
|
||||||
|
button(theme::Button::Secondary)
|
||||||
|
.text("Save")
|
||||||
|
.on_press(Message::Save),
|
||||||
|
]
|
||||||
|
.align_items(Alignment::Center)
|
||||||
|
.spacing(8),
|
||||||
|
text_box(&self.editor)
|
||||||
|
]
|
||||||
|
.spacing(8)
|
||||||
|
.padding(16)
|
||||||
|
.into();
|
||||||
|
|
||||||
|
// Uncomment to debug layout: content.explain(Color::WHITE)
|
||||||
|
content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_attrs<'a, T: Edit<'a>>(editor: &mut T, attrs: Attrs<'a>) {
|
||||||
|
editor.buffer_mut().lines.iter_mut().for_each(|line| {
|
||||||
|
line.set_attrs_list(AttrsList::new(attrs));
|
||||||
|
});
|
||||||
|
}
|
||||||
411
src/text_box.rs
Normal file
411
src/text_box.rs
Normal file
|
|
@ -0,0 +1,411 @@
|
||||||
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
|
||||||
|
use cosmic::{
|
||||||
|
iced_native::{
|
||||||
|
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},
|
||||||
|
Padding, {Color, Element, Length, Point, Rectangle, Shell, Size},
|
||||||
|
},
|
||||||
|
theme::Theme,
|
||||||
|
};
|
||||||
|
use cosmic_text::{Action, Edit, SwashCache};
|
||||||
|
use std::{cmp, sync::Mutex, time::Instant};
|
||||||
|
|
||||||
|
pub struct Appearance {
|
||||||
|
background_color: Option<Color>,
|
||||||
|
text_color: Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TextBox<'a, Editor> {
|
||||||
|
editor: &'a Mutex<Editor>,
|
||||||
|
padding: Padding,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Editor> TextBox<'a, Editor> {
|
||||||
|
pub fn new(editor: &'a Mutex<Editor>) -> Self {
|
||||||
|
Self {
|
||||||
|
editor,
|
||||||
|
padding: Padding::new(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
|
||||||
|
self.padding = padding.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn text_box<'a, Editor>(editor: &'a Mutex<Editor>) -> TextBox<'a, Editor> {
|
||||||
|
TextBox::new(editor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_pixel(
|
||||||
|
buffer: &mut [u8],
|
||||||
|
width: i32,
|
||||||
|
height: i32,
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
color: cosmic_text::Color,
|
||||||
|
) {
|
||||||
|
let alpha = (color.0 >> 24) & 0xFF;
|
||||||
|
if alpha == 0 {
|
||||||
|
// Do not draw if alpha is zero
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if y < 0 || y >= height {
|
||||||
|
// Skip if y out of bounds
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if x < 0 || x >= width {
|
||||||
|
// Skip if x out of bounds
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let offset = (y as usize * width as usize + x as usize) * 4;
|
||||||
|
|
||||||
|
let mut current = buffer[offset + 2] as u32
|
||||||
|
| (buffer[offset + 1] as u32) << 8
|
||||||
|
| (buffer[offset + 0] as u32) << 16
|
||||||
|
| (buffer[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);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[offset + 2] = current as u8;
|
||||||
|
buffer[offset + 1] = (current >> 8) as u8;
|
||||||
|
buffer[offset + 0] = (current >> 16) as u8;
|
||||||
|
buffer[offset + 3] = (current >> 24) as u8;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'editor, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor>
|
||||||
|
where
|
||||||
|
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
|
||||||
|
Renderer::Theme: StyleSheet,
|
||||||
|
Editor: Edit<'editor>,
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
//TODO: allow lazy shape
|
||||||
|
let mut editor = self.editor.lock().unwrap();
|
||||||
|
editor.buffer_mut().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);
|
||||||
|
|
||||||
|
layout::Node::new(limits.resolve(size))
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(
|
||||||
|
&self,
|
||||||
|
tree: &widget::Tree,
|
||||||
|
renderer: &mut Renderer,
|
||||||
|
theme: &Renderer::Theme,
|
||||||
|
_style: &renderer::Style,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
_cursor_position: Point,
|
||||||
|
viewport: &Rectangle,
|
||||||
|
) {
|
||||||
|
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;
|
||||||
|
editor.buffer_mut().set_size(view_w, view_h);
|
||||||
|
|
||||||
|
editor.shape_as_needed();
|
||||||
|
|
||||||
|
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(
|
||||||
|
layout.position()
|
||||||
|
+ [x as f32, y as f32].into()
|
||||||
|
+ [self.padding.left as f32, self.padding.top as f32].into(),
|
||||||
|
Size::new(w as f32, h as f32),
|
||||||
|
),
|
||||||
|
border_radius: 0.0.into(),
|
||||||
|
border_width: 0.0,
|
||||||
|
border_color: Color::TRANSPARENT,
|
||||||
|
},
|
||||||
|
Color::from_rgba8(
|
||||||
|
color.r(),
|
||||||
|
color.g(),
|
||||||
|
color.b(),
|
||||||
|
(color.a() as f32) / 255.0,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
draw_pixel(&mut pixels, view_w, view_h, x, y, color);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
let handle = image::Handle::from_pixels(view_w as u32, view_h as u32, pixels);
|
||||||
|
image::Renderer::draw(
|
||||||
|
renderer,
|
||||||
|
handle,
|
||||||
|
Rectangle::new(
|
||||||
|
layout.position() + [self.padding.left as f32, self.padding.top as f32].into(),
|
||||||
|
Size::new(view_w as f32, view_h as f32),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
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<'_>,
|
||||||
|
cursor_position: Point,
|
||||||
|
_renderer: &Renderer,
|
||||||
|
_clipboard: &mut dyn Clipboard,
|
||||||
|
_shell: &mut Shell<'_, Message>,
|
||||||
|
) -> Status {
|
||||||
|
let state = tree.state.downcast_mut::<State>();
|
||||||
|
let mut editor = self.editor.lock().unwrap();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
Event::Keyboard(KeyEvent::CharacterReceived(character)) => {
|
||||||
|
editor.action(Action::Insert(character));
|
||||||
|
status = Status::Captured;
|
||||||
|
}
|
||||||
|
Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => {
|
||||||
|
if layout.bounds().contains(cursor_position) {
|
||||||
|
editor.action(Action::Click {
|
||||||
|
x: (cursor_position.x - layout.bounds().x) as i32
|
||||||
|
- self.padding.left as i32,
|
||||||
|
y: (cursor_position.y - layout.bounds().y) as i32 - self.padding.top as i32,
|
||||||
|
});
|
||||||
|
state.is_dragging = true;
|
||||||
|
status = Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::Mouse(MouseEvent::ButtonReleased(Button::Left)) => {
|
||||||
|
state.is_dragging = false;
|
||||||
|
status = Status::Captured;
|
||||||
|
}
|
||||||
|
Event::Mouse(MouseEvent::CursorMoved { .. }) => {
|
||||||
|
if state.is_dragging {
|
||||||
|
editor.action(Action::Drag {
|
||||||
|
x: (cursor_position.x - layout.bounds().x) as i32
|
||||||
|
- self.padding.left as i32,
|
||||||
|
y: (cursor_position.y - layout.bounds().y) as i32 - self.padding.top as i32,
|
||||||
|
});
|
||||||
|
status = Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::Mouse(MouseEvent::WheelScrolled { delta }) => match delta {
|
||||||
|
ScrollDelta::Lines { x, y } => {
|
||||||
|
editor.action(Action::Scroll {
|
||||||
|
lines: (-y * 6.0) as i32,
|
||||||
|
});
|
||||||
|
status = Status::Captured;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
status
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'editor, Editor, Message, Renderer> From<TextBox<'a, Editor>>
|
||||||
|
for Element<'a, Message, Renderer>
|
||||||
|
where
|
||||||
|
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
|
||||||
|
Renderer::Theme: StyleSheet,
|
||||||
|
Editor: Edit<'editor>,
|
||||||
|
{
|
||||||
|
fn from(text_box: TextBox<'a, Editor>) -> Self {
|
||||||
|
Self::new(text_box)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct State {
|
||||||
|
is_dragging: bool,
|
||||||
|
cache: Mutex<SwashCache<'static>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
/// Creates a new [`State`].
|
||||||
|
pub fn new() -> State {
|
||||||
|
State {
|
||||||
|
is_dragging: false,
|
||||||
|
cache: Mutex::new(SwashCache::new(&crate::FONT_SYSTEM)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue