Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

book: Include ashpd only on linux so that listings run on macos #1867

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
dirs = "5.0"
gtk = { version = "0.9", package = "gtk4", features = ["v4_12"] }
Expand All @@ -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"

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\"");
}
Loading