Skip to content

Commit 9283328

Browse files
committed
Merge pull request #109491 from syntaxerror247/window-color
Android: Add method to set root window color at runtime
2 parents 3a16864 + 459cb33 commit 9283328

File tree

10 files changed

+56
-10
lines changed

10 files changed

+56
-10
lines changed

doc/classes/DisplayServer.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,14 @@
22612261
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.
22622262
</description>
22632263
</method>
2264+
<method name="window_set_color">
2265+
<return type="void" />
2266+
<param index="0" name="color" type="Color" />
2267+
<description>
2268+
Sets the background color of the root window.
2269+
[b]Note:[/b] This method is implemented only on Android.
2270+
</description>
2271+
</method>
22642272
<method name="window_set_current_screen">
22652273
<return type="void" />
22662274
<param index="0" name="screen" type="int" />

editor/editor_node.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,9 @@ void EditorNode::_update_theme(bool p_skip_creation) {
690690
editor_dock_manager->update_tab_styles();
691691
editor_dock_manager->update_docks_menu();
692692
editor_dock_manager->set_tab_icon_max_width(theme->get_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
693+
#ifdef ANDROID_ENABLED
694+
DisplayServer::get_singleton()->window_set_color(theme->get_color(SNAME("background"), EditorStringName(Editor)));
695+
#endif
693696
}
694697

695698
Ref<Texture2D> EditorNode::_get_editor_theme_native_menu_icon(const StringName &p_name, bool p_global_menu, bool p_dark_mode) const {

editor/project_manager/project_manager.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ void ProjectManager::_update_theme(bool p_skip_creation) {
297297
asset_library->add_theme_style_override(SceneStringName(panel), memnew(StyleBoxEmpty));
298298
}
299299
}
300+
#ifdef ANDROID_ENABLED
301+
DisplayServer::get_singleton()->window_set_color(theme->get_color(SNAME("background"), EditorStringName(Editor)));
302+
#endif
300303
}
301304

