Support dark mode on Android and iOS.

This commit is contained in:
Zae 2023-09-26 11:00:04 +08:00
parent c12d63556b
commit 428eb1309a
8 changed files with 84 additions and 2 deletions

View File

@ -776,14 +776,14 @@
<return type="bool" /> <return type="bool" />
<description> <description>
Returns [code]true[/code] if OS is using dark mode. Returns [code]true[/code] if OS is using dark mode.
[b]Note:[/b] This method is implemented on macOS, Windows and Linux (X11). [b]Note:[/b] This method is implemented on Android, iOS, macOS, Windows, and Linux (X11).
</description> </description>
</method> </method>
<method name="is_dark_mode_supported" qualifiers="const"> <method name="is_dark_mode_supported" qualifiers="const">
<return type="bool" /> <return type="bool" />
<description> <description>
Returns [code]true[/code] if OS supports dark mode. Returns [code]true[/code] if OS supports dark mode.
[b]Note:[/b] This method is implemented on macOS, Windows and Linux (X11). [b]Note:[/b] This method is implemented on Android, iOS, macOS, Windows, and Linux (X11).
</description> </description>
</method> </method>
<method name="is_touchscreen_available" qualifiers="const"> <method name="is_touchscreen_available" qualifiers="const">

View File

@ -111,6 +111,20 @@ void DisplayServerAndroid::tts_stop() {
TTS_Android::stop(); TTS_Android::stop();
} }
bool DisplayServerAndroid::is_dark_mode_supported() const {
GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
ERR_FAIL_NULL_V(godot_java, false);
return godot_java->is_dark_mode_supported();
}
bool DisplayServerAndroid::is_dark_mode() const {
GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
ERR_FAIL_NULL_V(godot_java, false);
return godot_java->is_dark_mode();
}
void DisplayServerAndroid::clipboard_set(const String &p_text) { void DisplayServerAndroid::clipboard_set(const String &p_text) {
GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java(); GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
ERR_FAIL_NULL(godot_java); ERR_FAIL_NULL(godot_java);

View File

@ -103,6 +103,9 @@ public:
virtual void tts_resume() override; virtual void tts_resume() override;
virtual void tts_stop() override; virtual void tts_stop() override;
virtual bool is_dark_mode_supported() const override;
virtual bool is_dark_mode() const override;
virtual void clipboard_set(const String &p_text) override; virtual void clipboard_set(const String &p_text) override;
virtual String clipboard_get() const override; virtual String clipboard_get() const override;
virtual bool clipboard_has() const override; virtual bool clipboard_has() const override;

View File

@ -35,6 +35,7 @@ import android.app.Activity
import android.app.AlertDialog import android.app.AlertDialog
import android.content.* import android.content.*
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources import android.content.res.Resources
import android.graphics.Rect import android.graphics.Rect
import android.hardware.Sensor import android.hardware.Sensor
@ -694,6 +695,25 @@ class Godot(private val context: Context) : SensorEventListener {
} }
} }
/**
* Returns true if dark mode is supported, false otherwise.
*/
@Keep
private fun isDarkModeSupported(): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
}
/**
* Returns true if dark mode is supported and enabled, false otherwise.
*/
@Keep
private fun isDarkMode(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return context.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
}
return false
}
fun hasClipboard(): Boolean { fun hasClipboard(): Boolean {
return mClipboard.hasPrimaryClip() return mClipboard.hasPrimaryClip()
} }

View File

