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

prep for release #50

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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
4 changes: 2 additions & 2 deletions anathema-default-widgets/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anathema_backend::Backend;
use anathema_geometry::{Pos, Size};
use anathema_state::{State, StateId, States, Value};
use anathema_templates::blueprints::Blueprint;
use anathema_templates::{Document, Globals};
use anathema_templates::{Document, Globals, ToSourceKind};
use anathema_widgets::components::ComponentRegistry;
use anathema_widgets::layout::text::StringStorage;
use anathema_widgets::layout::{layout_widget, position_widget, Constraints, LayoutCtx, LayoutFilter, Viewport};
Expand Down Expand Up @@ -44,7 +44,7 @@ impl TestRunner {
@main
";
let mut doc = Document::new(root);
let main = doc.add_component("main", src).unwrap();
let main = doc.add_component("main", src.to_template()).unwrap();
components.add_component(main.into(), (), ());

let (blueprint, globals) = doc.compile().unwrap();
Expand Down
16 changes: 8 additions & 8 deletions anathema-runtime/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anathema_geometry::Size;
use anathema_state::{AnyState, States};
use anathema_store::storage::strings::Strings;
use anathema_widgets::components::events::{Event, KeyCode, KeyEvent, KeyState};
use anathema_widgets::components::{AssociatedEvents, Context, Emitter};
use anathema_widgets::components::{AssociatedEvents, Emitter, UntypedContext};
use anathema_widgets::layout::{Constraints, Viewport};
use anathema_widgets::{AttributeStorage, Components, Elements, WidgetKind, WidgetTree};

Expand Down Expand Up @@ -60,7 +60,7 @@ impl EventHandler {

let Some((node, values)) = tree.get_node_by_path(path) else { return };
let elements = Elements::new(node.children(), values, attribute_storage);
let context = Context {
let context = UntypedContext {
emitter,
viewport: *viewport,
assoc_events,
Expand Down Expand Up @@ -99,7 +99,7 @@ impl EventHandler {

let Some((node, values)) = tree.get_node_by_path(path) else { return };
let elements = Elements::new(node.children(), values, attribute_storage);
let context = Context {
let context = UntypedContext {
emitter,
viewport: *viewport,
assoc_events,
Expand Down Expand Up @@ -149,7 +149,7 @@ impl EventHandler {

let Some((node, values)) = tree.get_node_by_path(path) else { return };
let elements = Elements::new(node.children(), values, attribute_storage);
let context = Context {
let context = UntypedContext {
emitter,
viewport: *viewport,
assoc_events,
Expand All @@ -161,7 +161,7 @@ impl EventHandler {

component
.dyn_component
.any_callback(state, event_ident, common_val, elements, context);
.any_receive(state, event_ident, common_val, elements, context);
});
})
}
Expand Down Expand Up @@ -219,7 +219,7 @@ pub fn global_event<'bp, T: Backend>(
let Some((node, values)) = tree.get_node_by_path(path) else { return };
let elements = Elements::new(node.children(), values, attribute_storage);
let state = states.get_mut(state_id);
let context = Context {
let context = UntypedContext {
emitter,
viewport,
assoc_events,
Expand All @@ -244,7 +244,7 @@ pub fn global_event<'bp, T: Backend>(
let Some((node, values)) = tree.get_node_by_path(path) else { return };
let elements = Elements::new(node.children(), values, attribute_storage);
let state = states.get_mut(state_id);
let context = Context {
let context = UntypedContext {
emitter,
viewport,
assoc_events,
Expand Down Expand Up @@ -275,7 +275,7 @@ pub fn global_event<'bp, T: Backend>(
let Some((node, values)) = tree.get_node_by_path(path) else { return };
let elements = Elements::new(node.children(), values, attribute_storage);
let state = states.get_mut(state_id);
let context = Context {
let context = UntypedContext {
emitter,
viewport,
assoc_events,
Expand Down
68 changes: 33 additions & 35 deletions anathema-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// -----------------------------------------------------------------------------

use std::fmt::Write;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};

Expand All @@ -30,15 +29,15 @@ use anathema_state::{
};
use anathema_store::tree::{root_node, AsNodePath};
use anathema_templates::blueprints::Blueprint;
use anathema_templates::{Document, Globals};
use anathema_templates::{Document, Globals, ToSourceKind};
use anathema_widgets::components::{
AssociatedEvents, Component, ComponentId, ComponentKind, ComponentRegistry, Context, Emitter, ViewMessage,
AssociatedEvents, Component, ComponentId, ComponentKind, ComponentRegistry, Emitter, UntypedContext, ViewMessage,
};
use anathema_widgets::layout::text::StringStorage;
use anathema_widgets::layout::{layout_widget, position_widget, Constraints, LayoutCtx, LayoutFilter, Viewport};
use anathema_widgets::{
eval_blueprint, try_resolve_future_values, update_tree, AnyWidget, AttributeStorage, Attributes, Components,
Elements, EvalContext, Factory, FloatingWidgets, Scope, Widget, WidgetKind, WidgetTree,
eval_blueprint, try_resolve_future_values, update_tree, AttributeStorage, Components, Elements, EvalContext,
Factory, FloatingWidgets, Scope, WidgetKind, WidgetTree,
};
use events::EventHandler;
use notify::{recommended_watcher, Event, RecommendedWatcher, RecursiveMode, Watcher};
Expand All @@ -63,36 +62,20 @@ impl<T> RuntimeBuilder<T> {
pub fn register_component<C: Component + 'static>(
&mut self,
ident: impl Into<String>,
template_path: impl Into<PathBuf>,
template: impl ToSourceKind,
component: C,
state: C::State,
) -> Result<ComponentId<C::Message>> {
let ident = ident.into();
let id = self.document.add_component(ident, template_path.into())?.into();
let id = self.document.add_component(ident, template.to_source_kind())?.into();
self.component_registry.add_component(id, component, state);
Ok(id.into())
}

pub fn register_default<C>(
&mut self,
ident: impl Into<String>,
template_path: impl Into<PathBuf>,
) -> Result<ComponentId<C::Message>>
where
C: Component + Default + 'static,
C::State: Default,
{
let ident = ident.into();
let id = self.document.add_component(ident, template_path.into())?.into();
self.component_registry
.add_component(id, C::default(), C::State::default());
Ok(id.into())
}

pub fn register_prototype<FC, FS, C>(
&mut self,
ident: impl Into<String>,
template_path: impl Into<PathBuf>,
template: impl ToSourceKind,
proto: FC,
state: FS,
) -> Result<()>
Expand All @@ -102,17 +85,25 @@ impl<T> RuntimeBuilder<T> {
C: Component + 'static,
{
let ident = ident.into();
let id = self.document.add_component(ident, template_path.into())?.into();
let id = self.document.add_component(ident, template.to_source_kind())?.into();
self.component_registry.add_prototype(id, proto, state);
Ok(())
}

pub fn register_default_widget<W: 'static + Widget + Default>(&mut self, ident: &str) {
self.factory.register_default::<W>(ident);
}

pub fn register_widget(&mut self, ident: &str, factory: impl Fn(&Attributes<'_>) -> Box<dyn AnyWidget> + 'static) {
self.factory.register_widget(ident, factory);
pub fn register_default<C>(
&mut self,
ident: impl Into<String>,
template: impl ToSourceKind,
) -> Result<ComponentId<C::Message>>
where
C: Component + Default + 'static,
C::State: Default,
{
let ident = ident.into();
let id = self.document.add_component(ident, template.to_source_kind())?.into();
self.component_registry
.add_component(id, C::default(), C::State::default());
Ok(id.into())
}

pub fn emitter(&self) -> Emitter {
Expand Down Expand Up @@ -316,6 +307,13 @@ where
return;
}

// use std::io::Write;
// let mut file = std::fs::OpenOptions::new()
// .append(true)
// .write(true)
// .open("/tmp/log.lol").unwrap();
// file.write(format!("{}\n", self.changes.len()).as_bytes()).unwrap();

let mut scope = Scope::new();
self.changes.drain().rev().for_each(|(sub, change)| {
sub.iter().for_each(|sub| {
Expand Down Expand Up @@ -367,7 +365,7 @@ where
let Some((node, values)) = tree.get_node_by_path(path) else { return };
let elements = Elements::new(node.children(), values, attribute_storage);

let context = Context {
let context = UntypedContext {
emitter: &self.emitter,
viewport: self.viewport,
assoc_events,
Expand Down Expand Up @@ -453,7 +451,7 @@ where

let Some((node, values)) = tree.get_node_by_path(path) else { return };
let elements = Elements::new(node.children(), values, &mut attribute_storage);
let context = Context {
let context = UntypedContext {
emitter: &self.emitter,
viewport: self.viewport,
assoc_events: &mut assoc_events,
Expand Down Expand Up @@ -505,7 +503,7 @@ where
});

let mut document = Document::new(tpl);
let _component_id = document.add_component("errors", errors);
let _component_id = document.add_component("errors", errors.to_template());
let (blueprint, globals) = document.compile().expect("the error template can't fail");
self.blueprint = blueprint;
self.globals = globals;
Expand Down Expand Up @@ -697,7 +695,7 @@ where
let Some((node, values)) = tree.get_node_by_path(path) else { return };
let elements = Elements::new(node.children(), values, attribute_storage);

let context = Context {
let context = UntypedContext {
emitter: &self.emitter,
viewport: self.viewport,
assoc_events,
Expand Down
27 changes: 27 additions & 0 deletions anathema-state/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::{self, Display};
use std::ops::Deref;

use crate::{Hex, Number};

Expand All @@ -20,6 +21,14 @@ impl<'bp> CommonString<'bp> {
}
}

impl Deref for CommonString<'_> {
type Target = str;

fn deref(&self) -> &str {
self.to_str()
}
}

impl AsRef<str> for CommonString<'_> {
fn as_ref(&self) -> &str {
self.to_str()
Expand Down Expand Up @@ -203,6 +212,21 @@ macro_rules! impl_try_from_int {
};
}

macro_rules! impl_try_from_float {
($t:ty) => {
impl TryFrom<CommonVal<'_>> for $t {
type Error = ();

fn try_from(value: CommonVal<'_>) -> Result<Self, Self::Error> {
match value {
CommonVal::Float(n) => Ok(n as $t),
_ => Err(()),
}
}
}
};
}

impl_try_from_int!(usize);
impl_try_from_int!(isize);
impl_try_from_int!(u64);
Expand All @@ -214,6 +238,9 @@ impl_try_from_int!(i16);
impl_try_from_int!(u8);
impl_try_from_int!(i8);

impl_try_from_float!(f64);
impl_try_from_float!(f32);

#[cfg(test)]
mod test {
use std::rc::Rc;
Expand Down
11 changes: 11 additions & 0 deletions anathema-state/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ impl ElementState {
}
}

fn try_as_ref<T: 'static>(&self) -> Option<&T> {
match self {
Self::Dropped => unreachable!(),
Self::Alive(ref value) => value.to_any_ref().downcast_ref(),
}
}

fn drop_value(&mut self) {
let _ = std::mem::take(self);
}
Expand All @@ -221,6 +228,10 @@ impl<'a, T> Shared<'a, T> {
_p: PhantomData,
}
}

pub fn try_as_ref(&self) -> Option<&T> {
self.state.inner.try_as_ref()
}
}

impl<'a, T> Deref for Shared<'a, T> {
Expand Down
35 changes: 34 additions & 1 deletion anathema-templates/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,44 @@ use crate::token::Tokens;
use crate::variables::Variables;
use crate::Lexer;

pub(crate) enum SourceKind {
pub trait ToSourceKind {
fn to_path(self) -> SourceKind;

fn to_template(self) -> SourceKind;

fn to_source_kind(self) -> SourceKind
where
Self: Sized,
{
self.to_path()
}
}

impl<T: AsRef<str>> ToSourceKind for T {
fn to_path(self) -> SourceKind {
SourceKind::Path(self.as_ref().into())
}

fn to_template(self) -> SourceKind {
SourceKind::Str(self.as_ref().into())
}
}

pub enum SourceKind {
Path(PathBuf),
Str(String),
}

impl ToSourceKind for SourceKind {
fn to_path(self) -> SourceKind {
self
}

fn to_template(self) -> SourceKind {
self
}
}

impl From<PathBuf> for SourceKind {
fn from(value: PathBuf) -> Self {
Self::Path(value)
Expand Down
3 changes: 1 addition & 2 deletions anathema-templates/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ impl Document {
}

#[allow(private_bounds)]
pub fn add_component(&mut self, name: impl Into<String>, src: impl Into<SourceKind>) -> Result<usize> {
pub fn add_component(&mut self, name: impl Into<String>, src: SourceKind) -> Result<usize> {
let name = name.into();
let src = src.into();

let component_src = match src {
SourceKind::Str(s) => ComponentSource::InMemory(s),
Expand Down
2 changes: 1 addition & 1 deletion anathema-templates/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use crate::components::WidgetComponentId;
pub use crate::components::{SourceKind, ToSourceKind, WidgetComponentId};
pub use crate::document::Document;
pub use crate::expressions::Expression;
pub use crate::lexer::Lexer;
Expand Down
Loading