Merge remote-tracking branch 'origin/main' into more_todomvc

This commit is contained in:
Richard Dodd 2023-07-12 10:22:36 +01:00
commit 7361c59db3
11 changed files with 109 additions and 43 deletions

4
Cargo.lock generated
View File

@ -613,6 +613,7 @@ dependencies = [
name = "counter"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"wasm-bindgen",
"web-sys",
"xilem_html",
@ -622,6 +623,7 @@ dependencies = [
name = "counter_untyped"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"wasm-bindgen",
"web-sys",
"xilem_html",
@ -3346,7 +3348,7 @@ dependencies = [
]
[[package]]
name = "xilemsvg"
name = "xilem_svg"
version = "0.1.0"
dependencies = [
"bitflags 1.3.2",

View File

@ -20,14 +20,14 @@ pub use xilem_core::MessageResult;
pub use app::App;
pub use class::class;
pub use context::ChangeFlags;
pub use context::{ChangeFlags, Cx};
#[cfg(feature = "typed")]
pub use element::elements;
pub use element::{element, Element, ElementState};
#[cfg(feature = "typed")]
pub use event::events;
pub use event::{on_event, Action, Event, OnEvent, OnEventState, OptionalAction};
pub use view::{Adapt, AdaptThunk, Either, Pod, View, ViewMarker, ViewSequence};
pub use view::{Adapt, AdaptThunk, AnyView, Either, Pod, View, ViewMarker, ViewSequence};
#[cfg(feature = "typed")]
pub use view_ext::ViewExt;

View File

@ -126,8 +126,8 @@ where
impl<T, A, V1, V2> View<T, A> for Either<V1, V2>
where
V1: View<T, A>,
V2: View<T, A>,
V1: View<T, A> + ViewMarker,
V2: View<T, A> + ViewMarker,
V1::Element: AsRef<web_sys::Node> + 'static,
V2::Element: AsRef<web_sys::Node> + 'static,
{
@ -155,38 +155,36 @@ where
state: &mut Self::State,
element: &mut Self::Element,
) -> ChangeFlags {
let mut change_flags = ChangeFlags::empty();
match (prev, self) {
(Either::Left(_), Either::Right(view)) => {
let (new_id, new_state, new_element) = view.build(cx);
*id = new_id;
*state = Either::Right(new_state);
*element = Either::Right(new_element);
change_flags |= ChangeFlags::STRUCTURE;
ChangeFlags::STRUCTURE
}
(Either::Right(_), Either::Left(view)) => {
let (new_id, new_state, new_element) = view.build(cx);
*id = new_id;
*state = Either::Left(new_state);
*element = Either::Left(new_element);
change_flags |= ChangeFlags::STRUCTURE;
ChangeFlags::STRUCTURE
}
(Either::Left(prev_view), Either::Left(view)) => {
let (Either::Left(state), Either::Left(element)) = (state, element) else {
throw_str("invalid state/view in Either (unreachable)");
};
// Cannot do mutable casting, so take ownership of state.
change_flags |= view.rebuild(cx, prev_view, id, state, element);
view.rebuild(cx, prev_view, id, state, element)
}
(Either::Right(prev_view), Either::Right(view)) => {
let (Either::Right(state), Either::Right(element)) = (state, element) else {
throw_str("invalid state/view in Either (unreachable)");
};
// Cannot do mutable casting, so take ownership of state.
change_flags |= view.rebuild(cx, prev_view, id, state, element);
view.rebuild(cx, prev_view, id, state, element)
}
}
change_flags
}
fn message(
@ -213,6 +211,94 @@ where
}
}
impl<T, A, V1, V2> ViewSequence<T, A> for Either<V1, V2>
where
V1: ViewSequence<T, A>,
V2: ViewSequence<T, A>,
{
type State = Either<V1::State, V2::State>;
fn build(&self, cx: &mut Cx, elements: &mut Vec<Pod>) -> Self::State {
match self {
Either::Left(view_sequence) => Either::Left(view_sequence.build(cx, elements)),
Either::Right(view_sequence) => Either::Right(view_sequence.build(cx, elements)),
}
}
fn rebuild(
&self,
cx: &mut Cx,
prev: &Self,
state: &mut Self::State,
element: &mut xilem_core::VecSplice<Pod>,
) -> ChangeFlags {
match (prev, self) {
(Either::Left(_), Either::Right(view_sequence)) => {
let new_state = element.as_vec(|elements| view_sequence.build(cx, elements));
*state = Either::Right(new_state);
ChangeFlags::STRUCTURE
}
(Either::Right(_), Either::Left(view_sequence)) => {
let new_state = element.as_vec(|elements| view_sequence.build(cx, elements));
*state = Either::Left(new_state);
ChangeFlags::STRUCTURE
}
(Either::Left(prev_view), Either::Left(view_sequence)) => {
let Either::Left(state) = state else {
throw_str("invalid state/view_sequence in Either (unreachable)");
};
view_sequence.rebuild(cx, prev_view, state, element)
}
(Either::Right(prev_view), Either::Right(view_sequence)) => {
let Either::Right(state) = state else {
throw_str("invalid state/view_sequence in Either (unreachable)");
};
view_sequence.rebuild(cx, prev_view, state, element)
}
}
}
fn message(
&self,
id_path: &[xilem_core::Id],
state: &mut Self::State,
message: Box<dyn std::any::Any>,
app_state: &mut T,
) -> MessageResult<A> {
match self {
Either::Left(view_sequence) => {
let Either::Left(state) = state else {
throw_str("invalid state/view_sequence in Either (unreachable)");
};
view_sequence.message(id_path, state, message, app_state)
}
Either::Right(view_sequence) => {
let Either::Right(state) = state else {
throw_str("invalid state/view_sequence in Either (unreachable)");
};
view_sequence.message(id_path, state, message, app_state)
}
}
}
fn count(&self, state: &Self::State) -> usize {
match self {
Either::Left(view_sequence) => {
let Either::Left(state) = state else {
throw_str("invalid state/view_sequence in Either (unreachable)");
};
view_sequence.count(state)
}
Either::Right(view_sequence) => {
let Either::Right(state) = state else {
throw_str("invalid state/view_sequence in Either (unreachable)");
};
view_sequence.count(state)
}
}
}
}
// strings -> text nodes
impl ViewMarker for &'static str {}

View File

@ -4,10 +4,8 @@ version = "0.1.0"
license = "Apache-2.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
console_error_panic_hook = "0.1"
wasm-bindgen = "0.2.87"
web-sys = "0.3.64"
xilem_html = { path = "../.." }

View File

@ -1,4 +1,3 @@
use wasm_bindgen::{prelude::*, JsValue};
use xilem_html::{
document_body, elements as el,
events::{self as evt},
@ -64,11 +63,8 @@ fn app_logic(state: &mut AppState) -> impl View<AppState> {
))
}
// Called by our JS entry point to run the example
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
pub fn main() {
console_error_panic_hook::set_once();
let app = App::new(AppState::default(), app_logic);
app.run(&document_body());
Ok(())
}

View File

@ -4,10 +4,8 @@ version = "0.1.0"
license = "Apache-2.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
console_error_panic_hook = "0.1"
wasm-bindgen = "0.2.87"
web-sys = { version = "0.3.64", features = ["HtmlButtonElement"] }
xilem_html = { path = "../..", default-features = false }

View File

@ -1,4 +1,3 @@
use wasm_bindgen::{prelude::*, JsValue};
use xilem_html::{document_body, element, on_event, App, Event, View, ViewMarker};
#[derive(Default)]
@ -43,11 +42,8 @@ fn app_logic(state: &mut AppState) -> impl View<AppState> {
)
}
// Called by our JS entry point to run the example
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
pub fn main() {
console_error_panic_hook::set_once();
let app = App::new(AppState::default(), app_logic);
app.run(&document_body());
Ok(())
}

View File

@ -4,9 +4,6 @@ version = "0.1.0"
license = "Apache-2.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
console_error_panic_hook = "0.1.7"
serde = { version = "1.0.170", features = ["derive"] }

View File

@ -1,10 +1,7 @@
use std::panic;
mod state;
use state::{AppState, Filter, Todo};
use wasm_bindgen::{prelude::*, JsValue};
use xilem_html::{
elements as el, events::on_click, get_element_by_id, Action, Adapt, App, MessageResult, View,
ViewExt, ViewMarker,
@ -213,12 +210,8 @@ fn app_logic(state: &mut AppState) -> impl View<AppState> {
))
}
// Called by our JS entry point to run the example
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
panic::set_hook(Box::new(console_error_panic_hook::hook));
pub fn main() {
console_error_panic_hook::set_once();
tracing_wasm::set_as_global_default();
App::new(AppState::load(), app_logic).run(&get_element_by_id("todoapp"));
Ok(())
}

View File

@ -72,7 +72,7 @@ impl AppState {
}
pub fn start_editing(&mut self, id: u64) {
if let Some(ref mut todo) = self.todos.iter_mut().filter(|todo| todo.id == id).next() {
if let Some(ref mut todo) = self.todos.iter_mut().find(|todo| todo.id == id) {
todo.title_editing.clear();
todo.title_editing.push_str(&todo.title);
self.editing_id = Some(id)

View File

@ -1,5 +1,5 @@
[package]
name = "xilemsvg"
name = "xilem_svg"
version = "0.1.0"
license = "Apache-2.0"
edition = "2021"