Skip to content

Commit

Permalink
Implement dynamic navigation properly
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Sep 9, 2024
1 parent cd14d61 commit a572cd5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
22 changes: 11 additions & 11 deletions examples/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
use nuit::{prelude::*, NavigationLink, NavigationStack, Text, VStack};

#[derive(Bind, Default)]
struct NavigationView {
struct NavigationContent {
i: i32,
}

impl NavigationView {
impl NavigationContent {
pub const fn new(i: i32) -> Self {
Self { i }
}
}

impl View for NavigationView {
impl View for NavigationContent {
type Body = impl View;

fn body(&self) -> Self::Body {
let i = self.i;
NavigationStack::from(
VStack::from((
Text::new(format!("This is page {i}")),
NavigationLink::with_text("Next", i + 1),
))
.navigation_destination(Self::new)
)
VStack::from((
Text::new(format!("This is page {i}")),
NavigationLink::with_text("Next", i + 1),
))
.navigation_destination(Self::new)
}
}

fn main() {
nuit::run_app(NavigationView::new(0));
nuit::run_app(NavigationStack::from(
NavigationContent::new(0)
));
}
16 changes: 11 additions & 5 deletions nuit-core/src/compose/view/navigation/destination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ where
if let Some(head) = event_path.head() {
match head {
Id::Index(0) => self.wrapped.fire(event, event_path.tail(), &context.child(0)),
i => panic!("Cannot fire event for child id {i} on NavigationDestination which only has one child"),
Id::Index(i) => panic!("Cannot fire event for child id {i} on NavigationDestination"),
Id::String(value_json) => {
let value = serde_json::from_str(&value_json).expect("Could not deserialize navigation destination id");
let destination = (self.destination_func)(value);
destination.fire(event, event_path.tail(), &context.child(value_json))
},
}
} else if let Event::GetNavigationDestination { value } = event {
let value_json = serde_json::to_string(value).expect("Could not serialize navigation destination id");
let id = Id::string(value_json);

let value = serde_json::from_value(value.clone()).expect("Could not deserialize navigation destination value");
let destination = (self.destination_func)(value);
// TODO: How do we deal with events that go here? Maybe use the
// JSON-serialized value as id and then dynamically rerender during
// firing?
let node = destination.render(&context.child(1)).identify(1);

let node = destination.render(&context.child(id.clone())).identify(id);
EventResponse::Node { node }
} else {
EventResponse::default()
Expand Down

0 comments on commit a572cd5

Please sign in to comment.