Skip to content

Commit d843374

Browse files
committed
Merge pull request #112766 from syntaxerror247/script-editor-auto-resize
Android Editor: Adjust script editor size for virtual keyboard
2 parents 51c470e + 826f60d commit d843374

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

editor/script/script_editor_plugin.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "editor/gui/window_wrapper.h"
5959
#include "editor/inspector/editor_context_menu_plugin.h"
6060
#include "editor/run/editor_run_bar.h"
61+
#include "editor/scene/editor_scene_tabs.h"
6162
#include "editor/script/editor_script.h"
6263
#include "editor/script/find_in_files.h"
6364
#include "editor/settings/editor_command_palette.h"
@@ -1864,8 +1865,54 @@ void ScriptEditor::_notification(int p_what) {
18641865

18651866
EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &ScriptEditor::_editor_settings_changed));
18661867
EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &ScriptEditor::_filesystem_changed));
1868+
#ifdef ANDROID_ENABLED
1869+
set_process(true);
1870+
#endif
18671871
} break;
18681872

1873+
#ifdef ANDROID_ENABLED
1874+
case NOTIFICATION_VISIBILITY_CHANGED: {
1875+
set_process(is_visible_in_tree());
1876+
} break;
1877+
1878+
case NOTIFICATION_PROCESS: {
1879+
const int kb_height = DisplayServer::get_singleton()->virtual_keyboard_get_height();
1880+
if (kb_height == last_kb_height) {
1881+
break;
1882+
}
1883+
1884+
last_kb_height = kb_height;
1885+
float spacer_height = 0.0f;
1886+
const float status_bar_height = 28 * EDSCALE; // Magic number
1887+
const bool kb_visible = kb_height > 0;
1888+
1889+
if (kb_visible) {
1890+
if (ScriptEditorBase *editor = _get_current_editor()) {
1891+
if (CodeTextEditor *code_editor = editor->get_code_editor()) {
1892+
if (CodeEdit *text_editor = code_editor->get_text_editor()) {
1893+
if (!text_editor->has_focus()) {
1894+
break;
1895+
}
1896+
text_editor->adjust_viewport_to_caret();
1897+
}
1898+
}
1899+
}
1900+
1901+
const float control_bottom = get_global_position().y + get_size().y;
1902+
const float extra_bottom = get_viewport_rect().size.y - control_bottom;
1903+
spacer_height = float(kb_height) - extra_bottom - status_bar_height;
1904+
1905+
if (spacer_height < 0.0f) {
1906+
spacer_height = 0.0f;
1907+
}
1908+
}
1909+
1910+
virtual_keyboard_spacer->set_custom_minimum_size(Size2(0, spacer_height));
1911+
EditorSceneTabs::get_singleton()->set_visible(!kb_height);
1912+
menu_hb->set_visible(!kb_visible);
1913+
} break;
1914+
#endif
1915+
18691916
case NOTIFICATION_EXIT_TREE: {
18701917
EditorRunBar::get_singleton()->disconnect("stop_pressed", callable_mp(this, &ScriptEditor::_editor_stop));
18711918
} break;
@@ -4187,6 +4234,12 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
41874234
main_container->add_child(script_split);
41884235
script_split->set_v_size_flags(SIZE_EXPAND_FILL);
41894236

4237+
#ifdef ANDROID_ENABLED
4238+
virtual_keyboard_spacer = memnew(Control);
4239+
virtual_keyboard_spacer->set_h_size_flags(SIZE_EXPAND_FILL);
4240+
main_container->add_child(virtual_keyboard_spacer);
4241+
#endif
4242+
41904243
list_split = memnew(VSplitContainer);
41914244
script_split->add_child(list_split);
41924245
list_split->set_v_size_flags(SIZE_EXPAND_FILL);

editor/script/script_editor_plugin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ class ScriptEditor : public PanelContainer {
372372

373373
WindowWrapper *window_wrapper = nullptr;
374374

375+
#ifdef ANDROID_ENABLED
376+
Control *virtual_keyboard_spacer = nullptr;
377+
int last_kb_height = -1;
378+
#endif
379+
375380
enum {
376381
SCRIPT_EDITOR_FUNC_MAX = 32,
377382
};

0 commit comments

Comments
 (0)