Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Remove Env type and Data trait (#54)
Browse files Browse the repository at this point in the history
Fix some clippy lints
Update text snapshots
Update documentation and examples
Update roadmap
Add debug properties to PaintCtx
Add maybe_eq method to TextStorage as a monkey-patch

---

Based on PR #53
by: Shekhinah Memmel <[email protected]>
  • Loading branch information
PoignardAzur authored Mar 26, 2024
1 parent 4ae3309 commit 2d27d8c
Show file tree
Hide file tree
Showing 55 changed files with 746 additions and 2,384 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl AppDelegate for Delegate {
_window_id: WindowId,
_widget_id: WidgetId,
action: Action,
_env: &Env,
) {
match action {
Action::ButtonPressed => {
Expand Down Expand Up @@ -145,7 +144,7 @@ Issues and PRs are welcome. See [`help-wanted` issues](https://github.com/Poigna

The immediate next steps are:

- [ ] Remove Env type and Data trait (#8)
- [X] Remove Env type and Data trait (#8)
- [ ] Re-add Dialog feature (#25)
- [ ] Switch to using Vello and Glazier (#24)
- [ ] Refactor TextLayout (#23)
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

# TODO - Next steps

- [ ] Remove Env type and Data trait (#8)
- [X] Remove Env type and Data trait (#8)
- [ ] Re-add Dialog feature (#25)
- [ ] Switch to using Vello and Glazier (#24)
- [ ] Refactor TextLayout (#23)
Expand Down
24 changes: 12 additions & 12 deletions examples/blocking_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl MainWidget {
}

impl Widget for MainWidget {
fn on_event(&mut self, ctx: &mut EventCtx, event: &Event, env: &Env) {
fn on_event(&mut self, ctx: &mut EventCtx, event: &Event) {
match event {
Event::MouseDown(_) => {
if !ctx.is_disabled() {
Expand All @@ -62,7 +62,7 @@ impl Widget for MainWidget {
.expect("command failed to submit");
});

self.content.on_event(ctx, event, env);
self.content.on_event(ctx, event);
let mut flex_mut = ctx.get_mut(&mut self.content);
flex_mut.clear();
flex_mut.add_child(Spinner::new());
Expand All @@ -74,7 +74,7 @@ impl Widget for MainWidget {
Event::Command(cmd) if cmd.is(FINISH_SLOW_FUNCTION) => {
let value = *cmd.get(FINISH_SLOW_FUNCTION);

self.content.on_event(ctx, event, env);
self.content.on_event(ctx, event);

let mut flex_mut = ctx.get_mut(&mut self.content);
flex_mut.clear();
Expand All @@ -87,23 +87,23 @@ impl Widget for MainWidget {
}
_ => (),
}
self.content.on_event(ctx, event, env);
self.content.on_event(ctx, event);
}

fn on_status_change(&mut self, _ctx: &mut LifeCycleCtx, _event: &StatusChange, _env: &Env) {}
fn on_status_change(&mut self, _ctx: &mut LifeCycleCtx, _event: &StatusChange) {}

fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, env: &Env) {
self.content.lifecycle(ctx, event, env);
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle) {
self.content.lifecycle(ctx, event);
}

fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, env: &Env) -> Size {
let content_size = self.content.layout(ctx, bc, env);
ctx.place_child(&mut self.content, Point::ORIGIN, env);
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
let content_size = self.content.layout(ctx, bc);
ctx.place_child(&mut self.content, Point::ORIGIN);
content_size
}

fn paint(&mut self, ctx: &mut PaintCtx, env: &Env) {
self.content.paint(ctx, env);
fn paint(&mut self, ctx: &mut PaintCtx) {
self.content.paint(ctx);
}

fn children(&self) -> SmallVec<[WidgetRef<'_, dyn Widget>; 16]> {
Expand Down
26 changes: 12 additions & 14 deletions examples/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use std::sync::Arc;

use masonry::widget::{Align, CrossAxisAlignment, Flex, Label, SizedBox, WidgetRef};
use masonry::{
Action, AppDelegate, AppLauncher, BoxConstraints, Color, Env, Event, EventCtx, LayoutCtx,
LifeCycle, LifeCycleCtx, PaintCtx, Point, Size, StatusChange, Widget, WidgetPod,
WindowDescription,
Action, AppDelegate, AppLauncher, BoxConstraints, Color, Event, EventCtx, LayoutCtx, LifeCycle,
LifeCycleCtx, PaintCtx, Point, Size, StatusChange, Widget, WidgetPod, WindowDescription,
};
use smallvec::{smallvec, SmallVec};
use tracing::{trace, trace_span, Span};
Expand Down Expand Up @@ -138,7 +137,7 @@ impl CalcButton {
}

impl Widget for CalcButton {
fn on_event(&mut self, ctx: &mut EventCtx, event: &Event, env: &Env) {
fn on_event(&mut self, ctx: &mut EventCtx, event: &Event) {
match event {
Event::MouseDown(_) => {
if !ctx.is_disabled() {
Expand All @@ -160,10 +159,10 @@ impl Widget for CalcButton {
}
_ => (),
}
self.inner.on_event(ctx, event, env);
self.inner.on_event(ctx, event);
}

fn on_status_change(&mut self, ctx: &mut LifeCycleCtx, event: &StatusChange, _env: &Env) {
fn on_status_change(&mut self, ctx: &mut LifeCycleCtx, event: &StatusChange) {
match event {
StatusChange::HotChanged(true) => {
ctx.get_mut(&mut self.inner).set_border(Color::WHITE, 3.0);
Expand All @@ -178,19 +177,19 @@ impl Widget for CalcButton {
}
}

fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, env: &Env) {
self.inner.lifecycle(ctx, event, env)
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle) {
self.inner.lifecycle(ctx, event)
}

fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, env: &Env) -> Size {
let size = self.inner.layout(ctx, bc, env);
ctx.place_child(&mut self.inner, Point::ORIGIN, env);
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
let size = self.inner.layout(ctx, bc);
ctx.place_child(&mut self.inner, Point::ORIGIN);

size
}

fn paint(&mut self, ctx: &mut PaintCtx, env: &Env) {
self.inner.paint(ctx, env);
fn paint(&mut self, ctx: &mut PaintCtx) {
self.inner.paint(ctx);
}

fn children(&self) -> SmallVec<[WidgetRef<'_, dyn Widget>; 16]> {
Expand All @@ -209,7 +208,6 @@ impl AppDelegate for CalcState {
_window_id: masonry::WindowId,
_widget_id: masonry::WidgetId,
action: Action,
_env: &Env,
) {
match action {
Action::Other(payload) => match payload.downcast_ref::<CalcAction>().unwrap() {
Expand Down
14 changes: 7 additions & 7 deletions examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use masonry::piet::{FontFamily, ImageFormat, InterpolationMode, Text, TextLayout
use masonry::text::{FontDescriptor, TextLayout};
use masonry::widget::WidgetRef;
use masonry::{
Affine, AppLauncher, BoxConstraints, Color, Env, Event, EventCtx, LayoutCtx, LifeCycle,
Affine, AppLauncher, BoxConstraints, Color, Event, EventCtx, LayoutCtx, LifeCycle,
LifeCycleCtx, PaintCtx, Point, Rect, RenderContext, Size, StatusChange, Widget,
WindowDescription,
};
Expand All @@ -26,13 +26,13 @@ struct CustomWidget(String);
// (and lifecycle) methods as well to make sure it works. Some things can be filtered,
// but a general rule is to just pass it through unless you really know you don't want it.
impl Widget for CustomWidget {
fn on_event(&mut self, _ctx: &mut EventCtx, _event: &Event, _env: &Env) {}
fn on_event(&mut self, _ctx: &mut EventCtx, _event: &Event) {}

fn lifecycle(&mut self, _ctx: &mut LifeCycleCtx, _event: &LifeCycle, _env: &Env) {}
fn lifecycle(&mut self, _ctx: &mut LifeCycleCtx, _event: &LifeCycle) {}

fn on_status_change(&mut self, _ctx: &mut LifeCycleCtx, _event: &StatusChange, _env: &Env) {}
fn on_status_change(&mut self, _ctx: &mut LifeCycleCtx, _event: &StatusChange) {}

fn layout(&mut self, _layout_ctx: &mut LayoutCtx, bc: &BoxConstraints, _env: &Env) -> Size {
fn layout(&mut self, _layout_ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
// BoxConstraints are passed by the parent widget.
// This method can return any Size within those constraints:
// bc.constrain(my_size)
Expand All @@ -55,7 +55,7 @@ impl Widget for CustomWidget {
// The paint method gets called last, after an event flow.
// It goes event -> update -> layout -> paint, and each method can influence the next.
// Basically, anything that changes the appearance of a widget causes a paint.
fn paint(&mut self, ctx: &mut PaintCtx, env: &Env) {
fn paint(&mut self, ctx: &mut PaintCtx) {
// Clear the whole widget with the color of your choice
// (ctx.size() returns the size of the layout rect we're painting in)
// Note: ctx also has a `clear` method, but that clears the whole context,
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Widget for CustomWidget {
let mut layout = TextLayout::<String>::from_text(&self.0);
layout.set_font(FontDescriptor::new(FontFamily::SERIF).with_size(24.0));
layout.set_text_color(fill_color);
layout.rebuild_if_needed(ctx.text(), env);
layout.rebuild_if_needed(ctx.text());

// Let's rotate our text slightly. First we save our current (default) context:
ctx.with_save(|ctx| {
Expand Down
1 change: 0 additions & 1 deletion examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ impl AppDelegate for Delegate {
_window_id: WindowId,
_widget_id: WidgetId,
action: Action,
_env: &Env,
) {
if let Action::ButtonPressed = action {
println!("Hello");
Expand Down
22 changes: 11 additions & 11 deletions examples/promise_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use masonry::text::TextLayout;
use masonry::widget::prelude::*;
use masonry::widget::WidgetRef;
use masonry::{AppLauncher, WindowDescription};
use masonry::{ArcStr, Color, KeyOrValue, Point};
use masonry::{ArcStr, Color, Point};
use smallvec::SmallVec;
use tracing::{trace, trace_span, Span};

Expand All @@ -28,7 +28,7 @@ pub struct PromiseButton {
line_break_mode: LineBreaking,
promise_token: PromiseToken<u32>,

default_text_color: KeyOrValue<Color>,
default_text_color: Color,
}

/// Options for handling lines that are too wide for the label.
Expand All @@ -55,15 +55,15 @@ impl PromiseButton {
text_layout,
line_break_mode: LineBreaking::Overflow,
promise_token: PromiseToken::empty(),
default_text_color: masonry::theme::TEXT_COLOR.into(),
default_text_color: masonry::theme::TEXT_COLOR,
}
}
}

// --- TRAIT IMPLS ---

impl Widget for PromiseButton {
fn on_event(&mut self, ctx: &mut EventCtx, event: &Event, _env: &Env) {
fn on_event(&mut self, ctx: &mut EventCtx, event: &Event) {
match event {
Event::MouseUp(_event) => {
let value = self.value;
Expand Down Expand Up @@ -97,15 +97,15 @@ impl Widget for PromiseButton {
}
}

fn on_status_change(&mut self, _ctx: &mut LifeCycleCtx, _event: &StatusChange, _env: &Env) {}
fn on_status_change(&mut self, _ctx: &mut LifeCycleCtx, _event: &StatusChange) {}

fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, _env: &Env) {
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle) {
match event {
LifeCycle::DisabledChanged(disabled) => {
let color = if *disabled {
KeyOrValue::Key(masonry::theme::DISABLED_TEXT_COLOR)
masonry::theme::DISABLED_TEXT_COLOR
} else {
self.default_text_color.clone()
self.default_text_color
};
self.text_layout.set_text_color(color);
ctx.request_layout();
Expand All @@ -114,14 +114,14 @@ impl Widget for PromiseButton {
}
}

fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, env: &Env) -> Size {
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
let width = match self.line_break_mode {
LineBreaking::WordWrap => bc.max().width - LABEL_X_PADDING * 2.0,
_ => f64::INFINITY,
};

self.text_layout.set_wrap_width(width);
self.text_layout.rebuild_if_needed(ctx.text(), env);
self.text_layout.rebuild_if_needed(ctx.text());

let text_metrics = self.text_layout.layout_metrics();
ctx.set_baseline_offset(text_metrics.size.height - text_metrics.first_baseline);
Expand All @@ -133,7 +133,7 @@ impl Widget for PromiseButton {
size
}

fn paint(&mut self, ctx: &mut PaintCtx, _env: &Env) {
fn paint(&mut self, ctx: &mut PaintCtx) {
let origin = Point::new(LABEL_X_PADDING, 0.0);
let label_size = ctx.size();

Expand Down
1 change: 0 additions & 1 deletion examples/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ impl AppDelegate for Delegate {
_window_id: WindowId,
_widget_id: WidgetId,
action: Action,
_env: &Env,
) {
match action {
Action::ButtonPressed => {
Expand Down
4 changes: 1 addition & 3 deletions examples/to_do_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use masonry::widget::{
Button, CrossAxisAlignment, Flex, Label, Portal, SizedBox, TextBox, WidgetMut,
};
use masonry::{
Action, AppDelegate, AppLauncher, Color, DelegateCtx, Env, WidgetId, WindowDescription,
WindowId,
Action, AppDelegate, AppLauncher, Color, DelegateCtx, WidgetId, WindowDescription, WindowId,
};

struct Delegate {
Expand All @@ -23,7 +22,6 @@ impl AppDelegate for Delegate {
_window_id: WindowId,
_widget_id: WidgetId,
action: Action,
_env: &Env,
) {
match action {
Action::ButtonPressed | Action::TextEntered(_) => {
Expand Down
3 changes: 2 additions & 1 deletion src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl PartialEq for Action {
(Self::TextChanged(l0), Self::TextChanged(r0)) => l0 == r0,
(Self::TextEntered(l0), Self::TextEntered(r0)) => l0 == r0,
(Self::CheckboxChecked(l0), Self::CheckboxChecked(r0)) => l0 == r0,
#[allow(clippy::vtable_address_comparisons)]
#[allow(ambiguous_wide_pointer_comparisons)]
// FIXME
(Self::Other(val_l), Self::Other(val_r)) => Arc::ptr_eq(val_l, val_r),
_ => false,
}
Expand Down
18 changes: 5 additions & 13 deletions src/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use crate::command::{Command, CommandQueue};
use crate::ext_event::{ExtEventQueue, ExtEventSink};
use crate::widget::{StoreInWidgetMut, WidgetMut, WidgetRef};
use crate::{
Env, Event, Handled, SingleUse, Target, Widget, WidgetId, WindowDescription, WindowId,
WindowRoot,
Event, Handled, SingleUse, Target, Widget, WidgetId, WindowDescription, WindowId, WindowRoot,
};

/// A context provided to [`AppDelegate`] methods.
Expand Down Expand Up @@ -77,13 +76,7 @@ pub trait AppDelegate {
///
/// This function receives all non-command events, before they are passed down
/// the tree. If it returns [`Handled::Yes`], events are short-circuited.
fn on_event(
&mut self,
ctx: &mut DelegateCtx,
window_id: WindowId,
event: &Event,
env: &Env,
) -> Handled {
fn on_event(&mut self, ctx: &mut DelegateCtx, window_id: WindowId, event: &Event) -> Handled {
#![allow(unused)]
Handled::No
}
Expand All @@ -92,7 +85,7 @@ pub trait AppDelegate {
///
/// This function receives all command events, before they are passed down
/// the tree. If it returns [`Handled::Yes`], commands are short-circuited.
fn on_command(&mut self, ctx: &mut DelegateCtx, cmd: &Command, env: &Env) -> Handled {
fn on_command(&mut self, ctx: &mut DelegateCtx, cmd: &Command) -> Handled {
#![allow(unused)]
Handled::No
}
Expand All @@ -106,7 +99,6 @@ pub trait AppDelegate {
window_id: WindowId,
widget_id: WidgetId,
action: Action,
env: &Env,
) {
#![allow(unused)]
}
Expand All @@ -115,14 +107,14 @@ pub trait AppDelegate {
///
/// This function is called after a window has been added,
/// allowing you to customize the window creation behavior of your app.
fn on_window_added(&mut self, ctx: &mut DelegateCtx, id: WindowId, env: &Env) {
fn on_window_added(&mut self, ctx: &mut DelegateCtx, id: WindowId) {
#![allow(unused)]
}

/// The handler for window deletion events.
///
/// This function is called after a window has been removed.
fn on_window_removed(&mut self, ctx: &mut DelegateCtx, id: WindowId, env: &Env) {
fn on_window_removed(&mut self, ctx: &mut DelegateCtx, id: WindowId) {
#![allow(unused)]
}
}
Expand Down
Loading

0 comments on commit 2d27d8c

Please sign in to comment.