1
0
Fork 0

Merge pull request #109980 from bruvzg/emb_theme

[macOS] Move system theme properties to the `DisplayServerMacOSBase`.
This commit is contained in:
Rémi Verschelde 2025-12-19 11:51:39 +01:00
commit 15265bc241
No known key found for this signature in database
GPG Key ID: C3336907360768E1
5 changed files with 101 additions and 96 deletions

View File

@ -225,8 +225,6 @@ private:
};
List<MenuCall> 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<String> 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;

View File

@ -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<String> p_buttons, const Callable &p_callback) {
_THREAD_SAFE_METHOD_

View File

@ -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<Dictionary> tts_get_voices() const override;

View File

@ -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();

View File

@ -131,7 +131,7 @@
}
- (void)system_theme_changed:(NSNotification *)notification {
DisplayServerMacOS *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton());
DisplayServerMacOSBase *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton());
if (ds) {
ds->emit_system_theme_changed();
}