From 454ed20cb33651c5e627c226281c89055488a726 Mon Sep 17 00:00:00 2001 From: qwertz19281 Date: Wed, 3 Feb 2021 17:09:52 +0100 Subject: [PATCH] improve bounds in env bounds and affected traits --- src/backend.rs | 2 +- src/ctx/mod.rs | 6 +++-- src/event/filter.rs | 2 +- src/event/imp.rs | 2 +- src/event/mod.rs | 4 ++-- src/event/variant.rs | 2 +- src/handler/standard/imp.rs | 44 ++++++++++++++++++------------------- src/id/mod.rs | 10 ++++----- src/id/standard.rs | 11 ++++++++++ src/state/mod.rs | 2 +- 10 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index 87bd28fd..ea64114a 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -2,7 +2,7 @@ use super::*; /// Type compound -pub trait Backend: Sized + 'static where E: Env { +pub trait Backend: Sized + 'static where E: Env { type Renderer: Render; type Event: Event; ///TODO move tree'd back to Event diff --git a/src/ctx/mod.rs b/src/ctx/mod.rs index c8f938e6..32bea166 100644 --- a/src/ctx/mod.rs +++ b/src/ctx/mod.rs @@ -1,4 +1,6 @@ //! The [`Context`] trait housing handlers, queue and other side stuff +use std::ops::DerefMut; + use super::*; pub mod queue; @@ -6,7 +8,7 @@ pub mod clipboard; /// The Context contains the [`Handlers`](Handler), the [`Queue`] and other side data and is also the entry point for most actions. /// A Context is regularly referenced in parallel with the [widget tree](Env::Storage) -pub trait Context: Sized + 'static where E: Env { +pub trait Context: AsRefMut + Sized + 'static where E: Env, E::Context: AsRefMut { type Handler: Handler; type Queue: Queue,StdOrder>; @@ -37,7 +39,7 @@ pub trait Context: Sized + 'static where E: Env { #[inline] fn link<'l: 's,'s>(&'s mut self, w: Resolved<'l,E>) -> Link<'s,E> { Link{ - ctx: self, + ctx: self.as_mut(), widget: w, } } diff --git a/src/event/filter.rs b/src/event/filter.rs index 3d3c0e46..94b37d81 100644 --- a/src/event/filter.rs +++ b/src/event/filter.rs @@ -1,6 +1,6 @@ use super::*; -pub trait Filter: Clone + Default + Sized where E: Env, EEFilter: From { +pub trait Filter: Clone + Default + Sized where E: Env { fn _filter(&self, dest: &Link, e: &EventCompound) -> Option>; fn attach_path_prefix(self, prefix: E::WidgetPath) -> Self; } diff --git a/src/event/imp.rs b/src/event/imp.rs index 38c9b8e9..9d37f163 100644 --- a/src/event/imp.rs +++ b/src/event/imp.rs @@ -49,7 +49,7 @@ pub trait StdVarSup: VariantSupport + VariantSupport + VariantSupport,E> -where E: Env, E::Backend: Backend { +where E: Env { #[inline] fn is_kbd_down(&self) -> Option> { self.is::>() diff --git a/src/event/mod.rs b/src/event/mod.rs index f5788289..61596989 100644 --- a/src/event/mod.rs +++ b/src/event/mod.rs @@ -11,10 +11,10 @@ pub mod standard; pub mod imp; /// an Event holds one of the support [`Variant`] and can be downcasted to a specific Variant -pub trait Event: Sized + Clone + Debug where E: Env, E::Backend: Backend { +pub trait Event: Sized + Clone + Debug where E: Env { type Dest: Destination; type Key: Key; - + /// True if container widgets should sent this to only one widget fn consuming(&self) -> bool; /// Where there Event should be initially injected into the context diff --git a/src/event/variant.rs b/src/event/variant.rs index 12d561b3..15cc959c 100644 --- a/src/event/variant.rs +++ b/src/event/variant.rs @@ -1,6 +1,6 @@ use super::*; -pub trait VariantSupport: Event where E: Env, E::Backend: Backend, V: Variant { +pub trait VariantSupport: Event where E: Env, V: Variant { fn from_variant(v: V) -> Self; fn to_variant(&self) -> Option; } diff --git a/src/handler/standard/imp.rs b/src/handler/standard/imp.rs index 34d95798..96dc7cb1 100644 --- a/src/handler/standard/imp.rs +++ b/src/handler/standard/imp.rs @@ -15,7 +15,7 @@ impl Handler for StdHandler where #[inline] fn _event_direct(mut l: Link, e: &EventCompound) -> EventResp { if let Some(_) = e.event.is::() { - l.as_mut().s.mouse.hovered = Some(l.ident()); + (l.as_mut() as &mut Self).s.mouse.hovered = Some(l.ident()); } S::_event_direct(l,e) //todo!() @@ -23,7 +23,7 @@ impl Handler for StdHandler where #[inline] fn _send_event(l: Link, e: &EventCompound, child: E::WidgetPath) -> Result> { /*if let Some(_) = e.0.is::() { - l.as_mut().s.mouse.hovered = Some(l.ident()); + (l.as_mut() as &mut Self).s.mouse.hovered = Some(l.ident()); }*/ S::_send_event(l,e,child) } @@ -35,12 +35,12 @@ impl Handler for StdHandler where match ee { RootEvent::KbdDown{key} => { //Self::_event_root(l.reference(),(Event::from(RootEvent::KbdUp{key: key.clone()}),e.1,e.2)); - if let Some(id) = l.as_ref().s.kbd.focused.clone() { + if let Some(id) = (l.as_ref() as &Self).s.kbd.focused.clone() { if !l.widget.stor.has_widget(id.refc().path) { //drop event if widget is gone return false; } - l.as_mut().s.key.down( + (l.as_mut() as &mut Self).s.key.down( key.clone(), id.clone(), e.ts, @@ -65,14 +65,14 @@ impl Handler for StdHandler where //l._event_root((Event::from(RootEvent::KbdPress{key}),e.1,e.2)); }, RootEvent::KbdUp{key} => { - let old = l.as_mut().s.key.up(key); + let old = (l.as_mut() as &mut Self).s.key.up(key); if let Some(p) = old { let event = KbdUp{ key: p.key, down_widget: p.down.refc(), down_ts: p.ts, }; - if let Some(id) = l.as_ref().s.kbd.focused.clone() { + if let Some(id) = (l.as_ref() as &Self).s.kbd.focused.clone() { passed |= l.send_event( &e.default_filter().with_event(Event::from(event.clone())), id.path, @@ -86,7 +86,7 @@ impl Handler for StdHandler where } }, RootEvent::KbdPress{key} => { - let old = l.as_mut().s.key.get(key.clone()); + let old = (l.as_mut() as &mut Self).s.key.get(key.clone()); //TODO send up event to the widget which downed it if let Some(p) = old { let event = KbdPress{ @@ -94,7 +94,7 @@ impl Handler for StdHandler where down_widget: p.down.refc(), down_ts: p.ts, }; - if let Some(id) = l.as_ref().s.kbd.focused.clone() { + if let Some(id) = (l.as_ref() as &Self).s.kbd.focused.clone() { passed |= l.send_event( &e.default_filter().with_event(Event::from(event.clone())), id.path.refc(), @@ -110,7 +110,7 @@ impl Handler for StdHandler where let dir = if reverse {TabulateDirection::Backward} else {TabulateDirection::Forward}; let path = tabi(l.reference(),id.path,dir).expect("TODO"); //better way than this hack to get the ident - l.as_mut().s.kbd.focused = Some(WidgetIdent::from_path(path,l.widget.stor).expect("TODO")); + (l.as_mut() as &mut Self).s.kbd.focused = Some(WidgetIdent::from_path(path,l.widget.stor).expect("TODO")); } } } @@ -121,9 +121,9 @@ impl Handler for StdHandler where passed |= Self::unfocus(l.reference(),e.bounds,e.ts); //the currently hovered widget - if let Some(pos) = l.as_ref().s.mouse.pos { - if let Some(hovered) = l.as_ref().s.mouse.hovered.clone() { - l.as_mut().s.key.down(key.clone(),hovered.clone(),e.ts,Some(pos)); + if let Some(pos) = (l.as_ref() as &Self).s.mouse.pos { + if let Some(hovered) = (l.as_ref() as &Self).s.mouse.hovered.clone() { + (l.as_mut() as &mut Self).s.key.down(key.clone(),hovered.clone(),e.ts,Some(pos)); passed |= l.send_event( &e.default_filter().with_event(Event::from(MouseDown{key,pos})), @@ -146,10 +146,10 @@ impl Handler for StdHandler where } } RootEvent::MouseUp{key} => { - let old = l.as_mut().s.key.up(key); + let old = (l.as_mut() as &mut Self).s.key.up(key); //TODO send up event to the widget which downed it if let Some(p) = old { - if let Some(pos) = l.as_ref().s.mouse.pos { + if let Some(pos) = (l.as_ref() as &Self).s.mouse.pos { let event = MouseUp{ key: p.key, pos, @@ -157,7 +157,7 @@ impl Handler for StdHandler where down_widget: p.down.refc(), down_ts: p.ts }; - if let Some(hovered) = l.as_ref().s.mouse.hovered.clone() { + if let Some(hovered) = (l.as_ref() as &Self).s.mouse.hovered.clone() { if hovered != p.down { passed |= l.send_event( &e.default_filter().with_event(Event::from(event.clone())), @@ -175,9 +175,9 @@ impl Handler for StdHandler where } RootEvent::MouseMove{pos} => { //set pos - l.as_mut().s.mouse.pos = Some(pos); + (l.as_mut() as &mut Self).s.mouse.pos = Some(pos); //previous hovered widget - if let Some(p) = l.as_mut().s.mouse.hovered.take() { + if let Some(p) = (l.as_mut() as &mut Self).s.mouse.hovered.take() { passed |= l.send_event( &e.default_filter().with_event(Event::from(MouseLeave{})), p.path, @@ -185,7 +185,7 @@ impl Handler for StdHandler where } //hover state will be updated as the event passes through the widget tree passed |= l._event_root(&e.with_event(Event::from(MouseMove{pos}))); - if let Some(p) = l.as_ref().s.mouse.hovered.clone() {//TODO optimize clone + if let Some(p) = (l.as_ref() as &Self).s.mouse.hovered.clone() {//TODO optimize clone passed |= l.send_event( &e.default_filter().with_event(Event::from(MouseEnter{})), p.path, @@ -194,13 +194,13 @@ impl Handler for StdHandler where } RootEvent::MouseLeaveWindow{} => { - if let Some(p) = l.as_ref().s.mouse.hovered.clone() {//TODO optimize clone + if let Some(p) = (l.as_ref() as &Self).s.mouse.hovered.clone() {//TODO optimize clone passed |= l.send_event( &e.default_filter().with_event(Event::from(MouseLeave{})), p.path, ).unwrap_or(false); } - let mouse = &mut l.as_mut().s.mouse; + let mouse = &mut (l.as_mut() as &mut Self).s.mouse; mouse.pos = None; mouse.hovered = None; } @@ -211,7 +211,7 @@ impl Handler for StdHandler where passed |= l._event_root(&e.with_event(Event::from(WindowResize{size}))) } RootEvent::TextInput{text} => { - if let Some(id) = l.as_ref().s.kbd.focused.clone() { + if let Some(id) = (l.as_ref() as &Self).s.kbd.focused.clone() { passed |= l.send_event( &e.default_filter().with_event(Event::from(TextInput{text})), id.path, @@ -219,7 +219,7 @@ impl Handler for StdHandler where } } RootEvent::MouseScroll{x,y} => { - if let Some(hovered) = l.as_ref().s.mouse.hovered.clone() { + if let Some(hovered) = (l.as_ref() as &Self).s.mouse.hovered.clone() { passed |= l.send_event( &e.default_filter().with_event(Event::from(MouseScroll{x,y})), hovered.path, diff --git a/src/id/mod.rs b/src/id/mod.rs index 4b71990a..320439fe 100644 --- a/src/id/mod.rs +++ b/src/id/mod.rs @@ -8,7 +8,7 @@ pub mod standard; /// Unique ID for [`Widgets`](Widget::id) /// /// WidgetID shall be easily clonable -pub trait WidgetID: Clone + PartialEq + Sized + Debug + 'static { //should also implement Eq + Hash + Send +pub trait WidgetID: AsRefMut + Clone + PartialEq + Sized + Debug + 'static { //should also implement Eq + Hash + Send #[inline] fn id_eq(&self, o: &I) -> bool where Self: 'static { Any::downcast_ref::(o) @@ -16,12 +16,12 @@ pub trait WidgetID: Clone + PartialEq + Sized + Debug + 'static { //should also } #[inline] - fn is_hovered>(&self, c: &E::Context) -> bool where E::Context: CtxStdState, EPressedKey: PressedKey { - c.state().is_hovered(self) + fn is_hovered(&self, c: &E::Context) -> bool where E::Context: CtxStdState, EPressedKey: PressedKey, Self: AsRefMut { + c.state().is_hovered(self.as_ref()) } #[inline] - fn is_focused>(&self, c: &E::Context) -> bool where E::Context: CtxStdState, EPressedKey: PressedKey { - c.state().is_focused(self) + fn is_focused(&self, c: &E::Context) -> bool where E::Context: CtxStdState, EPressedKey: PressedKey, Self: AsRefMut { + c.state().is_focused(self.as_ref()) } } diff --git a/src/id/standard.rs b/src/id/standard.rs index 78e57bd1..ffde5e3a 100644 --- a/src/id/standard.rs +++ b/src/id/standard.rs @@ -79,6 +79,17 @@ impl SubPath for StdID where E: Env, E::WidgetID: Into + From } } +impl AsRefMut for StdID { + #[inline] + fn as_ref(&self) -> &Self { + self + } + #[inline] + fn as_mut(&mut self) -> &mut Self { + self + } +} + #[allow(unused)] mod const_id_test { const_std_id!(foo bar); diff --git a/src/state/mod.rs b/src/state/mod.rs index 2c1b13c7..55952e73 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -9,7 +9,7 @@ pub mod standard; //move StdState trait to standard StdState trait. StdState is not a core feature! -pub trait CtxStdState: Context + Sized where E: Env { +pub trait CtxStdState: Context + Sized where E: Env, E::Context: AsRefMut { type T: StdState; fn state_mut(&mut self) -> &mut Self::T;