Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Jan 22, 2024
1 parent 4d31305 commit e014b39
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 251 deletions.
20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ freya-testing = { path = "crates/testing", version = "0.1" }
freya-engine = { path = "crates/engine", version = "0.1" }
torin = { path = "crates/torin", version = "0.1" }

dioxus = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269", default-features = false }
dioxus-native-core-macro = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269" }
dioxus-rsx = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269", features = ["hot_reload"] }
dioxus-native-core = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269", features = ["dioxus"] }
dioxus-core-macro = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269" }
dioxus-hooks = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269" }
dioxus-signals = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269" }
dioxus-core = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269" }
dioxus-hot-reload = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269", features = ["file_watcher"], default-features = false }
dioxus-router = { git = "https://github.com/DioxusLabs/dioxus", rev = "fc0b0f02a16c38dc4744bf7a1264a5cc62088269", default-features = false }
dioxus = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5", default-features = false, features = ["macro", "signals", "hooks", "hot-reload"]}
dioxus-native-core-macro = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5" }
dioxus-rsx = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5", features = ["hot_reload"] }
dioxus-native-core = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5", features = ["dioxus"] }
dioxus-core-macro = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5" }
dioxus-hooks = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5" }
dioxus-signals = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5" }
dioxus-core = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5" }
dioxus-hot-reload = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5", features = ["file_watcher"], default-features = false }
dioxus-router = { git = "https://github.com/DioxusLabs/dioxus", rev = "8f786d85cdd47fa87f0275a368b8738d1815f4d5", default-features = false }

skia-safe = { version = "0.67.0", features = ["gl", "textlayout", "svg"] }