302305
Button *ProjectManager::_add_main_view(MainViewTab p_id, const String &p_name, const Ref<Texture2D> &p_icon, Control *p_view_control) {

platform/android/display_server_android.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,12 @@ bool DisplayServerAndroid::can_any_window_draw() const {
626626
return true;
627627
}
628628

629+
void DisplayServerAndroid::window_set_color(const Color &p_color) {
630+
GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
631+
ERR_FAIL_NULL(godot_java);
632+
godot_java->set_window_color(p_color);
633+
}
634+
629635
void DisplayServerAndroid::process_events() {
630636
Input::get_singleton()->flush_buffered_events();
631637
}

platform/android/display_server_android.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ class DisplayServerAndroid : public DisplayServer {
220220
virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
221221
virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
222222

223+
virtual void window_set_color(const Color &p_color) override;
224+
223225
virtual void process_events() override;
224226

225227
void process_accelerometer(const Vector3 &p_accelerometer);

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,7 @@ class Godot private constructor(val context: Context) {
260260
useImmersive.set(true)
261261
newArgs.add(commandLine[i])
262262
} else if (commandLine[i] == "--background_color") {
263-
val colorStr = commandLine[i + 1]
264-
try {
265-
backgroundColor = colorStr.toColorInt()
266-
Log.d(TAG, "background color = $backgroundColor")
267-
} catch (e: java.lang.IllegalArgumentException) {
268-
Log.d(TAG, "Failed to parse background color: $colorStr")
269-
}
270-
runOnHostThread {
271-
getActivity()?.window?.decorView?.setBackgroundColor(backgroundColor)
272-
}
263+
setWindowColor(commandLine[i + 1])
273264
} else if (commandLine[i] == "--use_apk_expansion") {
274265
useApkExpansion = true
275266
} else if (hasExtra && commandLine[i] == "--apk_expansion_md5") {
@@ -492,6 +483,21 @@ class Godot private constructor(val context: Context) {
492483
}
493484
}
494485

486+
fun setWindowColor(colorStr: String) {
487+
val color = try {
488+
colorStr.toColorInt()
489+
} catch (e: java.lang.IllegalArgumentException) {
490+
Log.w(TAG, "Failed to parse background color: $colorStr", e)
491+
return
492+
}
493+
val decorView = getActivity()?.window?.decorView ?: return
494+
runOnHostThread {
495+
decorView.setBackgroundColor(color)
496+
backgroundColor = color
497+
setSystemBarsAppearance()
498+
}
499+
}
500+
495501
/**
496502
* Used to complete initialization of the view used by the engine for rendering.
497503
*

platform/android/java_godot_wrapper.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ GodotJavaWrapper::GodotJavaWrapper(JNIEnv *p_env, jobject p_godot_instance) {
8585
_verify_apk = p_env->GetMethodID(godot_class, "nativeVerifyApk", "(Ljava/lang/String;)I");
8686
_enable_immersive_mode = p_env->GetMethodID(godot_class, "nativeEnableImmersiveMode", "(Z)V");
8787
_is_in_immersive_mode = p_env->GetMethodID(godot_class, "isInImmersiveMode", "()Z");
88+
_set_window_color = p_env->GetMethodID(godot_class, "setWindowColor", "(Ljava/lang/String;)V");
8889
_on_editor_workspace_selected = p_env->GetMethodID(godot_class, "nativeOnEditorWorkspaceSelected", "(Ljava/lang/String;)V");
8990
_get_activity = p_env->GetMethodID(godot_class, "getActivity", "()Landroid/app/Activity;");
9091
}
@@ -587,6 +588,16 @@ bool GodotJavaWrapper::is_in_immersive_mode() {
587588
}
588589
}
589590

591+
void GodotJavaWrapper::set_window_color(const Color &p_color) {
592+
if (_set_window_color) {
593+
JNIEnv *env = get_jni_env();
594+
ERR_FAIL_NULL(env);
595+
String color = "#" + p_color.to_html(false);
596+
jstring jStrColor = env->NewStringUTF(color.utf8().get_data());
597+
env->CallVoidMethod(godot_instance, _set_window_color, jStrColor);
598+
}
599+
}
600+
590601
void GodotJavaWrapper::on_editor_workspace_selected(const String &p_workspace) {
591602
if (_on_editor_workspace_selected) {
592603
JNIEnv *env = get_jni_env();

platform/android/java_godot_wrapper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class GodotJavaWrapper {
8181
jmethodID _verify_apk = nullptr;
8282
jmethodID _enable_immersive_mode = nullptr;
8383
jmethodID _is_in_immersive_mode = nullptr;
84+
jmethodID _set_window_color = nullptr;
8485
jmethodID _on_editor_workspace_selected = nullptr;
8586
jmethodID _get_activity = nullptr;
8687

@@ -137,5 +138,7 @@ class GodotJavaWrapper {
137138
void enable_immersive_mode(bool p_enabled);
138139
bool is_in_immersive_mode();
139140

141+
void set_window_color(const Color &p_color);
142+
140143
void on_editor_workspace_selected(const String &p_workspace);
141144
};

servers/display_server.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,8 @@ void DisplayServer::_bind_methods() {
14761476
ClassDB::bind_method(D_METHOD("window_start_drag", "window_id"), &DisplayServer::window_start_drag, DEFVAL(MAIN_WINDOW_ID));
14771477
ClassDB::bind_method(D_METHOD("window_start_resize", "edge", "window_id"), &DisplayServer::window_start_resize, DEFVAL(MAIN_WINDOW_ID));
14781478

1479+
ClassDB::bind_method(D_METHOD("window_set_color", "color"), &DisplayServer::window_set_color);
1480+
14791481
ClassDB::bind_method(D_METHOD("accessibility_should_increase_contrast"), &DisplayServer::accessibility_should_increase_contrast);
14801482
ClassDB::bind_method(D_METHOD("accessibility_should_reduce_animation"), &DisplayServer::accessibility_should_reduce_animation);
14811483
ClassDB::bind_method(D_METHOD("accessibility_should_reduce_transparency"), &DisplayServer::accessibility_should_reduce_transparency);

servers/display_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ class DisplayServer : public Object {
525525

526526
virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) {}
527527

528+
virtual void window_set_color(const Color &p_color) {}
529+
528530
enum WindowResizeEdge {
529531
WINDOW_EDGE_TOP_LEFT,
530532
WINDOW_EDGE_TOP,

0 commit comments

Comments
 (0)