diff options
author | Nils ANDRÉ-CHANG <nils@nilsand.re> | 2019-09-12 21:28:49 +0100 |
---|---|---|
committer | Nils ANDRÉ-CHANG <nils@nilsand.re> | 2019-09-26 20:36:12 +0100 |
commit | 0024dd7bb5a8a5194ed0283fc506edcd8b4a7737 (patch) | |
tree | 86316cccbf4fda58a275a7451e37fda83465bb20 /platform/osx | |
parent | cafb888361eba08297dd88b18dc71f4d418525c0 (diff) | |
parent | 24e1039eb6fe32115e8d1a62a84965e9be19a2ed (diff) |
Merge branch 'master' into tab_key
Diffstat (limited to 'platform/osx')
-rw-r--r-- | platform/osx/dir_access_osx.h | 3 | ||||
-rw-r--r-- | platform/osx/export/export.cpp | 21 | ||||
-rw-r--r-- | platform/osx/os_osx.h | 28 | ||||
-rw-r--r-- | platform/osx/os_osx.mm | 115 |
4 files changed, 142 insertions, 25 deletions
diff --git a/platform/osx/dir_access_osx.h b/platform/osx/dir_access_osx.h index e1aa038c61..c5951a570e 100644 --- a/platform/osx/dir_access_osx.h +++ b/platform/osx/dir_access_osx.h @@ -41,9 +41,6 @@ #include "core/os/dir_access.h" #include "drivers/unix/dir_access_unix.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ class DirAccessOSX : public DirAccessUnix { protected: virtual String fix_unicode_name(const char *p_name) const; diff --git a/platform/osx/export/export.cpp b/platform/osx/export/export.cpp index 8cabc45250..56b0a44dbc 100644 --- a/platform/osx/export/export.cpp +++ b/platform/osx/export/export.cpp @@ -29,9 +29,11 @@ /*************************************************************************/ #include "export.h" + #include "core/io/marshalls.h" #include "core/io/resource_saver.h" #include "core/io/zip_io.h" +#include "core/os/dir_access.h" #include "core/os/file_access.h" #include "core/os/os.h" #include "core/project_settings.h" @@ -244,13 +246,17 @@ void EditorExportPlatformOSX::_make_icon(const Ref<Image> &p_icon, Vector<uint8_ copy->resize(icon_infos[i].size, icon_infos[i].size); if (icon_infos[i].is_png) { - //encode png icon + // Encode PNG icon. it->create_from_image(copy); String path = EditorSettings::get_singleton()->get_cache_dir().plus_file("icon.png"); ResourceSaver::save(path, it); FileAccess *f = FileAccess::open(path, FileAccess::READ); - ERR_FAIL_COND(!f); + if (!f) { + // Clean up generated file. + DirAccess::remove_file_or_error(path); + ERR_FAIL(); + } int ofs = data.size(); uint32_t len = f->get_len(); @@ -261,6 +267,10 @@ void EditorExportPlatformOSX::_make_icon(const Ref<Image> &p_icon, Vector<uint8_ len = BSWAP32(len); copymem(&data.write[ofs], icon_infos[i].name, 4); encode_uint32(len, &data.write[ofs + 4]); + + // Clean up generated file. + DirAccess::remove_file_or_error(path); + } else { PoolVector<uint8_t> src_data = copy->get_data(); @@ -561,7 +571,6 @@ Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_p } } } - //bleh? } if (data.size() > 0) { @@ -687,7 +696,8 @@ Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_p // Clean up temporary .app dir OS::get_singleton()->move_to_trash(tmp_app_path_name); - } else { + + } else { // pck String pack_path = EditorSettings::get_singleton()->get_cache_dir().plus_file(pkg_name + ".pck"); @@ -747,6 +757,9 @@ Error EditorExportPlatformOSX::export_project(const Ref<EditorExportPreset> &p_p zipCloseFileInZip(dst_pkg_zip); } } + + // Clean up generated file. + DirAccess::remove_file_or_error(pack_path); } } diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index a83d5084ed..f1f37e24d2 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -51,9 +51,6 @@ #include <CoreVideo/CoreVideo.h> #undef CursorShape -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ class OS_OSX : public OS_Unix { public: @@ -160,6 +157,26 @@ public: int video_driver_index; virtual int get_current_video_driver() const; + struct GlobalMenuItem { + String label; + Variant signal; + Variant meta; + + GlobalMenuItem() { + //NOP + } + + GlobalMenuItem(const String &p_label, const Variant &p_signal, const Variant &p_meta) { + label = p_label; + signal = p_signal; + meta = p_meta; + } + }; + + Map<String, Vector<GlobalMenuItem> > global_menus; + + void _update_global_menu(); + protected: virtual void initialize_core(); virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); @@ -171,6 +188,11 @@ protected: public: static OS_OSX *singleton; + void global_menu_add_item(const String &p_menu, const String &p_label, const Variant &p_signal, const Variant &p_meta); + void global_menu_add_separator(const String &p_menu); + void global_menu_remove_item(const String &p_menu, int p_idx); + void global_menu_clear(const String &p_menu); + void wm_minimized(bool p_minimized); virtual String get_name() const; diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index 726882438b..f48d4a307d 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -204,11 +204,53 @@ static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeSt } } +- (void)globalMenuCallback:(id)sender { + + if (![sender representedObject]) + return; + + OS_OSX::GlobalMenuItem *item = (OS_OSX::GlobalMenuItem *)[[sender representedObject] pointerValue]; + + if (!item) + return; + + OS_OSX::singleton->main_loop->global_menu_action(item->signal, item->meta); +} + +- (NSMenu *)applicationDockMenu:(NSApplication *)sender { + + NSMenu *menu = [[[NSMenu alloc] initWithTitle:@""] autorelease]; + + Vector<OS_OSX::GlobalMenuItem> &E = OS_OSX::singleton->global_menus["_dock"]; + for (int i = 0; i < E.size(); i++) { + if (E[i].label == String()) { + [menu addItem:[NSMenuItem separatorItem]]; + } else { + NSMenuItem *menu_item = [menu addItemWithTitle:[NSString stringWithUTF8String:E[i].label.utf8().get_data()] action:@selector(globalMenuCallback:) keyEquivalent:@""]; + [menu_item setRepresentedObject:[NSValue valueWithPointer:&(E[i])]]; + } + } + + return menu; +} + - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename { - // Note: called before main loop init! + // Note: may be called called before main loop init! char *utfs = strdup([filename UTF8String]); OS_OSX::singleton->open_with_filename.parse_utf8(utfs); free(utfs); + +#ifdef TOOLS_ENABLED + // Open new instance + if (OS_OSX::singleton->get_main_loop()) { + List<String> args; + args.push_back(OS_OSX::singleton->open_with_filename); + String exec = OS::get_singleton()->get_executable_path(); + + OS::ProcessID pid = 0; + OS::get_singleton()->execute(exec, args, false, &pid); + } +#endif return YES; } @@ -1266,6 +1308,56 @@ inline void sendPanEvent(double dx, double dy, int modifierFlags) { @end +void OS_OSX::_update_global_menu() { + + NSMenu *main_menu = [NSApp mainMenu]; + + for (int i = 1; i < [main_menu numberOfItems]; i++) { + [main_menu removeItemAtIndex:i]; + } + for (Map<String, Vector<GlobalMenuItem> >::Element *E = global_menus.front(); E; E = E->next()) { + if (E->key() != "_dock") { + NSMenu *menu = [[[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:E->key().utf8().get_data()]] autorelease]; + for (int i = 0; i < E->get().size(); i++) { + if (E->get()[i].label == String()) { + [menu addItem:[NSMenuItem separatorItem]]; + } else { + NSMenuItem *menu_item = [menu addItemWithTitle:[NSString stringWithUTF8String:E->get()[i].label.utf8().get_data()] action:@selector(globalMenuCallback:) keyEquivalent:@""]; + [menu_item setRepresentedObject:[NSValue valueWithPointer:&(E->get()[i])]]; + } + } + NSMenuItem *menu_item = [main_menu addItemWithTitle:[NSString stringWithUTF8String:E->key().utf8().get_data()] action:nil keyEquivalent:@""]; + [main_menu setSubmenu:menu forItem:menu_item]; + } + } +} + +void OS_OSX::global_menu_add_item(const String &p_menu, const String &p_label, const Variant &p_signal, const Variant &p_meta) { + + global_menus[p_menu].push_back(GlobalMenuItem(p_label, p_signal, p_meta)); + _update_global_menu(); +} + +void OS_OSX::global_menu_add_separator(const String &p_menu) { + + global_menus[p_menu].push_back(GlobalMenuItem()); + _update_global_menu(); +} + +void OS_OSX::global_menu_remove_item(const String &p_menu, int p_idx) { + + ERR_FAIL_INDEX(p_idx, global_menus[p_menu].size()); + + global_menus[p_menu].remove(p_idx); + _update_global_menu(); +} + +void OS_OSX::global_menu_clear(const String &p_menu) { + + global_menus[p_menu].clear(); + _update_global_menu(); +} + Point2 OS_OSX::get_ime_selection() const { return im_selection; @@ -1599,6 +1691,7 @@ void OS_OSX::finalize() { memdelete(joypad_osx); memdelete(input); + cursors_cache.clear(); visual_server->finish(); memdelete(visual_server); //memdelete(rasterizer); @@ -1718,10 +1811,7 @@ Error OS_OSX::open_dynamic_library(const String p_path, void *&p_library_handle, } p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW); - if (!p_library_handle) { - ERR_EXPLAIN("Can't open dynamic library: " + p_path + ". Error: " + dlerror()); - ERR_FAIL_V(ERR_CANT_OPEN); - } + ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ", error: " + dlerror() + "."); return OK; } @@ -1961,15 +2051,10 @@ void OS_OSX::set_native_icon(const String &p_filename) { memdelete(f); NSData *icon_data = [[[NSData alloc] initWithBytes:&data.write[0] length:len] autorelease]; - if (!icon_data) { - ERR_EXPLAIN("Error reading icon data"); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!icon_data, "Error reading icon data."); + NSImage *icon = [[[NSImage alloc] initWithData:icon_data] autorelease]; - if (!icon) { - ERR_EXPLAIN("Error loading icon"); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!icon, "Error loading icon."); [NSApp setApplicationIconImage:icon]; } @@ -2410,7 +2495,7 @@ Size2 OS_OSX::get_min_window_size() const { void OS_OSX::set_min_window_size(const Size2 p_size) { if ((p_size != Size2()) && (max_size != Size2()) && ((p_size.x > max_size.x) || (p_size.y > max_size.y))) { - WARN_PRINT("Minimum window size can't be larger than maximum window size!"); + ERR_PRINT("Minimum window size can't be larger than maximum window size!"); return; } min_size = p_size; @@ -2426,7 +2511,7 @@ void OS_OSX::set_min_window_size(const Size2 p_size) { void OS_OSX::set_max_window_size(const Size2 p_size) { if ((p_size != Size2()) && ((p_size.x < min_size.x) || (p_size.y < min_size.y))) { - WARN_PRINT("Maximum window size can't be smaller than minimum window size!"); + ERR_PRINT("Maximum window size can't be smaller than minimum window size!"); return; } max_size = p_size; |