Expand Down
51 changes: 29 additions & 22 deletions crates/components/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ use freya_hooks::{use_applied_theme, use_focus, use_platform, ButtonTheme, Butto
use winit::window::CursorIcon;

/// [`Button`] component properties.
#[derive(Props)]
pub struct ButtonProps<'a> {
#[derive(Props, Clone, PartialEq)]
pub struct ButtonProps {
/// Theme override.
#[props(optional)]
pub theme: Option<ButtonThemeWith>,
/// Inner children for the Button.
pub children: Element<'a>,
pub children: Element,
/// Handler for the `onclick` event.
#[props(optional)]
pub onclick: Option<EventHandler<'a, Option<MouseEvent>>>,
pub onclick: Option<EventHandler<Option<MouseEvent>>>,
}

/// Identifies the current status of the Button.
Expand Down Expand Up @@ -53,12 +53,13 @@ pub enum ButtonStatus {
/// ```
///
#[allow(non_snake_case)]
pub fn Button<'a>(cx: Scope<'a, ButtonProps<'a>>) -> Element {
let focus = use_focus(cx);
let status = use_state(cx, ButtonStatus::default);
let platform = use_platform(cx);
pub fn Button(props: ButtonProps) -> Element {
let focus = use_focus();
let mut status = use_signal(ButtonStatus::default);
let platform = use_platform();

let focus_id = focus.attribute(cx);
let focus_id = focus.attribute();
let click = &props.onclick;

let ButtonTheme {
background,
Expand All @@ -71,19 +72,22 @@ pub fn Button<'a>(cx: Scope<'a, ButtonProps<'a>>) -> Element {
width,
height,
font_theme,
} = use_applied_theme!(cx, &cx.props.theme, button);
} = use_applied_theme!(&props.theme, button);

let onclick = move |ev| {
focus.focus();
if let Some(onclick) = &cx.props.onclick {
onclick.call(Some(ev))
let onclick = {
to_owned![focus, click];
move |ev| {
focus.focus();
if let Some(onclick) = &click {
onclick.call(Some(ev))
}
}
};

use_on_destroy(cx, {
use_on_destroy({
to_owned![status, platform];
move || {
if *status.current() == ButtonStatus::Hovering {
if *status.read() == ButtonStatus::Hovering {
platform.set_cursor(CursorIcon::default());
}
}
Expand All @@ -102,15 +106,18 @@ pub fn Button<'a>(cx: Scope<'a, ButtonProps<'a>>) -> Element {
status.set(ButtonStatus::default());
};

let onkeydown = |e: KeyboardEvent| {
if focus.validate_keydown(e) {
if let Some(onclick) = &cx.props.onclick {
onclick.call(None)
let onkeydown = {
to_owned![focus];
move |e: KeyboardEvent| {
if focus.validate_keydown(e) {
if let Some(onclick) = &props.onclick {
onclick.call(None)
}
}
}
};

let background = match *status.get() {
let background = match *status.read() {
ButtonStatus::Hovering => hover_background,
ButtonStatus::Idle => background,
};
Expand Down Expand Up @@ -142,7 +149,7 @@ pub fn Button<'a>(cx: Scope<'a, ButtonProps<'a>>) -> Element {
text_align: "center",
main_align: "center",
cross_align: "center",
&cx.props.children
{&props.children}
}
)
}
84 changes: 40 additions & 44 deletions crates/components/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
//! # Freya Components
//! A collection of basic components to be used in Freya.

/*
mod accordion;
mod body;
// mod accordion;
// mod body;
mod button;
mod canvas;
mod cursor_area;
mod drag_drop;
mod dropdown;
mod external_link;
mod gesture_area;
mod graph;
mod icons;
mod input;
mod loader;
mod network_image;
mod progress_bar;
mod scroll_views;
mod slider;
mod switch;
mod table;
mod theme;
mod tooltip;
// mod canvas;
// mod cursor_area;
// mod drag_drop;
// mod dropdown;
// mod external_link;
// mod gesture_area;
// mod graph;
// mod icons;
// mod input;
// mod loader;
// mod network_image;
// mod progress_bar;
// mod scroll_views;
// mod slider;
// mod switch;
// mod table;
// mod theme;
// mod tooltip;

pub use accordion::*;
pub use body::*;
// pub use accordion::*;
// pub use body::*;
pub use button::*;
pub use canvas::*;
pub use cursor_area::*;
pub use drag_drop::*;
pub use dropdown::*;
pub use external_link::*;
pub use gesture_area::*;
pub use graph::*;
pub use icons::*;
pub use input::*;
pub use loader::*;
pub use network_image::*;
pub use progress_bar::*;
pub use scroll_views::*;
pub use slider::*;
pub use switch::*;
pub use table::*;
pub use theme::*;
pub use tooltip::*;
*/
// pub use canvas::*;
// pub use cursor_area::*;
// pub use drag_drop::*;
// pub use dropdown::*;
// pub use external_link::*;
// pub use gesture_area::*;
// pub use graph::*;
// pub use icons::*;
// pub use input::*;
// pub use loader::*;
// pub use network_image::*;
// pub use progress_bar::*;
// pub use scroll_views::*;
// pub use slider::*;
// pub use switch::*;
// pub use table::*;
// pub use theme::*;
// pub use tooltip::*;
31 changes: 14 additions & 17 deletions crates/dom/src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::sync::MutexGuard;
use torin::prelude::*;
use tracing::info;

use crate::dom_adapter::DioxusDOMAdapter;
use crate::{dom_adapter::DioxusDOMAdapter, mutations_writer::MutationsWriter};

pub type DioxusDOM = RealDom<CustomAttributeValues>;
pub type DioxusNode<'a> = NodeRef<'a, CustomAttributeValues>;
Expand Down Expand Up @@ -111,9 +111,12 @@ impl FreyaDOM {

/// Create the initial DOM from the given Mutations
pub fn init_dom(&mut self, vdom: &mut VirtualDom, scale_factor: f32) {
let mut rdom = &mut self.rdom;
let mut state = &mut self.dioxus_integration_state;
vdom.rebuild(&mut state.create_mutation_writer(&mut rdom));
vdom.rebuild(&mut MutationsWriter {
native_writer: self
.dioxus_integration_state
.create_mutation_writer(&mut self.rdom),
layout: &mut self.torin.lock().unwrap(),
});

let mut ctx = SendAnyMap::new();
ctx.insert(scale_factor);
Expand All @@ -123,19 +126,13 @@ impl FreyaDOM {
}

/// Process the given mutations from the [`VirtualDOM`](dioxus_core::VirtualDom).
pub fn rebuild(&mut self, vdom: &mut VirtualDom, scale_factor: f32) -> (bool, bool) {
let mut dom_adapter = DioxusDOMAdapter::new_with_cache(self.rdom());
// Apply the mutations to the layout
// self.layout()
//.apply_mutations(&mutations, &self.dioxus_integration_state, &mut dom_adapter);

// Apply the mutations the integration state
//self.dioxus_integration_state
//.apply_mutations(&mut self.rdom, mutations);

let mut rdom = &mut self.rdom;
let mut state = &mut self.dioxus_integration_state;
vdom.rebuild(&mut state.create_mutation_writer(&mut rdom));
pub fn render_mutations(&mut self, vdom: &mut VirtualDom, scale_factor: f32) -> (bool, bool) {
vdom.render_immediate(&mut MutationsWriter {
native_writer: self
.dioxus_integration_state
.create_mutation_writer(&mut self.rdom),
layout: &mut self.torin.lock().unwrap(),
});

// Update the Nodes states
let mut ctx = SendAnyMap::new();
Expand Down
1 change: 1 addition & 0 deletions crates/dom/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod dom;
pub mod dom_adapter;
mod mutations_writer;

pub mod prelude {
pub use crate::dom::*;
Expand Down
104 changes: 104 additions & 0 deletions crates/dom/src/mutations_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use dioxus_core::WriteMutations;
use dioxus_native_core::{dioxus::DioxusNativeCoreMutationWriter, NodeId};
use freya_node_state::CustomAttributeValues;
use torin::torin::Torin;

use crate::prelude::DioxusDOMAdapter;

pub struct MutationsWriter<'a> {
pub native_writer: DioxusNativeCoreMutationWriter<'a, CustomAttributeValues>,
pub layout: &'a mut Torin<NodeId>,
}

impl<'a> WriteMutations for MutationsWriter<'a> {
fn register_template(&mut self, template: dioxus_core::prelude::Template) {
self.native_writer.register_template(template);
}

fn append_children(&mut self, id: dioxus_core::ElementId, m: usize) {
self.native_writer.append_children(id, m);
}

fn assign_node_id(&mut self, path: &'static [u8], id: dioxus_core::ElementId) {
self.native_writer.assign_node_id(path, id);
}

fn create_placeholder(&mut self, id: dioxus_core::ElementId) {
self.native_writer.create_placeholder(id);
}

fn create_text_node(&mut self, value: &str, id: dioxus_core::ElementId) {
self.native_writer.create_text_node(value, id);
}

fn hydrate_text_node(&mut self, path: &'static [u8], value: &str, id: dioxus_core::ElementId) {
self.native_writer.hydrate_text_node(path, value, id);
}

fn load_template(&mut self, name: &'static str, index: usize, id: dioxus_core::ElementId) {
self.native_writer.load_template(name, index, id);
}

fn replace_node_with(&mut self, id: dioxus_core::ElementId, m: usize) {
if m > 0 {
let mut dom_adapter = DioxusDOMAdapter::new_with_cache(&self.native_writer.rdom);
self.layout.remove(
self.native_writer.state.element_to_node_id(id),
&mut dom_adapter,
true,
);
}

self.native_writer.replace_node_with(id, m);
}

fn replace_placeholder_with_nodes(&mut self, path: &'static [u8], m: usize) {
self.native_writer.replace_placeholder_with_nodes(path, m);
}

fn insert_nodes_after(&mut self, id: dioxus_core::ElementId, m: usize) {
self.native_writer.insert_nodes_after(id, m);
}

fn insert_nodes_before(&mut self, id: dioxus_core::ElementId, m: usize) {
self.native_writer.insert_nodes_before(id, m);
}

fn set_attribute(
&mut self,
name: &'static str,
ns: Option<&'static str>,
value: &dioxus_core::AttributeValue,
id: dioxus_core::ElementId,
) {
self.native_writer.set_attribute(name, ns, value, id);
}

fn set_node_text(&mut self, value: &str, id: dioxus_core::ElementId) {
self.layout
.invalidate(self.native_writer.state.element_to_node_id(id));
self.native_writer.set_node_text(value, id);
}

fn create_event_listener(&mut self, name: &'static str, id: dioxus_core::ElementId) {
self.native_writer.create_event_listener(name, id);
}

fn remove_event_listener(&mut self, name: &'static str, id: dioxus_core::ElementId) {
self.native_writer.remove_event_listener(name, id);
}

fn remove_node(&mut self, id: dioxus_core::ElementId) {
let mut dom_adapter = DioxusDOMAdapter::new_with_cache(&self.native_writer.rdom);
self.layout.remove(
self.native_writer.state.element_to_node_id(id),
&mut dom_adapter,
true,
);
self.native_writer.remove_node(id);
}

fn push_root(&mut self, id: dioxus_core::ElementId) {
self.native_writer.push_root(id);
}
}
Loading

0 comments on commit e014b39

Please sign in to comment.