From 3b0c071ce209a407893cfd80ed00705ef87b9d37 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Thu, 13 May 2021 09:25:40 +0300 Subject: [PATCH] [macOS] Prefer .app bundle icon over the default one. --- core/os/os.cpp | 5 +++++ core/os/os.h | 1 + main/main.cpp | 8 +++++--- platform/osx/os_osx.h | 1 + platform/osx/os_osx.mm | 24 ++++++++++++++++++------ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/core/os/os.cpp b/core/os/os.cpp index fba014703d2..57487438acd 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -329,6 +329,11 @@ String OS::get_bundle_resource_dir() const { return "."; }; +// Path to macOS .app bundle embedded icon +String OS::get_bundle_icon_path() const { + return String(); +} + // OS specific path for user:// String OS::get_user_data_dir() const { return "."; diff --git a/core/os/os.h b/core/os/os.h index f5994cd0c16..fa429ee6e72 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -434,6 +434,7 @@ public: virtual String get_config_path() const; virtual String get_cache_path() const; virtual String get_bundle_resource_dir() const; + virtual String get_bundle_icon_path() const; virtual String get_user_data_dir() const; virtual String get_resource_dir() const; diff --git a/main/main.cpp b/main/main.cpp index 083dc029deb..a33fe2ed0a7 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1365,8 +1365,10 @@ Error Main::setup2(Thread::ID p_main_tid_override) { } #ifdef TOOLS_ENABLED - Ref icon = memnew(Image(app_icon_png)); - OS::get_singleton()->set_icon(icon); + if (OS::get_singleton()->get_bundle_icon_path().empty()) { + Ref icon = memnew(Image(app_icon_png)); + OS::get_singleton()->set_icon(icon); + } #endif } @@ -1989,7 +1991,7 @@ bool Main::start() { #endif } - if (!hasicon) { + if (!hasicon && OS::get_singleton()->get_bundle_icon_path().empty()) { Ref icon = memnew(Image(app_icon_png)); OS::get_singleton()->set_icon(icon); } diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index a9c6074a97c..59684f8315e 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -225,6 +225,7 @@ public: virtual String get_data_path() const; virtual String get_cache_path() const; virtual String get_bundle_resource_dir() const; + virtual String get_bundle_icon_path() const; virtual String get_godot_dir_name() const; virtual String get_system_dir(SystemDir p_dir) const; diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index 3b37f4fea25..49e74f70668 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -2266,14 +2266,26 @@ String OS_OSX::get_cache_path() const { } String OS_OSX::get_bundle_resource_dir() const { - NSBundle *main = [NSBundle mainBundle]; - NSString *resourcePath = [main resourcePath]; - - char *utfs = strdup([resourcePath UTF8String]); String ret; - ret.parse_utf8(utfs); - free(utfs); + NSBundle *main = [NSBundle mainBundle]; + if (main) { + NSString *resourcePath = [main resourcePath]; + ret.parse_utf8([resourcePath UTF8String]); + } + return ret; +} + +String OS_OSX::get_bundle_icon_path() const { + String ret; + + NSBundle *main = [NSBundle mainBundle]; + if (main) { + NSString *iconPath = [[main infoDictionary] objectForKey:@"CFBundleIconFile"]; + if (iconPath) { + ret.parse_utf8([iconPath UTF8String]); + } + } return ret; }