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

Wrap EnvScope child in WidgetPod and pass down the new Env during layout. #1100

Merged
merged 3 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ You can find its changes [documented below](#060---2020-06-01).
- GTK: Don't interrupt `KeyEvent.repeat` when releasing another key. ([#1081] by [@raphlinus])
- X11: Set some more common window properties. ([#1097] by [@psychon])
- X11: Support timers. ([#1096] by [@psychon])
- `EnvScope` now also updates the `Env` during `Widget::lifecycle`. ([#1100] by [@finnerale])
- `WidgetExt::debug_widget_id` and `debug_paint_layout` now also apply to the widget they are called on. ([#1100] by [@finnerale])

### Visual

Expand Down Expand Up @@ -365,6 +367,7 @@ Last release without a changelog :(
[#1081]: https://github.com/linebender/druid/pull/1081
[#1096]: https://github.com/linebender/druid/pull/1096
[#1097]: https://github.com/linebender/druid/pull/1097
[#1100]: https://github.com/linebender/druid/pull/1100

[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0
Expand Down
30 changes: 11 additions & 19 deletions druid/src/widget/env_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
use crate::kurbo::Size;
use crate::{
BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx,
UpdateCtx, Widget, WidgetId,
UpdateCtx, Widget, WidgetPod,
};

/// A widget that accepts a closure to update the environment for its child.
pub struct EnvScope<T, W> {
pub(crate) f: Box<dyn Fn(&mut Env, &T)>,
pub(crate) child: W,
pub(crate) child: WidgetPod<T, W>,
}

impl<T, W> EnvScope<T, W> {
impl<T, W: Widget<T>> EnvScope<T, W> {
/// Create a widget that updates the environment for its descendants.
///
/// Accepts a closure that sets Env values.
Expand All @@ -53,7 +53,7 @@ impl<T, W> EnvScope<T, W> {
pub fn new(f: impl Fn(&mut Env, &T) + 'static, child: W) -> EnvScope<T, W> {
EnvScope {
f: Box::new(f),
child,
child: WidgetPod::new(child),
}
}
}
Expand All @@ -69,29 +69,25 @@ impl<T: Data, W: Widget<T>> Widget<T> for EnvScope<T, W> {
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) {
let mut new_env = env.clone();
(self.f)(&mut new_env, &data);
self.child.lifecycle(ctx, event, data, env)
self.child.lifecycle(ctx, event, data, &new_env)
}

fn update(&mut self, ctx: &mut UpdateCtx, old_data: &T, data: &T, env: &Env) {
fn update(&mut self, ctx: &mut UpdateCtx, _old_data: &T, data: &T, env: &Env) {
let mut new_env = env.clone();
(self.f)(&mut new_env, &data);

self.child.update(ctx, old_data, data, &new_env);
self.child.update(ctx, data, &new_env);
}

fn layout(
&mut self,
layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
data: &T,
env: &Env,
) -> Size {
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size {
bc.debug_check("EnvScope");

let mut new_env = env.clone();
(self.f)(&mut new_env, &data);

self.child.layout(layout_ctx, &bc, data, &new_env)
let size = self.child.layout(ctx, &bc, data, &new_env);
self.child.set_layout_rect(ctx, data, env, size.to_rect());
size
}

fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) {
Expand All @@ -100,8 +96,4 @@ impl<T: Data, W: Widget<T>> Widget<T> for EnvScope<T, W> {

self.child.paint(ctx, data, &new_env);
}

fn id(&self) -> Option<WidgetId> {
self.child.id()
}
}