Skip to content

Commit 855ba59

Browse files
committed
refactor(display): use Task::stream instead of on_enter sender
The on_enter sender was introduced before iced supported creating tasks from streams
1 parent c8b9ee9 commit 855ba59

File tree

1 file changed

+48
-24
lines changed
  • cosmic-settings/src/pages/display

1 file changed

+48
-24
lines changed

cosmic-settings/src/pages/display/mod.rs

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub struct Page {
139139
mirror_map: SecondaryMap<OutputKey, OutputKey>,
140140
mirror_menu: widget::dropdown::multi::Model<String, Mirroring>,
141141
active_display: OutputKey,
142-
background_service_cancel: Option<oneshot::Sender<()>>,
142+
randr_handle: Option<(oneshot::Sender<()>, cosmic::iced::task::Handle)>,
143143
hotplug_handle: Option<(oneshot::Sender<()>, cosmic::iced::task::Handle)>,
144144
config: Config,
145145
cache: ViewCache,
@@ -176,7 +176,7 @@ impl Default for Page {
176176
mirror_map: SecondaryMap::new(),
177177
mirror_menu: widget::dropdown::multi::model(),
178178
active_display: OutputKey::default(),
179-
background_service_cancel: None,
179+
randr_handle: None,
180180
hotplug_handle: None,
181181
config: Config::default(),
182182
cache: ViewCache::default(),
@@ -254,7 +254,7 @@ impl page::Page<crate::pages::Message> for Page {
254254
#[cfg(not(feature = "test"))]
255255
fn on_enter(
256256
&mut self,
257-
sender: tokio::sync::mpsc::Sender<crate::pages::Message>,
257+
_sender: tokio::sync::mpsc::Sender<crate::pages::Message>,
258258
) -> Task<crate::pages::Message> {
259259
use std::time::Duration;
260260

@@ -265,8 +265,17 @@ impl page::Page<crate::pages::Message> for Page {
265265
fl!("orientation", "rotate-270"),
266266
];
267267

268-
if let Some(canceller) = self.background_service_cancel.take() {
268+
let mut tasks = Vec::with_capacity(3);
269+
tasks.push(cosmic::task::future(on_enter()));
270+
271+
if let Some((canceller, handle)) = self.randr_handle.take() {
269272
_ = canceller.send(());
273+
handle.abort();
274+
}
275+
276+
if let Some((canceller, handle)) = self.hotplug_handle.take() {
277+
_ = canceller.send(());
278+
handle.abort();
270279
}
271280

272281
self.refreshing_page.store(true, Ordering::SeqCst);
@@ -275,7 +284,7 @@ impl page::Page<crate::pages::Message> for Page {
275284
{
276285
let refreshing_page = self.refreshing_page.clone();
277286
let (tx, mut rx) = tachyonix::channel(4);
278-
let (canceller, cancelled) = oneshot::channel();
287+
let (canceller, cancelled) = oneshot::channel::<()>();
279288
let runtime = tokio::runtime::Handle::current();
280289

281290
// Spawns a background service to monitor for display state changes.
@@ -298,28 +307,41 @@ impl page::Page<crate::pages::Message> for Page {
298307
});
299308

300309
// Forward messages from another thread to prevent the monitoring thread from blocking.
301-
tokio::task::spawn(async move {
302-
while let Ok(message) = rx.recv().await {
303-
if sender.is_closed() {
304-
return;
305-
}
306-
307-
if let cosmic_randr::Message::ManagerDone = message {
308-
if !refreshing_page.swap(true, Ordering::SeqCst) {
309-
let sender = sender.clone();
310-
tokio::spawn(async move {
311-
_ = sender.send(on_enter().await).await;
312-
});
310+
let (randr_task, randr_handle) =
311+
Task::stream(async_fn_stream::fn_stream(|emitter| async move {
312+
while let Ok(message) = rx.recv().await {
313+
if let cosmic_randr::Message::ManagerDone = message {
314+
if !refreshing_page.swap(true, Ordering::SeqCst) {
315+
_ = emitter.emit(on_enter().await).await;
316+
}
313317
}
314318
}
315-
}
316-
});
317-
318-
self.background_service_cancel = Some(canceller);
319+
}))
320+
.abortable();
321+
322+
// tokio::task::spawn(async move {
323+
// while let Ok(message) = rx.recv().await {
324+
// if sender.is_closed() {
325+
// return;
326+
// }
327+
328+
// if let cosmic_randr::Message::ManagerDone = message {
329+
// if !refreshing_page.swap(true, Ordering::SeqCst) {
330+
// let sender = sender.clone();
331+
// tokio::spawn(async move {
332+
// _ = sender.send(on_enter().await).await;
333+
// });
334+
// }
335+
// }
336+
// }
337+
// });
338+
339+
tasks.push(randr_task);
340+
self.randr_handle = Some((canceller, randr_handle));
319341
}
320342

321343
// Channels for communicating messages from the DRM hotplug thread.
322-
let (hotplug_cancel_tx, hotplug_cancel_rx) = tokio::sync::oneshot::channel::<()>();
344+
let (hotplug_cancel_tx, hotplug_cancel_rx) = oneshot::channel::<()>();
323345
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
324346

325347
// Spawn a background thread for asynchronously polling a udev monitor for DRM
@@ -383,9 +405,10 @@ impl page::Page<crate::pages::Message> for Page {
383405
}))
384406
.abortable();
385407

408+
tasks.push(hotplug_task);
386409
self.hotplug_handle = Some((hotplug_cancel_tx, hotplug_handle));
387410

388-
cosmic::task::batch(vec![cosmic::task::future(on_enter()), hotplug_task])
411+
cosmic::task::batch(tasks)
389412
}
390413

391414
fn on_leave(&mut self) -> Task<crate::pages::Message> {
@@ -394,8 +417,9 @@ impl page::Page<crate::pages::Message> for Page {
394417
handle.abort();
395418
}
396419

397-
if let Some(canceller) = self.background_service_cancel.take() {
420+
if let Some((canceller, handle)) = self.randr_handle.take() {
398421
_ = canceller.send(());
422+
handle.abort();
399423
}
400424

401425
Task::none()

0 commit comments

Comments
 (0)