Skip to content

Commit

Permalink
Make extension method .transform
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Jan 17, 2025
1 parent 05190d6 commit 77aebb4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
20 changes: 9 additions & 11 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 _};
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,20 +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))
.transformed()
.translate(self.translation)
// In an actual app, you wouldn't use `.transformed` here.
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.
.transformed()
.rotate(self.rotation)
.transformed()
.scale(self.scale);
status.transform(Affine::translate(self.translation)),
)
.rotate(self.rotation)
.scale(self.scale);

let controls = (
button("↶", |this: &mut Self| {
Expand Down
10 changes: 5 additions & 5 deletions xilem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,14 @@ pub trait WidgetView<State, Action = ()>:

/// This widget with a 2d transform applied.
///
/// The returned view has methods to control individual components of
/// the transform, as well as apply an arbitrary transform.
/// See [`transformed`] for more details.
fn transformed(self) -> Transformed<Self, State, Action>
/// 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)
transformed(self).transform(by)
}
}

Expand Down

0 comments on commit 77aebb4

Please sign in to comment.