Skip to content

Commit 7d950c1

Browse files
committed
Merge pull request #98712 from syntaxerror247/android_accent_color
[Android] Implement support for accent color retrieval
2 parents ef8aafc + 7b866f3 commit 7d950c1

File tree

6 files changed

+36
-1
lines changed

6 files changed

+36
-1
lines changed

doc/classes/DisplayServer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
<return type="Color" />
186186
<description>
187187
Returns OS theme accent color. Returns [code]Color(0, 0, 0, 0)[/code], if accent color is unknown.
188-
[b]Note:[/b] This method is implemented on macOS and Windows.
188+
[b]Note:[/b] This method is implemented on macOS, Windows, and Android.
189189
</description>
190190
</method>
191191
<method name="get_base_color" qualifiers="const">

platform/android/display_server_android.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ void DisplayServerAndroid::emit_file_picker_callback(bool p_ok, const Vector<Str
203203
}
204204
}
205205

206+
Color DisplayServerAndroid::get_accent_color() const {
207+
GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
208+
ERR_FAIL_NULL_V(godot_java, Color(0, 0, 0, 0));
209+
return godot_java->get_accent_color();
210+
}
211+
206212
TypedArray<Rect2> DisplayServerAndroid::get_display_cutouts() const {
207213
GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
208214
ERR_FAIL_NULL_V(godot_io_java, Array());

platform/android/display_server_android.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class DisplayServerAndroid : public DisplayServer {
125125
virtual Error file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, const FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) override;
126126
void emit_file_picker_callback(bool p_ok, const Vector<String> &p_selected_paths);
127127

128+
virtual Color get_accent_color() const override;
129+
128130
virtual TypedArray<Rect2> get_display_cutouts() const override;
129131
virtual Rect2i get_display_safe_area() const override;
130132

platform/android/java/lib/src/org/godotengine/godot/Godot.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,12 @@ class Godot(private val context: Context) {
925925
}
926926
}
927927

928+
@Keep
929+
private fun getAccentColor(): Int {
930+
val value = TypedValue()
931+
context.theme.resolveAttribute(android.R.attr.colorAccent, value, true)
932+
return value.data
933+
}
928934

929935
/**
930936
* Destroys the Godot Engine and kill the process it's running in.

platform/android/java_godot_wrapper.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ GodotJavaWrapper::GodotJavaWrapper(JNIEnv *p_env, jobject p_activity, jobject p_
6464
_alert = p_env->GetMethodID(godot_class, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
6565
_is_dark_mode_supported = p_env->GetMethodID(godot_class, "isDarkModeSupported", "()Z");
6666
_is_dark_mode = p_env->GetMethodID(godot_class, "isDarkMode", "()Z");
67+
_get_accent_color = p_env->GetMethodID(godot_class, "getAccentColor", "()I");
6768
_get_clipboard = p_env->GetMethodID(godot_class, "getClipboard", "()Ljava/lang/String;");
6869
_set_clipboard = p_env->GetMethodID(godot_class, "setClipboard", "(Ljava/lang/String;)V");
6970
_has_clipboard = p_env->GetMethodID(godot_class, "hasClipboard", "()Z");
@@ -214,6 +215,23 @@ bool GodotJavaWrapper::is_dark_mode() {
214215
}
215216
}
216217

218+
Color GodotJavaWrapper::get_accent_color() {
219+
if (_get_accent_color) {
220+
JNIEnv *env = get_jni_env();
221+
ERR_FAIL_NULL_V(env, Color(0, 0, 0, 0));
222+
int accent_color = env->CallIntMethod(godot_instance, _get_accent_color);
223+
224+
// Convert ARGB to RGBA.
225+
int alpha = (accent_color >> 24) & 0xFF;
226+
int red = (accent_color >> 16) & 0xFF;
227+
int green = (accent_color >> 8) & 0xFF;
228+
int blue = accent_color & 0xFF;
229+
return Color(red / 255.0f, green / 255.0f, blue / 255.0f, alpha / 255.0f);
230+
} else {
231+
return Color(0, 0, 0, 0);
232+
}
233+
}
234+
217235
bool GodotJavaWrapper::has_get_clipboard() {
218236
return _get_clipboard != nullptr;
219237
}

platform/android/java_godot_wrapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "java_godot_view_wrapper.h"
3535
#include "string_android.h"
3636

37+
#include "core/math/color.h"
3738
#include "core/templates/list.h"
3839

3940
#include <android/log.h>
@@ -55,6 +56,7 @@ class GodotJavaWrapper {
5556
jmethodID _alert = nullptr;
5657
jmethodID _is_dark_mode_supported = nullptr;
5758
jmethodID _is_dark_mode = nullptr;
59+
jmethodID _get_accent_color = nullptr;
5860
jmethodID _get_clipboard = nullptr;
5961
jmethodID _set_clipboard = nullptr;
6062
jmethodID _has_clipboard = nullptr;
@@ -99,6 +101,7 @@ class GodotJavaWrapper {
99101
void alert(const String &p_message, const String &p_title);
100102
bool is_dark_mode_supported();
101103
bool is_dark_mode();
104+
Color get_accent_color();
102105
bool has_get_clipboard();
103106
String get_clipboard();
104107
bool has_set_clipboard();

0 commit comments

Comments
 (0)