Skip to content

Commit

Permalink
Reserve ability to create Pod to ViewCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
PoignardAzur committed Sep 18, 2024
1 parent 0d56c59 commit c6f60a9
Show file tree
Hide file tree
Showing 26 changed files with 94 additions and 93 deletions.
12 changes: 6 additions & 6 deletions xilem/src/any_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ use crate::{Pod, ViewCtx};
pub type AnyWidgetView<State, Action = ()> =
dyn AnyView<State, Action, ViewCtx, Pod<DynWidget>> + Send + Sync;

impl<W: Widget> SuperElement<Pod<W>> for Pod<DynWidget> {
fn upcast(child: Pod<W>) -> Self {
WidgetPod::new(DynWidget {
inner: child.inner.boxed(),
impl<W: Widget> SuperElement<Pod<W>, ViewCtx> for Pod<DynWidget> {
fn upcast(ctx: &mut ViewCtx, child: Pod<W>) -> Self {
let boxed_pod = ctx.boxed_pod(child);
ctx.new_pod(DynWidget {
inner: boxed_pod.inner.boxed(),
})
.into()
}

fn with_downcast_val<R>(
Expand All @@ -47,7 +47,7 @@ impl<W: Widget> SuperElement<Pod<W>> for Pod<DynWidget> {
}
}

impl<W: Widget> AnyElement<Pod<W>> for Pod<DynWidget> {
impl<W: Widget> AnyElement<Pod<W>, ViewCtx> for Pod<DynWidget> {
fn replace_inner(mut this: Self::Mut<'_>, child: Pod<W>) -> Self::Mut<'_> {
DynWidget::replace_inner(&mut this, child.inner.boxed());
this
Expand Down
31 changes: 15 additions & 16 deletions xilem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,26 +156,13 @@ pub struct Pod<W: Widget> {
pub inner: WidgetPod<W>,
}

impl<W: Widget> Pod<W> {
/// Create a new `Pod` for `inner`.
pub fn new(inner: W) -> Self {
Self::from(WidgetPod::new(inner))
}
}

impl<W: Widget> From<WidgetPod<W>> for Pod<W> {
fn from(inner: WidgetPod<W>) -> Self {
Pod { inner }
}
}

impl<W: Widget> ViewElement for Pod<W> {
type Mut<'a> = WidgetMut<'a, W>;
}

impl<W: Widget> SuperElement<Pod<W>> for Pod<Box<dyn Widget>> {
fn upcast(child: Pod<W>) -> Self {
child.inner.boxed().into()
impl<W: Widget> SuperElement<Pod<W>, ViewCtx> for Pod<Box<dyn Widget>> {
fn upcast(ctx: &mut ViewCtx, child: Pod<W>) -> Self {
ctx.boxed_pod(child)
}

fn with_downcast_val<R>(
Expand Down Expand Up @@ -274,6 +261,18 @@ impl ViewPathTracker for ViewCtx {
}

impl ViewCtx {
pub fn new_pod<W: Widget>(&mut self, widget: W) -> Pod<W> {
Pod {
inner: WidgetPod::new(widget),
}
}

pub fn boxed_pod<W: Widget>(&mut self, pod: Pod<W>) -> Pod<Box<dyn Widget>> {
Pod {
inner: pod.inner.boxed(),
}
}

pub fn mark_changed(&mut self) {
if cfg!(debug_assertions) {
self.view_tree_changed = true;
Expand Down
19 changes: 10 additions & 9 deletions xilem/src/one_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl<
}
}
fn upcast_one_of_element(
&mut self,
elem: xilem_core::one_of::OneOf<
Pod<A>,
Pod<B>,
Expand All @@ -133,15 +134,15 @@ impl<
>,
) -> Self::OneOfElement {
match elem {
xilem_core::one_of::OneOf::A(w) => Pod::new(OneOfWidget::A(w.inner)),
xilem_core::one_of::OneOf::B(w) => Pod::new(OneOfWidget::B(w.inner)),
xilem_core::one_of::OneOf::C(w) => Pod::new(OneOfWidget::C(w.inner)),
xilem_core::one_of::OneOf::D(w) => Pod::new(OneOfWidget::D(w.inner)),
xilem_core::one_of::OneOf::E(w) => Pod::new(OneOfWidget::E(w.inner)),
xilem_core::one_of::OneOf::F(w) => Pod::new(OneOfWidget::F(w.inner)),
xilem_core::one_of::OneOf::G(w) => Pod::new(OneOfWidget::G(w.inner)),
xilem_core::one_of::OneOf::H(w) => Pod::new(OneOfWidget::H(w.inner)),
xilem_core::one_of::OneOf::I(w) => Pod::new(OneOfWidget::I(w.inner)),
xilem_core::one_of::OneOf::A(w) => self.new_pod(OneOfWidget::A(w.inner)),
xilem_core::one_of::OneOf::B(w) => self.new_pod(OneOfWidget::B(w.inner)),
xilem_core::one_of::OneOf::C(w) => self.new_pod(OneOfWidget::C(w.inner)),
xilem_core::one_of::OneOf::D(w) => self.new_pod(OneOfWidget::D(w.inner)),
xilem_core::one_of::OneOf::E(w) => self.new_pod(OneOfWidget::E(w.inner)),
xilem_core::one_of::OneOf::F(w) => self.new_pod(OneOfWidget::F(w.inner)),
xilem_core::one_of::OneOf::G(w) => self.new_pod(OneOfWidget::G(w.inner)),
xilem_core::one_of::OneOf::H(w) => self.new_pod(OneOfWidget::H(w.inner)),
xilem_core::one_of::OneOf::I(w) => self.new_pod(OneOfWidget::I(w.inner)),
}
}

Expand Down
2 changes: 1 addition & 1 deletion xilem/src/view/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
type ViewState = ();

fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
ctx.with_leaf_action_widget(|_| Pod::new(widget::Button::new(self.label.clone())))
ctx.with_leaf_action_widget(|ctx| ctx.new_pod(widget::Button::new(self.label.clone())))
}

fn rebuild<'el>(
Expand Down
4 changes: 2 additions & 2 deletions xilem/src/view/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ where
type ViewState = ();

fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
ctx.with_leaf_action_widget(|_| {
Pod::new(masonry::widget::Checkbox::new(
ctx.with_leaf_action_widget(|ctx| {
ctx.new_pod(masonry::widget::Checkbox::new(
self.checked,
self.label.clone(),
))
Expand Down
17 changes: 7 additions & 10 deletions xilem/src/view/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ where
FlexElement::FlexSpacer(flex) => widget.with_flex_spacer(flex),
}
}
(Pod::new(widget), seq_state)
(ctx.new_pod(widget), seq_state)
}

fn rebuild<'el>(
Expand Down Expand Up @@ -209,8 +209,8 @@ impl ViewElement for FlexElement {
type Mut<'w> = FlexElementMut<'w>;
}

impl SuperElement<FlexElement> for FlexElement {
fn upcast(child: FlexElement) -> Self {
impl SuperElement<FlexElement, ViewCtx> for FlexElement {
fn upcast(_ctx: &mut ViewCtx, child: FlexElement) -> Self {
child
}

Expand All @@ -230,9 +230,9 @@ impl SuperElement<FlexElement> for FlexElement {
}
}

impl<W: Widget> SuperElement<Pod<W>> for FlexElement {
fn upcast(child: Pod<W>) -> Self {
FlexElement::Child(child.inner.boxed().into(), FlexParams::default())
impl<W: Widget> SuperElement<Pod<W>, ViewCtx> for FlexElement {
fn upcast(ctx: &mut ViewCtx, child: Pod<W>) -> Self {
FlexElement::Child(ctx.boxed_pod(child), FlexParams::default())
}

fn with_downcast_val<R>(
Expand Down Expand Up @@ -450,10 +450,7 @@ where

fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
let (pod, state) = self.view.build(ctx);
(
FlexElement::Child(pod.inner.boxed().into(), self.params),
state,
)
(FlexElement::Child(ctx.boxed_pod(pod), self.params), state)
}

fn rebuild<'el>(
Expand Down
21 changes: 9 additions & 12 deletions xilem/src/view/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ where
GridElement::Child(child, params) => widget.with_child_pod(child.inner, params),
}
}
(Pod::new(widget), seq_state)
(ctx.new_pod(widget), seq_state)
}

fn rebuild<'el>(
Expand Down Expand Up @@ -133,8 +133,8 @@ impl ViewElement for GridElement {
}

// Used to allow the item to be used as a generic item in ViewSequence.
impl SuperElement<GridElement> for GridElement {
fn upcast(child: GridElement) -> Self {
impl SuperElement<GridElement, ViewCtx> for GridElement {
fn upcast(_ctx: &mut ViewCtx, child: GridElement) -> Self {
child
}

Expand All @@ -154,14 +154,14 @@ impl SuperElement<GridElement> for GridElement {
}
}

impl<W: Widget> SuperElement<Pod<W>> for GridElement {
fn upcast(child: Pod<W>) -> Self {
impl<W: Widget> SuperElement<Pod<W>, ViewCtx> for GridElement {
fn upcast(ctx: &mut ViewCtx, child: Pod<W>) -> Self {
// Getting here means that the widget didn't use .grid_item or .grid_pos.
// This currently places the widget in the top left cell.
// There is not much else, beyond purposefully failing, that can be done here,
// because there isn't enough information to determine an appropriate spot
// for the widget.
GridElement::Child(child.inner.boxed().into(), GridParams::new(1, 1, 1, 1))
GridElement::Child(ctx.boxed_pod(child), GridParams::new(1, 1, 1, 1))
}

fn with_downcast_val<R>(
Expand Down Expand Up @@ -362,12 +362,9 @@ where

type ViewState = V::ViewState;

fn build(&self, cx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
let (pod, state) = self.view.build(cx);
(
GridElement::Child(pod.inner.boxed().into(), self.params),
state,
)
fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
let (pod, state) = self.view.build(ctx);
(GridElement::Child(ctx.boxed_pod(pod), self.params), state)
}

fn rebuild<'el>(
Expand Down
4 changes: 2 additions & 2 deletions xilem/src/view/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl<State, Action> View<State, Action, ViewCtx> for Image {
type Element = Pod<widget::Image>;
type ViewState = ();

fn build(&self, _: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
(Pod::new(widget::Image::new(self.image.clone())), ())
fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
(ctx.new_pod(widget::Image::new(self.image.clone())), ())
}

fn rebuild<'el>(
Expand Down
4 changes: 2 additions & 2 deletions xilem/src/view/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl<State, Action> View<State, Action, ViewCtx> for Label {
type Element = Pod<widget::Label>;
type ViewState = ();

fn build(&self, _ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
let widget_pod = Pod::new(
fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
let widget_pod = ctx.new_pod(
widget::Label::new(self.label.clone())
.with_text_brush(self.text_brush.clone())
.with_text_alignment(self.alignment)
Expand Down
2 changes: 1 addition & 1 deletion xilem/src/view/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
// The Portal `View` doesn't get any messages directly (yet - scroll events?), so doesn't need to
// use ctx.with_id.
let (child, child_state) = self.child.build(ctx);
let widget_pod = Pod::new(widget::Portal::new_pod(child.inner));
let widget_pod = ctx.new_pod(widget::Portal::new_pod(child.inner));
(widget_pod, child_state)
}

Expand Down
4 changes: 3 additions & 1 deletion xilem/src/view/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ impl<State, Action> View<State, Action, ViewCtx> for ProgressBar {
type ViewState = ();

fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
ctx.with_leaf_action_widget(|_| Pod::new(masonry::widget::ProgressBar::new(self.progress)))
ctx.with_leaf_action_widget(|ctx| {
ctx.new_pod(masonry::widget::ProgressBar::new(self.progress))
})
}

fn rebuild<'el>(
Expand Down
4 changes: 2 additions & 2 deletions xilem/src/view/prose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl<State, Action> View<State, Action, ViewCtx> for Prose {
type Element = Pod<widget::Prose>;
type ViewState = ();

fn build(&self, _ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
let widget_pod = Pod::new(
fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
let widget_pod = ctx.new_pod(
widget::Prose::new(self.content.clone())
.with_text_brush(self.text_brush.clone())
.with_text_alignment(self.alignment)
Expand Down
2 changes: 1 addition & 1 deletion xilem/src/view/sized_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ where
if let Some(border) = &self.border {
widget = widget.border(border.color, border.width);
}
(Pod::new(widget), child_state)
(ctx.new_pod(widget), child_state)
}

fn rebuild<'el>(
Expand Down
4 changes: 2 additions & 2 deletions xilem/src/view/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl<State, Action> View<State, Action, ViewCtx> for Spinner {
type Element = Pod<widget::Spinner>;
type ViewState = ();

fn build(&self, _: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
(Pod::new(widget::Spinner::new()), ())
fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
(ctx.new_pod(widget::Spinner::new()), ())
}

fn rebuild<'el>(
Expand Down
4 changes: 2 additions & 2 deletions xilem/src/view/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ impl<State: 'static, Action: 'static> View<State, Action, ViewCtx> for Textbox<S
type ViewState = ();

fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
ctx.with_leaf_action_widget(|_| {
Pod::new(
ctx.with_leaf_action_widget(|ctx| {
ctx.new_pod(
masonry::widget::Textbox::new(self.contents.clone())
.with_text_brush(self.text_brush.clone())
.with_text_alignment(self.alignment),
Expand Down
4 changes: 2 additions & 2 deletions xilem/src/view/variable_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ impl<State, Action> View<State, Action, ViewCtx> for VariableLabel {
type Element = Pod<widget::VariableLabel>;
type ViewState = ();

fn build(&self, _ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
let widget_pod = Pod::new(
fn build(&self, ctx: &mut ViewCtx) -> (Self::Element, Self::ViewState) {
let widget_pod = ctx.new_pod(
widget::VariableLabel::new(self.label.clone())
.with_text_brush(self.text_brush.clone())
.with_line_break_mode(widget::LineBreaking::WordWrap)
Expand Down
6 changes: 3 additions & 3 deletions xilem_core/examples/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ impl<V, State, Action> FileView<State, Action> for V where

type DynFileView<State, Action = ()> = Box<dyn AnyView<State, Action, ViewCtx, FsPath>>;

impl SuperElement<FsPath> for FsPath {
fn upcast(child: FsPath) -> Self {
impl SuperElement<FsPath, ViewCtx> for FsPath {
fn upcast(_ctx: &mut ViewCtx, child: FsPath) -> Self {
child
}

Expand All @@ -112,7 +112,7 @@ impl SuperElement<FsPath> for FsPath {
}
}

impl AnyElement<FsPath> for FsPath {
impl AnyElement<FsPath, ViewCtx> for FsPath {
fn replace_inner(this: Self::Mut<'_>, child: FsPath) -> Self::Mut<'_> {
*this = child.0;
this
Expand Down
4 changes: 2 additions & 2 deletions xilem_core/examples/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ impl Widget for ButtonWidget {
}
}

impl<W: Widget> SuperElement<WidgetPod<W>> for WidgetPod<Box<dyn Widget>> {
fn upcast(child: WidgetPod<W>) -> Self {
impl<W: Widget> SuperElement<WidgetPod<W>, ViewCtx> for WidgetPod<Box<dyn Widget>> {
fn upcast(_ctx: &mut ViewCtx, child: WidgetPod<W>) -> Self {
WidgetPod {
widget: Box::new(child.widget),
}
Expand Down
4 changes: 2 additions & 2 deletions xilem_core/src/any_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub trait AnyView<State, Action, Context, Element: ViewElement, Message = DynMes
impl<State, Action, Context, DynamicElement, Message, V>
AnyView<State, Action, Context, DynamicElement, Message> for V
where
DynamicElement: AnyElement<V::Element>,
DynamicElement: AnyElement<V::Element, Context>,
Context: ViewPathTracker,
V: View<State, Action, Context, Message> + 'static,
V::ViewState: 'static,
Expand All @@ -80,7 +80,7 @@ where
let generation = 0;
let (element, view_state) = ctx.with_id(ViewId::new(generation), |ctx| self.build(ctx));
(
DynamicElement::upcast(element),
DynamicElement::upcast(ctx, element),
AnyViewState {
inner_state: Box::new(view_state),
generation,
Expand Down
Loading

0 comments on commit c6f60a9

Please sign in to comment.