-
Notifications
You must be signed in to change notification settings - Fork 127
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 support for impl View for Arc/Rc<impl View>
#164
Closed
Closed
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1a6c45a
Add support for `impl View for Arc/Rc<impl View>`
Philipp-M acb3596
Be consistent with braces
Philipp-M 7ff11fa
Added example for memoization
Philipp-M a9f9089
Improve syntax of the parameters of generate_rc_view macro by removin…
Philipp-M 6d69019
Postfix `::` instead of prefix
Philipp-M e1e9f78
Separate super and state bounds for view macros, as state doesn't nee…
Philipp-M f2216c2
Beautify parameter syntax of the macros via optionals for the bounds
Philipp-M 12dc722
Implement `View` for `Arc<dyn AnyView>`, and add super bounds of `Vie…
Philipp-M b85d8ad
Merge branch 'main' into arc-view
Philipp-M 9413b63
Fix clippy lints
Philipp-M 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2024 the Xilem Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#[macro_export] | ||
macro_rules! generate_rc_view { | ||
($($rc_ty:ident)::*, $viewtrait:ident, $viewmarker:ty, $cx:ty, $changeflags:ty; $($ss:tt)*) => { | ||
impl<V> $viewmarker for $($rc_ty)::*<V> {} | ||
|
||
impl<T, A, V: $viewtrait<T, A> $( $ss )*> $viewtrait<T, A> for $($rc_ty)::*<V> { | ||
type State = V::State; | ||
|
||
type Element = V::Element; | ||
|
||
fn build(&self, cx: &mut Cx) -> ($crate::Id, Self::State, Self::Element) { | ||
V::build(self, cx) | ||
} | ||
|
||
fn rebuild( | ||
&self, | ||
cx: &mut $cx, | ||
prev: &Self, | ||
id: &mut $crate::Id, | ||
state: &mut Self::State, | ||
element: &mut Self::Element, | ||
) -> ChangeFlags { | ||
if !$($rc_ty)::*::ptr_eq(self, prev) { | ||
V::rebuild(self, cx, prev, id, state, element) | ||
} else { | ||
ChangeFlags::empty() | ||
} | ||
} | ||
|
||
fn message( | ||
&self, | ||
id_path: &[$crate::Id], | ||
state: &mut Self::State, | ||
message: Box<dyn std::any::Any>, | ||
app_state: &mut T, | ||
) -> $crate::MessageResult<A> { | ||
V::message(self, id_path, state, message, app_state) | ||
} | ||
} | ||
}; | ||
} |
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,65 @@ | ||
use std::sync::Arc; | ||
use xilem::view::{button, memoize, v_stack, BoxedView}; | ||
use xilem::{view::View, App, AppLauncher}; | ||
|
||
// There are currently two ways to do memoization | ||
|
||
fn app_logic(state: &mut AppState) -> impl View<AppState> { | ||
// The following is an example to do memoization with an Arc | ||
let increase_button = if let Some(view) = &state.count_view { | ||
view.clone() | ||
} else { | ||
let view = state.make_increase_button(); | ||
state.count_view = Some(view.clone()); | ||
view | ||
}; | ||
|
||
v_stack(( | ||
increase_button, | ||
// This is the alternative with Memoize | ||
// Note how this requires a closure that returns the memoized view, while Arc does not | ||
memoize(state.count, |count| { | ||
button( | ||
format!("decrease the count: {count}"), | ||
|data: &mut AppState| { | ||
data.count_view = None; | ||
data.count -= 1; | ||
}, | ||
) | ||
}), | ||
button("reset", |data: &mut AppState| { | ||
if data.count != 0 { | ||
data.count_view = None; | ||
} | ||
data.count = 0; | ||
}), | ||
)) | ||
.with_spacing(20.0) | ||
} | ||
|
||
struct AppState { | ||
count: i32, | ||
// TODO Maybe support `impl View for Arc<dyn AnyView>` to avoid double indirection | ||
count_view: Option<Arc<BoxedView<AppState>>>, | ||
} | ||
|
||
impl AppState { | ||
fn make_increase_button(&self) -> Arc<BoxedView<AppState>> { | ||
Arc::new(Box::new(button( | ||
format!("current count is {}", self.count), | ||
|state: &mut AppState| { | ||
state.count += 1; | ||
state.count_view = None; | ||
}, | ||
))) | ||
} | ||
} | ||
|
||
fn main() { | ||
let data = AppState { | ||
count: 0, | ||
count_view: None, | ||
}; | ||
|
||
AppLauncher::new(App::new(data, app_logic)).run() | ||
} |
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
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.
I was going to suggest changing the input to avoid the parentheses here too, but unfortunately that runs into rust-lang/rust#18700 (last comment has a workaround, but probably not worth using that here).
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.
Maybe make the last two parameters optional using
$()?
though? The invocation with(), ()
looks weird.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.
Yes I think it's difficult to get rid of
()
in general (while covering everything that is allowed in trait bounds).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.
I've used optionals as you said