Skip to content

Commit

Permalink
Merge with main
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jul 3, 2024
2 parents 709d505 + f7fe94f commit f657a96
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 82 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ style_config = { git = "https://github.com/dioxuslabs/stylo", rev = "10767f4" }
style_traits = { git = "https://github.com/dioxuslabs/stylo", rev = "10767f4" } # 2024-05-15 + dioxus patches
selectors = { git = "https://github.com/dioxuslabs/stylo", rev = "10767f4" } # 2024-05-15 + dioxus patches
html5ever = "0.27" # needs to match stylo markup5ever version
taffy = { version = "0.5.1" }
taffy = { git = "https://github.com/dioxuslabs/taffy", rev = "9651a18b7da88204741018545edd13eda8fb2b53" }
parley = { git = "https://github.com/nicoburns/parley", rev = "482d0fbd59eceaa68cc879e0102a7a9a87636a0d" }
dioxus = { git = "https://github.com/dioxuslabs/dioxus", rev = "a3aa6ae771a2d0a4d8cb6055c41efc0193b817ef"}
dioxus-ssr = { git = "https://github.com/dioxuslabs/dioxus", rev = "a3aa6ae771a2d0a4d8cb6055c41efc0193b817ef" }
Expand All @@ -39,7 +39,9 @@ publish = false
opt-level = 2

# Need to force specific versions of these dependencies
[dependencies]
[dev-dependencies]
# webrender = "0.61.0"
# mozbuild = "0.1.0"
blitz = { path = "./packages/blitz" }
blitz-dom = { path = "./packages/dom" }
comrak = { version = "0.21.0", default-features = false }
Expand Down
12 changes: 2 additions & 10 deletions packages/blitz/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,19 +1152,11 @@ impl ElementCx<'_> {
fn draw_solid_frame(&self, scene: &mut Scene) {
let background = self.style.get_background();

// todo: handle non-absolute colors
let bg_color = background.background_color.clone();
let bg_color = bg_color.as_absolute().unwrap();
let bg_color = background.background_color.as_vello();
let shape = self.frame.frame();

// Fill the color
scene.fill(
Fill::NonZero,
self.transform,
bg_color.as_vello(),
None,
&shape,
);
scene.fill(Fill::NonZero, self.transform, bg_color, None, &shape);
}

/// Stroke a border
Expand Down
18 changes: 13 additions & 5 deletions packages/dioxus-blitz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ edition = "2021"

[features]
accessibility = ["dep:accesskit", "dep:accesskit_winit"]
hot-reload = []
default = ["accessibility"]
hot-reload = ["dep:dioxus-cli-config", "dep:dioxus-hot-reload"]
menu = ["dep:muda"]
default = ["accessibility", "menu"]

[dependencies]
accesskit = { version = "0.15.0", optional = true }
accesskit_winit = { version = "0.21.1", optional = true }
winit = { version = "0.30.2", features = ["rwh_06"] }
muda = { version = "0.11.5", features = ["serde"] }
muda = { version = "0.11.5", features = ["serde"], optional = true }
tokio = { workspace = true, features = ["full"] }
dioxus = { workspace = true }
dioxus-cli-config = { git = "https://github.com/dioxuslabs/dioxus", rev = "a3aa6ae771a2d0a4d8cb6055c41efc0193b817ef"}
dioxus-hot-reload = { git = "https://github.com/dioxuslabs/dioxus", rev = "a3aa6ae771a2d0a4d8cb6055c41efc0193b817ef"}
dioxus-cli-config = { git = "https://github.com/dioxuslabs/dioxus", rev = "a3aa6ae771a2d0a4d8cb6055c41efc0193b817ef", optional = true }
dioxus-hot-reload = { git = "https://github.com/dioxuslabs/dioxus", rev = "a3aa6ae771a2d0a4d8cb6055c41efc0193b817ef", optional = true }
futures-util = "0.3.30"
vello = { workspace = true }
wgpu = { workspace = true }
Expand All @@ -26,3 +27,10 @@ blitz-dom = { path = "../dom" }
url = { version = "2.5.0", features = ["serde"] }
ureq = "2.9"
rustc-hash = "1.1.0"

[target.'cfg(target_os = "android")'.dependencies]
android-activity = { version = "0.6.0", features = ["native-activity"] }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
2 changes: 2 additions & 0 deletions packages/dioxus-blitz/src/documents/dioxus_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ impl MutationWriter<'_> {
}
// If element_id is already mapping to a node, remove that node from the document
else if let Some(mapped_node_id) = self.state.node_id_mapping[element_id] {
// todo: we should mark these as needing garbage collection?
self.doc.remove_node(mapped_node_id);
}

