Skip to content

Commit e246a55

Browse files
committed
Dioxus Native: Add use_window and use_raw_window_handle hooks
1 parent 43b6c5d commit e246a55

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/browser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ blitz-html = { workspace = true }
4141
linebender_resource_handle = { workspace = true }
4242
tracing-subscriber = { workspace = true, optional = true }
4343
webbrowser = { workspace = true }
44+
winit = { workspace = true }

packages/dioxus-native/src/dioxus_application.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use blitz_shell::{BlitzApplication, BlitzShellProxy, View};
22
use dioxus_core::{provide_context, ScopeId};
33
use dioxus_history::{History, MemoryHistory};
44
use std::rc::Rc;
5+
use std::sync::Arc;
56
use winit::application::ApplicationHandler;
67
use winit::event::{StartCause, WindowEvent};
78
use winit::event_loop::ActiveEventLoop;
@@ -131,6 +132,7 @@ impl ApplicationHandler for DioxusNativeApplication {
131132

132133
if let Some(config) = self.pending_window.take() {
133134
let mut window = View::init(config, event_loop, &self.inner.proxy);
135+
let winit_window = Arc::clone(&window.window);
134136
let renderer = window.renderer.clone();
135137
let window_id = window.window_id();
136138
let doc = window.downcast_doc_mut::<DioxusDocument>();
@@ -157,6 +159,10 @@ impl ApplicationHandler for DioxusNativeApplication {
157159
doc.vdom
158160
.in_scope(ScopeId::ROOT, move || provide_context(renderer));
159161

162+
// Add winit window
163+
doc.vdom
164+
.in_scope(ScopeId::ROOT, move || provide_context(winit_window));
165+
160166
// Queue rebuild
161167
doc.initial_build();
162168

packages/dioxus-native/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,27 @@ pub use {
4242
use blitz_shell::{
4343
create_default_event_loop, BlitzShellEvent, BlitzShellProxy, Config, WindowConfig,
4444
};
45-
use dioxus_core::{ComponentFunction, Element, VirtualDom};
45+
use dioxus_core::{consume_context, use_hook, ComponentFunction, Element, VirtualDom};
4646
use link_handler::DioxusNativeNavigationProvider;
4747
use std::any::Any;
4848
use std::sync::Arc;
49-
use winit::window::WindowAttributes;
49+
use winit::{
50+
raw_window_handle::{HasWindowHandle as _, RawWindowHandle},
51+
window::{Window, WindowAttributes},
52+
};
53+
54+
pub fn use_window() -> Arc<dyn Window> {
55+
use_hook(|| consume_context::<Arc<dyn Window>>())
56+
}
57+
58+
pub fn use_raw_window_handle() -> RawWindowHandle {
59+
use_hook(|| {
60+
consume_context::<Arc<dyn Window>>()
61+
.window_handle()
62+
.unwrap()
63+
.as_raw()
64+
})
65+
}
5066

5167
/// Launch an interactive HTML/CSS renderer driven by the Dioxus virtualdom
5268
pub fn launch(app: fn() -> Element) {

0 commit comments

Comments
 (0)