Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::*;

/// Type compound
pub trait Backend<E>: Sized + 'static where E: Env<Backend=Self> {
pub trait Backend<E>: Sized + 'static where E: Env {
type Renderer: Render<E>;
type Event: Event<E>;
///TODO move tree'd back to Event
Expand Down
6 changes: 4 additions & 2 deletions src/ctx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//! The [`Context`] trait housing handlers, queue and other side stuff
use std::ops::DerefMut;

use super::*;

pub mod queue;
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<E>: Sized + 'static where E: Env<Context=Self> {
pub trait Context<E>: AsRefMut<E::Context> + Sized + 'static where E: Env, E::Context: AsRefMut<Self> {
type Handler: Handler<E>;
type Queue: Queue<StdEnqueueable<E>,StdOrder>;

Expand Down Expand Up @@ -37,7 +39,7 @@ pub trait Context<E>: Sized + 'static where E: Env<Context=Self> {
#[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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/event/filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

pub trait Filter<E>: Clone + Default + Sized where E: Env, EEFilter<E>: From<Self> {
pub trait Filter<E>: Clone + Default + Sized where E: Env {
fn _filter(&self, dest: &Link<E>, e: &EventCompound<E>) -> Option<EventCompound<E>>;
fn attach_path_prefix(self, prefix: E::WidgetPath) -> Self;
}
Expand Down
2 changes: 1 addition & 1 deletion src/event/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait StdVarSup<E>:
VariantSupport<Focus,E> +
VariantSupport<Unfocus,E> +
VariantSupport<RootEvent<E>,E>
where E: Env, E::Backend: Backend<E,Event=Self> {
where E: Env {
#[inline]
fn is_kbd_down(&self) -> Option<KbdDown<E>> {
self.is::<KbdDown<E>>()
Expand Down
4 changes: 2 additions & 2 deletions src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<E>: Sized + Clone + Debug where E: Env, E::Backend: Backend<E,Event=Self> {
pub trait Event<E>: 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
Expand Down
2 changes: 1 addition & 1 deletion src/event/variant.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

pub trait VariantSupport<V,E>: Event<E> where E: Env, E::Backend: Backend<E,Event=Self>, V: Variant<E> {
pub trait VariantSupport<V,E>: Event<E> where E: Env, V: Variant<E> {
fn from_variant(v: V) -> Self;
fn to_variant(&self) -> Option<V>;
}
Expand Down
44 changes: 22 additions & 22 deletions src/handler/standard/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ impl<S,E> Handler<E> for StdHandler<S,E> where
#[inline]
fn _event_direct(mut l: Link<E>, e: &EventCompound<E>) -> EventResp {
if let Some(_) = e.event.is::<MouseMove>() {
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!()
}
#[inline]
fn _send_event(l: Link<E>, e: &EventCompound<E>, child: E::WidgetPath) -> Result<EventResp,GuionError<E>> {
/*if let Some(_) = e.0.is::<MouseMove>() {
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)
}
Expand All @@ -35,12 +35,12 @@ impl<S,E> Handler<E> for StdHandler<S,E> 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,
Expand All @@ -65,14 +65,14 @@ impl<S,E> Handler<E> for StdHandler<S,E> 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,
Expand All @@ -86,15 +86,15 @@ impl<S,E> Handler<E> for StdHandler<S,E> 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{
key: p.key.clone(),
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(),
Expand All @@ -110,7 +110,7 @@ impl<S,E> Handler<E> for StdHandler<S,E> 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"));
}
}
}
Expand All @@ -121,9 +121,9 @@ impl<S,E> Handler<E> for StdHandler<S,E> 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})),
Expand All @@ -146,18 +146,18 @@ impl<S,E> Handler<E> for StdHandler<S,E> 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,
down_pos: p.cursor.expect("TODO"), //fails if a invalid press was inserted into the state
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())),
Expand All @@ -175,17 +175,17 @@ impl<S,E> Handler<E> for StdHandler<S,E> 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,
).unwrap_or(false);
}
//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,
Expand All @@ -194,13 +194,13 @@ impl<S,E> Handler<E> for StdHandler<S,E> 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;
}
Expand All @@ -211,15 +211,15 @@ impl<S,E> Handler<E> for StdHandler<S,E> 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,
).unwrap_or(false);
}
}
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,
Expand Down
10 changes: 5 additions & 5 deletions src/id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ 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<Self> + Clone + PartialEq + Sized + Debug + 'static { //should also implement Eq + Hash + Send
#[inline]
fn id_eq<I: WidgetID + 'static>(&self, o: &I) -> bool where Self: 'static {
Any::downcast_ref::<Self>(o)
.map_or(false, #[inline] |r| self.eq(r) )
}

#[inline]
fn is_hovered<E: Env<WidgetID=Self>>(&self, c: &E::Context) -> bool where E::Context: CtxStdState<E>, EPressedKey<E>: PressedKey<E> {
c.state().is_hovered(self)
fn is_hovered<E: Env>(&self, c: &E::Context) -> bool where E::Context: CtxStdState<E>, EPressedKey<E>: PressedKey<E>, Self: AsRefMut<E::WidgetID> {
c.state().is_hovered(self.as_ref())
}
#[inline]
fn is_focused<E: Env<WidgetID=Self>>(&self, c: &E::Context) -> bool where E::Context: CtxStdState<E>, EPressedKey<E>: PressedKey<E> {
c.state().is_focused(self)
fn is_focused<E: Env>(&self, c: &E::Context) -> bool where E::Context: CtxStdState<E>, EPressedKey<E>: PressedKey<E>, Self: AsRefMut<E::WidgetID> {
c.state().is_focused(self.as_ref())
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/id/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ impl<E> SubPath<E> for StdID where E: Env, E::WidgetID: Into<Self> + From<Self>
}
}

impl AsRefMut<Self> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod standard;

//move StdState trait to standard StdState trait. StdState is not a core feature!

pub trait CtxStdState<E>: Context<E> + Sized where E: Env<Context=Self> {
pub trait CtxStdState<E>: Context<E> + Sized where E: Env, E::Context: AsRefMut<Self> {
type T: StdState<E>;

fn state_mut(&mut self) -> &mut Self::T;
Expand Down