event system!!!
This commit is contained in:
1 parent
4deeabe611
commit
21f15fb9c5
7 files changed
+255
-50
No files matched your search
+24
-2
@@ -1,11 +1,18 @@
|
||||
use ui::{core::CursorState, layout::Vec2};
|
||||
use winit::event::{MouseButton, WindowEvent};
|
||||
use ui::{
|
||||
core::{CursorState, Modifiers},
|
||||
layout::Vec2,
|
||||
};
|
||||
use winit::{
|
||||
event::{MouseButton, WindowEvent},
|
||||
keyboard::{Key, NamedKey},
|
||||
};
|
||||
|
||||
use crate::testing::Client;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Input {
|
||||
cursor: CursorState,
|
||||
pub modifiers: Modifiers,
|
||||
}
|
||||
|
||||
impl Input {
|
||||
@@ -27,6 +34,21 @@ impl Input {
|
||||
}
|
||||
WindowEvent::CursorLeft { .. } => {
|
||||
self.cursor.exists = false;
|
||||
self.modifiers.clear();
|
||||
}
|
||||
WindowEvent::KeyboardInput { event, .. } => {
|
||||
if let Key::Named(named) = event.logical_key {
|
||||
let pressed = event.state.is_pressed();
|
||||
match named {
|
||||
NamedKey::Control => {
|
||||
self.modifiers.control = pressed;
|
||||
}
|
||||
NamedKey::Shift => {
|
||||
self.modifiers.shift = pressed;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return false,
|
||||
}
|
||||
|
||||
+31
-14
@@ -24,6 +24,13 @@ pub struct Client {
|
||||
focus: Option<WidgetId<TextEdit>>,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Hash)]
|
||||
struct Submit;
|
||||
|
||||
impl DefaultEvent for Submit {
|
||||
type Data = ();
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(window: Arc<Window>) -> Self {
|
||||
let renderer = Renderer::new(window);
|
||||
@@ -119,7 +126,7 @@ impl Client {
|
||||
.span(Dir::DOWN, sized())
|
||||
.add_static(&mut ui);
|
||||
|
||||
let texts = Span::empty(Dir::DOWN).add(&mut ui);
|
||||
let texts = Span::empty(Dir::DOWN).add_static(&mut ui);
|
||||
let add_text = text_edit("add")
|
||||
.text_align(Align::Left)
|
||||
.font_size(30)
|
||||
@@ -127,23 +134,26 @@ impl Client {
|
||||
client.ui.text(id).select(ctx.cursor, ctx.size);
|
||||
client.focus = Some(id.clone());
|
||||
})
|
||||
.id_on(Submit, move |id, client: &mut Client, _| {
|
||||
let content = client.ui.text(id).take();
|
||||
let text = text_edit(content)
|
||||
.font_size(30)
|
||||
.id_on(Sense::click(), |id, client: &mut Client, ctx| {
|
||||
client.ui.text(id).select(ctx.cursor, ctx.size);
|
||||
client.focus = Some(id.clone());
|
||||
})
|
||||
.pad(10)
|
||||
.add(&mut client.ui);
|
||||
client.ui[texts].children.push((text.any(), sized()));
|
||||
})
|
||||
.add(&mut ui);
|
||||
let text_edit_scroll = (
|
||||
(Rect::new(Color::SKY), texts.clone()).stack(),
|
||||
(Rect::new(Color::SKY), texts).stack(),
|
||||
(
|
||||
add_text.clone(),
|
||||
Rect::new(Color::GREEN)
|
||||
.on(Sense::click(), move |client: &mut Client, _| {
|
||||
let content = client.ui.text(&add_text).take();
|
||||
let text = text_edit(content)
|
||||
.font_size(30)
|
||||
.id_on(Sense::click(), |id, client: &mut Client, ctx| {
|
||||
client.ui.text(id).select(ctx.cursor, ctx.size);
|
||||
client.focus = Some(id.clone());
|
||||
})
|
||||
.pad(10)
|
||||
.add(&mut client.ui);
|
||||
client.ui[&texts].children.push((text.any(), sized()));
|
||||
Ui::run_event(client, &add_text, Submit, ());
|
||||
})
|
||||
.size(40),
|
||||
)
|
||||
@@ -228,9 +238,16 @@ impl Client {
|
||||
WindowEvent::KeyboardInput { event, .. } => {
|
||||
if let Some(sel) = &self.focus
|
||||
&& event.state.is_pressed()
|
||||
&& self.ui.text(sel).apply_event(&event).unfocus()
|
||||
{
|
||||
self.focus = None;
|
||||
match self.ui.text(sel).apply_event(&event, &self.input.modifiers) {
|
||||
TextInputResult::Unfocus => {
|
||||
self.focus = None;
|
||||
}
|
||||
TextInputResult::Submit => {
|
||||
Ui::run_event(self, &sel.clone(), Submit, ());
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
|
||||
Reference in new issue
Block a user