Skip to content

Commit 8a4ca82

Browse files
Avoid cloning Config and mutable state
1 parent e99d456 commit 8a4ca82

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/background.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::collections::{hash_map::Entry, HashMap};
44

5-
use cosmic::{iced::window, iced_runtime::command::Action, widget};
5+
use cosmic::{iced::window, widget};
66
use futures::{FutureExt, TryFutureExt};
77
use tokio::sync::mpsc::Sender;
88
use zbus::{fdo, object_server::SignalContext, zvariant};
@@ -21,17 +21,12 @@ const POP_SHELL_PATH: &str = "/com.System76.PopShell";
2121
/// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.impl.portal.Background.html
2222
pub struct Background {
2323
tx: Sender<subscription::Event>,
24-
config: config::background::Background,
2524
}
2625

2726
impl Background {
28-
pub fn new(tx: Sender<subscription::Event>) -> Self {
29-
// FIXME: Will need to change this to handle external updates (e.g. from cosmic-settings)
30-
let config = config::Config::load().0.background;
31-
Self { tx, config }
27+
pub const fn new(tx: Sender<subscription::Event>) -> Self {
28+
Self { tx }
3229
}
33-
34-
// fn dialog(name: &str) -> widget::Dialog<'_, PermissionResponse> {}
3530
}
3631

3732
#[zbus::interface(name = "org.freedesktop.impl.portal.Background")]
@@ -48,7 +43,7 @@ impl Background {
4843

4944
/// Notifies the user that an app is running in the background
5045
async fn notify_background(
51-
&mut self,
46+
&self,
5247
// #[zbus(connection)] connection: &zbus::Connection,
5348
#[zbus(signal_context)] context: SignalContext<'_>,
5449
handle: zvariant::ObjectPath<'_>,
@@ -57,6 +52,18 @@ impl Background {
5752
) -> PortalResponse<NotifyBackgroundResult> {
5853
log::debug!("[background] Request handle: {handle:?}");
5954

55+
// Request only what's needed to avoid cloning and receiving the entire config
56+
// This is also cleaner than storing the config because it's difficult to keep it
57+
// updated without synch primitives
58+
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
59+
let config = self
60+
.tx
61+
.send(subscription::Event::BackgroundReq(tx, app_id.clone()))
62+
.inspect_err(|e| log::error!("[background] Failed receiving background config from main app {e:?}"))
63+
.and_then(|_| rx.recv())
64+
.unwrap_or_default()
65+
.await;
66+
6067
match self.config.apps.entry(app_id) {
6168
// Skip dialog based on default response set in configs
6269
_ if self.config.default_perm == PermissionDialog::Allow => {

0 commit comments

Comments
 (0)