1- use ashpd:: desktop:: account:: UserInformation ;
2- use ashpd:: WindowIdentifier ;
31use glib:: clone;
42use gtk:: prelude:: * ;
53use gtk:: { glib, Application , ApplicationWindow , Button } ;
@@ -34,23 +32,7 @@ fn build_ui(app: &Application) {
3432 glib:: spawn_future_local ( clone ! (
3533 #[ weak]
3634 button,
37- async move {
38- // Get native of button for window identifier
39- let native = button. native( ) . expect( "Need to be able to get native." ) ;
40- // Get window identifier so that the dialog will be modal to the main window
41- let identifier = WindowIdentifier :: from_native( & native) . await ;
42- let request = UserInformation :: request( )
43- . reason( "App would like to access user information." )
44- . identifier( identifier)
45- . send( )
46- . await ;
47-
48- if let Ok ( response) = request. and_then( |r| r. response( ) ) {
49- println!( "User name: {}" , response. name( ) ) ;
50- } else {
51- println!( "Could not access user information." )
52- }
53- }
35+ async move { fetch_user_information( button) . await }
5436 ) ) ;
5537 } ) ;
5638 // ANCHOR_END: callback
@@ -65,3 +47,30 @@ fn build_ui(app: &Application) {
6547 // Present window
6648 window. present ( ) ;
6749}
50+
51+ #[ cfg( target_os = "linux" ) ]
52+ async fn fetch_user_information ( button : Button ) {
53+ use ashpd:: desktop:: account:: UserInformation ;
54+ use ashpd:: WindowIdentifier ;
55+
56+ // Get native of button for window identifier
57+ let native = button. native ( ) . expect ( "Need to be able to get native." ) ;
58+ // Get window identifier so that the dialog will be modal to the main window
59+ let identifier = WindowIdentifier :: from_native ( & native) . await ;
60+ let request = UserInformation :: request ( )
61+ . reason ( "App would like to access user information." )
62+ . identifier ( identifier)
63+ . send ( )
64+ . await ;
65+
66+ if let Ok ( response) = request. and_then ( |r| r. response ( ) ) {
67+ println ! ( "User name: {}" , response. name( ) ) ;
68+ } else {
69+ println ! ( "Could not access user information." )
70+ }
71+ }
72+
73+ #[ cfg( not( target_os = "linux" ) ) ]
74+ async fn fetch_user_information ( _button : Button ) {
75+ println ! ( "fetching user information not available outside target_os = \" linux\" " ) ;
76+ }
0 commit comments