Skip to content

Commit

Permalink
refactor(ribir): 💡 todos
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Aug 19, 2023
1 parent 5dd524e commit 6707b1b
Show file tree
Hide file tree
Showing 23 changed files with 364 additions and 532 deletions.
19 changes: 10 additions & 9 deletions core/src/animation/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use std::{ops::Deref, rc::Rc, time::Duration};

/// Transition use rate to describe how the state change form init to final
/// smoothly.
#[derive(Declare, Clone, Debug, PartialEq)]
pub struct Transition<E> {
#[derive(Declare2, Clone, Debug, PartialEq)]
pub struct Transition<E: 'static> {
#[declare(default, convert=strip_option)]
pub delay: Option<Duration>,
pub duration: Duration,
#[declare(strict)]
pub easing: E,
#[declare(default, convert=strip_option)]
pub repeat: Option<f32>,
Expand Down Expand Up @@ -86,6 +87,13 @@ impl<E: Easing> Roc for Transition<E> {
}
}

impl<T: Share> Roc for T
where
T::V: Roc,
{
fn rate_of_change(&self, dur: Duration) -> AnimateProgress { self.as_ref().rate_of_change(dur) }
}

impl<S: Share + 'static> TransitionState for S {}
impl<S, F> TransitionState for LerpFnState<S, F>
where
Expand All @@ -94,13 +102,6 @@ where
{
}

impl<T: Roc> Roc for Stateful<T> {
#[inline]
fn rate_of_change(&self, dur: Duration) -> AnimateProgress {
self.state_ref().rate_of_change(dur)
}
}