Expand Down Expand Up @@ -271,6 +272,7 @@ impl WriteMutations for MutationWriter<'_> {
let parent = self.state.element_to_node_id(id);
for child in children {
self.doc.get_node_mut(parent).unwrap().children.push(child);
self.doc.get_node_mut(child).unwrap().parent = Some(parent);
}
}

Expand Down
63 changes: 46 additions & 17 deletions packages/dioxus-blitz/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(docsrs, feature(doc_cfg))]

mod documents;
mod waker;
mod window;
Expand All @@ -12,7 +14,6 @@ use blitz::RenderState;
use blitz_dom::DocumentLike;
use dioxus::prelude::*;
use documents::DioxusDocument;
use muda::{MenuEvent, MenuId};
use std::collections::HashMap;
use url::Url;
use winit::event_loop::EventLoop;
Expand All @@ -22,6 +23,10 @@ use winit::{
event_loop::ControlFlow,
};

pub mod exports {
pub use dioxus;
}

#[derive(Default)]
pub struct Config {
pub stylesheets: Vec<String>,
Expand Down Expand Up @@ -96,15 +101,25 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
let _guard = rt.enter();

// Build an event loop for the application
let event_loop = EventLoop::<UserEvent>::with_user_event().build().unwrap();
let mut builder = EventLoop::<UserEvent>::with_user_event();

#[cfg(target_os = "android")]
{
use winit::platform::android::EventLoopBuilderExtAndroid;
builder.with_android_app(current_android_app());
}

let event_loop = builder.build().unwrap();
let proxy = event_loop.create_proxy();

// Multiwindow ftw
let mut windows: HashMap<WindowId, window::View<'_, Doc>> = HashMap::new();
let mut pending_windows = Vec::new();

pending_windows.push(window);
let menu_channel = MenuEvent::receiver();

#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
let menu_channel = muda::MenuEvent::receiver();

#[cfg(not(any(target_os = "android", target_os = "ios")))]
let mut initial = true;
Expand All @@ -117,21 +132,16 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
not(target_os = "ios")
))]
{
let Ok(cfg) = dioxus_cli_config::CURRENT_CONFIG.as_ref() else {
return;
};

dioxus_hot_reload::connect_at(cfg.target_dir.join("dioxusin"), {
let proxy = proxy.clone();
move |template| {
let _ = proxy.send_event(UserEvent::HotReloadEvent(template));
}
});
if let Ok(cfg) = dioxus_cli_config::CURRENT_CONFIG.as_ref() {
dioxus_hot_reload::connect_at(cfg.target_dir.join("dioxusin"), {
let proxy = proxy.clone();
move |template| {
let _ = proxy.send_event(UserEvent::HotReloadEvent(template));
}
})
}
}

// the move to winit wants us to use a struct with a run method instead of the callback approach
// we want to just keep the callback approach for now
#[allow(deprecated)]
// the move to winit wants us to use a struct with a run method instead of the callback approach
// we want to just keep the callback approach for now
#[allow(deprecated)]
Expand Down Expand Up @@ -261,8 +271,9 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
_ => (),
}

#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
if let Ok(event) = menu_channel.try_recv() {
if event.id == MenuId::new("dev.show_layout") {
if event.id == muda::MenuId::new("dev.show_layout") {
for (_, view) in windows.iter_mut() {
view.renderer.devtools.show_layout = !view.renderer.devtools.show_layout;
view.request_redraw();
Expand All @@ -272,3 +283,21 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
})
.unwrap();
}

#[cfg(target_os = "android")]
static ANDROID_APP: std::sync::OnceLock<android_activity::AndroidApp> = std::sync::OnceLock::new();

#[cfg(target_os = "android")]
#[cfg_attr(docsrs, doc(cfg(target_os = "android")))]
/// Set the current [`AndroidApp`](android_activity::AndroidApp).
pub fn set_android_app(app: android_activity::AndroidApp) {
ANDROID_APP.set(app).unwrap()
}

#[cfg(target_os = "android")]
#[cfg_attr(docsrs, doc(cfg(target_os = "android")))]
/// Get the current [`AndroidApp`](android_activity::AndroidApp).
/// This will panic if the android activity has not been setup with [`set_android_app`].
pub fn current_android_app(app: android_activity::AndroidApp) -> AndroidApp {
ANDROID_APP.get().unwrap().clone()
}
44 changes: 25 additions & 19 deletions packages/dioxus-blitz/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use winit::keyboard::PhysicalKey;
#[allow(unused)]
use wgpu::rwh::HasWindowHandle;

use muda::{AboutMetadata, Menu, MenuId, MenuItem, PredefinedMenuItem, Submenu};
use std::mem;
use std::sync::Arc;
use std::task::Waker;
use vello::Scene;
Expand All @@ -22,7 +20,8 @@ struct State {
accessibility: crate::accessibility::AccessibilityState,

/// Main menu bar of this view's window.
_menu: Menu,
#[cfg(feature = "menu")]
_menu: muda::Menu,
}

pub(crate) struct View<'s, Doc: DocumentLike> {
Expand All @@ -33,7 +32,7 @@ pub(crate) struct View<'s, Doc: DocumentLike> {
/// need to store them in order to have access to them when processing keypress events
keyboard_modifiers: ModifiersState,

/// State of this view, created on [`View::resume`].
#[cfg(any(feature = "accessibility", feature = "menu"))]
state: Option<State>,
}

