More cleaning up of the desktop crate

This commit is contained in:
Jonathan Kelley committed 2024-01-04 17:28:54 -08:00
1 parent 6e08703c6c
commit d47e494786
4 files changed
+39 -50

No files matched your search

+5 -6
View File
@@ -2,7 +2,7 @@ pub use crate::assets::{AssetFuture, AssetHandler, AssetRequest, AssetResponse};
pub use crate::cfg::{Config, WindowCloseBehaviour}; pub use crate::cfg::{Config, WindowCloseBehaviour};
pub use crate::desktop_context::DesktopContext; pub use crate::desktop_context::DesktopContext;
pub use crate::desktop_context::{window, DesktopService, WryEventHandler, WryEventHandlerId}; pub use crate::desktop_context::{window, DesktopService, WryEventHandler, WryEventHandlerId};
use crate::edits::{send_edits, EditQueue, WebviewQueue}; use crate::edits::{EditQueue, WebviewQueue};
use crate::element::DesktopElement; use crate::element::DesktopElement;
use crate::eval::init_eval; use crate::eval::init_eval;
use crate::events::{IpcMessage, IpcMethod}; use crate::events::{IpcMessage, IpcMethod};
@@ -198,7 +198,7 @@ impl<P: 'static> App<P> {
pub fn handle_initialize_msg(&mut self, id: WindowId) { pub fn handle_initialize_msg(&mut self, id: WindowId) {
let view = self.webviews.get_mut(&id).unwrap(); let view = self.webviews.get_mut(&id).unwrap();
send_edits(view.dom.rebuild(), &view.desktop_context); view.desktop_context.send_edits(view.dom.rebuild());
view.desktop_context view.desktop_context
.window .window
.set_visible(self.is_visible_before_start); .set_visible(self.is_visible_before_start);
@@ -271,8 +271,7 @@ impl<P: 'static> App<P> {
}; };
view.dom.handle_event(&name, as_any, element, bubbles); view.dom.handle_event(&name, as_any, element, bubbles);
view.desktop_context.send_edits(view.dom.render_immediate());
send_edits(view.dom.render_immediate(), &view.desktop_context);
} }
#[cfg(all(feature = "hot-reload", debug_assertions))] #[cfg(all(feature = "hot-reload", debug_assertions))]
@@ -319,7 +318,7 @@ impl<P: 'static> App<P> {
view.dom.handle_event(event_name, data, id, event_bubbles); view.dom.handle_event(event_name, data, id, event_bubbles);
} }
send_edits(view.dom.render_immediate(), &view.desktop_context); view.desktop_context.send_edits(view.dom.render_immediate());
} }
} }
@@ -344,7 +343,7 @@ impl<P: 'static> App<P> {
} }
} }
send_edits(view.dom.render_immediate(), &view.desktop_context); view.desktop_context.send_edits(view.dom.render_immediate());
} }
} }
} }
+15 -1
View File
@@ -5,8 +5,8 @@ use crate::Config;
use crate::{assets::AssetFuture, edits::WebviewQueue}; use crate::{assets::AssetFuture, edits::WebviewQueue};
use crate::{assets::AssetHandlerRegistry, edits::EditQueue}; use crate::{assets::AssetHandlerRegistry, edits::EditQueue};
use crate::{events::IpcMessage, webview::WebviewHandler}; use crate::{events::IpcMessage, webview::WebviewHandler};
use dioxus_core::ScopeState;
use dioxus_core::VirtualDom; use dioxus_core::VirtualDom;
use dioxus_core::{Mutations, ScopeState};
use dioxus_interpreter_js::binary_protocol::Channel; use dioxus_interpreter_js::binary_protocol::Channel;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use slab::Slab; use slab::Slab;
@@ -121,6 +121,20 @@ impl DesktopService {
} }
} }
/// Send a list of mutations to the webview
pub(crate) fn send_edits(&self, edits: Mutations) {
let mut channel = self.channel.borrow_mut();
let mut templates = self.templates.borrow_mut();
if let Some(bytes) = crate::edits::apply_edits(
edits,
&mut channel,
&mut templates,
&self.max_template_count,
) {
self.edit_queue.add_edits(bytes)
}
}
/// Create a new window using the props and window builder /// Create a new window using the props and window builder
/// ///
/// Returns the webview handle for the new window. /// Returns the webview handle for the new window.
+14 -40
View File
@@ -1,24 +1,21 @@
use crate::assets::AssetHandlerRegistry;
use crate::query::QueryEngine; use crate::query::QueryEngine;
use crate::shortcut::{HotKey, ShortcutId, ShortcutRegistry, ShortcutRegistryError}; use crate::shortcut::{HotKey, ShortcutId, ShortcutRegistry, ShortcutRegistryError};
use crate::AssetHandler; use crate::AssetHandler;
use crate::Config; use crate::Config;
use crate::{assets::AssetFuture, DesktopContext}; use crate::{assets::AssetFuture, DesktopContext};
use crate::{assets::AssetHandlerRegistry, DesktopService};
use crate::{events::IpcMessage, webview::WebviewHandler}; use crate::{events::IpcMessage, webview::WebviewHandler};
use dioxus_core::{BorrowedAttributeValue, Template, TemplateAttribute, TemplateNode, VirtualDom}; use dioxus_core::{BorrowedAttributeValue, Template, TemplateAttribute, TemplateNode, VirtualDom};
use dioxus_core::{Mutations, ScopeState}; use dioxus_core::{Mutations, ScopeState};
use dioxus_html::event_bubbles; use dioxus_html::event_bubbles;
use dioxus_interpreter_js::binary_protocol::Channel; use dioxus_interpreter_js::binary_protocol::Channel;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use slab::Slab;
use std::{ use std::{
cell::RefCell, fmt::Debug, fmt::Formatter, rc::Rc, rc::Weak, sync::atomic::AtomicU16, cell::RefCell,
sync::Arc, sync::Mutex, rc::Rc,
}; sync::atomic::AtomicU16,
use tao::{ sync::Arc,
event::Event, sync::{atomic::Ordering, Mutex},
event_loop::{EventLoopProxy, EventLoopWindowTarget},
window::{Fullscreen as WryFullscreen, Window, WindowId},
}; };
use wry::{RequestAsyncResponder, WebView}; use wry::{RequestAsyncResponder, WebView};
@@ -33,17 +30,6 @@ pub(crate) struct EditQueue {
responder: Arc<Mutex<Option<RequestAsyncResponder>>>, responder: Arc<Mutex<Option<RequestAsyncResponder>>>,
} }
impl Debug for EditQueue {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EditQueue")
.field("queue", &self.queue)
.field("responder", {
&self.responder.lock().unwrap().as_ref().map(|_| ())
})
.finish()
}
}
impl EditQueue { impl EditQueue {
pub fn handle_request(&self, responder: RequestAsyncResponder) { pub fn handle_request(&self, responder: RequestAsyncResponder) {
let mut queue = self.queue.lock().unwrap(); let mut queue = self.queue.lock().unwrap();
@@ -64,33 +50,21 @@ impl EditQueue {
} }
} }
/// Send a list of mutations to the webview pub(crate) fn apply_edits(
pub fn send_edits(edits: Mutations, desktop_context: &DesktopContext) {
let mut channel = desktop_context.channel.borrow_mut();
let mut templates = desktop_context.templates.borrow_mut();
if let Some(bytes) = apply_edits(
edits,
&mut channel,
&mut templates,
&desktop_context.max_template_count,
) {
desktop_context.edit_queue.add_edits(bytes)
}
}
pub fn apply_edits(
mutations: Mutations, mutations: Mutations,
channel: &mut Channel, channel: &mut Channel,
templates: &mut FxHashMap<String, u16>, templates: &mut FxHashMap<String, u16>,
max_template_count: &AtomicU16, max_template_count: &AtomicU16,
) -> Option<Vec<u8>> { ) -> Option<Vec<u8>> {
use dioxus_core::Mutation::*;
if mutations.templates.is_empty() && mutations.edits.is_empty() { if mutations.templates.is_empty() && mutations.edits.is_empty() {
return None; return None;
} }
for template in mutations.templates { for template in mutations.templates {
add_template(&template, channel, templates, max_template_count); add_template(&template, channel, templates, max_template_count);
} }
use dioxus_core::Mutation::*;
for edit in mutations.edits { for edit in mutations.edits {
match edit { match edit {
AppendChildren { id, m } => channel.append_children(id.0 as u32, m as u16), AppendChildren { id, m } => channel.append_children(id.0 as u32, m as u16),
@@ -156,19 +130,19 @@ pub fn add_template(
templates: &mut FxHashMap<String, u16>, templates: &mut FxHashMap<String, u16>,
max_template_count: &AtomicU16, max_template_count: &AtomicU16,
) { ) {
let current_max_template_count = max_template_count.load(std::sync::atomic::Ordering::Relaxed); let current_max_template_count = max_template_count.load(Ordering::Relaxed);
for root in template.roots.iter() { for root in template.roots.iter() {
create_template_node(channel, root); create_template_node(channel, root);
templates.insert(template.name.to_owned(), current_max_template_count); templates.insert(template.name.to_owned(), current_max_template_count);
} }
channel.add_templates(current_max_template_count, template.roots.len() as u16); channel.add_templates(current_max_template_count, template.roots.len() as u16);
max_template_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed); max_template_count.fetch_add(1, Ordering::Relaxed);
} }
pub fn create_template_node(channel: &mut Channel, v: &'static TemplateNode<'static>) { pub fn create_template_node(channel: &mut Channel, node: &'static TemplateNode<'static>) {
use TemplateNode::*; use TemplateNode::*;
match v { match node {
Element { Element {
tag, tag,
namespace, namespace,
+5 -3
View File
@@ -1,12 +1,14 @@
use muda::{Menu, MenuItem, PredefinedMenuItem, Submenu}; use muda::{Menu, PredefinedMenuItem, Submenu};
use tao::window::Window; use tao::window::Window;
#[allow(unused)]
pub fn build_menu_bar(menu: Menu, window: &Window) { pub fn build_menu_bar(menu: Menu, window: &Window) {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
menu.init_for_hwnd(window_hwnd); menu.init_for_hwnd(window);
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
menu.init_for_gtk_window(&gtk_window, Some(&vertical_gtk_box)); menu.init_for_gtk_window(window, None);
// menu.init_for_gtk_window(window, Some(&vertical_gtk_box));
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
menu.init_for_nsapp(); menu.init_for_nsapp();