Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the transform related boilerplate in Xilem to a single View #828

Merged
merged 8 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
14 changes: 14 additions & 0 deletions masonry/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ impl_context_method!(
.expect("get_child_state: child not found");
child_state_ref.item
}

/// The current (local) transform of this widget.
pub fn transform(&self) -> Affine {
self.widget_state.transform
}
}
);

Expand Down Expand Up @@ -231,6 +236,15 @@ impl MutateCtx<'_> {
widget_children: self.widget_children.reborrow_mut(),
}
}

/// Whether the (local) transform of this widget has been modified since
/// the last time this widget's transformation was resolved.
///
/// This is exposed for Xilem, and is more likely to change or be removed
/// in major releases of Masonry.
pub fn transform_has_changed(&self) -> bool {
self.widget_state.transform_changed
}
}

// --- MARK: WIDGET_REF ---
Expand Down
18 changes: 11 additions & 7 deletions xilem/examples/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use std::f64::consts::{PI, TAU};

use winit::error::EventLoopError;
use xilem::view::{button, grid, label, sized_box, GridExt as _, Transformable as _};
use xilem::{Color, EventLoop, Vec2, WidgetView, Xilem};
use xilem::view::{button, grid, label, sized_box, transformed, GridExt as _};
use xilem::{Affine, Color, EventLoop, Vec2, WidgetView, Xilem};

