From 65c20dde28cb73b1845c07adecb40944b12e5176 Mon Sep 17 00:00:00 2001 From: Dax Huiberts Date: Thu, 17 Oct 2024 21:37:23 +0200 Subject: [PATCH] book: limit user fetching using ashpd only to linux in main_event_loop_7 --- book/listings/main_event_loop/7/main.rs | 45 +++++++++++++++---------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/book/listings/main_event_loop/7/main.rs b/book/listings/main_event_loop/7/main.rs index 3a6769432e2b..42f69debf34b 100644 --- a/book/listings/main_event_loop/7/main.rs +++ b/book/listings/main_event_loop/7/main.rs @@ -1,5 +1,3 @@ -use ashpd::desktop::account::UserInformation; -use ashpd::WindowIdentifier; use glib::clone; use gtk::prelude::*; use gtk::{glib, Application, ApplicationWindow, Button}; @@ -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 } )); }); @@ -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\""); +} \ No newline at end of file