move ScopeId methods to the runtime (#4723)

* move `ScopeId` methods to runtime methods

* cargo check unused

* wip...

* wip...

* fix consume context

* fix test

* guard hoisted

* fix doc comment
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2025-10-04 01:55:52 -07:00
1 parent c56bac04f4
commit c29cef4ea3
40 files changed
+466 -697

No files matched your search

+5 -11
View File
@@ -8,7 +8,7 @@ use crate::{
shortcut::ShortcutRegistry,
webview::{PendingWebview, WebviewInstance},
};
use dioxus_core::{ElementId, ScopeId, VirtualDom};
use dioxus_core::{consume_context, ElementId, ScopeId, VirtualDom};
use dioxus_history::History;
use dioxus_html::PlatformEventData;
use std::{
@@ -550,11 +550,8 @@ impl App {
return;
};
let url = webview.dom.in_runtime(|| {
ScopeId::ROOT
.consume_context::<Rc<dyn History>>()
.unwrap()
.current_route()
let url = webview.dom.in_scope(ScopeId::ROOT, || {
consume_context::<Rc<dyn History>>().current_route()
});
let state = PreservedWindowState {
@@ -619,12 +616,9 @@ impl App {
}
// Set the url if it exists
webview.dom.in_runtime(|| {
webview.dom.in_scope(ScopeId::ROOT, || {
if let Some(url) = state.url {
ScopeId::ROOT
.consume_context::<Rc<dyn History>>()
.unwrap()
.replace(url);
consume_context::<Rc<dyn History>>().replace(url);
}
})
}
+3 -3
View File
@@ -22,13 +22,13 @@ pub fn use_wry_event_handler(
use dioxus_core::current_scope_id;
// Capture the current runtime and scope ID.
let runtime = Runtime::current().unwrap();
let scope_id = current_scope_id().unwrap();
let runtime = Runtime::current();
let scope_id = current_scope_id();
use_hook_with_cleanup(
move || {
window().create_wry_event_handler(move |event, target| {
runtime.on_scope(scope_id, || handler(event, target))
runtime.in_scope(scope_id, || handler(event, target))
})
},
move |handler| handler.remove(),
+10 -16
View File
@@ -60,37 +60,31 @@ pub fn launch_virtual_dom_blocking(virtual_dom: VirtualDom, mut desktop_config:
// Windows-only drag-n-drop fix events. We need to call the interpreter drag-n-drop code.
UserWindowEvent::WindowsDragDrop(id) => {
if let Some(webview) = app.webviews.get(&id) {
webview.dom.in_runtime(|| {
ScopeId::ROOT.in_runtime(|| {
eval("window.interpreter.handleWindowsDragDrop();");
});
webview.dom.in_scope(ScopeId::ROOT, || {
eval("window.interpreter.handleWindowsDragDrop();");
});
}
}
UserWindowEvent::WindowsDragLeave(id) => {
if let Some(webview) = app.webviews.get(&id) {
webview.dom.in_runtime(|| {
ScopeId::ROOT.in_runtime(|| {
eval("window.interpreter.handleWindowsDragLeave();");
});
webview.dom.in_scope(ScopeId::ROOT, || {
eval("window.interpreter.handleWindowsDragLeave();");
});
}
}
UserWindowEvent::WindowsDragOver(id, x_pos, y_pos) => {
if let Some(webview) = app.webviews.get(&id) {
webview.dom.in_runtime(|| {
ScopeId::ROOT.in_runtime(|| {
let e = eval(
r#"
webview.dom.in_scope(ScopeId::ROOT, || {
let e = eval(
r#"
const xPos = await dioxus.recv();
const yPos = await dioxus.recv();
window.interpreter.handleWindowsDragOver(xPos, yPos)
"#,
);
);
_ = e.send(x_pos);
_ = e.send(y_pos);
});
_ = e.send(x_pos);
_ = e.send(y_pos);
});
}
}
+8 -10
View File
@@ -10,7 +10,7 @@ use crate::{
};
use crate::{document::DesktopDocument, WeakDesktopContext};
use base64::prelude::BASE64_STANDARD;
use dioxus_core::{Runtime, ScopeId, VirtualDom};
use dioxus_core::{consume_context, provide_context, Runtime, ScopeId, VirtualDom};
use dioxus_document::Document;
use dioxus_history::{History, MemoryHistory};
use dioxus_hooks::to_owned;
@@ -427,10 +427,10 @@ impl WebviewInstance {
edits.set_desktop_context(Rc::downgrade(&desktop_context));
let provider: Rc<dyn Document> = Rc::new(DesktopDocument::new(desktop_context.clone()));
let history_provider: Rc<dyn History> = Rc::new(MemoryHistory::default());
dom.in_runtime(|| {
ScopeId::ROOT.provide_context(desktop_context.clone());
ScopeId::ROOT.provide_context(provider);
ScopeId::ROOT.provide_context(history_provider);
dom.in_scope(ScopeId::ROOT, || {
provide_context(desktop_context.clone());
provide_context(provider);
provide_context(history_provider);
});
WebviewInstance {
@@ -566,11 +566,9 @@ impl PendingWebview {
pub(crate) fn create_window(self, shared: &Rc<SharedContext>) -> WebviewInstance {
let window = WebviewInstance::new(self.cfg, self.dom, shared.clone());
let cx = window.dom.in_runtime(|| {
ScopeId::ROOT
.consume_context::<Rc<DesktopService>>()
.unwrap()
});
let cx = window
.dom
.in_scope(ScopeId::ROOT, consume_context::<Rc<DesktopService>>);
_ = self.sender.send(cx);
window