apply new_env to all trait methods
//! A widget that themes its child.
use crate::{
- BaseState, BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, PaintCtx, Rect, Size,
- UpdateCtx, Widget, WidgetPod,
+ BaseState, BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, PaintCtx, Size, UpdateCtx,
+ Widget,
};
-use crate::kurbo::Point;
-
/// A widget that accepts a closure to update the theme for its child.
pub struct StyleChild<T: Data, F: Fn(&mut Env) + 'static> {
f: F,
- child: WidgetPod<T, Box<dyn Widget<T>>>,
+ child: Box<dyn Widget<T>>,
}
impl<T: Data, F: Fn(&mut Env) + 'static> StyleChild<T, F> {
@@ -32,24 +30,37 @@ impl<T: Data, F: Fn(&mut Env) + 'static> StyleChild<T, F> {
///
/// Accepts a closure that sets theme values:
/// ```
+ /// # use druid::{theme, Widget};
+ /// # use druid::piet::{Color};
+ /// # use druid::widget::{Label, StyleChild};
+ ///
+ /// # fn build_width() -> impl Widget<String> {
+ ///
+ /// StyleChild::new(
/// |env| {
/// env.set(theme::LABEL_COLOR, Color::WHITE);
- /// }
+ /// },
+ /// Label::new("White text!")
+ /// )
+ ///
+ /// # }
+ ///
+
/// ```
pub fn new(f: F, child: impl Widget<T> + 'static) -> StyleChild<T, F> {
StyleChild {
f,
- child: WidgetPod::new(child).boxed(),
+ child: Box::new(child),
}
}
}
impl<T: Data, F: Fn(&mut Env) + 'static> Widget<T> for StyleChild<T, F> {
- fn paint(&mut self, paint_ctx: &mut PaintCtx, _base_state: &BaseState, data: &T, env: &Env) {
+ fn paint(&mut self, paint_ctx: &mut PaintCtx, base_state: &BaseState, data: &T, env: &Env) {
let mut new_env = env.clone();
(self.f)(&mut new_env);
- self.child.paint_with_offset(paint_ctx, data, &new_env);
+ self.child.paint(paint_ctx, base_state, data, &new_env);
}
fn layout(
@@ -61,18 +72,23 @@ impl<T: Data, F: Fn(&mut Env) + 'static> Widget<T> for StyleChild<T, F> {
) -> Size {
bc.debug_check("StyleChild");
- let size = self.child.layout(layout_ctx, &bc, data, env);
- self.child
- .set_layout_rect(Rect::from_origin_size(Point::ORIGIN, size));
+ let mut new_env = env.clone();
+ (self.f)(&mut new_env);
- size
+ self.child.layout(layout_ctx, &bc, data, &new_env)
}
fn event(&mut self, event: &Event, ctx: &mut EventCtx, data: &mut T, env: &Env) {
- self.child.event(event, ctx, data, env)
+ let mut new_env = env.clone();
+ (self.f)(&mut new_env);
+
+ self.child.event(event, ctx, data, &new_env)
}
- fn update(&mut self, ctx: &mut UpdateCtx, _old_data: Option<&T>, data: &T, env: &Env) {
- self.child.update(ctx, data, env);
+ fn update(&mut self, ctx: &mut UpdateCtx, old_data: Option<&T>, data: &T, env: &Env) {
+ let mut new_env = env.clone();
+ (self.f)(&mut new_env);
+
+ self.child.update(ctx, old_data, data, &new_env);
}
}
