-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
103 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![feature(type_alias_impl_trait, impl_trait_in_assoc_type)] | ||
|
||
use nuit::{prelude::*, GeometryReader, Insets, Text}; | ||
|
||
#[derive(Bind)] | ||
struct GeometryReaderView; | ||
|
||
impl View for GeometryReaderView { | ||
type Body = impl View; | ||
|
||
fn body(&self) -> Self::Body { | ||
GeometryReader::new(|geometry| { | ||
Text::new(format!("This view has width {} and height {}", geometry.width(), geometry.height())) | ||
.padding(Insets::default()) | ||
}) | ||
} | ||
} | ||
|
||
fn main() { | ||
nuit::run_app(GeometryReaderView); | ||
} |
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,57 @@ | ||
use std::marker::PhantomData; | ||
|
||
use nuit_derive::Bind; | ||
|
||
use crate::{Context, Event, EventResponse, Geometry, Id, IdPath, IdentifyExt, Node, View}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Bind)] | ||
pub struct GeometryReader<F, T> { | ||
view_func: F, | ||
phantom_view: PhantomData<T>, | ||
} | ||
|
||
impl<F, T> GeometryReader<F, T> | ||
where | ||
F: Fn(Geometry) -> T, | ||
T: View, | ||
{ | ||
#[must_use] | ||
pub const fn new(view_func: F) -> Self { | ||
Self { | ||
view_func, | ||
phantom_view: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<F, T> View for GeometryReader<F, T> | ||
where | ||
F: Fn(Geometry) -> T, | ||
T: View, | ||
{ | ||
fn fire(&self, event: &Event, event_path: &IdPath, context: &Context) -> EventResponse { | ||
if let Some(head) = event_path.head() { | ||
match head { | ||
Id::Index(i) => panic!("Cannot fire event for child id {i} on GeometryReader"), | ||
Id::String(geometry_json) => { | ||
let geometry = serde_json::from_str(&geometry_json).expect("Could not deserialize geometry reader id"); | ||
let view = (self.view_func)(geometry); | ||
view.fire(event, event_path.tail(), &context.child(geometry_json)) | ||
}, | ||
} | ||
} else if let Event::GetGeometryReaderView { geometry } = event { | ||
let geometry_json = serde_json::to_string(geometry).expect("Could not serialize geometry reader id"); | ||
let id = Id::string(geometry_json); | ||
|
||
let view = (self.view_func)(*geometry); | ||
let node = view.render(&context.child(id.clone())).identify(id); | ||
EventResponse::Node { node } | ||
} else { | ||
EventResponse::default() | ||
} | ||
} | ||
|
||
fn render(&self, _context: &Context) -> Node { | ||
Node::GeometryReader {} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod geometry_reader; | ||
mod list; | ||
mod overlay; | ||
mod stack; | ||
|
||
pub use geometry_reader::*; | ||
pub use list::*; | ||
pub use overlay::*; | ||
pub use stack::*; |
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