adding systray

This commit is contained in:
Chris Cochrun 2025-10-14 13:41:55 -05:00
parent aaa21aff21
commit b693e71e5f
4 changed files with 813 additions and 677 deletions

View file

@ -9,11 +9,12 @@ use freedesktop_icons::lookup;
use hyprland::data::{Client, Workspace, Workspaces};
use hyprland::event_listener::{self, AsyncEventListener};
use hyprland::shared::{HyprData, HyprDataActiveOptional, HyprDataVec};
use iced::alignment::Vertical;
use iced::font::Weight;
use iced::futures::SinkExt;
use iced::stream;
use iced::widget::image::Handle;
use iced::widget::{button, container, horizontal_space, image, row, svg, text};
use iced::widget::{button, container, horizontal_space, image, row, svg, text, Button};
use iced::{stream, ContentFit};
use iced::{
time, Background, Border, Color, Element, Event, Font, Length, Shadow, Subscription, Task,
Vector,
@ -87,7 +88,8 @@ struct Panel {
active_window: String,
active_window_icon_svg: Option<svg::Handle>,
active_window_icon_raster: Option<Handle>,
system_tray: Option<SystemTray>,
system_tray_svg: Option<Vec<svg::Handle>>,
system_tray_raster: Option<Vec<Handle>>,
apps: Vec<String>,
}
@ -142,7 +144,8 @@ impl Panel {
active_window_icon_svg,
active_window_icon_raster,
battery,
system_tray: None,
system_tray_svg: None,
system_tray_raster: None,
};
let sys_task = Task::perform(add_sys_tray(), |m| m);
@ -194,7 +197,7 @@ impl Panel {
Message::SizeChange(_) => todo!(),
Message::VirtualKeyboardPressed { time, key } => todo!(),
Message::IcedEvent(event) => {
debug!(?event)
// debug!(?event)
}
Message::WorkspaceChange(workspaces) => {
// debug!(?workspaces);
@ -231,7 +234,32 @@ impl Panel {
}
Message::AddSystemTray(system_tray) => {
debug!(?system_tray);
self.system_tray = Some(system_tray);
let mut raster_vec = vec![];
let mut svg_vec = vec![];
for item in system_tray.items.into_values() {
if let Some(icon) = item.0.icon_name {
let icon_path = PathBuf::from(icon.clone());
if icon_path.exists() {
debug!(?icon_path);
raster_vec.push(image::Handle::from_path(icon_path))
} else if let Some(icon) = lookup(&icon)
.with_theme("Papirus-Dark")
.force_svg()
.with_size(16)
.find()
{
svg_vec.push(svg::Handle::from_path(icon));
} else if let Some(icons) = item.0.icon_pixmap {
for icon in icons {
raster_vec.push(image::Handle::from_bytes(icon.pixels));
}
} else {
raster_vec.push( image::Handle::from("/home/chris/pics/wojaks/reddit_the_xenomorph_s bigass wojak folder/Chads/ChristianChad.png"))
}
}
}
self.system_tray_svg = Some(svg_vec);
self.system_tray_raster = Some(raster_vec);
}
Message::None => {}
_ => unreachable!(),
@ -338,27 +366,30 @@ impl Panel {
.iter()
.filter(|w| w.name != "special:special")
.map(|w| {
text!("{}", {
match w.id {
1 => "".to_owned(),
2 => "".to_owned(),
3 => "󰈙".to_owned(),
4 => "󰭹".to_owned(),
5 => "".to_owned(),
6 => "".to_owned(),
7 => "󰕧".to_owned(),
8 => "󰭹".to_owned(),
9 => "".to_owned(),
_ => w.name.clone(),
}
})
.color({
if w.id == self.active_workspace {
Color::parse("#ff6ac1").unwrap()
} else {
Color::parse("#57c7ff").unwrap()
}
})
container(
text!("{}", {
match w.id {
1 => "".to_owned(),
2 => "".to_owned(),
3 => "󰈙".to_owned(),
4 => "󰭹".to_owned(),
5 => "".to_owned(),
6 => "".to_owned(),
7 => "󰕧".to_owned(),
8 => "󰭹".to_owned(),
9 => "".to_owned(),
_ => w.name.clone(),
}
})
.color({
if w.id == self.active_workspace {
Color::parse("#ff6ac1").unwrap()
} else {
Color::parse("#57c7ff").unwrap()
}
}),
)
.center(Length::Shrink)
.into()
})
.collect();
@ -375,7 +406,11 @@ impl Panel {
// }
// }
if let Some(handle) = &self.active_window_icon_svg {
icon = svg(handle.clone()).width(16).height(16).into();
icon = svg(handle.clone())
.width(16)
.height(16)
.content_fit(ContentFit::Fill)
.into();
} else {
if let Some(handle) = &self.active_window_icon_raster {
icon = image(handle.clone()).width(16).height(16).into();
@ -383,7 +418,7 @@ impl Panel {
icon = image("/home/chris/pics/wojaks/reddit_the_xenomorph_s bigass wojak folder/Chads/ChristianChad.png").width(16).height(16).into();
}
}
let icon = container(icon).center(Length::Shrink).padding(4);
let icon = container(icon).center(Length::Shrink).padding([2, 4]);
let window = text!("{}", self.active_window).color(Color::parse("#57c7ff").unwrap());
let disk = text!("{}", self.disk).color(Color::parse("#ff9f43").unwrap());
@ -415,13 +450,47 @@ impl Panel {
let spacer = text!(" | ");
// let sys_tray = vec![button()]
let mut sys_tray_buttons: Vec<Element<Message>> = vec![];
if let Some(sys_tray) = &self.system_tray_svg {
for item in sys_tray.iter() {
sys_tray_buttons.push(
svg(item.clone())
.width(16)
.height(16)
.content_fit(ContentFit::Fill)
.into(),
);
}
}
if let Some(sys_tray) = &self.system_tray_raster {
for item in sys_tray.iter() {
sys_tray_buttons.push(
image(item)
.width(16)
.height(16)
.content_fit(ContentFit::Fill)
.into(),
);
}
}
let row = row!(
container(row!(workspaces, icon, window, horizontal_space()).spacing(10)),
container(clock),
container(row!(horizontal_space(), disk, cpu, mem, battery).spacing(5)),
spacer,
container(
row!(
horizontal_space(),
disk,
cpu,
mem,
battery,
spacer,
row(sys_tray_buttons).spacing(5)
)
.spacing(5)
),
)
.spacing(5)
.padding([0, 20]);
@ -534,12 +603,16 @@ struct SystemTray {
impl SystemTray {
async fn new() -> Result<Self> {
let client = system_tray::client::Client::new().await.into_diagnostic()?;
let _ = tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
let receiver = client.subscribe();
let rec_length = receiver.len();
let items = client.items();
let items = items
.lock()
.map_err(|_| SystemTrayError::SomeError)
.into_diagnostic()?
.clone();
debug!(?items);
Ok(Self { client, items })
}
}