Skip to content

Commit

Permalink
refactor(ribir): 💡 add a Query derive macro
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Sep 27, 2023
1 parent 270656a commit 8370e05
Show file tree
Hide file tree
Showing 49 changed files with 233 additions and 289 deletions.
8 changes: 2 additions & 6 deletions core/src/builtin_widgets/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ pub enum VAlign {
}

/// A widget that align its child in x-axis, base on child's width.
#[derive(Declare2, SingleChild)]
#[derive(Declare2, Query, SingleChild)]
pub struct HAlignWidget {
#[declare(default, builtin)]
pub h_align: HAlign,
}

/// A widget that align its child in y-axis, base on child's height.
#[derive(Declare2, SingleChild)]
#[derive(Declare2, Query, SingleChild)]
pub struct VAlignWidget {
#[declare(default, builtin)]
pub v_align: VAlign,
Expand Down Expand Up @@ -91,8 +91,6 @@ impl Render for HAlignWidget {
}
}

crate::impl_query_self_only!(HAlignWidget);

impl Render for VAlignWidget {
fn perform_layout(&self, mut clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size {
let mut layouter = ctx.assert_single_child_layouter();
Expand All @@ -117,8 +115,6 @@ impl Render for VAlignWidget {
}
}

crate::impl_query_self_only!(VAlignWidget);

impl Align {
pub fn align_value(self, child_size: f32, box_size: f32) -> f32 {
match self {
Expand Down
13 changes: 4 additions & 9 deletions core/src/builtin_widgets/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,34 @@ pub enum PositionUnit {
}

/// Widget use to anchor child constraints with the left edge of parent widget.
#[derive(Declare2, SingleChild)]
#[derive(Declare2, Query, SingleChild)]
pub struct LeftAnchor {
#[declare(builtin, default = 0.)]
pub left_anchor: PositionUnit,
}

/// Widget use to anchor child constraints with the right edge of parent widget.
#[derive(Declare2, SingleChild)]
#[derive(Declare2, Query, SingleChild)]
pub struct RightAnchor {
#[declare(builtin, default = 0.)]
pub right_anchor: PositionUnit,
}

/// Widget use to anchor child constraints with the top edge of parent widget.
#[derive(Declare2, SingleChild)]
#[derive(Declare2, Query, SingleChild)]
pub struct TopAnchor {
#[declare(builtin, default = 0.)]
pub top_anchor: PositionUnit,
}

/// Widget use to anchor child constraints with the bottom edge of parent
/// widget.
#[derive(Declare2, SingleChild)]
#[derive(Declare2, Query, SingleChild)]
pub struct BottomAnchor {
#[declare(builtin, default = 0.)]
pub bottom_anchor: PositionUnit,
}

crate::impl_query_self_only!(LeftAnchor);
crate::impl_query_self_only!(TopAnchor);
crate::impl_query_self_only!(RightAnchor);
crate::impl_query_self_only!(BottomAnchor);

impl Render for LeftAnchor {
fn perform_layout(&self, clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size {
let mut layouter = ctx.assert_single_child_layouter();
Expand Down
6 changes: 2 additions & 4 deletions core/src/builtin_widgets/box_decoration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{impl_query_self_only, prelude::*};
use crate::prelude::*;

/// The BoxDecoration provides a variety of ways to draw a box.
#[derive(SingleChild, Default, Clone, Declare2)]
#[derive(SingleChild, Default, Clone, Declare2, Query)]
pub struct BoxDecoration {
/// The background of the box.
#[declare(builtin, default)]
Expand Down Expand Up @@ -59,8 +59,6 @@ impl Render for BoxDecoration {
}
}

impl_query_self_only!(BoxDecoration);

impl BoxDecoration {
fn paint_border(&self, painter: &mut Painter, rect: &Rect) {
if self.border.is_none() {
Expand Down
6 changes: 2 additions & 4 deletions core/src/builtin_widgets/clip.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{impl_query_self_only, prelude::*};
use crate::prelude::*;

#[derive(Clone, Default)]
pub enum ClipType {
Expand All @@ -7,7 +7,7 @@ pub enum ClipType {
Path(Path),
}

#[derive(SingleChild, Clone, Declare2)]
#[derive(SingleChild, Query, Clone, Declare2)]
pub struct Clip {
#[declare(default)]
pub clip: ClipType,
Expand Down Expand Up @@ -40,5 +40,3 @@ impl Render for Clip {
ctx.painter().clip(path);
}
}

impl_query_self_only!(Clip);
6 changes: 2 additions & 4 deletions core/src/builtin_widgets/delay_drop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{impl_query_self_only, prelude::*};
use crate::prelude::*;

/// A widget that can delay drop its child until the `delay_drop_until` field be
/// set to `true`.
Expand All @@ -12,7 +12,7 @@ use crate::{impl_query_self_only, prelude::*};
/// dropped.
///
/// It's useful when you need run a leave animation for a widget.
#[derive(Declare2)]
#[derive(Declare2, Query)]
pub struct DelayDrop {
#[declare(builtin)]
pub delay_drop_until: bool,
Expand All @@ -28,5 +28,3 @@ impl ComposeChild for DelayDrop {
}
}
}

impl_query_self_only!(DelayDrop);
4 changes: 1 addition & 3 deletions core/src/builtin_widgets/fitted_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum BoxFit {
}

/// Widget set how its child should be scale to fit its box.
#[derive(Declare2, SingleChild)]
#[derive(Declare2, Query, SingleChild)]
pub struct FittedBox {
#[declare(builtin)]
pub box_fit: BoxFit,
Expand Down Expand Up @@ -74,8 +74,6 @@ impl Render for FittedBox {
fn get_transform(&self) -> Option<Transform> { Some(self.scale_cache.get()) }
}

crate::impl_query_self_only!(FittedBox);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 2 additions & 7 deletions core/src/builtin_widgets/focus_node.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::{
events::focus_mgr::{FocusHandle, FocusType},
impl_query_self_only,
prelude::*,
};

#[derive(Default, Declare2)]
#[derive(Default, Query, Declare2)]
pub struct FocusNode {
/// Indicates that `widget` can be focused, and where it participates in
/// sequential keyboard navigation (usually with the Tab key, hence the name.
Expand Down Expand Up @@ -75,9 +74,7 @@ impl ComposeChild for FocusNode {
}
}

impl_query_self_only!(FocusNode);

#[derive(Declare2)]
#[derive(Declare2, Query)]
pub struct RequestFocus {
#[declare(default)]
handle: Option<FocusHandle>,
Expand Down Expand Up @@ -112,8 +109,6 @@ impl RequestFocus {
}
}

impl_query_self_only!(RequestFocus);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 2 additions & 4 deletions core/src/builtin_widgets/focus_scope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{events::focus_mgr::FocusType, impl_query_self_only, prelude::*};
use crate::{events::focus_mgr::FocusType, prelude::*};

#[derive(Declare2, Clone, Default)]
#[derive(Declare2, Query, Clone, Default)]
pub struct FocusScope {
/// If true, the descendants can not be focused.
/// Default value is false, then the hold FocusScope subtree can be focused
Expand Down Expand Up @@ -28,8 +28,6 @@ impl ComposeChild for FocusScope {
}
}

impl_query_self_only!(FocusScope);

#[cfg(test)]
mod tests {
use winit::{
Expand Down
6 changes: 2 additions & 4 deletions core/src/builtin_widgets/ignore_pointer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{impl_query_self_only, prelude::*};
use crate::prelude::*;

#[derive(Declare2, SingleChild, Clone)]
#[derive(Declare2, SingleChild, Query, Clone)]
pub struct IgnorePointer {
#[declare(default = true)]
pub ignore: bool,
Expand All @@ -23,8 +23,6 @@ impl Render for IgnorePointer {
}
}

impl_query_self_only!(IgnorePointer);

impl IgnorePointer {
#[inline]
pub fn new(ignore: bool) -> Self { Self { ignore } }
Expand Down
6 changes: 4 additions & 2 deletions core/src/builtin_widgets/image_widget.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{impl_query_self_only, prelude::*};
use crate::prelude::*;
use ribir_geom::{Rect, Size};

impl Render for ShareResource<PixelImage> {
Expand All @@ -17,4 +17,6 @@ impl Render for ShareResource<PixelImage> {
}
}

impl_query_self_only!(ShareResource<PixelImage>);
impl Query for ShareResource<PixelImage> {
crate::widget::impl_query_self_only!();
}
6 changes: 4 additions & 2 deletions core/src/builtin_widgets/key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{impl_query_self_only, prelude::*};
use crate::prelude::*;
use std::{
cmp::{Eq, Ord, PartialOrd},
fmt::Debug,
Expand Down Expand Up @@ -94,7 +94,9 @@ impl<V: 'static + Default + Clone + PartialEq> ComposeChild for KeyWidget<V> {
}
}

impl_query_self_only!(Box<dyn AnyKey>);
impl Query for Box<dyn AnyKey> {
crate::widget::impl_query_self_only!();
}

impl<V> KeyWidget<V>
where
Expand Down
9 changes: 2 additions & 7 deletions core/src/builtin_widgets/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use crate::{
impl_all_event, impl_compose_child_for_listener, impl_query_self_only, prelude::*,
window::WindowId,
};
use crate::{impl_all_event, impl_compose_child_for_listener, prelude::*, window::WindowId};
use rxrust::prelude::*;
use std::{convert::Infallible, rc::Rc};

define_widget_context!(LifecycleEvent);

pub type LifecycleSubject = MutRefItemSubject<'static, AllLifecycle, Infallible>;

#[derive(Default)]
#[derive(Default, Query)]
pub struct LifecycleListener {
lifecycle: LifecycleSubject,
}
Expand Down Expand Up @@ -84,8 +81,6 @@ impl LifecycleListener {
fn subject(&mut self) -> LifecycleSubject { self.lifecycle.clone() }
}

impl_query_self_only!(LifecycleListener);

impl EventListener for LifecycleListener {
type Event = AllLifecycle;
#[inline]
Expand Down
6 changes: 2 additions & 4 deletions core/src/builtin_widgets/margin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{impl_query_self_only, prelude::*};
use crate::prelude::*;

#[derive(Debug, Copy, Clone, Default, PartialEq, Lerp)]
pub struct EdgeInsets {
Expand All @@ -9,7 +9,7 @@ pub struct EdgeInsets {
}

/// A widget that create space around its child.
#[derive(SingleChild, Default, Clone, PartialEq, Declare2)]
#[derive(SingleChild, Default, Query, Clone, PartialEq, Declare2)]
pub struct Margin {
#[declare(builtin, default)]
pub margin: EdgeInsets,
Expand Down Expand Up @@ -37,8 +37,6 @@ impl Render for Margin {
fn paint(&self, _: &mut PaintingCtx) {}
}

impl_query_self_only!(Margin);

impl Margin {
#[inline]
pub fn new(margin: EdgeInsets) -> Self { Self { margin } }
Expand Down
5 changes: 1 addition & 4 deletions core/src/builtin_widgets/opacity.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use crate::impl_query_self_only;
use crate::prelude::*;

#[derive(Declare2, Default, Clone, SingleChild)]
#[derive(Declare2, Default, Query, Clone, SingleChild)]
pub struct Opacity {
#[declare(builtin, default = 1.)]
pub opacity: f32,
}

impl_query_self_only!(Opacity);

impl Render for Opacity {
#[inline]
fn perform_layout(&self, clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size {
Expand Down
6 changes: 2 additions & 4 deletions core/src/builtin_widgets/padding.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{impl_query_self_only, prelude::*};
use crate::prelude::*;

/// A widget that insets its child by the given padding.
#[derive(SingleChild, Clone, Declare2)]
#[derive(SingleChild, Query, Clone, Declare2)]
pub struct Padding {
#[declare(builtin)]
pub padding: EdgeInsets,
Expand Down Expand Up @@ -52,8 +52,6 @@ impl Render for Padding {
fn paint(&self, _: &mut PaintingCtx) {}
}

impl_query_self_only!(Padding);

impl Padding {
#[inline]
pub fn new(padding: EdgeInsets) -> Self { Self { padding } }
Expand Down
9 changes: 2 additions & 7 deletions core/src/builtin_widgets/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ impl ScrollableWidget {

#[cfg(test)]
mod tests {
use crate::{
impl_query_self_only,
test_helper::{MockBox, TestWindow},
};
use crate::test_helper::{MockBox, TestWindow};

use super::*;
use winit::event::{DeviceId, ModifiersState, MouseScrollDelta, TouchPhase, WindowEvent};
Expand Down Expand Up @@ -178,7 +175,7 @@ mod tests {
test_assert(Scrollable::Both, 100., 100., 0., 0.);
}

#[derive(SingleChild, Declare2, Clone)]
#[derive(SingleChild, Query, Declare2, Clone)]
pub struct FixedBox {
pub size: Size,
}
Expand All @@ -194,8 +191,6 @@ mod tests {
fn paint(&self, _: &mut PaintingCtx) {}
}

impl_query_self_only!(FixedBox);

#[test]
fn scroll_content_expand() {
let _guard = unsafe { AppCtx::new_lock_scope() };
Expand Down
6 changes: 4 additions & 2 deletions core/src/builtin_widgets/svg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{impl_query_self_only, prelude::*};
use crate::prelude::*;

impl Render for Svg {
#[inline]
Expand All @@ -11,4 +11,6 @@ impl Render for Svg {
}
}

impl_query_self_only!(Svg);
impl Query for Svg {
crate::widget::impl_query_self_only!();
}
Loading

0 comments on commit 8370e05

Please sign in to comment.