status-area: Use struct instead of enum for IconUpdate
This commit is contained in:
parent
e2dcfeac7c
commit
e7e275abf7
2 changed files with 27 additions and 42 deletions
|
|
@ -61,29 +61,23 @@ impl State {
|
|||
iced::Task::none()
|
||||
}
|
||||
Msg::Icon(update) => {
|
||||
match update {
|
||||
IconUpdate::Name(name) => {
|
||||
self.icon_name = name;
|
||||
}
|
||||
IconUpdate::Pixmap(icons) => {
|
||||
self.icon_pixmap = icons
|
||||
.into_iter()
|
||||
.max_by_key(|i| (i.width, i.height))
|
||||
.map(|mut i| {
|
||||
if i.width <= 0 || i.height <= 0 || i.bytes.is_empty() {
|
||||
// App sent invalid icon data during initialization - show placeholder until NewIcon signal
|
||||
eprintln!("Skipping invalid icon: {}x{} with {} bytes, app may still be initializing",
|
||||
i.width, i.height, i.bytes.len());
|
||||
return icon::from_name("dialog-question").symbolic(true).handle();
|
||||
}
|
||||
// Convert ARGB to RGBA
|
||||
for pixel in i.bytes.chunks_exact_mut(4) {
|
||||
pixel.rotate_left(1);
|
||||
}
|
||||
icon::from_raster_pixels(i.width as u32, i.height as u32, i.bytes)
|
||||
});
|
||||
}
|
||||
}
|
||||
self.icon_name = update.name.unwrap_or_default();
|
||||
self.icon_pixmap = update.pixmap.and_then(|icons| icons
|
||||
.into_iter()
|
||||
.max_by_key(|i| (i.width, i.height))
|
||||
.map(|mut i| {
|
||||
if i.width <= 0 || i.height <= 0 || i.bytes.is_empty() {
|
||||
// App sent invalid icon data during initialization - show placeholder until NewIcon signal
|
||||
eprintln!("Skipping invalid icon: {}x{} with {} bytes, app may still be initializing",
|
||||
i.width, i.height, i.bytes.len());
|
||||
return icon::from_name("dialog-question").symbolic(true).handle();
|
||||
}
|
||||
// Convert ARGB to RGBA
|
||||
for pixel in i.bytes.chunks_exact_mut(4) {
|
||||
pixel.rotate_left(1);
|
||||
}
|
||||
icon::from_raster_pixels(i.width as u32, i.height as u32, i.bytes)
|
||||
}));
|
||||
|
||||
iced::Task::none()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,9 +21,9 @@ pub struct Icon {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum IconUpdate {
|
||||
Name(String),
|
||||
Pixmap(Vec<Icon>),
|
||||
pub struct IconUpdate {
|
||||
pub name: Option<String>,
|
||||
pub pixmap: Option<Vec<Icon>>,
|
||||
}
|
||||
|
||||
impl StatusNotifierItem {
|
||||
|
|
@ -93,22 +93,13 @@ impl StatusNotifierItem {
|
|||
}
|
||||
|
||||
pub fn icon_subscription(&self) -> iced::Subscription<IconUpdate> {
|
||||
fn icon_events(
|
||||
item_proxy: StatusNotifierItemProxy<'static>,
|
||||
) -> impl futures::Stream<Item = IconUpdate> + 'static {
|
||||
async move {
|
||||
let icon_name = item_proxy.icon_name().await;
|
||||
let icon_pixmap = item_proxy.icon_pixmap().await;
|
||||
futures::stream::iter(
|
||||
[
|
||||
icon_name.map(IconUpdate::Name),
|
||||
icon_pixmap.map(IconUpdate::Pixmap),
|
||||
]
|
||||
.into_iter()
|
||||
.filter_map(Result::ok),
|
||||
)
|
||||
async fn icon_events(item_proxy: StatusNotifierItemProxy<'static>) -> IconUpdate {
|
||||
let icon_name = item_proxy.icon_name().await;
|
||||
let icon_pixmap = item_proxy.icon_pixmap().await;
|
||||
IconUpdate {
|
||||
name: icon_name.ok(),
|
||||
pixmap: icon_pixmap.ok(),
|
||||
}
|
||||
.flatten_stream()
|
||||
}
|
||||
|
||||
let item_proxy = self.item_proxy.clone();
|
||||
|
|
@ -118,7 +109,7 @@ impl StatusNotifierItem {
|
|||
let new_icon_stream = item_proxy.receive_new_icon().await.unwrap();
|
||||
futures::stream::once(async {})
|
||||
.chain(new_icon_stream.map(|_| ()))
|
||||
.flat_map(move |()| icon_events(item_proxy.clone()))
|
||||
.then(move |()| icon_events(item_proxy.clone()))
|
||||
}
|
||||
.flatten_stream(),
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue