11//! Demonstrate opening additional windows from a pure Dioxus app.
22
33use dioxus:: prelude:: * ;
4- use dioxus_native:: { DioxusWindowHandle , DioxusWindowInfo , DioxusWindowOptions } ;
4+ use dioxus_native:: { DioxusWindowHandle , DioxusWindowInfo } ;
5+ use std:: time:: Duration ;
6+ use winit:: window:: { WindowAttributes , WindowButtons } ;
57
68fn main ( ) {
7- dioxus_native:: launch ( app) ;
9+ // Demonstrate how to pass custom WindowAttributes (title, size, decorations).
10+ let attrs = WindowAttributes :: default ( )
11+ . with_title ( "Multi-window Demo" )
12+ . with_inner_size ( winit:: dpi:: LogicalSize :: new ( 600.0 , 800.0 ) )
13+ . with_enabled_buttons ( WindowButtons :: all ( ) ) ;
14+ dioxus_native:: launch_cfg ( app, vec ! [ ] , vec ! [ Box :: new( attrs) ] ) ;
815}
916
1017fn app ( ) -> Element {
@@ -20,39 +27,22 @@ fn app() -> Element {
2027 let known_windows_signal = known_windows. clone ( ) ;
2128 let refresh_handle_for_list = refresh_handle. clone ( ) ;
2229 let rename_counter = use_signal ( || 1u32 ) ;
23- let window_rows = {
24- let windows_snapshot = known_windows ( ) ;
25- windows_snapshot
26- . into_iter ( )
27- . map ( |info| {
28- let focus_handle = base_focus_handle. clone ( ) ;
29- let rename_handle = base_rename_handle. clone ( ) ;
30- let mut rename_counter_signal = rename_counter. clone ( ) ;
31- let mut update_list_signal = known_windows_signal. clone ( ) ;
32- let update_source = refresh_handle_for_list. clone ( ) ;
33- let id = info. id ;
34- let title = info. title ;
35- rsx ! {
36- li {
37- "{title} (ID: {id:?})"
38- button {
39- onclick: move |_| focus_handle. focus_window( id) ,
40- "Focus"
41- }
42- button {
43- onclick: move |_| {
44- let idx = rename_counter_signal( ) ;
45- rename_handle. set_window_title( id, format!( "Renamed {idx}" ) ) ;
46- rename_counter_signal += 1 ;
47- update_list_signal. set( update_source. list_windows( ) ) ;
48- } ,
49- "Rename"
50- }
51- }
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 ( ) ) ;
5240 }
53- } )
54- . collect :: < Vec < _ > > ( )
55- } ;
41+ }
42+ } ) ;
43+ }
44+
45+ let windows_snapshot = known_windows ( ) ;
5646
5747 rsx ! {
5848 main {
@@ -62,12 +52,13 @@ fn app() -> Element {
6252 div {
6353 button {
6454 onclick: move |_| {
65- open_simple. open_window_with_options (
55+ open_simple. open_window_with_attributes (
6656 secondary_window,
67- DioxusWindowOptions {
68- title: Some ( "Secondary window" . into( ) ) ,
69- ..Default :: default ( )
70- } ,
57+ Some (
58+ WindowAttributes :: default ( )
59+ . with_title( "Secondary window" )
60+ . with_inner_size( winit:: dpi:: LogicalSize :: new( 400.0 , 300.0 ) ) ,
61+ ) ,
7162 ) ;
7263 known_windows. set( open_simple. list_windows( ) ) ;
7364 } ,
@@ -76,15 +67,16 @@ fn app() -> Element {
7667 button {
7768 onclick: move |_| {
7869 let idx = counter( ) ;
79- open_with_props. open_window_with_props_and_options (
70+ open_with_props. open_window_with_props_and_attributes (
8071 message_window,
8172 MessageWindowProps {
8273 message: format!( "Window #{idx}" ) ,
8374 } ,
84- DioxusWindowOptions {
85- title: Some ( format!( "Window #{idx}" ) ) ,
86- ..Default :: default ( )
87- } ,
75+ Some (
76+ WindowAttributes :: default ( )
77+ . with_title( format!( "Window #{idx}" ) )
78+ . with_inner_size( winit:: dpi:: LogicalSize :: new( 320.0 , 240.0 ) ) ,
79+ ) ,
8880 ) ;
8981 counter += 1 ;
9082 known_windows. set( open_with_props. list_windows( ) ) ;
@@ -96,7 +88,35 @@ fn app() -> Element {
9688 "Refresh list"
9789 }
9890 }
99- ul { { window_rows. into_iter( ) } }
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+ }
100120 }
101121 }
102122}
0 commit comments