App icons now work!
This commit is contained in:
parent
f009b88978
commit
4074358337
5 changed files with 209 additions and 14 deletions
|
|
@ -10,7 +10,10 @@ futures-util = "0.3.21"
|
|||
libcosmic-widgets = { git = "https://github.com/pop-os/libcosmic", branch = "lucy/widgets" }
|
||||
libpulse-binding = "2.26.0"
|
||||
pulsectl-rs = "0.3.2"
|
||||
relm4 = { git = "https://github.com/AaronErhardt/relm4", branch = "new-approach", features = ["macros"] }
|
||||
relm4 = { git = "https://github.com/AaronErhardt/relm4", branch = "new-approach", features = [
|
||||
"macros",
|
||||
] }
|
||||
tracker = "0.1.1"
|
||||
freedesktop-desktop-entry = "0.5.0"
|
||||
|
||||
[features]
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use crate::icons::{parse_desktop_icons, DesktopApplication};
|
||||
use futures_util::StreamExt;
|
||||
use libcosmic_widgets::LabeledItem;
|
||||
use libpulse_binding::{
|
||||
|
|
@ -22,7 +23,7 @@ use relm4::{
|
|||
},
|
||||
view, ComponentParts, RelmContainerExt, Sender, SimpleComponent,
|
||||
};
|
||||
use std::rc::Rc;
|
||||
use std::{collections::HashMap, rc::Rc};
|
||||
use tracker::track;
|
||||
|
||||
pub enum AppInput {
|
||||
|
|
@ -46,6 +47,8 @@ pub struct App {
|
|||
#[no_eq]
|
||||
now_playing: Vec<ApplicationInfo>,
|
||||
#[do_not_track]
|
||||
desktop_icons: HashMap<DesktopApplication, String>,
|
||||
#[do_not_track]
|
||||
handler: Handler,
|
||||
}
|
||||
|
||||
|
|
@ -60,6 +63,7 @@ impl Default for App {
|
|||
let default_output = output_controller.get_default_device().ok();
|
||||
let outputs = output_controller.list_devices().unwrap_or_default();
|
||||
let now_playing = output_controller.list_applications().unwrap_or_default();
|
||||
let desktop_icons = parse_desktop_icons();
|
||||
let handler = Handler::connect("com.system76.cosmic.applets.audio")
|
||||
.expect("failed to connect to pulse");
|
||||
relm4::spawn_local(clone!(@weak handler.mainloop as main_loop => async move {
|
||||
|
|
@ -75,6 +79,7 @@ impl Default for App {
|
|||
default_output,
|
||||
outputs,
|
||||
now_playing,
|
||||
desktop_icons,
|
||||
handler,
|
||||
tracker: 0,
|
||||
}
|
||||
|
|
@ -234,7 +239,20 @@ impl App {
|
|||
let icon_name = app
|
||||
.proplist
|
||||
.get_str("application.icon_name")
|
||||
.or_else(|| {
|
||||
app.proplist
|
||||
.get_str("application.name")
|
||||
.and_then(|name| self.desktop_icons.get(&DesktopApplication::Name(name)))
|
||||
.cloned()
|
||||
})
|
||||
.or_else(|| {
|
||||
app.proplist
|
||||
.get_str("application.process.binary")
|
||||
.and_then(|name| self.desktop_icons.get(&DesktopApplication::Binary(name)))
|
||||
.cloned()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let name = app.name.clone().unwrap_or_default();
|
||||
view! {
|
||||
item = GtkBox {
|
||||
|
|
|
|||
37
applets/cosmic-applet-audio/src/icons.rs
Normal file
37
applets/cosmic-applet-audio/src/icons.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use freedesktop_desktop_entry::{default_paths, DesktopEntry, Iter};
|
||||
|
||||
#[derive(Debug, Hash, PartialEq, Eq)]
|
||||
pub enum DesktopApplication {
|
||||
Name(String),
|
||||
Binary(String),
|
||||
}
|
||||
|
||||
pub fn parse_desktop_icons() -> HashMap<DesktopApplication, String> {
|
||||
let mut out = HashMap::new();
|
||||
for path in Iter::new(default_paths()) {
|
||||
let file = match std::fs::read_to_string(&path) {
|
||||
Ok(data) => data,
|
||||
_ => continue,
|
||||
};
|
||||
let entry = match DesktopEntry::decode(&path, &file) {
|
||||
Ok(entry) => entry,
|
||||
_ => continue,
|
||||
};
|
||||
let icon = match entry.icon() {
|
||||
Some(icon) => icon,
|
||||
None => continue,
|
||||
};
|
||||
if let Some(name) = entry.name(None) {
|
||||
out.insert(DesktopApplication::Name(name.into_owned()), icon.to_owned());
|
||||
};
|
||||
if let Some(exec) = entry
|
||||
.exec()
|
||||
.and_then(|entry| entry.split_whitespace().next())
|
||||
{
|
||||
out.insert(DesktopApplication::Binary(exec.to_owned()), icon.to_owned());
|
||||
};
|
||||
}
|
||||
out
|
||||
}
|
||||
|
|
@ -4,19 +4,10 @@
|
|||
extern crate relm4;
|
||||
|
||||
mod app;
|
||||
mod icons;
|
||||
|
||||
use relm4::RelmApp;
|
||||
|
||||
fn main() {
|
||||
figure_out_apps();
|
||||
RelmApp::<app::App>::new("com.system76.cosmic.applets.audio").run(());
|
||||
}
|
||||
|
||||
fn figure_out_apps() {
|
||||
use pulsectl::controllers::{AppControl, SinkController};
|
||||
|
||||
let mut sink = SinkController::create().unwrap();
|
||||
for app in sink.list_applications().unwrap() {
|
||||
println!("{:#?}", app);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue