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 65c20dd
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 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 @@ -35,21 +33,7 @@ fn build_ui(app: &Application) {
#[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.")
}
fetch_user_information(button).await
}
));
});
Expand All @@ -65,3 +49,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 65c20dd

Please sign in to comment.