Android: Add method to set root window color at runtime

This commit is contained in:
Anish Mishra 2025-08-10 19:57:02 +05:30
parent a3b42d85d2
commit 0ad232423d
8 changed files with 50 additions and 10 deletions

View file

@ -2261,6 +2261,14 @@
Makes the window specified by [param window_id] request attention, which is materialized by the window title and taskbar entry blinking until the window is focused. This usually has no visible effect if the window is currently focused. The exact behavior varies depending on the operating system. Makes the window specified by [param window_id] request attention, which is materialized by the window title and taskbar entry blinking until the window is focused. This usually has no visible effect if the window is currently focused. The exact behavior varies depending on the operating system.
</description> </description>
</method> </method>
<method name="window_set_color">
<return type="void" />
<param index="0" name="color" type="Color" />
<description>
Sets the background color of the root window.
[b]Note:[/b] This method is implemented only on Android.
</description>
</method>
<method name="window_set_current_screen"> <method name="window_set_current_screen">
<return type="void" /> <return type="void" />
<param index="0" name="screen" type="int" /> <param index="0" name="screen" type="int" />

View file

@ -626,6 +626,12 @@ bool DisplayServerAndroid::can_any_window_draw() const {
return true; return true;
} }
void DisplayServerAndroid::window_set_color(const Color &p_color) {
GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
ERR_FAIL_NULL(godot_java);
godot_java->set_window_color(p_color);
}
void DisplayServerAndroid::process_events() { void DisplayServerAndroid::process_events() {
Input::get_singleton()->flush_buffered_events(); Input::get_singleton()->flush_buffered_events();
} }

View file

@ -220,6 +220,8 @@ public:
virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override; virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override; virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
virtual void window_set_color(const Color &p_color) override;
virtual void process_events() override; virtual void process_events() override;
void process_accelerometer(const Vector3 &p_accelerometer); void process_accelerometer(const Vector3 &p_accelerometer);

View file

