Skip to content

Commit

Permalink
feat: Add macOS-specific resource path resolution for app bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
silviot committed Dec 8, 2024
1 parent 73ca85b commit 2159ccc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,19 @@ pub static VERSION: &str = "0.1.0";
pub static GETTEXT_PACKAGE: &str = "aardvark";
pub static LOCALEDIR: &str = "/app/share/locale";
pub static PKGDATADIR: &str = "/app/share/aardvark";

#[cfg(target_os = "macos")]
pub fn get_resource_base() -> String {
if let Ok(exe_path) = std::env::current_exe() {
// On macOS, executable is in MacOS folder, resources should be in Resources
if let Some(app_bundle) = exe_path.parent().and_then(|p| p.parent()) {
return app_bundle.join("Resources").to_string_lossy().into_owned();
}
}
PKGDATADIR.to_owned() // fallback to the default
}

#[cfg(not(target_os = "macos"))]
pub fn get_resource_base() -> String {
PKGDATADIR.to_owned()
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use self::window::AardvarkWindow;

use config::{GETTEXT_PACKAGE, LOCALEDIR, PKGDATADIR};
use gettextrs::{bind_textdomain_codeset, bindtextdomain, textdomain};
use gtk::{gio, glib};
use gtk::prelude::*;
use gtk::{gio, glib};

fn main() -> glib::ExitCode {
// Set up gettext translations
Expand All @@ -39,8 +39,8 @@ fn main() -> glib::ExitCode {
textdomain(GETTEXT_PACKAGE).expect("Unable to switch to the text domain");

// Load resources
let resources = gio::Resource::load(PKGDATADIR.to_owned() + "/aardvark.gresource")
.expect("Could not load resources");
let resource_path = config::get_resource_base() + "/aardvark.gresource";
let resources = gio::Resource::load(&resource_path).expect("Could not load resources");
gio::resources_register(&resources);

// Create a new GtkApplication. The application manages our main loop,
Expand Down

0 comments on commit 2159ccc

Please sign in to comment.