Skip to content

Commit

Permalink
book: Include ashpd only on linux so that listings run on macos (#1867)
Browse files Browse the repository at this point in the history
* book: Include ashpd only on linux so that listings run on macos

* book: Limit user fetching using ashpd only to linux in main_event_loop_7
  • Loading branch information
daxhuiberts authored Oct 17, 2024
1 parent 3f68339 commit b7f4575
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
4 changes: 3 additions & 1 deletion book/listings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
gtk = { version = "0.9", package = "gtk4", features = ["v4_12"] }
reqwest = { version = "0.12", default-features = false, features = [
Expand All @@ -19,6 +18,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"

Expand Down
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 b7f4575

Please sign in to comment.