11//! Demonstrate opening additional windows from a pure Dioxus app.
22
3+ use blitz_traits:: shell:: ShellProvider ;
34use dioxus:: prelude:: * ;
4- use dioxus_native:: { DioxusWindowHandle , DioxusWindowInfo } ;
5- use std:: time :: Duration ;
5+ use dioxus_native:: DioxusNativeProvider ;
6+ use std:: sync :: Arc ;
67use winit:: window:: { WindowAttributes , WindowButtons } ;
78
89fn main ( ) {
@@ -15,153 +16,42 @@ fn main() {
1516}
1617
1718fn app ( ) -> Element {
18- let window_handle = use_context :: < DioxusWindowHandle > ( ) ;
19- let mut counter = use_signal ( || 1u32 ) ;
20- let open_simple = window_handle. clone ( ) ;
21- let open_with_props = window_handle. clone ( ) ;
22- let refresh_handle = window_handle. clone ( ) ;
23- let base_focus_handle = window_handle. clone ( ) ;
24- let base_rename_handle = window_handle. clone ( ) ;
25- let mut known_windows: Signal < Vec < DioxusWindowInfo > > =
26- use_signal ( || window_handle. list_windows ( ) ) ;
27- let known_windows_signal = known_windows. clone ( ) ;
28- let refresh_handle_for_list = refresh_handle. clone ( ) ;
29- let rename_counter = use_signal ( || 1u32 ) ;
30-
31- {
32- let refresh_handle = window_handle. clone ( ) ;
33- let mut known_windows_signal = known_windows. clone ( ) ;
34- use_future ( move || {
35- let refresh_handle = refresh_handle. clone ( ) ;
36- async move {
37- loop {
38- tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
39- known_windows_signal. set ( refresh_handle. list_windows ( ) ) ;
40- }
41- }
42- } ) ;
43- }
44-
45- let windows_snapshot = known_windows ( ) ;
46-
19+ let provider = use_context :: < DioxusNativeProvider > ( ) ;
20+ let mut counter = use_signal ( || 0 ) ;
4721 rsx ! {
4822 main {
49- style { { PRIMARY_STYLES } }
5023 h1 { "Blitz multi-window" }
5124 p { "Click the button to open another RSX window." }
5225 div {
5326 button {
5427 onclick: move |_| {
55- open_simple. open_window_with_attributes(
56- secondary_window,
57- Some (
58- WindowAttributes :: default ( )
59- . with_title( "Secondary window" )
60- . with_inner_size( winit:: dpi:: LogicalSize :: new( 400.0 , 300.0 ) ) ,
61- ) ,
62- ) ;
63- known_windows. set( open_simple. list_windows( ) ) ;
64- } ,
65- "Open secondary window"
66- }
67- button {
68- onclick: move |_| {
69- let idx = counter( ) ;
70- open_with_props. open_window_with_props_and_attributes(
71- message_window,
72- MessageWindowProps {
73- message: format!( "Window #{idx}" ) ,
74- } ,
75- Some (
76- WindowAttributes :: default ( )
77- . with_title( format!( "Window #{idx}" ) )
78- . with_inner_size( winit:: dpi:: LogicalSize :: new( 320.0 , 240.0 ) ) ,
79- ) ,
80- ) ;
28+ let vdom = VirtualDom :: new( secondary_window) ;
29+ let attributes = WindowAttributes :: default ( )
30+ . with_title( format!( "window#{}" , counter) )
31+ . with_inner_size( winit:: dpi:: LogicalSize :: new( 400.0 , 300.0 ) ) ;
32+ provider. create_document_window( vdom, attributes) ;
8133 counter += 1 ;
82- known_windows. set( open_with_props. list_windows( ) ) ;
8334 } ,
84- "Open window with props"
85- }
86- button {
87- onclick: move |_| known_windows. set( refresh_handle. list_windows( ) ) ,
88- "Refresh list"
35+ "Open secondary window"
8936 }
9037 }
91- ul {
92- { windows_snapshot. into_iter( ) . map( |info| {
93- let focus_handle = base_focus_handle. clone( ) ;
94- let rename_handle = base_rename_handle. clone( ) ;
95- let mut rename_counter_signal = rename_counter. clone( ) ;
96- let mut update_list_signal = known_windows_signal. clone( ) ;
97- let update_source = refresh_handle_for_list. clone( ) ;
98- let id = info. id;
99- let title = info. title;
100- rsx! {
101- li {
102- "{title} (ID: {id:?})"
103- button {
104- onclick: move |_| focus_handle. focus_window( id) ,
105- "Focus"
106- }
107- button {
108- onclick: move |_| {
109- let idx = rename_counter_signal( ) ;
110- rename_handle. set_window_title( id, format!( "Renamed {idx}" ) ) ;
111- rename_counter_signal += 1 ;
112- update_list_signal. set( update_source. list_windows( ) ) ;
113- } ,
114- "Rename"
115- }
116- }
117- }
118- } ) }
119- }
12038 }
12139 }
12240}
12341
12442fn secondary_window ( ) -> Element {
43+ let shell_provider = use_context :: < Arc < dyn ShellProvider > > ( ) ;
44+
12545 rsx ! {
12646 main {
127- style { { SECONDARY_STYLES } }
12847 h1 { "Secondary window" }
12948 p { "This content comes from another RSX function." }
49+ button {
50+ onclick: move |_| {
51+ shell_provider. set_window_title( format!( "Time: {:?}" , std:: time:: SystemTime :: now( ) ) )
52+ } ,
53+ "click to update title" ,
54+ }
13055 }
13156 }
13257}
133-
134- #[ derive( Props , Clone , PartialEq ) ]
135- struct MessageWindowProps {
136- message : String ,
137- }
138-
139- fn message_window ( props : MessageWindowProps ) -> Element {
140- rsx ! {
141- main {
142- style { { SECONDARY_STYLES } }
143- h1 { "Message window" }
144- p { { props. message} }
145- }
146- }
147- }
148-
149- const PRIMARY_STYLES : & str = r#"
150- font-family: system-ui, sans-serif;
151- min-height: 100vh;
152- padding: 40px;
153- margin: 0;
154- display: flex;
155- flex-direction: column;
156- gap: 12px;
157- background: radial-gradient(circle, #f8fafc, #dbeafe);
158- "# ;
159-
160- const SECONDARY_STYLES : & str = r#"
161- font-family: system-ui, sans-serif;
162- min-height: 100vh;
163- padding: 56px;
164- margin: 0;
165- background: #0f172a;
166- color: white;
167- "# ;
0 commit comments