fix: handle slight delay in availability of clipboard data

a better fix in the future would probably be an event indicating availability of the data.
This commit is contained in:
Ashley Wulber 2026-03-26 13:21:17 -04:00 committed by Jacob Kauffmann
parent 175f8ba724
commit cf328771c3
2 changed files with 36 additions and 10 deletions

View file

@ -422,6 +422,7 @@ pub enum Message {
CheckClipboardImage,
CheckClipboardVideo,
CheckClipboardText,
RetryCheckClipboard(ClipboardCache),
ClipboardCached(ClipboardCache),
PendingCancel(u64),
PendingCancelAll,
@ -4036,9 +4037,12 @@ impl Application for App {
// Check if clipboard has any paste-able content and cache it
return clipboard::read_data::<ClipboardPaste>().map(|contents_opt| {
match contents_opt {
Some(contents) if !contents.paths.is_empty() => cosmic::action::app(
Message::ClipboardCached(ClipboardCache::Files(contents)),
Some(contents) if contents.paths.is_empty() => cosmic::action::app(
Message::RetryCheckClipboard(ClipboardCache::Files(contents)),
),
Some(contents) => cosmic::action::app(Message::ClipboardCached(
ClipboardCache::Files(contents),
)),
_ => cosmic::action::app(Message::CheckClipboardImage),
}
});
@ -4071,6 +4075,28 @@ impl Application for App {
}))
});
}
Message::RetryCheckClipboard(cache) => {
let mut cmds = Vec::new();
cmds.push(self.update(Message::ClipboardCached(cache)));
cmds.push(
iced::Task::future(tokio::time::sleep(Duration::from_millis(300)))
.discard()
.chain(
clipboard::read_data::<ClipboardPaste>().map(|contents_opt| {
match contents_opt {
Some(contents) if !contents.paths.is_empty() => {
cosmic::action::app(Message::ClipboardCached(
ClipboardCache::Files(contents),
))
}
_ => cosmic::action::app(Message::CheckClipboardImage),
}
}),
),
);
return Task::batch(cmds);
}
Message::ClipboardCached(cache) => {
self.clipboard_cache = cache;
}
@ -4478,6 +4504,7 @@ impl Application for App {
widget::Id::unique(),
)),
);
commands.push(self.update(Message::CheckClipboard));
commands.push(self.update(Message::Surface(
cosmic::surface::action::app_popup(
move |app: &mut Self| -> SctkPopupSettings {
@ -5344,7 +5371,11 @@ impl Application for App {
};
}
// Check clipboard when window gains focus
return self.update(Message::CheckClipboard);
// HACK: Wait a moment for the data to be available.
return cosmic::task::future(async {
_ = tokio::time::sleep(Duration::from_millis(300)).await;
cosmic::action::app(Message::CheckClipboard)
});
}
Message::Surface(action) => {
return cosmic::task::message(cosmic::Action::Cosmic(