event system!!!
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::prelude::*;
|
||||
use cosmic_text::{Attrs, Cursor, Family, FontSystem, Metrics, Motion, Shaping};
|
||||
use cosmic_text::{Affinity, Attrs, Cursor, Family, FontSystem, Metrics, Motion, Shaping};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use winit::{
|
||||
event::KeyEvent,
|
||||
@@ -133,6 +133,11 @@ impl<'a> TextEditCtx<'a> {
|
||||
self.text
|
||||
.buf
|
||||
.set_text(self.font_system, "", &Attrs::new(), Shaping::Advanced);
|
||||
if let Some(cursor) = &mut self.text.cursor {
|
||||
cursor.line = 0;
|
||||
cursor.index = 0;
|
||||
cursor.affinity = Affinity::default();
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
@@ -219,13 +224,19 @@ impl<'a> TextEditCtx<'a> {
|
||||
self.text.cursor = None;
|
||||
}
|
||||
|
||||
pub fn apply_event(&mut self, event: &KeyEvent) -> TextInputResult {
|
||||
pub fn apply_event(&mut self, event: &KeyEvent, modifiers: &Modifiers) -> TextInputResult {
|
||||
match &event.logical_key {
|
||||
Key::Named(named) => match named {
|
||||
NamedKey::Backspace => self.backspace(),
|
||||
NamedKey::Delete => self.delete(),
|
||||
NamedKey::Space => self.insert(" "),
|
||||
NamedKey::Enter => self.insert("\n"),
|
||||
NamedKey::Enter => {
|
||||
if modifiers.shift {
|
||||
self.newline();
|
||||
} else {
|
||||
return TextInputResult::Submit;
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowRight => self.motion(Motion::Right),
|
||||
NamedKey::ArrowLeft => self.motion(Motion::Left),
|
||||
NamedKey::ArrowUp => self.motion(Motion::Up),
|
||||
@@ -243,10 +254,24 @@ impl<'a> TextEditCtx<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Modifiers {
|
||||
pub shift: bool,
|
||||
pub control: bool,
|
||||
}
|
||||
|
||||
impl Modifiers {
|
||||
pub fn clear(&mut self) {
|
||||
self.shift = false;
|
||||
self.control = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub enum TextInputResult {
|
||||
Used,
|
||||
Unused,
|
||||
Unfocus,
|
||||
Submit,
|
||||
}
|
||||
|
||||
impl TextInputResult {
|
||||
|
||||
Reference in New Issue
Block a user