| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | use gpui_kit::{AssetSource, SharedString, assets::AllAssets}; |
| 4 | |
| 5 | pub const PATH: &str = "yumush/logo.svg"; |
| 6 | pub const NEEDLE: &str = "yumush/needle.svg"; |
| 7 | const WINDOW_ICON: u32 = 256; |
| 8 | |
| 9 | mod svg; |
| 10 | |
| 11 | pub use svg::{NAME, icon, mark, needle, rgba}; |
| 12 | |
| 13 | pub fn window_icon() -> std::sync::Arc<image::RgbaImage> { |
| 14 | std::sync::Arc::new( |
| 15 | image::RgbaImage::from_raw(WINDOW_ICON, WINDOW_ICON, rgba(WINDOW_ICON)) |
| 16 | .expect("icon pixels"), |
| 17 | ) |
| 18 | } |
| 19 | |
| 20 | #[cfg(target_os = "linux")] |
| 21 | pub fn install() -> std::io::Result<()> { |
| 22 | let data = dirs::data_dir().ok_or(std::io::ErrorKind::NotFound)?; |
| 23 | let exe = crate::exe().ok_or(std::io::ErrorKind::NotFound)?; |
| 24 | put( |
| 25 | &data |
| 26 | .join("icons/hicolor/scalable/apps") |
| 27 | .join(format!("{NAME}.svg")), |
| 28 | &icon(), |
| 29 | )?; |
| 30 | put( |
| 31 | &data.join("applications").join(format!("{NAME}.desktop")), |
| 32 | &format!( |
| 33 | "[Desktop Entry]\nType=Application\nName={}\nExec=\"{}\"\nIcon={NAME}\nStartupWMClass={NAME}\nCategories=Network;Chat;\nTerminal=false\n", |
| 34 | crate::NAME, |
| 35 | exe.display() |
| 36 | ), |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 41 | pub(crate) fn put(path: &std::path::Path, content: &str) -> std::io::Result<()> { |
| 42 | if std::fs::read_to_string(path).is_ok_and(|old| old == content) { |
| 43 | return Ok(()); |
| 44 | } |
| 45 | if let Some(parent) = path.parent() { |
| 46 | std::fs::create_dir_all(parent)?; |
| 47 | } |
| 48 | std::fs::write(path, content) |
| 49 | } |
| 50 | |
| 51 | pub struct Assets; |
| 52 | |
| 53 | impl AssetSource for Assets { |
| 54 | fn load(&self, path: &str) -> gpui_kit::Result<Option<Cow<'static, [u8]>>> { |
| 55 | if path == PATH { |
| 56 | return Ok(Some(Cow::Owned(mark().into_bytes()))); |
| 57 | } |
| 58 | if path == NEEDLE { |
| 59 | return Ok(Some(Cow::Owned(needle().into_bytes()))); |
| 60 | } |
| 61 | AllAssets.load(path) |
| 62 | } |
| 63 | |
| 64 | fn list(&self, path: &str) -> gpui_kit::Result<Vec<SharedString>> { |
| 65 | AllAssets.list(path) |
| 66 | } |
| 67 | } |