|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2024-present Anish Mishra (syntaxerror247) |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +@tool |
| 24 | +extends Node |
| 25 | + |
| 26 | +const _plugin_name: String = "SystemBarColorChanger" |
| 27 | +var is_light_status_bar: bool = false |
| 28 | +var is_light_navigation_bar: bool = false |
| 29 | + |
| 30 | +var android_runtime: Object |
| 31 | + |
| 32 | +func _ready() -> void: |
| 33 | + if Engine.has_singleton("AndroidRuntime"): |
| 34 | + android_runtime = Engine.get_singleton("AndroidRuntime") |
| 35 | + var layout_params = JavaClassWrapper.wrap("android.view.WindowManager$LayoutParams") |
| 36 | + var window = android_runtime.getActivity().getWindow() |
| 37 | + window.addFlags(layout_params.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) |
| 38 | + else: |
| 39 | + printerr("AndroidRuntime singleton not found! Try it on an Android device.") |
| 40 | + |
| 41 | + |
| 42 | +func set_status_bar_color(color: Color) -> void: |
| 43 | + if not android_runtime: |
| 44 | + printerr("%s plugin not initialized!" % _plugin_name) |
| 45 | + return |
| 46 | + |
| 47 | + var activity = android_runtime.getActivity() |
| 48 | + var callable = func (): |
| 49 | + var window = activity.getWindow() |
| 50 | + window.setStatusBarColor(color.to_argb32()) |
| 51 | + if is_light_status_bar != (color.get_luminance() > 0.6): |
| 52 | + is_light_status_bar = color.get_luminance() > 0.6 |
| 53 | + var wic = JavaClassWrapper.wrap("android.view.WindowInsetsController") |
| 54 | + var insets_controller = window.getInsetsController() |
| 55 | + insets_controller.setSystemBarsAppearance( |
| 56 | + wic.APPEARANCE_LIGHT_STATUS_BARS if is_light_status_bar else 0, |
| 57 | + wic.APPEARANCE_LIGHT_STATUS_BARS) |
| 58 | + |
| 59 | + activity.runOnUiThread(android_runtime.createRunnableFromGodotCallable(callable)) |
| 60 | + |
| 61 | + |
| 62 | +func set_navigation_bar_color(color: Color) -> void: |
| 63 | + if not android_runtime: |
| 64 | + printerr("%s plugin not initialized!" % _plugin_name) |
| 65 | + return |
| 66 | + |
| 67 | + var activity = android_runtime.getActivity() |
| 68 | + var callable = func (): |
| 69 | + var window = activity.getWindow() |
| 70 | + window.setNavigationBarColor(color.to_argb32()) |
| 71 | + if is_light_navigation_bar != (color.get_luminance() > 0.6): |
| 72 | + is_light_navigation_bar = color.get_luminance() > 0.6 |
| 73 | + var wic = JavaClassWrapper.wrap("android.view.WindowInsetsController") |
| 74 | + var insets_controller = window.getInsetsController() |
| 75 | + insets_controller.setSystemBarsAppearance( |
| 76 | + wic.APPEARANCE_LIGHT_NAVIGATION_BARS if is_light_navigation_bar else 0, |
| 77 | + wic.APPEARANCE_LIGHT_NAVIGATION_BARS) |
| 78 | + |
| 79 | + activity.runOnUiThread(android_runtime.createRunnableFromGodotCallable(callable)) |
| 80 | + |
| 81 | + |
| 82 | +func set_translucent_system_bars(translucent = true) -> void: |
| 83 | + if not android_runtime: |
| 84 | + printerr("%s plugin not initialized!" % _plugin_name) |
| 85 | + return |
| 86 | + |
| 87 | + var activity = android_runtime.getActivity() |
| 88 | + var callable = func (): |
| 89 | + var layout_params = JavaClassWrapper.wrap("android.view.WindowManager$LayoutParams") |
| 90 | + var window = activity.getWindow() |
| 91 | + if translucent: |
| 92 | + window.addFlags(layout_params.FLAG_TRANSLUCENT_STATUS) |
| 93 | + window.addFlags(layout_params.FLAG_TRANSLUCENT_NAVIGATION) |
| 94 | + else: |
| 95 | + window.clearFlags(layout_params.FLAG_TRANSLUCENT_STATUS) |
| 96 | + window.clearFlags(layout_params.FLAG_TRANSLUCENT_NAVIGATION) |
| 97 | + |
| 98 | + activity.runOnUiThread(android_runtime.createRunnableFromGodotCallable(callable)) |
0 commit comments