Skip to content

Commit 65c20dd

Browse files
committed
book: limit user fetching using ashpd only to linux in main_event_loop_7
1 parent 5a28147 commit 65c20dd

File tree

1 file changed

+28
-17
lines changed
  • book/listings/main_event_loop/7

1 file changed

+28
-17
lines changed

book/listings/main_event_loop/7/main.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use ashpd::desktop::account::UserInformation;
2-
use ashpd::WindowIdentifier;
31
use glib::clone;
42
use gtk::prelude::*;
53
use gtk::{glib, Application, ApplicationWindow, Button};
@@ -35,21 +33,7 @@ fn build_ui(app: &Application) {
3533
#[weak]
3634
button,
3735
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-
}
36+
fetch_user_information(button).await
5337
}
5438
));
5539
});
@@ -65,3 +49,30 @@ fn build_ui(app: &Application) {
6549
// Present window
6650
window.present();
6751
}
52+
53+
#[cfg(target_os = "linux")]
54+
async fn fetch_user_information(button: Button) {
55+
use ashpd::desktop::account::UserInformation;
56+
use ashpd::WindowIdentifier;
57+
58+
// Get native of button for window identifier
59+
let native = button.native().expect("Need to be able to get native.");
60+
// Get window identifier so that the dialog will be modal to the main window
61+
let identifier = WindowIdentifier::from_native(&native).await;
62+
let request = UserInformation::request()
63+
.reason("App would like to access user information.")
64+
.identifier(identifier)
65+
.send()
66+
.await;
67+
68+
if let Ok(response) = request.and_then(|r| r.response()) {
69+
println!("User name: {}", response.name());
70+
} else {
71+
println!("Could not access user information.")
72+
}
73+
}
74+
75+
#[cfg(not(target_os = "linux"))]
76+
async fn fetch_user_information(_button: Button) {
77+
println!("fetching user information not available outside target_os = \"linux\"");
78+
}

0 commit comments

Comments
 (0)