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

Assign view id paths by parents #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ use crate::{
pub struct App<T, V: View<T>> {
req_chan: tokio::sync::mpsc::Sender<AppReq>,
response_chan: tokio::sync::mpsc::Receiver<RenderResponse<V, V::State>>,
return_chan: tokio::sync::mpsc::Sender<(V, V::State, HashSet<Id>)>,
id: Option<Id>,
return_chan: tokio::sync::mpsc::Sender<(V, V::State, HashSet<IdPath>)>,
events: Vec<Event>,
window_handle: WindowHandle,
root_state: WidgetState,
Expand All @@ -53,14 +52,14 @@ const RENDER_DELAY: Duration = Duration::from_millis(5);
struct AppTask<T, V: View<T>, F: FnMut(&mut T) -> V> {
req_chan: tokio::sync::mpsc::Receiver<AppReq>,
response_chan: tokio::sync::mpsc::Sender<RenderResponse<V, V::State>>,
return_chan: tokio::sync::mpsc::Receiver<(V, V::State, HashSet<Id>)>,
return_chan: tokio::sync::mpsc::Receiver<(V, V::State, HashSet<IdPath>)>,

data: T,
app_logic: F,
view: Option<V>,
state: Option<V::State>,
idle_handle: Option<IdleHandle>,
pending_async: HashSet<Id>,
pending_async: HashSet<IdPath>,
ui_state: UiState,
}

Expand Down Expand Up @@ -145,7 +144,6 @@ where
req_chan: req_tx,
response_chan: response_rx,
return_chan: return_tx,
id: None,
root_pod: None,
events: Vec::new(),
window_handle: Default::default(),
Expand Down Expand Up @@ -245,7 +243,6 @@ where
let changed = response.view.rebuild(
&mut self.cx,
response.prev.as_ref().unwrap(),
self.id.as_mut().unwrap(),
&mut state,
element,
);
Expand All @@ -255,10 +252,9 @@ where
assert!(self.cx.is_empty(), "id path imbalance on rebuild");
state
} else {
let (id, state, element) = response.view.build(&mut self.cx);
let (state, element) = response.view.build(&mut self.cx);
assert!(self.cx.is_empty(), "id path imbalance on build");
self.root_pod = Some(Pod::new(element));
self.id = Some(id);
state
};
let pending = std::mem::take(&mut self.cx.pending_async);
Expand Down Expand Up @@ -290,9 +286,8 @@ where
AppReq::SetIdleHandle(handle) => self.idle_handle = Some(handle),
AppReq::Events(events) => {
for event in events {
let id_path = &event.id_path[1..];
self.view.as_ref().unwrap().event(
id_path,
&event.id_path,
self.state.as_mut().unwrap(),
event.body,
&mut self.data,
Expand All @@ -301,7 +296,7 @@ where
}
AppReq::Wake(id_path) => {
let result = self.view.as_ref().unwrap().event(
&id_path[1..],
&id_path,
self.state.as_mut().unwrap(),
Box::new(AsyncWake),
&mut self.data,
Expand All @@ -314,8 +309,7 @@ where
}
self.ui_state = UiState::WokeUI;
}
let id = id_path.last().unwrap();
self.pending_async.remove(&id);
self.pending_async.remove(&id_path);
if self.pending_async.is_empty() && self.ui_state == UiState::Delayed {
self.render().await;
deadline = None;
Expand Down
2 changes: 1 addition & 1 deletion src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::num::NonZeroU64;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Hash)]
/// A stable identifier for an element.
/// A stable identifier for a view tree path segment.
pub struct Id(NonZeroU64);

pub type IdPath = Vec<Id>;
Expand Down
9 changes: 4 additions & 5 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub trait View<T, A = ()>: Send {
type Element: Widget;

/// Build the associated widget and initialize state.
fn build(&self, cx: &mut Cx) -> (Id, Self::State, Self::Element);
fn build(&self, cx: &mut Cx) -> (Self::State, Self::Element);

/// Update the associated widget.
///
Expand All @@ -70,7 +70,6 @@ pub trait View<T, A = ()>: Send {
&self,
cx: &mut Cx,
prev: &Self,
id: &mut Id,
state: &mut Self::State,
element: &mut Self::Element,
) -> bool;
Expand All @@ -92,7 +91,7 @@ pub trait View<T, A = ()>: Send {
pub struct Cx {
id_path: IdPath,
req_chan: SyncSender<IdPath>,
pub(crate) pending_async: HashSet<Id>,
pub(crate) pending_async: HashSet<IdPath>,
}

struct MyWaker {
Expand Down Expand Up @@ -165,7 +164,7 @@ impl Cx {
/// Rendering may be delayed when there are pending async futures, to avoid
/// flashing, and continues when all futures complete, or a timeout, whichever
/// is first.
pub fn add_pending_async(&mut self, id: Id) {
self.pending_async.insert(id);
pub fn add_pending_async(&mut self, id_path: IdPath) {
self.pending_async.insert(id_path);
}
}
8 changes: 3 additions & 5 deletions src/view/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@ impl<T, A> View<T, A> for Button<T, A> {

type Element = crate::widget::button::Button;

fn build(&self, cx: &mut Cx) -> (Id, Self::State, Self::Element) {
let (id, element) = cx
.with_new_id(|cx| crate::widget::button::Button::new(cx.id_path(), self.label.clone()));
(id, (), element)
fn build(&self, cx: &mut Cx) -> (Self::State, Self::Element) {
let element = crate::widget::button::Button::new(cx.id_path(), self.label.clone());
((), element)
}

fn rebuild(
&self,
_cx: &mut Cx,
prev: &Self,
_id: &mut crate::id::Id,
_state: &mut Self::State,
element: &mut Self::Element,
) -> bool {
Expand Down
7 changes: 3 additions & 4 deletions src/view/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ impl<T, A> View<T, A> for String {

type Element = crate::widget::text::TextWidget;

fn build(&self, cx: &mut Cx) -> (Id, Self::State, Self::Element) {
let (id, element) = cx.with_new_id(|_| crate::widget::text::TextWidget::new(self.clone()));
(id, (), element)
fn build(&self, _cx: &mut Cx) -> (Self::State, Self::Element) {
let element = crate::widget::text::TextWidget::new(self.clone());
((), element)
}

fn rebuild(
&self,
_cx: &mut Cx,
prev: &Self,
_id: &mut crate::id::Id,
_state: &mut Self::State,
element: &mut Self::Element,
) -> bool {
Expand Down