Expand All @@ -44,6 +43,7 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
scene: Scene::new(),
waker: None,
keyboard_modifiers: Default::default(),
#[cfg(any(feature = "accessibility", feature = "menu"))]
state: None,
}
}
Expand All @@ -60,7 +60,7 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
{
if let Some(ref mut state) = self.state {
// TODO send fine grained accessibility tree updates.
let changed = mem::take(&mut self.renderer.dom.as_mut().changed);
let changed = std::mem::take(&mut self.renderer.dom.as_mut().changed);
if !changed.is_empty() {
state.accessibility.build_tree(self.renderer.dom.as_ref());
}
Expand Down Expand Up @@ -329,19 +329,22 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
}))
.unwrap();

// Initialize the menu and accessibility adapter in this view's state.
let menu = init_menu(
#[cfg(target_os = "windows")]
&window,
);
self.state = Some(State {
#[cfg(feature = "accessibility")]
accessibility: crate::accessibility::AccessibilityState::new(
&window,
proxy.clone(),
),
_menu: menu,
});
// Initialize the accessibility and menu bar state.
#[cfg(any(feature = "accessibility", feature = "menu"))]
{
self.state = Some(State {
#[cfg(feature = "accessibility")]
accessibility: crate::accessibility::AccessibilityState::new(
&window,
proxy.clone(),
),
#[cfg(feature = "menu")]
_menu: init_menu(
#[cfg(target_os = "windows")]
&window,
),
});
}

let size: winit::dpi::PhysicalSize<u32> = window.inner_size();
let mut viewport = Viewport::new((size.width, size.height));
Expand All @@ -367,7 +370,10 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
}

/// Initialize the default menu bar.
pub fn init_menu(#[cfg(target_os = "windows")] window: &Window) -> Menu {
#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
pub fn init_menu(#[cfg(target_os = "windows")] window: &Window) -> muda::Menu {
use muda::{AboutMetadata, Menu, MenuId, MenuItem, PredefinedMenuItem, Submenu};

let menu = Menu::new();

// Build the about section
Expand Down
3 changes: 1 addition & 2 deletions packages/dom/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ impl Document {
let node = &self.nodes[node_id];
let node_child_idx = node.child_idx;

// Get this node's parent, or the root node if it has none.
let parent_id = node.parent.unwrap_or_default();
let parent_id = node.parent.unwrap();
let parent = &mut self.nodes[parent_id];

// Mark the node's parent as changed.
Expand Down
24 changes: 17 additions & 7 deletions packages/dom/src/layout/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ pub(crate) fn collect_layout_children(
} else {
match display.outside() {
DisplayOutside::None => {}
DisplayOutside::Inline => all_block = false,
DisplayOutside::Block => all_inline = false,

// TODO: Implement table layout
DisplayOutside::TableCaption => {}
DisplayOutside::InternalTable => {}
DisplayOutside::Block
| DisplayOutside::TableCaption
| DisplayOutside::InternalTable => all_inline = false,
DisplayOutside::Inline => {
all_block = false;

// We need the "complex" tree fixing when an inline contains a block
if child.is_or_contains_block() {
all_inline = false;
}
}
}
}
}
Expand Down Expand Up @@ -162,11 +167,16 @@ fn collect_complex_layout_children(
let child_node_kind = doc.nodes[child_id].raw_dom_data.kind();

// Get Display style. Default to inline because nodes without styles are probably text nodes
let contains_block = doc.nodes[child_id].is_or_contains_block();
let child_display = &doc.nodes[child_id]
.display_style()
.unwrap_or(Display::inline());
let display_inside = child_display.inside();
let display_outside = child_display.outside();
let display_outside = if contains_block {
DisplayOutside::Block
} else {
child_display.outside()
};

let is_whitespace_node = match &doc.nodes[child_id].raw_dom_data {
NodeData::Text(data) => data.content.chars().all(|c| c.is_ascii_whitespace()),
Expand Down
Loading

0 comments on commit f657a96

Please sign in to comment.