diff --git a/book/listings/Cargo.toml b/book/listings/Cargo.toml index 56a5d9ebf527..fed51455ab97 100644 --- a/book/listings/Cargo.toml +++ b/book/listings/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] adw = { version = "0.7", package = "libadwaita", features = ["v1_5"] } anyhow = "1.0" -ashpd = { version = "0.9", features = ["gtk4"] } async-channel = "2.0" dirs = "5.0" gtk = { version = "0.9", package = "gtk4", features = ["v4_12"] } @@ -20,6 +19,9 @@ tokio = { version = "1.33.0", features = ["rt-multi-thread"] } walkdir = "2.3" xshell = "0.2" +[target.'cfg(target_os = "linux")'.dependencies] +ashpd = { version = "0.9", features = ["gtk4"] } + [build-dependencies] glib-build-tools = "0.20" diff --git a/book/listings/main_event_loop/7/main.rs b/book/listings/main_event_loop/7/main.rs index 3a6769432e2b..b0a0bf24613f 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}; @@ -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 @@ -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\""); +}