struct TransformsGame {
rotation: f64,
Expand Down Expand Up @@ -41,14 +41,18 @@ impl TransformsGame {
[1.0, 0.0, 0.0, 0.2]
};

let status = sized_box(status).background(Color::new(bg_color));
// Every view can be transformed similar as with CSS transforms in the web.
// Currently only 2D transforms are supported.
// Note that the order of the transformations is relevant.
let transformed_status = sized_box(status)
.background(Color::new(bg_color))
.translate(self.translation)
.rotate(self.rotation)
.scale(self.scale);
let transformed_status = transformed(
// In an actual app, you wouldn't use both `transformed` and `.transform`.
// This is here to validate that Xilem's support for nested `Transformed`
// values works as expected.
status.transform(Affine::translate(self.translation)),
)
.rotate(self.rotation)
.scale(self.scale);

let controls = (
button("↶", |this: &mut Self| {
Expand Down
23 changes: 18 additions & 5 deletions xilem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use std::sync::Arc;
use masonry::dpi::LogicalSize;
use masonry::widget::{RootWidget, WidgetMut};
use masonry::{event_loop_runner, Widget, WidgetId, WidgetPod};
use view::{transformed, Transformed};
use winit::error::EventLoopError;
use winit::window::{Window, WindowAttributes};

Expand Down Expand Up @@ -188,6 +189,11 @@ where
pub struct Pod<W: Widget> {
pub widget: W,
pub id: WidgetId,
/// The transform the widget will be created with.
///
/// If changing transforms of widgets, prefer to use [`WidgetView::transformed`].
/// This has a protocol to ensure that multiple views changing the
/// transform interoperate successfully.
pub transform: Affine,
}

Expand Down Expand Up @@ -257,6 +263,18 @@ pub trait WidgetView<State, Action = ()>:
{
Box::new(self)
}

/// This widget with a 2d transform applied.
///
/// See [`transformed`] for similar functionality with a builder-API using this.
/// The return type is the same as for `transformed`, and so also has these
/// builder methods.
fn transform(self, by: Affine) -> Transformed<Self, State, Action>
where
Self: Sized,
{
transformed(self).transform(by)
}
}

impl<V, State, Action, W> WidgetView<State, Action> for V
Expand Down Expand Up @@ -321,11 +339,6 @@ impl ViewCtx {
pub fn new_pod<W: Widget>(&mut self, widget: W) -> Pod<W> {
Pod::new(widget)
}
pub fn new_pod_with_transform<W: Widget>(&mut self, widget: W, transform: Affine) -> Pod<W> {
let mut pod = Pod::new(widget);
pod.transform = transform;
pod
}

pub fn with_leaf_action_widget<E: Widget>(
&mut self,
Expand Down
30 changes: 1 addition & 29 deletions xilem/src/one_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use vello::Scene;

use crate::core::one_of::OneOf;
use crate::core::Mut;
use crate::view::Transformable;
use crate::{Affine, Pod, ViewCtx};
use crate::{Pod, ViewCtx};

impl<
A: Widget,
Expand Down Expand Up @@ -143,33 +142,6 @@ impl<
}
}

impl<A, B, C, D, E, F, G, H, I> Transformable for OneOf<A, B, C, D, E, F, G, H, I>
where
A: Transformable,
B: Transformable,
C: Transformable,
D: Transformable,
E: Transformable,
F: Transformable,
G: Transformable,
H: Transformable,
I: Transformable,
{
fn transform_mut(&mut self) -> &mut Affine {
match self {
Self::A(w) => w.transform_mut(),
Self::B(w) => w.transform_mut(),
Self::C(w) => w.transform_mut(),
Self::D(w) => w.transform_mut(),
Self::E(w) => w.transform_mut(),
Self::F(w) => w.transform_mut(),
Self::G(w) => w.transform_mut(),
Self::H(w) => w.transform_mut(),
Self::I(w) => w.transform_mut(),
}
}
}

impl crate::core::one_of::PhantomElementCtx for ViewCtx {
type PhantomElement = Pod<Box<dyn Widget>>;
}
Expand Down
22 changes: 2 additions & 20 deletions xilem/src/view/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use xilem_core::ViewPathTracker;

use crate::core::{DynMessage, Mut, View, ViewMarker};
use crate::view::Label;
use crate::{Affine, MessageResult, Pod, ViewCtx, ViewId};

use super::Transformable;
use crate::{MessageResult, Pod, ViewCtx, ViewId};

/// A button which calls `callback` when the primary mouse button (normally left) is pressed.
pub fn button<State, Action>(
Expand All @@ -19,7 +17,6 @@ pub fn button<State, Action>(
{
Button {
label: label.into(),
transform: Affine::IDENTITY,
callback: move |state: &mut State, button| match button {
PointerButton::Primary => MessageResult::Action(callback(state)),
_ => MessageResult::Nop,
Expand All @@ -35,7 +32,6 @@ pub fn button_any_pointer<State, Action>(
{
Button {
label: label.into(),
transform: Affine::IDENTITY,
callback: move |state: &mut State, button| MessageResult::Action(callback(state, button)),
}
}
Expand All @@ -45,16 +41,9 @@ pub struct Button<F> {
// N.B. This widget is *implemented* to handle any kind of view with an element
// type of `Label` even though it currently does not do so.
label: Label,
transform: Affine,
callback: F,
}

impl<F> Transformable for Button<F> {
fn transform_mut(&mut self) -> &mut Affine {
&mut self.transform
}
}

const LABEL_VIEW_ID: ViewId = ViewId::new(0);

impl<F> ViewMarker for Button<F> {}
Expand All @@ -70,10 +59,7 @@ where
View::<State, Action, _>::build(&self.label, ctx)
});
ctx.with_leaf_action_widget(|ctx| {
ctx.new_pod_with_transform(
widget::Button::from_label_pod(child.into_widget_pod()),
self.transform,
)
ctx.new_pod(widget::Button::from_label_pod(child.into_widget_pod()))
})
}

Expand All @@ -84,10 +70,6 @@ where
ctx: &mut ViewCtx,
mut element: Mut<Self::Element>,
) {
if prev.transform != self.transform {
element.set_transform(self.transform);
}

ctx.with_id(LABEL_VIEW_ID, |ctx| {
View::<State, Action, _>::rebuild(
&self.label,
Expand Down
20 changes: 2 additions & 18 deletions xilem/src/view/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use masonry::text::ArcStr;
use masonry::widget;

use crate::core::{DynMessage, Mut, ViewMarker};
use crate::{Affine, MessageResult, Pod, View, ViewCtx, ViewId};

use super::Transformable;
use crate::{MessageResult, Pod, View, ViewCtx, ViewId};

pub fn checkbox<F, State, Action>(
label: impl Into<ArcStr>,
Expand All @@ -21,7 +19,6 @@ where
label: label.into(),
callback,
checked,
transform: Affine::IDENTITY,
}
}

Expand All @@ -30,13 +27,6 @@ pub struct Checkbox<F> {
label: ArcStr,
checked: bool,
callback: F,
transform: Affine,
}

impl<F> Transformable for Checkbox<F> {
fn transform_mut(&mut self) -> &mut Affine {
&mut self.transform
}
}

impl<F> ViewMarker for Checkbox<F> {}
Expand All @@ -49,10 +39,7 @@ where

fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
ctx.with_leaf_action_widget(|ctx| {
ctx.new_pod_with_transform(
widget::Checkbox::new(self.checked, self.label.clone()),
self.transform,
)
ctx.new_pod(widget::Checkbox::new(self.checked, self.label.clone()))
})
}

Expand All @@ -63,9 +50,6 @@ where
_ctx: &mut ViewCtx,
mut element: Mut<Self::Element>,
) {
if prev.transform != self.transform {
element.set_transform(self.transform);
}
if prev.label != self.label {
widget::Checkbox::set_text(&mut element, self.label.clone());
}
Expand Down
17 changes: 2 additions & 15 deletions xilem/src/view/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ use crate::core::{
AppendVec, DynMessage, ElementSplice, MessageResult, Mut, SuperElement, View, ViewElement,
ViewId, ViewMarker, ViewPathTracker, ViewSequence,
};
use crate::{Affine, AnyWidgetView, Pod, ViewCtx, WidgetView};
use crate::{AnyWidgetView, Pod, ViewCtx, WidgetView};

pub fn flex<State, Action, Seq: FlexSequence<State, Action>>(
sequence: Seq,
) -> Flex<Seq, State, Action> {
Flex {
sequence,
axis: Axis::Vertical,
transform: Affine::IDENTITY,
cross_axis_alignment: CrossAxisAlignment::Center,
main_axis_alignment: MainAxisAlignment::Start,
fill_major_axis: false,
Expand All @@ -32,7 +31,6 @@ pub fn flex<State, Action, Seq: FlexSequence<State, Action>>(
pub struct Flex<Seq, State, Action = ()> {
sequence: Seq,
axis: Axis,
transform: Affine,
cross_axis_alignment: CrossAxisAlignment,
main_axis_alignment: MainAxisAlignment,
fill_major_axis: bool,
Expand Down Expand Up @@ -87,12 +85,6 @@ impl<Seq, State, Action> Flex<Seq, State, Action> {
}
}

impl<Seq, State, Action> Transformable for Flex<Seq, State, Action> {
fn transform_mut(&mut self) -> &mut Affine {
&mut self.transform
}
}

impl<Seq, State, Action> ViewMarker for Flex<Seq, State, Action> {}
impl<State, Action, Seq> View<State, Action, ViewCtx> for Flex<Seq, State, Action>
where
Expand Down Expand Up @@ -121,7 +113,7 @@ where
FlexElement::FlexSpacer(flex) => widget.with_flex_spacer(flex),
}
}
let pod = ctx.new_pod_with_transform(widget, self.transform);
let pod = ctx.new_pod(widget);
(pod, seq_state)
}

Expand All @@ -132,9 +124,6 @@ where
ctx: &mut ViewCtx,
mut element: Mut<Self::Element>,
) {
if prev.transform != self.transform {
element.set_transform(self.transform);
}
if prev.axis != self.axis {
widget::Flex::set_direction(&mut element, self.axis);
}
Expand Down Expand Up @@ -646,8 +635,6 @@ mod hidden {
}
use hidden::AnyFlexChildState;

use super::Transformable;

impl<State, Action> ViewMarker for AnyFlexChild<State, Action> {}
impl<State, Action> View<State, Action, ViewCtx> for AnyFlexChild<State, Action>
where
Expand Down
Loading
Loading