@ -62,6 +62,8 @@ GodotJavaWrapper::GodotJavaWrapper(JNIEnv *p_env, jobject p_activity, jobject p_
_finish = p_env->GetMethodID(godot_class, "forceQuit", "(I)Z"); _finish = p_env->GetMethodID(godot_class, "forceQuit", "(I)Z");
_set_keep_screen_on = p_env->GetMethodID(godot_class, "setKeepScreenOn", "(Z)V"); _set_keep_screen_on = p_env->GetMethodID(godot_class, "setKeepScreenOn", "(Z)V");
_alert = p_env->GetMethodID(godot_class, "alert", "(Ljava/lang/String;Ljava/lang/String;)V"); _alert = p_env->GetMethodID(godot_class, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
_is_dark_mode_supported = p_env->GetMethodID(godot_class, "isDarkModeSupported", "()Z");
_is_dark_mode = p_env->GetMethodID(godot_class, "isDarkMode", "()Z");
_get_clipboard = p_env->GetMethodID(godot_class, "getClipboard", "()Ljava/lang/String;"); _get_clipboard = p_env->GetMethodID(godot_class, "getClipboard", "()Ljava/lang/String;");
_set_clipboard = p_env->GetMethodID(godot_class, "setClipboard", "(Ljava/lang/String;)V"); _set_clipboard = p_env->GetMethodID(godot_class, "setClipboard", "(Ljava/lang/String;)V");
_has_clipboard = p_env->GetMethodID(godot_class, "hasClipboard", "()Z"); _has_clipboard = p_env->GetMethodID(godot_class, "hasClipboard", "()Z");
@ -172,6 +174,26 @@ void GodotJavaWrapper::alert(const String &p_message, const String &p_title) {
} }
} }
bool GodotJavaWrapper::is_dark_mode_supported() {
if (_is_dark_mode_supported) {
JNIEnv *env = get_jni_env();
ERR_FAIL_NULL_V(env, false);
return env->CallBooleanMethod(godot_instance, _is_dark_mode_supported);
} else {
return false;
}
}
bool GodotJavaWrapper::is_dark_mode() {
if (_is_dark_mode) {
JNIEnv *env = get_jni_env();
ERR_FAIL_NULL_V(env, false);
return env->CallBooleanMethod(godot_instance, _is_dark_mode);
} else {
return false;
}
}
bool GodotJavaWrapper::has_get_clipboard() { bool GodotJavaWrapper::has_get_clipboard() {
return _get_clipboard != nullptr; return _get_clipboard != nullptr;
} }

View File

@ -53,6 +53,8 @@ private:
jmethodID _finish = nullptr; jmethodID _finish = nullptr;
jmethodID _set_keep_screen_on = nullptr; jmethodID _set_keep_screen_on = nullptr;
jmethodID _alert = nullptr; jmethodID _alert = nullptr;
jmethodID _is_dark_mode_supported = nullptr;
jmethodID _is_dark_mode = nullptr;
jmethodID _get_clipboard = nullptr; jmethodID _get_clipboard = nullptr;
jmethodID _set_clipboard = nullptr; jmethodID _set_clipboard = nullptr;
jmethodID _has_clipboard = nullptr; jmethodID _has_clipboard = nullptr;
@ -86,6 +88,8 @@ public:
bool force_quit(JNIEnv *p_env = nullptr, int p_instance_id = 0); bool force_quit(JNIEnv *p_env = nullptr, int p_instance_id = 0);
void set_keep_screen_on(bool p_enabled); void set_keep_screen_on(bool p_enabled);
void alert(const String &p_message, const String &p_title); void alert(const String &p_message, const String &p_title);
bool is_dark_mode_supported();
bool is_dark_mode();
bool has_get_clipboard(); bool has_get_clipboard();
String get_clipboard(); String get_clipboard();
bool has_set_clipboard(); bool has_set_clipboard();

View File

@ -141,6 +141,9 @@ public:
virtual void tts_resume() override; virtual void tts_resume() override;
virtual void tts_stop() override; virtual void tts_stop() override;
virtual bool is_dark_mode_supported() const override;
virtual bool is_dark_mode() const override;
virtual Rect2i get_display_safe_area() const override; virtual Rect2i get_display_safe_area() const override;
virtual int get_screen_count() const override; virtual int get_screen_count() const override;

View File

@ -354,6 +354,22 @@ void DisplayServerIOS::tts_stop() {
[tts stopSpeaking]; [tts stopSpeaking];
} }
bool DisplayServerIOS::is_dark_mode_supported() const {
if (@available(iOS 13.0, *)) {
return true;
} else {
return false;
}
}
bool DisplayServerIOS::is_dark_mode() const {
if (@available(iOS 13.0, *)) {
return [UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleDark;
} else {
return false;
}
}
Rect2i DisplayServerIOS::get_display_safe_area() const { Rect2i DisplayServerIOS::get_display_safe_area() const {
if (@available(iOS 11, *)) { if (@available(iOS 11, *)) {
UIEdgeInsets insets = UIEdgeInsetsZero; UIEdgeInsets insets = UIEdgeInsetsZero;