-
Notifications
You must be signed in to change notification settings - Fork 568
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
Add widget wrapper and implementation macros. #1511
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/// A trait for widgets that wrap a single child to expose that child for access and mutation | ||
pub trait WidgetWrapper { | ||
/// The type of the wrapped widget. | ||
/// Maybe we would like to constrain this to Widget<impl Data> (if existential bounds were supported). | ||
/// Any other scheme leads to T being unconstrained in unification at some point | ||
type Wrapped; | ||
/// Get immutable access to the wrapped child | ||
fn wrapped(&self) -> &Self::Wrapped; | ||
/// Get mutable access to the wrapped child | ||
fn wrapped_mut(&mut self) -> &mut Self::Wrapped; | ||
} | ||
|
||
/// A macro to help implementation of WidgetWrapper for a direct wrapper. | ||
/// Use it in the body of the impl. | ||
/// | ||
#[macro_export] | ||
macro_rules! widget_wrapper_body { | ||
($wrapped:ty, $field:ident) => { | ||
type Wrapped = $wrapped; | ||
|
||
fn wrapped(&self) -> &Self::Wrapped { | ||
&self.$field | ||
} | ||
|
||
fn wrapped_mut(&mut self) -> &mut Self::Wrapped { | ||
&mut self.$field | ||
} | ||
}; | ||
} | ||
|
||
/// A macro to help implementation of WidgetWrapper for a wrapper of a typed pod. | ||
/// Use it in the body of the impl. | ||
/// | ||
#[macro_export] | ||
macro_rules! widget_wrapper_pod_body { | ||
($wrapped:ty, $field:ident) => { | ||
type Wrapped = $wrapped; | ||
|
||
fn wrapped(&self) -> &Self::Wrapped { | ||
self.$field.widget() | ||
} | ||
|
||
fn wrapped_mut(&mut self) -> &mut Self::Wrapped { | ||
self.$field.widget_mut() | ||
} | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually make use of the trait-ness of this trait anywhere?
My inclination would be to just have independent methods on each widget, instead of a trait (which needs to be in scope). That way we would have just
Container::child
andContainer::child_mut
methods.An annoying consideration here is also that we are inconsistent about monomorphizing widget params; that is, some single-child containers are parameterized over their child type, and some just box unilaterally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't have the trait, I need to implement them all over again in bindings in order to treat this generically, and so would any other scheme trying to do the same thing. I guess coherence sometimes forces things like that anyway, but it didn't yet.
The lack of traits in Contexts does cause real problems with duplicated code.
Also, these methods are fairly useless for anything that isn't some kind of general scheme for access.
To use them in a dynamic way (which is the only real use case, otherwise you would set it up in construction), you need to implement so much boiler plate (controllers, commands, navigation, threading through widget ids), that an import seems like the last of our worries.
I have to admit I don't agree with the 'hanging methods off of traits' objection really. Importing is just normal, and IDEs will do it for you.