diff --git a/platform/macos/display_server_macos.h b/platform/macos/display_server_macos.h index 75a30dd89cd..f74c8416386 100644 --- a/platform/macos/display_server_macos.h +++ b/platform/macos/display_server_macos.h @@ -225,8 +225,6 @@ private: }; List deferred_menu_calls; - Callable system_theme_changed; - WindowID _create_window(WindowMode p_mode, VSyncMode p_vsync_mode, const Rect2i &p_rect); void _update_window_style(WindowData p_wd, WindowID p_window); @@ -256,8 +254,6 @@ private: public: void menu_callback(id p_sender); - void emit_system_theme_changed(); - bool has_window(WindowID p_window) const; WindowData &get_window(WindowID p_window); @@ -305,12 +301,6 @@ public: Callable _help_get_search_callback() const; Callable _help_get_action_callback() const; - virtual bool is_dark_mode_supported() const override; - virtual bool is_dark_mode() const override; - virtual Color get_accent_color() const override; - virtual Color get_base_color() const override; - virtual void set_system_theme_change_callback(const Callable &p_callable) override; - virtual Error dialog_show(String p_title, String p_description, Vector p_buttons, const Callable &p_callback) override; virtual Error dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) override; diff --git a/platform/macos/display_server_macos.mm b/platform/macos/display_server_macos.mm index 28b9af33330..0e73cfc5753 100644 --- a/platform/macos/display_server_macos.mm +++ b/platform/macos/display_server_macos.mm @@ -869,91 +869,6 @@ Callable DisplayServerMacOS::_help_get_action_callback() const { return help_action_callback; } -bool DisplayServerMacOS::is_dark_mode_supported() const { - if (@available(macOS 10.14, *)) { - return true; - } else { - return false; - } -} - -bool DisplayServerMacOS::is_dark_mode() const { - if (@available(macOS 10.14, *)) { - if (![[NSUserDefaults standardUserDefaults] objectForKey:@"AppleInterfaceStyle"]) { - return false; - } else { - return ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"] isEqual:@"Dark"]); - } - } else { - return false; - } -} - -Color DisplayServerMacOS::get_accent_color() const { - if (@available(macOS 10.14, *)) { - __block NSColor *color = nullptr; - if (@available(macOS 11.0, *)) { - [NSApp.effectiveAppearance performAsCurrentDrawingAppearance:^{ - color = [[NSColor controlAccentColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; - }]; - } else { - NSAppearance *saved_appearance = [NSAppearance currentAppearance]; - [NSAppearance setCurrentAppearance:[NSApp effectiveAppearance]]; - color = [[NSColor controlAccentColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; - [NSAppearance setCurrentAppearance:saved_appearance]; - } - if (color) { - CGFloat components[4]; - [color getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]]; - return Color(components[0], components[1], components[2], components[3]); - } else { - return Color(0, 0, 0, 0); - } - } else { - return Color(0, 0, 0, 0); - } -} - -Color DisplayServerMacOS::get_base_color() const { - if (@available(macOS 10.14, *)) { - __block NSColor *color = nullptr; - if (@available(macOS 11.0, *)) { - [NSApp.effectiveAppearance performAsCurrentDrawingAppearance:^{ - color = [[NSColor windowBackgroundColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; - }]; - } else { - NSAppearance *saved_appearance = [NSAppearance currentAppearance]; - [NSAppearance setCurrentAppearance:[NSApp effectiveAppearance]]; - color = [[NSColor controlColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; - [NSAppearance setCurrentAppearance:saved_appearance]; - } - if (color) { - CGFloat components[4]; - [color getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]]; - return Color(components[0], components[1], components[2], components[3]); - } else { - return Color(0, 0, 0, 0); - } - } else { - return Color(0, 0, 0, 0); - } -} - -void DisplayServerMacOS::set_system_theme_change_callback(const Callable &p_callable) { - system_theme_changed = p_callable; -} - -void DisplayServerMacOS::emit_system_theme_changed() { - if (system_theme_changed.is_valid()) { - Variant ret; - Callable::CallError ce; - system_theme_changed.callp(nullptr, 0, ret, ce); - if (ce.error != Callable::CallError::CALL_OK) { - ERR_PRINT(vformat("Failed to execute system theme changed callback: %s.", Variant::get_callable_error_text(system_theme_changed, nullptr, 0, ce))); - } - } -} - Error DisplayServerMacOS::dialog_show(String p_title, String p_description, Vector p_buttons, const Callable &p_callback) { _THREAD_SAFE_METHOD_ diff --git a/platform/macos/display_server_macos_base.h b/platform/macos/display_server_macos_base.h index a0aec622edb..1d44ff1ad44 100644 --- a/platform/macos/display_server_macos_base.h +++ b/platform/macos/display_server_macos_base.h @@ -52,6 +52,8 @@ class DisplayServerMacOSBase : public DisplayServer { mutable int current_layout = 0; mutable bool keyboard_layout_dirty = true; + Callable system_theme_changed; + protected: _THREAD_SAFE_CLASS_ @@ -76,6 +78,13 @@ public: virtual Key keyboard_get_label_from_physical(Key p_keycode) const override; virtual void show_emoji_and_symbol_picker() const override; + void emit_system_theme_changed(); + virtual bool is_dark_mode_supported() const override; + virtual bool is_dark_mode() const override; + virtual Color get_accent_color() const override; + virtual Color get_base_color() const override; + virtual void set_system_theme_change_callback(const Callable &p_callable) override; + virtual bool tts_is_speaking() const override; virtual bool tts_is_paused() const override; virtual TypedArray tts_get_voices() const override; diff --git a/platform/macos/display_server_macos_base.mm b/platform/macos/display_server_macos_base.mm index 62b22bd0a8f..f0e62b577b5 100644 --- a/platform/macos/display_server_macos_base.mm +++ b/platform/macos/display_server_macos_base.mm @@ -304,6 +304,97 @@ void DisplayServerMacOSBase::show_emoji_and_symbol_picker() const { [[NSApplication sharedApplication] orderFrontCharacterPalette:nil]; } +bool DisplayServerMacOSBase::is_dark_mode_supported() const { + if (@available(macOS 10.14, *)) { + return true; + } else { + return false; + } +} + +bool DisplayServerMacOSBase::is_dark_mode() const { + if (@available(macOS 10.14, *)) { + if (![[NSUserDefaults standardUserDefaults] objectForKey:@"AppleInterfaceStyle"]) { + return false; + } else { + return ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"] isEqual:@"Dark"]); + } + } else { + return false; + } +} + +Color DisplayServerMacOSBase::get_accent_color() const { + if (@available(macOS 10.14, *)) { + __block NSColor *color = nullptr; + if (@available(macOS 11.0, *)) { + [NSApp.effectiveAppearance performAsCurrentDrawingAppearance:^{ + color = [[NSColor controlAccentColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; + }]; + if (!color) { + color = [[NSColor controlAccentColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; + } + } else { + NSAppearance *saved_appearance = [NSAppearance currentAppearance]; + [NSAppearance setCurrentAppearance:[NSApp effectiveAppearance]]; + color = [[NSColor controlAccentColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; + [NSAppearance setCurrentAppearance:saved_appearance]; + } + if (color) { + CGFloat components[4]; + [color getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]]; + return Color(components[0], components[1], components[2], components[3]); + } else { + return Color(0, 0, 0, 0); + } + } else { + return Color(0, 0, 0, 0); + } +} + +Color DisplayServerMacOSBase::get_base_color() const { + if (@available(macOS 10.14, *)) { + __block NSColor *color = nullptr; + if (@available(macOS 11.0, *)) { + [NSApp.effectiveAppearance performAsCurrentDrawingAppearance:^{ + color = [[NSColor windowBackgroundColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; + }]; + if (!color) { + color = [[NSColor controlColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; + } + } else { + NSAppearance *saved_appearance = [NSAppearance currentAppearance]; + [NSAppearance setCurrentAppearance:[NSApp effectiveAppearance]]; + color = [[NSColor controlColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; + [NSAppearance setCurrentAppearance:saved_appearance]; + } + if (color) { + CGFloat components[4]; + [color getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]]; + return Color(components[0], components[1], components[2], components[3]); + } else { + return Color(0, 0, 0, 0); + } + } else { + return Color(0, 0, 0, 0); + } +} + +void DisplayServerMacOSBase::set_system_theme_change_callback(const Callable &p_callable) { + system_theme_changed = p_callable; +} + +void DisplayServerMacOSBase::emit_system_theme_changed() { + if (system_theme_changed.is_valid()) { + Variant ret; + Callable::CallError ce; + system_theme_changed.callp(nullptr, 0, ret, ce); + if (ce.error != Callable::CallError::CALL_OK) { + ERR_PRINT(vformat("Failed to execute system theme changed callback: %s.", Variant::get_callable_error_text(system_theme_changed, nullptr, 0, ce))); + } + } +} + DisplayServerMacOSBase::DisplayServerMacOSBase() { KeyMappingMacOS::initialize(); diff --git a/platform/macos/godot_application_delegate.mm b/platform/macos/godot_application_delegate.mm index 23bacb21f8b..bfc8d7374ea 100644 --- a/platform/macos/godot_application_delegate.mm +++ b/platform/macos/godot_application_delegate.mm @@ -131,7 +131,7 @@ } - (void)system_theme_changed:(NSNotification *)notification { - DisplayServerMacOS *ds = Object::cast_to(DisplayServer::get_singleton()); + DisplayServerMacOSBase *ds = Object::cast_to(DisplayServer::get_singleton()); if (ds) { ds->emit_system_theme_changed(); }