@ -260,16 +260,7 @@ class Godot private constructor(val context: Context) {
useImmersive.set(true) useImmersive.set(true)
newArgs.add(commandLine[i]) newArgs.add(commandLine[i])
} else if (commandLine[i] == "--background_color") { } else if (commandLine[i] == "--background_color") {
val colorStr = commandLine[i + 1] setWindowColor(commandLine[i + 1])
try {
backgroundColor = colorStr.toColorInt()
Log.d(TAG, "background color = $backgroundColor")
} catch (e: java.lang.IllegalArgumentException) {
Log.d(TAG, "Failed to parse background color: $colorStr")
}
runOnHostThread {
getActivity()?.window?.decorView?.setBackgroundColor(backgroundColor)
}
} else if (commandLine[i] == "--use_apk_expansion") { } else if (commandLine[i] == "--use_apk_expansion") {
useApkExpansion = true useApkExpansion = true
} else if (hasExtra && commandLine[i] == "--apk_expansion_md5") { } else if (hasExtra && commandLine[i] == "--apk_expansion_md5") {
@ -492,6 +483,21 @@ class Godot private constructor(val context: Context) {
} }
} }
fun setWindowColor(colorStr: String) {
val color = try {
colorStr.toColorInt()
} catch (e: java.lang.IllegalArgumentException) {
Log.w(TAG, "Failed to parse background color: $colorStr", e)
return
}
val decorView = getActivity()?.window?.decorView ?: return
runOnHostThread {
decorView.setBackgroundColor(color)
backgroundColor = color
setSystemBarsAppearance()
}
}
/** /**
* Used to complete initialization of the view used by the engine for rendering. * Used to complete initialization of the view used by the engine for rendering.
* *

View file

@ -85,6 +85,7 @@ GodotJavaWrapper::GodotJavaWrapper(JNIEnv *p_env, jobject p_godot_instance) {
_verify_apk = p_env->GetMethodID(godot_class, "nativeVerifyApk", "(Ljava/lang/String;)I"); _verify_apk = p_env->GetMethodID(godot_class, "nativeVerifyApk", "(Ljava/lang/String;)I");
_enable_immersive_mode = p_env->GetMethodID(godot_class, "nativeEnableImmersiveMode", "(Z)V"); _enable_immersive_mode = p_env->GetMethodID(godot_class, "nativeEnableImmersiveMode", "(Z)V");
_is_in_immersive_mode = p_env->GetMethodID(godot_class, "isInImmersiveMode", "()Z"); _is_in_immersive_mode = p_env->GetMethodID(godot_class, "isInImmersiveMode", "()Z");
_set_window_color = p_env->GetMethodID(godot_class, "setWindowColor", "(Ljava/lang/String;)V");
_on_editor_workspace_selected = p_env->GetMethodID(godot_class, "nativeOnEditorWorkspaceSelected", "(Ljava/lang/String;)V"); _on_editor_workspace_selected = p_env->GetMethodID(godot_class, "nativeOnEditorWorkspaceSelected", "(Ljava/lang/String;)V");
_get_activity = p_env->GetMethodID(godot_class, "getActivity", "()Landroid/app/Activity;"); _get_activity = p_env->GetMethodID(godot_class, "getActivity", "()Landroid/app/Activity;");
} }
@ -587,6 +588,16 @@ bool GodotJavaWrapper::is_in_immersive_mode() {
} }
} }
void GodotJavaWrapper::set_window_color(const Color &p_color) {
if (_set_window_color) {
JNIEnv *env = get_jni_env();
ERR_FAIL_NULL(env);
String color = "#" + p_color.to_html(false);
jstring jStrColor = env->NewStringUTF(color.utf8().get_data());
env->CallVoidMethod(godot_instance, _set_window_color, jStrColor);
}
}
void GodotJavaWrapper::on_editor_workspace_selected(const String &p_workspace) { void GodotJavaWrapper::on_editor_workspace_selected(const String &p_workspace) {
if (_on_editor_workspace_selected) { if (_on_editor_workspace_selected) {
JNIEnv *env = get_jni_env(); JNIEnv *env = get_jni_env();

View file

@ -81,6 +81,7 @@ private:
jmethodID _verify_apk = nullptr; jmethodID _verify_apk = nullptr;
jmethodID _enable_immersive_mode = nullptr; jmethodID _enable_immersive_mode = nullptr;
jmethodID _is_in_immersive_mode = nullptr; jmethodID _is_in_immersive_mode = nullptr;
jmethodID _set_window_color = nullptr;
jmethodID _on_editor_workspace_selected = nullptr; jmethodID _on_editor_workspace_selected = nullptr;
jmethodID _get_activity = nullptr; jmethodID _get_activity = nullptr;
@ -137,5 +138,7 @@ public:
void enable_immersive_mode(bool p_enabled); void enable_immersive_mode(bool p_enabled);
bool is_in_immersive_mode(); bool is_in_immersive_mode();
void set_window_color(const Color &p_color);
void on_editor_workspace_selected(const String &p_workspace); void on_editor_workspace_selected(const String &p_workspace);
}; };

View file

@ -1476,6 +1476,8 @@ void DisplayServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("window_start_drag", "window_id"), &DisplayServer::window_start_drag, DEFVAL(MAIN_WINDOW_ID)); ClassDB::bind_method(D_METHOD("window_start_drag", "window_id"), &DisplayServer::window_start_drag, DEFVAL(MAIN_WINDOW_ID));
ClassDB::bind_method(D_METHOD("window_start_resize", "edge", "window_id"), &DisplayServer::window_start_resize, DEFVAL(MAIN_WINDOW_ID)); ClassDB::bind_method(D_METHOD("window_start_resize", "edge", "window_id"), &DisplayServer::window_start_resize, DEFVAL(MAIN_WINDOW_ID));
ClassDB::bind_method(D_METHOD("window_set_color", "color"), &DisplayServer::window_set_color);
ClassDB::bind_method(D_METHOD("accessibility_should_increase_contrast"), &DisplayServer::accessibility_should_increase_contrast); ClassDB::bind_method(D_METHOD("accessibility_should_increase_contrast"), &DisplayServer::accessibility_should_increase_contrast);
ClassDB::bind_method(D_METHOD("accessibility_should_reduce_animation"), &DisplayServer::accessibility_should_reduce_animation); ClassDB::bind_method(D_METHOD("accessibility_should_reduce_animation"), &DisplayServer::accessibility_should_reduce_animation);
ClassDB::bind_method(D_METHOD("accessibility_should_reduce_transparency"), &DisplayServer::accessibility_should_reduce_transparency); ClassDB::bind_method(D_METHOD("accessibility_should_reduce_transparency"), &DisplayServer::accessibility_should_reduce_transparency);

View file

@ -525,6 +525,8 @@ public:
virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) {} virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) {}
virtual void window_set_color(const Color &p_color) {}
enum WindowResizeEdge { enum WindowResizeEdge {
WINDOW_EDGE_TOP_LEFT, WINDOW_EDGE_TOP_LEFT,
WINDOW_EDGE_TOP, WINDOW_EDGE_TOP,