Skip to content

Commit

Permalink
Update wayland stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
A6GibKm committed Jun 9, 2022
1 parent 57b9b0c commit 0e837ed
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 81 deletions.
28 changes: 11 additions & 17 deletions ashpd-demo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ashpd-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ gtk = {package = "gtk4", version = "0.4"}
adw = {version = "0.1", package = "libadwaita"}
serde = {version = "1.0", features = ["derive"]}
chrono = {version = "0.4", default-features = false, features = ["clock"]}
gdk4wayland = {package = "gdk4-wayland"}

[dependencies.shumate]
package = "libshumate"
version = "0.1.0-alpha.4"

[dependencies.ashpd]
git = "https://github.com/A6GibKm/ashpd"
branch = "wayland2"
path = "../"
features = ["gtk4", "wayland", "pipewire", "tracing"]
4 changes: 1 addition & 3 deletions ashpd-demo/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ impl Application {
}));
})
);
let is_sandboxed = futures::executor::block_on(async {
ashpd::is_sandboxed().await
});
let is_sandboxed = futures::executor::block_on(async { ashpd::is_sandboxed().await });
// The restart app requires the Flatpak portal
gtk_macros::get_action!(self, @restart).set_enabled(is_sandboxed);

Expand Down
68 changes: 48 additions & 20 deletions ashpd-demo/src/portals/desktop/open_uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,54 @@ impl OpenUriPage {
}

async fn open_uri(&self) {
let imp = self.imp();
let writable = imp.writeable_switch.is_active();
let ask = imp.ask_switch.is_active();
let root = self.native().unwrap();
let identifier = WindowIdentifier::from_native(&root).await;
let uri = imp.uri_entry.text();
let activation_token = if imp.activation_token_switch.is_active() {
Some(ashpd::ActivationToken::from_native(&root).unwrap())
} else {
None
};
match open_uri::open_uri(&identifier, &uri, writable, ask, activation_token).await {
Ok(_) => {
self.send_notification(
"Open URI request was successful",
NotificationKind::Success,
);
}
Err(_err) => {
self.send_notification("Request to open URI failed", NotificationKind::Error);
unsafe {
let imp = self.imp();
let writable = imp.writeable_switch.is_active();
let ask = imp.ask_switch.is_active();
let root = self.native().unwrap();
use gtk::glib::translate::ToGlibPtr;

let surface = root
.surface()
.downcast::<gdk4wayland::WaylandSurface>()
.unwrap();
let display = root
.display()
.downcast::<gdk4wayland::WaylandDisplay>()
.unwrap();

let surface_ptr =
gdk4wayland::ffi::gdk_wayland_surface_get_wl_surface(surface.to_glib_none().0);
let display_ptr =
gdk4wayland::ffi::gdk_wayland_display_get_wl_display(display.to_glib_none().0);

let identifier =
WindowIdentifier::from_wayland_raw(surface_ptr as *mut _, display_ptr as *mut _)
.await;
let app_id = String::from(crate::config::APP_ID);
let token =
ashpd::ActivationToken::from_wayland_raw(app_id, surface_ptr as *mut _, display_ptr as *mut _)
.await.unwrap();

tracing::debug!("Handle {identifier}");
tracing::debug!("Activation Token {}", token.as_str());

let uri = imp.uri_entry.text();
let activation_token = if imp.activation_token_switch.is_active() {
Some(token)
} else {
None
};
match open_uri::open_uri(&identifier, &uri, writable, ask, activation_token).await {
Ok(_) => {
self.send_notification(
"Open URI request was successful",
NotificationKind::Success,
);
}
Err(_err) => {
self.send_notification("Request to open URI failed", NotificationKind::Error);
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions ashpd-demo/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ mod imp {
if config::PROFILE == "Devel" {
obj.add_css_class("devel");
}
let is_sandboxed = futures::executor::block_on(async {
ashpd::is_sandboxed().await
});
let is_sandboxed = futures::executor::block_on(async { ashpd::is_sandboxed().await });
// Add pages based on whether the app is sandboxed
if is_sandboxed {
self.sidebar
Expand Down
8 changes: 3 additions & 5 deletions src/activation_token/gtk4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ pub struct Gtk4ActivationToken {
impl Gtk4ActivationToken {
pub fn from_native<N: glib::IsA<gdk::Display>>(native: &N) -> Option<Self> {
match native.backend() {
gdk::Backend::Wayland => native
.startup_notification_id()
.map(|token| Self {
token: token.to_string(),
}),
gdk::Backend::Wayland => native.startup_notification_id().map(|token| Self {
token: token.to_string(),
}),
gdk::Backend::X11 => todo!(),
_ => None,
}
Expand Down
25 changes: 21 additions & 4 deletions src/activation_token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,29 @@ impl Serialize for ActivationToken {

impl ActivationToken {
#[cfg(feature = "wayland")]
pub fn from_surface(
pub async fn from_wayland_surface(
app_id: String,
surface: &wayland_client::protocol::wl_surface::WlSurface,
) -> Result<Self, Box<dyn std::error::Error>> {
let token = WaylandActivationToken::from_surface(surface)?;
) -> Option<Self> {
let token = WaylandActivationToken::from_surface(app_id, surface).await?;

Ok(Self::Wayland(token))
Some(Self::Wayland(token))
}

#[cfg(feature = "wayland")]
/// Create an instance of [`ActivationToken`] from a Wayland surface.
///
/// ## Safety
///
/// The surface and display have to be valid Wayland pointers.
pub async unsafe fn from_wayland_raw(
app_id: String,
surface_ptr: *mut std::ffi::c_void,
display_ptr: *mut std::ffi::c_void,
) -> Option<Self> {
let token = WaylandActivationToken::from_raw(app_id, surface_ptr, display_ptr).await?;

Some(Self::Wayland(token))
}

#[cfg(feature = "gtk4")]
Expand Down
Loading

0 comments on commit 0e837ed

Please sign in to comment.