-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I based my implementation on the official specs as well as code from GNOME, KDE, Xapp (Xfce), and Pantheon (elementary). KDE's imminently readable codebase served as this implementation's primary inspiration. Autostart is deprecated but seemingly still used, so this implementation will still support it for compatibility. The Background portal depends on a working Access portal as `xdg-desktop-portal` calls it to show the initial warning. References: * https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.impl.portal.Background.html * https://gitlab.gnome.org/GNOME/xdg-desktop-portal-gnome/-/blob/main/src/background.c * https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/blob/master/src/background.cpp * https://github.com/linuxmint/xdg-desktop-portal-xapp/blob/f1c24244f90571209c56b7f45802b70e80da4922/src/background.c * https://github.com/elementary/portals/blob/d868cfa854c731e0f37615e225d5db07cc3f4604/src/Background/Portal.vala * flatpak/xdg-desktop-portal#1188
- Loading branch information
1 parent
05c37cd
commit 1885362
Showing
11 changed files
with
651 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Copy, Default, PartialEq, Deserialize, Serialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct Background { | ||
/// Default preference for NotifyBackground's dialog | ||
pub default_perm: PermissionDialog, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)] | ||
pub enum PermissionDialog { | ||
/// Grant apps permission to run in the background | ||
Allow, | ||
/// Deny apps permission to run in the background | ||
Deny, | ||
/// Always ask if new apps should be granted background permissions | ||
#[default] | ||
Ask, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[portal] | ||
DBusName=org.freedesktop.impl.portal.desktop.cosmic | ||
Interfaces=org.freedesktop.impl.portal.Access;org.freedesktop.impl.portal.FileChooser;org.freedesktop.impl.portal.Screenshot;org.freedesktop.impl.portal.Settings;org.freedesktop.impl.portal.ScreenCast | ||
Interfaces=org.freedesktop.impl.portal.Access;org.freedesktop.impl.portal.Background;org.freedesktop.impl.portal.FileChooser;org.freedesktop.impl.portal.Screenshot;org.freedesktop.impl.portal.Settings;org.freedesktop.impl.portal.ScreenCast | ||
UseIn=cosmic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
use ashpd::desktop::background::Background; | ||
use cosmic::{ | ||
app::{self, message, Core}, | ||
executor, | ||
iced::{Length, Size}, | ||
widget, Command, | ||
}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum Message { | ||
BackgroundResponse(bool), | ||
RequestBackground, | ||
} | ||
|
||
pub struct App { | ||
core: Core, | ||
executable: String, | ||
background_allowed: bool, | ||
} | ||
|
||
impl App { | ||
async fn request_background(executable: String) -> ashpd::Result<Background> { | ||
log::info!("Requesting permission to run in the background for: {executable}"); | ||
// Based off of the ashpd docs | ||
// https://docs.rs/ashpd/latest/ashpd/desktop/background/index.html | ||
Background::request() | ||
.reason("Testing the background portal") | ||
.auto_start(false) | ||
.dbus_activatable(false) | ||
.command(&[executable]) | ||
.send() | ||
.await? | ||
.response() | ||
} | ||
} | ||
|
||
impl cosmic::Application for App { | ||
type Executor = executor::single::Executor; | ||
type Flags = (); | ||
type Message = Message; | ||
const APP_ID: &'static str = "org.cosmic.BackgroundPortalExample"; | ||
|
||
fn core(&self) -> &Core { | ||
&self.core | ||
} | ||
|
||
fn core_mut(&mut self) -> &mut Core { | ||
&mut self.core | ||
} | ||
|
||
fn init(core: Core, _: Self::Flags) -> (Self, app::Command<Self::Message>) { | ||
( | ||
Self { | ||
core, | ||
executable: std::env::args().next().unwrap(), | ||
background_allowed: false, | ||
}, | ||
Command::none(), | ||
) | ||
} | ||
|
||
fn view(&self) -> cosmic::Element<Self::Message> { | ||
widget::row::with_children(vec![ | ||
widget::text::title3(if self.background_allowed { | ||
"Running in background" | ||
} else { | ||
"Not running in background" | ||
}) | ||
.width(Length::Fill) | ||
.into(), | ||
widget::button("Run in background") | ||
.on_press(Message::RequestBackground) | ||
.padding(8.0) | ||
.into(), | ||
]) | ||
.width(Length::Fill) | ||
.height(Length::Fixed(64.0)) | ||
.padding(16.0) | ||
.into() | ||
} | ||
|
||
fn update(&mut self, message: Self::Message) -> app::Command<Self::Message> { | ||
match message { | ||
Message::BackgroundResponse(background_allowed) => { | ||
log::info!("Permission to run in the background: {background_allowed}"); | ||
self.background_allowed = background_allowed; | ||
Command::none() | ||
} | ||
Message::RequestBackground => { | ||
let executable = self.executable.clone(); | ||
Command::perform(Self::request_background(executable), |result| { | ||
let background_allowed = match result { | ||
Ok(response) => { | ||
assert!( | ||
!response.auto_start(), | ||
"Auto start shouldn't have been enabled" | ||
); | ||
response.run_in_background() | ||
} | ||
Err(e) => { | ||
log::error!("Background portal request failed: {e:?}"); | ||
false | ||
} | ||
}; | ||
|
||
message::app(Message::BackgroundResponse(background_allowed)) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// TODO: Write a small flatpak manifest in order to test this better | ||
#[tokio::main] | ||
async fn main() -> cosmic::iced::Result { | ||
env_logger::Builder::from_default_env().init(); | ||
let settings = app::Settings::default() | ||
.resizable(None) | ||
.size(Size::new(512.0, 128.0)) | ||
.exit_on_close(false); | ||
app::run::<App>(settings, ()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.