@@ -32,48 +32,6 @@ macro_rules! run_dialog {
3232 } } ;
3333}
3434
35- /// Window parent definition.
36- #[ cfg( any( windows, target_os = "macos" ) ) ]
37- #[ cfg_attr( doc_cfg, doc( cfg( any( windows, target_os = "macos" ) ) ) ) ]
38- pub struct WindowParent {
39- #[ cfg( windows) ]
40- hwnd : * mut std:: ffi:: c_void ,
41- #[ cfg( target_os = "macos" ) ]
42- ns_window : * mut std:: ffi:: c_void ,
43- }
44-
45- #[ cfg( any( windows, target_os = "macos" ) ) ]
46- unsafe impl raw_window_handle:: HasRawWindowHandle for WindowParent {
47- #[ cfg( windows) ]
48- fn raw_window_handle ( & self ) -> raw_window_handle:: RawWindowHandle {
49- let mut handle = raw_window_handle:: windows:: WindowsHandle :: empty ( ) ;
50- handle. hwnd = self . hwnd ;
51- raw_window_handle:: RawWindowHandle :: Windows ( handle)
52- }
53-
54- #[ cfg( target_os = "macos" ) ]
55- fn raw_window_handle ( & self ) -> raw_window_handle:: RawWindowHandle {
56- let mut handle = raw_window_handle:: macos:: MacOSHandle :: empty ( ) ;
57- handle. ns_window = self . ns_window ;
58- raw_window_handle:: RawWindowHandle :: MacOS ( handle)
59- }
60- }
61-
62- #[ cfg( any( windows, target_os = "macos" ) ) ]
63- #[ cfg_attr( doc_cfg, doc( cfg( any( windows, target_os = "macos" ) ) ) ) ]
64- #[ doc( hidden) ]
65- pub fn window_parent < R : Runtime > ( window : & Window < R > ) -> crate :: Result < WindowParent > {
66- #[ cfg( windows) ]
67- let w = WindowParent {
68- hwnd : window. hwnd ( ) ?,
69- } ;
70- #[ cfg( target_os = "macos" ) ]
71- let w = WindowParent {
72- ns_window : window. ns_window ( ) ?,
73- } ;
74- Ok ( w)
75- }
76-
7735/// The file dialog builder.
7836///
7937/// Constructs file picker dialogs that can select single/multiple files or directories.
@@ -141,25 +99,24 @@ pub fn ask<R: Runtime, F: FnOnce(bool) + Send + 'static>(
14199 message : impl AsRef < str > ,
142100 f : F ,
143101) {
144- let title = title. as_ref ( ) . to_string ( ) ;
145- let message = message. as_ref ( ) . to_string ( ) ;
146- #[ allow( unused_mut) ]
147- let mut builder = rfd:: MessageDialog :: new ( )
148- . set_title ( & title)
149- . set_description ( & message)
150- . set_buttons ( rfd:: MessageButtons :: YesNo )
151- . set_level ( rfd:: MessageLevel :: Info ) ;
152-
153- #[ cfg( any( windows, target_os = "macos" ) ) ]
154- {
155- if let Some ( window) = parent_window {
156- if let Ok ( parent) = window_parent ( window) {
157- builder = builder. set_parent ( & parent) ;
158- }
159- }
160- }
102+ run_message_dialog ( parent_window, title, message, rfd:: MessageButtons :: YesNo , f)
103+ }
161104
162- run_dialog ! ( builder. show( ) , f)
105+ /// Displays a dialog with a message and an optional title with an "ok" and a "cancel" button.
106+ #[ allow( unused_variables) ]
107+ pub fn confirm < R : Runtime , F : FnOnce ( bool ) + Send + ' static > (
108+ parent_window : Option < & Window < R > > ,
109+ title : impl AsRef < str > ,
110+ message : impl AsRef < str > ,
111+ f : F ,
112+ ) {
113+ run_message_dialog (
114+ parent_window,
115+ title,
116+ message,
117+ rfd:: MessageButtons :: OkCancel ,
118+ f,
119+ )
163120}
164121
165122/// Displays a message dialog.
@@ -168,26 +125,39 @@ pub fn message<R: Runtime>(
168125 parent_window : Option < & Window < R > > ,
169126 title : impl AsRef < str > ,
170127 message : impl AsRef < str > ,
128+ ) {
129+ run_message_dialog (
130+ parent_window,
131+ title,
132+ message,
133+ rfd:: MessageButtons :: Ok ,
134+ |_| { } ,
135+ )
136+ }
137+
138+ #[ allow( unused_variables) ]
139+ fn run_message_dialog < R : Runtime , F : FnOnce ( bool ) + Send + ' static > (
140+ parent_window : Option < & Window < R > > ,
141+ title : impl AsRef < str > ,
142+ message : impl AsRef < str > ,
143+ buttons : rfd:: MessageButtons ,
144+ f : F ,
171145) {
172146 let title = title. as_ref ( ) . to_string ( ) ;
173147 let message = message. as_ref ( ) . to_string ( ) ;
174- let cb = |_| { } ;
175-
176148 #[ allow( unused_mut) ]
177149 let mut builder = rfd:: MessageDialog :: new ( )
178150 . set_title ( & title)
179151 . set_description ( & message)
180- . set_buttons ( rfd :: MessageButtons :: Ok )
152+ . set_buttons ( buttons )
181153 . set_level ( rfd:: MessageLevel :: Info ) ;
182154
183155 #[ cfg( any( windows, target_os = "macos" ) ) ]
184156 {
185157 if let Some ( window) = parent_window {
186- if let Ok ( parent) = window_parent ( window) {
187- builder = builder. set_parent ( & parent) ;
188- }
158+ builder = builder. set_parent ( window) ;
189159 }
190160 }
191161
192- run_dialog ! ( builder. show( ) , cb )
162+ run_dialog ! ( builder. show( ) , f )
193163}
0 commit comments