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

Common context state #970

Merged
merged 2 commits into from
May 22, 2020
Merged

Common context state #970

merged 2 commits into from
May 22, 2020

Conversation

cmyr
Copy link
Member

@cmyr cmyr commented May 21, 2020

This is two related commits:

  • one adds the set_menu method to the lifecycle and update contexts
  • one refactors contexts so that all of the state that is A) shared and B) comes from the window and never changes is in one place. This will let us put common implementations there, and reduce duplicate code.

This is based on #969, which should go in first.

@cmyr cmyr force-pushed the base-state-to-widget-state branch from 7cf7b60 to 66db763 Compare May 21, 2020 19:01
@cmyr cmyr force-pushed the common-context-state branch 2 times, most recently from 701d4e8 to f1d1650 Compare May 21, 2020 19:20
Copy link
Collaborator

@luleyleo luleyleo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sure cleans up quite a bit 👍 It would deserve a Maintenance changelog entry as well but as those purely internal only changes, there is no need for it.
Also I got a few more comments.

@@ -52,6 +53,14 @@ pub struct WidgetPod<T, W> {
debug_widget_text: Option<PietTextLayout>,
}

/// Static state that is shared between most contexts.
pub(crate) struct RootState<'a> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about calling this ContextState akin to WidgetState? Because RootState really could mean anything.

@@ -883,6 +855,49 @@ impl<'a, 'b: 'a> PaintCtx<'a, 'b> {
}
}

impl<'a> RootState<'a> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit odd to have the impl in a different file than the struct itself. Maybe put both into contexts.rs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fair, I think the impl really should be here so maybe I'll move RootState/ContextState back here as well.

Comment on lines 79 to 80
pub struct LayoutCtx<'a, 'b: 'a, 'c> {
pub(crate) root_state: &'a mut RootState<'c>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a bit annoying to change but it would be nice if all 'bs refer to the RootStates lifetime.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea that would be nice but doesn't work unfortunately. :(

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How so? The only 'issue' I see is PainCtx as it does not have RootState 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sorry I thought you meant that we reuse 'b here for both inner lifetimes.

@@ -708,15 +680,15 @@ impl<'a> UpdateCtx<'a> {
}
}

impl<'a, 'b> LayoutCtx<'a, 'b> {
impl<'a, 'b, 'c> LayoutCtx<'a, 'b, 'c> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be useful for the macro introduced by #972 :
This 'b and RootStates 'a are the only required lifetimes in this file (for two functions only), all others could be elided using '_.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting, I'll play around with that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, I was able to simplify the lifetimes quite a bit, i'd forgotten they'd relaxed that rule :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also still keep adding them and then only realize afterwards that it was completely unnecessary 😅

@luleyleo luleyleo added S-waiting-on-author waits for changes from the submitter S-blocked waits for another PR or dependency labels May 22, 2020
@cmyr cmyr force-pushed the base-state-to-widget-state branch from 66db763 to ddeeca5 Compare May 22, 2020 14:07
@cmyr cmyr force-pushed the base-state-to-widget-state branch from ddeeca5 to 5d1aa86 Compare May 22, 2020 14:47
Base automatically changed from base-state-to-widget-state to master May 22, 2020 15:34
I think it makes sense for show_context_menu to only be available
in EventCtx, but for the window menu I can imagine needing it in
lifecycle and update (and I need it in update in runebender)
@cmyr cmyr removed the S-blocked waits for another PR or dependency label May 22, 2020
@cmyr cmyr added S-needs-review waits for review and removed S-waiting-on-author waits for changes from the submitter labels May 22, 2020
@cmyr
Copy link
Member Author

cmyr commented May 22, 2020

/bloat

@github-actions
Copy link

🗜 Bloat check ⚖️

Comparing 74fa6a9 against e024f60

target old size new size difference
target/release/examples/calc 1.14 MB 1.14 MB 32 Bytes (0.00%)
target/release/examples/scroll_colors 1.12 MB 1.12 MB 48 Bytes (0.00%)
target/release/examples/multiwin 1.14 MB 1.14 MB 336 Bytes (0.03%)
target/release/examples/flex 1.5 MB 1.49 MB -3.69 KB (-0.24%)
target/release/examples/styled_text 1.25 MB 1.26 MB 4.45 KB (0.35%)
target/release/examples/custom_widget 1.07 MB 1.07 MB 72 Bytes (0.01%)

Copy link
Member

@xStrom xStrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generaly looks pretty nice!

CHANGELOG.md Outdated Show resolved Hide resolved
druid/src/contexts.rs Outdated Show resolved Hide resolved
Copy link
Member

@xStrom xStrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just two more nits!

child_state: &mut WidgetState,
window_id: WindowId,
window: &WindowHandle,
root_state: &mut ContextState,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this root_state.

CHANGELOG.md Outdated
@@ -51,6 +51,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
- `im::Vector` support for the `List` widget. ([#940] by [@xStrom])
- `LifeCycle::Size` event to inform widgets that their size changed. ([#953] by [@xStrom])
- `Button::dynamic` constructor. ([#963] by [@totsteps])
- Add `set_menu` method to `UpdateCtx` and `LifeCycleCtx` ([#970] by [@cmyr])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given how all the other items under the Added section are written, probably better to remove the word Add from the start here.

Copy link
Collaborator

@luleyleo luleyleo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!
Gotta be pedantic about that changelog entry tho 🙈

CHANGELOG.md Outdated
@@ -51,6 +51,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
- `im::Vector` support for the `List` widget. ([#940] by [@xStrom])
- `LifeCycle::Size` event to inform widgets that their size changed. ([#953] by [@xStrom])
- `Button::dynamic` constructor. ([#963] by [@totsteps])
- Add `set_menu` method to `UpdateCtx` and `LifeCycleCtx` ([#970] by [@cmyr])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the style consistent.

Suggested change
- Add `set_menu` method to `UpdateCtx` and `LifeCycleCtx` ([#970] by [@cmyr])
- `UpdateCtx::set_menu` and `LifeCycleCtx::set_menu` ([#970] by [@cmyr])

This adds a new type, RootState that holds onto all of the
app/window-scoped state that is shared between all non-paint
contexts.

Simplify context lifetimes

fixup
@cmyr cmyr merged commit 7c2fa9c into master May 22, 2020
@cmyr cmyr deleted the common-context-state branch May 22, 2020 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-needs-review waits for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants