Skip to content

Commit b7f4575

Browse files
authored
book: Include ashpd only on linux so that listings run on macos (#1867)
* 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
1 parent 3f68339 commit b7f4575

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

book/listings/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ edition = "2021"
77
[dependencies]
88
adw = { version = "0.7", package = "libadwaita", features = ["v1_5"] }
99
anyhow = "1.0"
10-
ashpd = { version = "0.9", features = ["gtk4"] }
1110
async-channel = "2.0"
1211
gtk = { version = "0.9", package = "gtk4", features = ["v4_12"] }
1312
reqwest = { version = "0.12", default-features = false, features = [
@@ -19,6 +18,9 @@ tokio = { version = "1.33.0", features = ["rt-multi-thread"] }
1918
walkdir = "2.3"
2019
xshell = "0.2"
2120

21+
[target.'cfg(target_os = "linux")'.dependencies]
22+
ashpd = { version = "0.9", features = ["gtk4"] }
23+
2224
[build-dependencies]
2325
glib-build-tools = "0.20"
2426

book/listings/main_event_loop/7/main.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use ashpd::desktop::account::UserInformation;
2-
use ashpd::WindowIdentifier;
31
use glib::clone;
42
use gtk::prelude::*;
53
use gtk::{glib, Application, ApplicationWindow, Button};
@@ -34,23 +32,7 @@ fn build_ui(app: &Application) {
3432
glib::spawn_future_local(clone!(
3533
#[weak]
3634
button,
37-
async move {
38-
// Get native of button for window identifier
39-
let native = button.native().expect("Need to be able to get native.");
40-
// Get window identifier so that the dialog will be modal to the main window
41-
let identifier = WindowIdentifier::from_native(&native).await;
42-
let request = UserInformation::request()
43-
.reason("App would like to access user information.")
44-
.identifier(identifier)
45-
.send()
46-
.await;
47-
48-
if let Ok(response) = request.and_then(|r| r.response()) {
49-
println!("User name: {}", response.name());
50-
} else {
51-
println!("Could not access user information.")
52-
}
53-
}
35+
async move { fetch_user_information(button).await }
5436
));
5537
});
5638
// ANCHOR_END: callback
@@ -65,3 +47,30 @@ fn build_ui(app: &Application) {
6547
// Present window
6648
window.present();
6749
}
50+
51+
#[cfg(target_os = "linux")]
52+
async fn fetch_user_information(button: Button) {
53+
use ashpd::desktop::account::UserInformation;
54+
use ashpd::WindowIdentifier;
55+
56+
// Get native of button for window identifier
57+
let native = button.native().expect("Need to be able to get native.");
58+
// Get window identifier so that the dialog will be modal to the main window
59+
let identifier = WindowIdentifier::from_native(&native).await;
60+
let request = UserInformation::request()
61+
.reason("App would like to access user information.")
62+
.identifier(identifier)
63+
.send()
64+
.await;
65+
66+
if let Ok(response) = request.and_then(|r| r.response()) {
67+
println!("User name: {}", response.name());
68+
} else {
69+
println!("Could not access user information.")
70+
}
71+
}
72+
73+
#[cfg(not(target_os = "linux"))]
74+
async fn fetch_user_information(_button: Button) {
75+
println!("fetching user information not available outside target_os = \"linux\"");
76+
}

0 commit comments

Comments
 (0)