Commit 707bd5f2 authored by Florian Blasius's avatar Florian Blasius 🤘

[update] key and mouse handler

parent cc697153
......@@ -70,7 +70,11 @@ impl Widget for MainView {
.margin((0.0, 8.0, 0.0, 0.0))
.font_icon(material_font_icons::CHECK_FONT_ICON)
.attach_property(GridColumn(0))
.attach_property(GridRow(1)),
.attach_property(GridRow(1))
.on_click(move |_| {
state.increment();
true
}),
)
.child(
Button::create()
......
......@@ -19,7 +19,7 @@ use crate::structs::{Point, Position, Size};
use crate::theme::Theme;
use crate::widget::MessageBox;
/// Implemenation of the OrbClient based backend.
/// Implementation of the OrbClient based backend.
pub struct OrbitalBackend {
inner: OrbWindow,
theme: Theme,
......
......@@ -22,13 +22,10 @@ pub fn target_backend(
resizable: bool,
theme: Theme,
) -> (Box<OrbitalBackendRunner>, Rc<RefCell<dyn Backend>>) {
let flags = {
if resizable {
vec![WindowFlag::Resizable]
} else {
vec![]
}
};
let mut flags = vec![WindowFlag::Async];
if resizable {
flags.push(WindowFlag::Resizable);
}
let backend = Rc::new(RefCell::new(OrbitalBackend::new(
theme,
......
use std::rc::Rc;
use crate::{Event, EventBox, EventHandler};
use crate::{Event, EventBox, EventHandler, Template};
#[derive(Copy, Clone, Debug, Hash, PartialEq)]
pub enum Key {
......@@ -293,46 +293,40 @@ pub struct KeyUpEvent {
impl Event for KeyUpEvent {}
pub type KeyHandler = Rc<dyn Fn(Key) -> bool + 'static>;
pub type KeyHandler = Fn(Key) -> bool + 'static;
#[derive(Default)]
pub struct KeyEventHandler {
key_up: Option<KeyHandler>,
key_down: Option<KeyHandler>,
pub struct KeyDownEventHandler {
handler: Rc<KeyHandler>,
}
impl KeyEventHandler {
pub fn on_key_up(mut self, handler: KeyHandler) -> Self {
self.key_up = Some(handler);
self
}
pub fn on_key_down(mut self, handler: KeyHandler) -> Self {
self.key_down = Some(handler);
self
}
}
impl Into<Rc<dyn EventHandler>> for KeyEventHandler {
impl Into<Rc<dyn EventHandler>> for KeyDownEventHandler {
fn into(self) -> Rc<dyn EventHandler> {
Rc::new(self)
}
}
impl EventHandler for KeyEventHandler {
impl EventHandler for KeyDownEventHandler {
fn handle_event(&self, event: &EventBox) -> bool {
if let Ok(event) = event.downcast_ref::<KeyDownEvent>() {
if let Some(handler) = &self.key_down {
return (handler)(event.key);
}
return (self.handler)(event.key);
}
if let Ok(event) = event.downcast_ref::<KeyUpEvent>() {
if let Some(handler) = &self.key_up {
return (handler)(event.key);
}
}
return false;
}
}
pub trait KeyDownHandler: Sized + From<Template> + Into<Template> {
/// Transforms the handler into a template.
fn template<F: FnOnce(Template) -> Template>(self, transform: F) -> Self {
Self::from(transform(self.into()))
}
false
/// Inserts a handler.
fn on_key_down<H: Fn(Key) -> bool + 'static>(self, handler: H) -> Self {
self.template(|template| {
template.event_handler(KeyDownEventHandler {
handler: Rc::new(handler),
})
})
}
}
......@@ -54,68 +54,42 @@ pub struct MouseDownEvent {
impl Event for MouseDownEvent {}
pub type MouseHandler = Rc<dyn Fn(Point) -> bool + 'static>;
pub type MouseHandler = Fn(Point) -> bool + 'static;
event_handler!(MouseHandler, TestHandler, [on_mouse_up]);
use crate::Template;
#[derive(Default)]
pub struct MouseEventHandler {
mouse_up: Option<MouseHandler>,
mouse_down: Option<MouseHandler>,
click: Option<MouseHandler>,
pub struct ClickEventHandler {
handler: Rc<MouseHandler>,
}
impl MouseEventHandler {
pub fn new() -> Self {
MouseEventHandler::default()
}
pub fn on_mouse_up(mut self, handler: MouseHandler) -> Self {
self.mouse_up = Some(handler);
self
}
pub fn on_mouse_down(mut self, handler: MouseHandler) -> Self {
self.mouse_down = Some(handler);
self
}
pub fn on_click(mut self, handler: MouseHandler) -> Self {
self.click = Some(handler);
self
}
}
impl Into<Rc<dyn EventHandler>> for MouseEventHandler {
impl Into<Rc<dyn EventHandler>> for ClickEventHandler {
fn into(self) -> Rc<dyn EventHandler> {
Rc::new(self)
}
}
impl EventHandler for MouseEventHandler {
impl EventHandler for ClickEventHandler {
fn handle_event(&self, event: &EventBox) -> bool {
if let Ok(event) = event.downcast_ref::<ClickEvent>() {
if let Some(handler) = &self.click {
return (handler)(event.position);
}
}
if let Ok(event) = event.downcast_ref::<MouseDownEvent>() {
if let Some(handler) = &self.mouse_down {
return (handler)(event.position);
}
}
if let Ok(event) = event.downcast_ref::<MouseUpEvent>() {
if let Some(handler) = &self.mouse_up {
(handler)(event.position);
return true;
}
return (self.handler)(event.position);
}
false
return false;
}
}
pub trait ClickHandler: Sized + From<Template> + Into<Template> {
/// Transforms the handler into a template.
fn template<F: FnOnce(Template) -> Template>(self, transform: F) -> Self {
Self::from(transform(self.into()))
}
/// Inserts a handler.
fn on_click<H: Fn(Point) -> bool + 'static>(self, handler: H) -> Self {
self.template(|template| {
template.event_handler(ClickEventHandler {
handler: Rc::new(handler),
})
})
}
}
......@@ -88,26 +88,4 @@ macro_rules! property {
}
}
};
}
/// Used to define an event handler.
#[macro_export]
macro_rules! event_handler {
($type:ident, $handler:ident, [ $( $method:ident ),* ]) => {
use crate::widget::Template;
pub trait $handler: Sized + From<Template> + Into<Template> {
/// Transforms the handler into a template.
fn template<F: FnOnce(Template) -> Template>(self, transform: F) -> Self {
Self::from(transform(self.into()))
}
$(
/// Inserts a handler.
fn $method<V: Into<$type>>(self, $method: V) -> Self {
self.template(|template| template)
}
)*
}
};
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ use crate::{
PressedProperty, Text, TextProperty,
},
theme::Selector,
event::ClickHandler,
widget::{Container, FontIconBlock, SharedProperty, Stack, Template, TextBlock, Widget},
};
......@@ -66,5 +67,5 @@ impl Widget for Button {
template!(
ButtonTemplate,
[TextProperty, FontIconProperty, PressedProperty]
[TextProperty, FontIconProperty, PressedProperty, ClickHandler]
);
......@@ -4,7 +4,7 @@ use std::{
};
use crate::{
event::{Key, KeyEventHandler, MouseEventHandler},
event::{Key, KeyDownHandler},
properties::{
Bounds, Constraint, Focused, FocusedProperty, Margin, Offset, OffsetProperty,
PaddingProperty, Point, ScrollMode, ScrollViewerMode, ScrollViewerModeProperty, Text,
......@@ -249,10 +249,7 @@ impl Widget for TextBox {
.shared_text_selection(selection)
.attach_shared_property(offset)
.shared_focused(focused)
.event_handler(
KeyEventHandler::default()
.on_key_down(Rc::new(move |key: Key| -> bool { state.update_text(key) })),
)
.on_key_down(move |key: Key| -> bool { state.update_text(key) })
}
}
......@@ -262,6 +259,7 @@ template!(
TextProperty,
FocusedProperty,
WaterMarkProperty,
TextSelectionProperty
TextSelectionProperty,
KeyDownHandler
]
);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment