Skip to content

Commit

Permalink
Try new elm-inspired design
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jan 27, 2024
1 parent 0a01b18 commit fba5b5f
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 684 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
resolver = "2"
members = [
"crates/concoct",
"crates/concoct-web",
# "crates/concoct-web",
# "crates/concoct-menu",
"web_examples/counter"
# "web_examples/counter"
]
36 changes: 0 additions & 36 deletions crates/concoct/src/action.rs

This file was deleted.

27 changes: 0 additions & 27 deletions crates/concoct/src/handle.rs

This file was deleted.

13 changes: 0 additions & 13 deletions crates/concoct/src/hook/mod.rs

This file was deleted.

9 changes: 0 additions & 9 deletions crates/concoct/src/hook/use_context.rs

This file was deleted.

17 changes: 0 additions & 17 deletions crates/concoct/src/hook/use_on_drop.rs

This file was deleted.

12 changes: 0 additions & 12 deletions crates/concoct/src/hook/use_provider.rs

This file was deleted.

30 changes: 0 additions & 30 deletions crates/concoct/src/hook/use_ref.rs

This file was deleted.

105 changes: 52 additions & 53 deletions crates/concoct/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
//! Concoct is a framework for user-interfaces in Rust.
//!
//! This crate provides a virtual DOM and state management system for any backend.
//! Concoct uses static typing to describe your UI at compile-time to create an efficient
//! tree without allocations.
//!
//! ```ignore
//! use concoct::{Scope, View};
//! use concoct_web::html;
//!
//! #[derive(Default)]
//! struct Counter {
//! count: i32,
//! }
//!
//! impl View<Self> for Counter {
//! fn body(&mut self, _cx: &Scope<Self>) -> impl View<Self> {
//! (
//! format!("High five count: {}", self.count),
//! html::button("Up high!").on_click(|_cx, state: &mut Self, _event| state.count += 1),
//! html::button("Down low!").on_click(|_cx, state: &mut Self, _event| state.count -= 1),
//! )
//! }
//! }
//!
//! fn main() {
//! concoct_web::launch(Counter::default())
//! }
//! ```
//!
use std::{ops::Deref, rc::Rc};

#![deny(missing_docs)]
pub struct Updater<M> {
send: Rc<dyn Fn(Option<M>)>,
}

use std::ops::DerefMut;
impl<M> Updater<M> {
pub fn new(send: impl Fn(Option<M>) + 'static) -> Self {
let f: Rc<dyn Fn(Option<M>)> = Rc::new(send);
f.into()
}

mod action;
pub use self::action::{Action, IntoAction};
pub fn send(&self, message: M) {
(self.send)(Some(message))
}

mod handle;
pub use self::handle::Handle;
pub fn rebuild(&self) {
(self.send)(None)
}
}

pub mod hook;
impl<M> From<Rc<dyn Fn(Option<M>)>> for Updater<M> {
fn from(value: Rc<dyn Fn(Option<M>)>) -> Self {
Self { send: value }
}
}

impl<M> Clone for Updater<M> {
fn clone(&self) -> Self {
Self {
send: self.send.clone(),
}
}
}

mod vdom;
pub use self::vdom::VirtualDom;
pub struct Context<'a, M> {
updater: &'a Updater<M>,
}

pub mod view;
pub use self::view::View;
impl<'a, M> Context<'a, M> {
pub fn new(updater: &'a Updater<M>) -> Self {
Self { updater }
}

mod scope;
pub use self::scope::Scope;
pub fn updater(&self) -> &Updater<M> {
&self.updater
}
}

/// Run a view on a new virtual dom.
pub async fn run<T, V>(content: V)
where
T: 'static,
V: View<T> + DerefMut<Target = T>,
{
let mut vdom = VirtualDom::new(content);
vdom.build();
impl<M> Deref for Context<'_, M> {
type Target = Updater<M>;

loop {
vdom.rebuild().await;
fn deref(&self) -> &Self::Target {
self.updater
}
}

pub trait View<M> {
type State;

fn build(self) -> Self::State;

fn rebuild(self, state: &mut Self::State);
}
98 changes: 0 additions & 98 deletions crates/concoct/src/scope.rs

This file was deleted.

Loading

0 comments on commit fba5b5f

Please sign in to comment.