impl Roc for Box<dyn Roc> {
#[inline]
fn rate_of_change(&self, dur: Duration) -> AnimateProgress { self.deref().rate_of_change(dur) }
Expand Down
72 changes: 11 additions & 61 deletions core/src/builtin_widgets/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,90 +1,40 @@
use crate::prelude::*;
use std::{cell::Cell, rc::Rc};
use winit::window::CursorIcon;

/// `Cursor` is an attribute to assign an `cursor` to a widget.
#[derive(Declare, Debug, Declare2)]
#[derive(Declare, Default, Debug, Declare2)]
pub struct Cursor {
#[declare(convert=custom, builtin, default)]
pub cursor: Rc<Cell<CursorIcon>>,
#[declare(builtin, default)]
pub cursor: CursorIcon,
}

impl ComposeChild for Cursor {
type Child = Widget;
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
widget! {
states {
save_cursor: Stateful::new(CursorIcon::Default),
this: this.into_readonly()
}
DynWidget {
dyns: child,
fn compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
fn_widget! {
let mut save_cursor = Stateful::new(CursorIcon::Default);
@$child {
on_pointer_enter: move |e: &mut PointerEvent| {
if e.point_type == PointerType::Mouse
&& e.mouse_buttons() == MouseButtons::empty()
{
let wnd = e.window();
*save_cursor = wnd.get_cursor();
wnd.set_cursor(this.cursor.get());
*$save_cursor = wnd.get_cursor();
wnd.set_cursor($this.get_cursor());
}
},
on_pointer_leave: move |e: &mut PointerEvent| {
e.window().set_cursor(*save_cursor);
e.window().set_cursor(*$save_cursor);
}
}
}
.into()
}
}

pub trait IntoCursorIcon {
fn into_cursor_icon(self) -> Rc<Cell<CursorIcon>>;
}

impl IntoCursorIcon for Rc<Cell<CursorIcon>> {
#[inline]
fn into_cursor_icon(self) -> Rc<Cell<CursorIcon>> { self }
}

impl IntoCursorIcon for CursorIcon {
#[inline]
fn into_cursor_icon(self) -> Rc<Cell<CursorIcon>> { Rc::new(Cell::new(self)) }
}

impl CursorDeclarer {
#[inline]
pub fn cursor<C: IntoCursorIcon>(mut self, icon: C) -> Self {
self.cursor = Some(icon.into_cursor_icon());
self
}
}

impl Cursor {
#[inline]
pub fn set_declare_cursor<C: IntoCursorIcon>(&mut self, icon: C) {
self.cursor = icon.into_cursor_icon();
}
}

impl Cursor {
#[inline]
pub fn icon(&self) -> CursorIcon { self.cursor.get() }

#[inline]
pub fn set_icon(&self, icon: CursorIcon) { self.cursor.set(icon) }

#[inline]
pub fn new_icon(icon: CursorIcon) -> Rc<Cell<CursorIcon>> { Rc::new(Cell::new(icon)) }
}

impl Default for Cursor {
#[inline]
fn default() -> Self {
Cursor {
cursor: Rc::new(Cell::new(CursorIcon::Default)),
}
}
fn get_cursor(&self) -> CursorIcon { self.cursor }
}

#[cfg(test)]
Expand Down
10 changes: 5 additions & 5 deletions core/src/builtin_widgets/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ pub struct KeyChange<V>(pub Option<V>, pub Option<V>);
impl<V> Default for KeyChange<V> {
fn default() -> Self { KeyChange(None, None) }
}
#[derive(Declare)]
pub struct KeyWidget<V = ()> {
#[derive(Declare, Declare2)]
pub struct KeyWidget<V: 'static = ()> {
#[declare(convert=into)]
pub key: Key,
pub value: Option<V>,

#[declare(default)]
pub value: Option<V>,
#[declare(skip)]
before_value: Option<V>,
#[declare(default)]
#[declare(skip)]
status: KeyStatus,
}

Expand Down
12 changes: 0 additions & 12 deletions core/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,11 @@ impl<V, U: From<V>> DeclareFrom<V, ()> for DeclareInit<U> {
fn declare_from(value: V) -> Self { Self::Value(value.into()) }
}

impl<V, U: From<V>> DeclareFrom<V, Option<()>> for DeclareInit<Option<U>> {
#[inline]
fn declare_from(value: V) -> Self { Self::Value(Some(value.into())) }
}

impl<V: 'static, U: From<V> + 'static> DeclareFrom<Pipe<V>, Pipe<()>> for DeclareInit<U> {
#[inline]
fn declare_from(value: Pipe<V>) -> Self { Self::Pipe(value.map(U::from)) }
}

impl<V: 'static, U: From<V> + 'static> DeclareFrom<Pipe<V>, Option<Pipe<()>>>
for DeclareInit<Option<U>>
{
#[inline]
fn declare_from(value: Pipe<V>) -> Self { Self::Pipe(value.map(|v| Some(v.into()))) }
}

#[derive(Debug, PartialEq, Hash)]
pub struct DeclareStripOption<O>(O);

Expand Down
36 changes: 17 additions & 19 deletions core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,31 @@ pub trait Share: Sized {
/// origin state the return state will be notified. But when modifies the
/// return state the origin state will not be notified.
#[inline]
fn partial_state<R>(&mut self, router: R) -> PartialState<R::Target, Self::S, R>
fn partial_state<R1, R2, Target>(
&mut self,
router: R1,
router_mut: R2,
) -> PartialState<Target, Self::S, R1, R2>
where
R: StateRouter<Origin = Self::V>,
R1: FnOnce(&Self::V) -> &Target + Copy,
R2: FnOnce(&mut Self::V) -> &mut Target + Copy,
{
PartialState::new(self.clone_state(), router)
PartialState::new(self.clone_state(), router, router_mut)
}

/// Create a state that help you quick route to the part of the origin state
/// before you access it. The return state and the origin state are the same
/// state. So when one of them is modified, they will both be notified.
#[inline]
fn route_state<R>(&mut self, router: R) -> RouteState<R::Target, Self::S, R>
fn route_state<R1, R2, Target>(
&mut self,
router: R1,
router_mut: R2,
) -> RouteState<Target, Self::S, R1, R2>
where
R: StateRouter<Origin = Self::V>,
R1: FnOnce(&Self::V) -> &Target + Copy,
R2: FnOnce(&mut Self::V) -> &mut Target + Copy,
{
RouteState::new(self.clone_state(), router)
RouteState::new(self.clone_state(), router, router_mut)
}

/// Return the origin state of the state if this state is a partial state.
Expand Down Expand Up @@ -103,15 +112,6 @@ pub trait RefShare: DerefMut {
fn forget_modifies(&self) -> ModifyScope;
}

/// A trait that to split part from a state.
pub trait StateRouter: Copy {
type Origin;
type Target;

fn route(self, origin: &Self::Origin) -> &Self::Target;
fn route_mut(self, origin: &mut Self::Origin) -> &mut Self::Target;
}

/// Enum to store both stateless and stateful object.
pub enum State<W> {
Stateless(W),
Expand Down Expand Up @@ -224,9 +224,7 @@ impl<W> State<W> {

pub fn clone_stateful(&mut self) -> Stateful<W> { self.to_stateful().clone() }

pub fn stateful_ref(&mut self) -> TrackRef<W> { self.to_stateful().state_ref() }

pub fn to_stateful(&mut self) -> &mut Stateful<W> {
fn to_stateful(&mut self) -> &mut Stateful<W> {
match self {
State::Stateless(w) => {
// convert the stateless value to stateful first.
Expand Down
Loading

0 comments on commit 6707b1b

Please sign in to comment.