Skip to content

Commit

Permalink
book: Limit user fetching using ashpd only to linux in main_event_loop_7
Browse files Browse the repository at this point in the history
  • Loading branch information
daxhuiberts committed Oct 17, 2024
1 parent 5a28147 commit d5a0833
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions book/listings/main_event_loop/7/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use ashpd::desktop::account::UserInformation;
use ashpd::WindowIdentifier;
use glib::clone;
use gtk::prelude::*;
use gtk::{glib, Application, ApplicationWindow, Button};
Expand Down Expand Up @@ -34,23 +32,7 @@ fn build_ui(app: &Application) {
glib::spawn_future_local(clone!(
#[weak]
button,
async move {
// Get native of button for window identifier
let native = button.native().expect("Need to be able to get native.");
// Get window identifier so that the dialog will be modal to the main window
let identifier = WindowIdentifier::from_native(&native).await;
let request = UserInformation::request()
.reason("App would like to access user information.")
.identifier(identifier)
.send()
.await;

if let Ok(response) = request.and_then(|r| r.response()) {
println!("User name: {}", response.name());
} else {
println!("Could not access user information.")
}
}
async move { fetch_user_information(button).await }
));
});
// ANCHOR_END: callback
Expand All @@ -65,3 +47,30 @@ fn build_ui(app: &Application) {
// Present window
window.present();
}

#[cfg(target_os = "linux")]
async fn fetch_user_information(button: Button) {
use ashpd::desktop::account::UserInformation;
use ashpd::WindowIdentifier;

// Get native of button for window identifier
let native = button.native().expect("Need to be able to get native.");
// Get window identifier so that the dialog will be modal to the main window
let identifier = WindowIdentifier::from_native(&native).await;
let request = UserInformation::request()
.reason("App would like to access user information.")
.identifier(identifier)
.send()
.await;

if let Ok(response) = request.and_then(|r| r.response()) {
println!("User name: {}", response.name());
} else {
println!("Could not access user information.")
}
}

#[cfg(not(target_os = "linux"))]
async fn fetch_user_information(_button: Button) {
println!("fetching user information not available outside target_os = \"linux\"");
}

0 comments on commit d5a0833

Please sign in to comment.