Skip to content

Commit e3190ce

Browse files
committed
Use relative paths instead of absolute paths for icons
1 parent 8337b59 commit e3190ce

6 files changed

Lines changed: 38 additions & 36 deletions

File tree

docs/icon.icns

188 KB
Binary file not shown.

docs/lemon.png

45.1 KB
Loading

src/app/apps.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! This modules handles the logic for each "app" that rustcast can load
22
//!
33
//! An "app" is effectively, one of the results that rustcast returns when you search for something
4-
use std::path::Path;
54
65
use iced::{
76
Alignment,
@@ -14,9 +13,11 @@ use crate::{
1413
clipboard::ClipBoardContentType,
1514
commands::Function,
1615
styles::{result_button_style, result_row_container_style},
17-
utils::handle_from_icns,
16+
utils::icns_data_to_handle,
1817
};
1918

19+
pub const ICNS_ICON: &[u8] = include_bytes!("../../docs/icon.icns");
20+
2021
/// This tells each "App" what to do when it is clicked, whether it is a function, a message, or a display
2122
#[allow(dead_code)]
2223
#[derive(Debug, Clone)]
@@ -69,58 +70,48 @@ impl App {
6970
pub fn basic_apps() -> Vec<App> {
7071
let app_version = option_env!("APP_VERSION").unwrap_or("Unknown Version");
7172

73+
let icons = icns_data_to_handle(ICNS_ICON.to_vec());
74+
7275
vec![
7376
App {
7477
open_command: AppCommand::Function(Function::Quit),
7578
desc: RUSTCAST_DESC_NAME.to_string(),
76-
icons: handle_from_icns(Path::new(
77-
"/Applications/Rustcast.app/Contents/Resources/icon.icns",
78-
)),
79+
icons: icons.clone(),
7980
name: "Quit RustCast".to_string(),
8081
name_lc: "quit".to_string(),
8182
},
8283
App {
8384
open_command: AppCommand::Function(Function::OpenPrefPane),
8485
desc: RUSTCAST_DESC_NAME.to_string(),
85-
icons: handle_from_icns(Path::new(
86-
"/Applications/Rustcast.app/Contents/Resources/icon.icns",
87-
)),
86+
icons: icons.clone(),
8887
name: "Open RustCast Preferences".to_string(),
8988
name_lc: "settings".to_string(),
9089
},
9190
App {
9291
open_command: AppCommand::Message(Message::SwitchToPage(Page::EmojiSearch)),
9392
desc: RUSTCAST_DESC_NAME.to_string(),
94-
icons: handle_from_icns(Path::new(
95-
"/Applications/Rustcast.app/Contents/Resources/icon.icns",
96-
)),
93+
icons: icons.clone(),
9794
name: "Search for an Emoji".to_string(),
9895
name_lc: "emoji".to_string(),
9996
},
10097
App {
10198
open_command: AppCommand::Message(Message::SwitchToPage(Page::ClipboardHistory)),
10299
desc: RUSTCAST_DESC_NAME.to_string(),
103-
icons: handle_from_icns(Path::new(
104-
"/Applications/Rustcast.app/Contents/Resources/icon.icns",
105-
)),
100+
icons: icons.clone(),
106101
name: "Clipboard History".to_string(),
107102
name_lc: "clipboard".to_string(),
108103
},
109104
App {
110105
open_command: AppCommand::Message(Message::ReloadConfig),
111106
desc: RUSTCAST_DESC_NAME.to_string(),
112-
icons: handle_from_icns(Path::new(
113-
"/Applications/Rustcast.app/Contents/Resources/icon.icns",
114-
)),
107+
icons: icons.clone(),
115108
name: "Reload RustCast".to_string(),
116109
name_lc: "refresh".to_string(),
117110
},
118111
App {
119112
open_command: AppCommand::Display,
120113
desc: RUSTCAST_DESC_NAME.to_string(),
121-
icons: handle_from_icns(Path::new(
122-
"/Applications/Rustcast.app/Contents/Resources/icon.icns",
123-
)),
114+
icons: icons.clone(),
124115
name: format!("Current RustCast Version: {app_version}"),
125116
name_lc: "version".to_string(),
126117
},

src/app/menubar.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! This has the menubar icon logic for the app
22
3+
use std::io::Cursor;
4+
35
use global_hotkey::hotkey::{Code, HotKey, Modifiers};
46
use image::{DynamicImage, ImageReader};
57
use tray_icon::{
@@ -54,13 +56,11 @@ pub fn menu_icon(hotkey: HotKey, sender: ExtSender) -> TrayIcon {
5456
}
5557

5658
fn get_image() -> DynamicImage {
57-
let image_path = if cfg!(debug_assertions) {
58-
"docs/icon.png"
59-
} else {
60-
"/Applications/Rustcast.app/Contents/Resources/icon.png"
61-
};
62-
63-
ImageReader::open(image_path).unwrap().decode().unwrap()
59+
ImageReader::new(Cursor::new(include_bytes!("../../docs/icon.png")))
60+
.with_guessed_format()
61+
.unwrap()
62+
.decode()
63+
.unwrap()
6464
}
6565

6666
fn init_event_handler(sender: ExtSender, hotkey_id: u32) {

src/app/tile/update.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This handles the update logic for the tile (AKA rustcast's main window)
22
use std::cmp::min;
33
use std::fs;
4-
use std::path::Path;
4+
use std::io::Cursor;
55
use std::thread;
66

77
use iced::Task;
@@ -436,9 +436,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
436436
tile.results.push(App {
437437
open_command: AppCommand::Display,
438438
desc: "Easter Egg".to_string(),
439-
icons: Some(Handle::from_path(Path::new(
440-
"/Applications/Rustcast.app/Contents/Resources/lemon.png",
441-
))),
439+
icons: lemon_icon_handle(),
442440
name: "Lemon".to_string(),
443441
name_lc: "".to_string(),
444442
});
@@ -490,3 +488,12 @@ fn open_window(height: f32) -> Task<Message> {
490488
fn single_item_resize_task(id: Id) -> Task<Message> {
491489
Task::done(Message::ResizeWindow(id, 55. + DEFAULT_WINDOW_HEIGHT))
492490
}
491+
492+
fn lemon_icon_handle() -> Option<Handle> {
493+
image::ImageReader::new(Cursor::new(include_bytes!("../../../docs/lemon.png")))
494+
.with_guessed_format()
495+
.unwrap()
496+
.decode()
497+
.ok()
498+
.map(|img| Handle::from_rgba(img.width(), img.height(), img.into_bytes()))
499+
}

src/utils.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ pub(crate) fn log_error_and_exit(msg: &str) -> ! {
2525
exit(-1)
2626
}
2727

28-
/// This converts an icns file to an iced image handle
29-
pub(crate) fn handle_from_icns(path: &Path) -> Option<Handle> {
30-
let data = std::fs::read(path).ok()?;
28+
pub fn icns_data_to_handle(data: Vec<u8>) -> Option<Handle> {
3129
let family = IconFamily::read(std::io::Cursor::new(&data)).ok()?;
3230

3331
let icon_type = family.available_icons();
3432

3533
let icon = family.get_icon_with_type(*icon_type.first()?).ok()?;
3634
let image = RgbaImage::from_raw(
37-
icon.width() as u32,
38-
icon.height() as u32,
35+
icon.width(),
36+
icon.height(),
3937
icon.data().to_vec(),
4038
)?;
4139
Some(Handle::from_rgba(
@@ -45,6 +43,12 @@ pub(crate) fn handle_from_icns(path: &Path) -> Option<Handle> {
4543
))
4644
}
4745

46+
/// This converts an icns file to an iced image handle
47+
pub(crate) fn handle_from_icns(path: &Path) -> Option<Handle> {
48+
let data = std::fs::read(path).ok()?;
49+
icns_data_to_handle(data)
50+
}
51+
4852
/// Open the settings file with the system default editor
4953
pub fn open_settings() {
5054
thread::spawn(move || {

0 commit comments

Comments
 (0)