Godot Engine – Multi-platform 2D and 3D game engine
[DisplayServer] Add method to set IME exclusion rectangle.
Pāvels Nadtočajevsc11e36726195
diff --git a/doc/classes/DisplayServer.xml b/doc/classes/DisplayServer.xmlindex c5a0f3d3afd..14aa064973e 100644--- a/doc/classes/DisplayServer.xml+++ b/doc/classes/DisplayServer.xml@@ -2493,6 +2493,15 @@Sets the position of the [url=https://en.wikipedia.org/wiki/Input_method]Input Method Editor[/url] popup for the specified [param window_id]. Only effective if [method window_set_ime_active] was set to [code]true[/code] for the specified [param window_id].</description></method>+ <method name="window_set_ime_rect">+ <return type="void" />+ <param index="0" name="rect" type="Rect2i" />+ <param index="1" name="window_id" type="int" default="0" />+ <description>+ Sets a rectangle which should not be obstructed by the [url=https://en.wikipedia.org/wiki/Input_method]Input Method Editor[/url] popup for the specified [param window_id]. Only effective if [method window_set_ime_active] was set to [code]true[/code] for the specified [param window_id].+ [b]Note:[/b] This method is implemented only on Windows.+ </description>+ </method><method name="window_set_input_event_callback"><return type="void" /><param index="0" name="callback" type="Callable" />diff --git a/doc/classes/Window.xml b/doc/classes/Window.xmlindex f41cabc5d26..0836b6efe2e 100644--- a/doc/classes/Window.xml+++ b/doc/classes/Window.xml@@ -612,6 +612,14 @@Moves IME to the given position.</description></method>+ <method name="set_ime_rect">+ <return type="void" />+ <param index="0" name="rect" type="Rect2i" />+ <description>+ Sets a rectangle which should not be obstructed by the [url=https://en.wikipedia.org/wiki/Input_method]Input Method Editor[/url] popup.+ [b]Note:[/b] This method is implemented only on Windows.+ </description>+ </method><method name="set_layout_direction"><return type="void" /><param index="0" name="direction" type="int" enum="Window.LayoutDirection" />diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cppindex fe1eca95216..c3ebe4b0075 100644--- a/platform/windows/display_server_windows.cpp+++ b/platform/windows/display_server_windows.cpp@@ -127,7 +127,7 @@ static void track_mouse_leave_event(HWND hWnd) {TrackMouseEvent(&tme);}-static void update_ime_form_positions(HIMC p_himc, const Point2i &p_pos) {+static void update_ime_form_positions(HIMC p_himc, const Point2i &p_pos, const Rect2i &p_rect) {if (p_himc == (HIMC) nullptr) {return;}@@ -154,10 +154,17 @@ static void update_ime_form_positions(HIMC p_himc, const Point2i &p_pos) {ImmSetCandidateWindow(p_himc, &cf);cf.dwStyle = CFS_EXCLUDE;- cf.rcArea.left = p_pos.x;- cf.rcArea.right = p_pos.x;- cf.rcArea.top = p_pos.y;- cf.rcArea.bottom = p_pos.y;+ if (p_rect == Rect2i()) {+ cf.rcArea.left = p_pos.x;+ cf.rcArea.right = p_pos.x + 1;+ cf.rcArea.top = p_pos.y;+ cf.rcArea.bottom = p_pos.y + 1;+ } else {+ cf.rcArea.left = p_rect.position.x;+ cf.rcArea.right = p_rect.position.x + p_rect.size.x;+ cf.rcArea.top = p_rect.position.y;+ cf.rcArea.bottom = p_rect.position.y + p_rect.size.y;+ }ImmSetCandidateWindow(p_himc, &cf);}@@ -3344,7 +3351,24 @@ void DisplayServerWindows::window_set_ime_position(const Point2i &p_pos, Displayreturn;}- update_ime_form_positions(himc, wd.im_position);+ update_ime_form_positions(himc, wd.im_position, wd.im_rect);+ ImmReleaseContext(wd.hWnd, himc);+}++void DisplayServerWindows::window_set_ime_rect(const Rect2i &p_rect, DisplayServerEnums::WindowID p_window) {+ _THREAD_SAFE_METHOD_++ ERR_FAIL_COND(!windows.has(p_window));+ WindowData &wd = windows[p_window];++ wd.im_rect = p_rect;++ HIMC himc = ImmGetContext(wd.hWnd);+ if (himc == (HIMC) nullptr) {+ return;+ }++ update_ime_form_positions(himc, wd.im_position, wd.im_rect);ImmReleaseContext(wd.hWnd, himc);}@@ -6926,7 +6950,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA} break;case WM_IME_COMPOSITION: {- update_ime_form_positions(windows[window_id].im_himc, windows[window_id].im_position);+ update_ime_form_positions(windows[window_id].im_himc, windows[window_id].im_position, windows[window_id].im_rect);if (windows[window_id].ime_active) {SetCaretPos(windows[window_id].im_position.x, windows[window_id].im_position.y);diff --git a/platform/windows/display_server_windows.h b/platform/windows/display_server_windows.hindex 2107d46df28..82c0431814e 100644--- a/platform/windows/display_server_windows.h+++ b/platform/windows/display_server_windows.h@@ -365,7 +365,8 @@ class DisplayServerWindows : public DisplayServer {// IMEHIMC im_himc;- Vector2 im_position;+ Vector2i im_position;+ Rect2i im_rect;bool ime_active = false;bool ime_in_progress = false;bool ime_suppress_next_keyup = false;@@ -714,6 +715,7 @@ public:virtual void window_set_ime_active(const bool p_active, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID) override;virtual void window_set_ime_position(const Point2i &p_pos, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID) override;+ virtual void window_set_ime_rect(const Rect2i &p_rect, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID) override;virtual int accessibility_should_increase_contrast() const override;virtual int accessibility_should_reduce_animation() const override;diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cppindex fb1ad6a05ae..25393177991 100644--- a/scene/gui/line_edit.cpp+++ b/scene/gui/line_edit.cpp@@ -126,7 +126,8 @@ void LineEdit::_close_ime_window() {if (wid == DisplayServerEnums::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_IME)) {return;}- DisplayServer::get_singleton()->window_set_ime_position(Point2(), wid);+ DisplayServer::get_singleton()->window_set_ime_position(Point2i(), wid);+ DisplayServer::get_singleton()->window_set_ime_rect(Rect2i(), wid);DisplayServer::get_singleton()->window_set_ime_active(false, wid);}@@ -135,15 +136,19 @@ void LineEdit::_update_ime_window_position() {if (wid == DisplayServerEnums::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_IME)) {return;}- DisplayServer::get_singleton()->window_set_ime_active(true, wid);Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2) + get_global_position();+ Rect2i rect = Rect2i(get_global_position(), get_size());if (get_window()->get_embedder()) {pos += get_viewport()->get_popup_base_transform().get_origin();+ rect.position += get_viewport()->get_popup_base_transform().get_origin();}// Take into account the window's transform.pos = get_window()->get_screen_transform().xform(pos);+ rect = get_window()->get_screen_transform().xform(rect);// The window will move to the updated position the next time the IME is updated, not immediately.+ DisplayServer::get_singleton()->window_set_ime_active(true, wid);DisplayServer::get_singleton()->window_set_ime_position(pos, wid);+ DisplayServer::get_singleton()->window_set_ime_rect(rect, wid);}bool LineEdit::has_ime_text() const {diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cppindex f7e0905dd5d..a9d3db642a9 100644--- a/scene/gui/text_edit.cpp+++ b/scene/gui/text_edit.cpp@@ -4035,7 +4035,8 @@ void TextEdit::_close_ime_window() {if (wid == DisplayServerEnums::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_IME)) {return;}- DisplayServer::get_singleton()->window_set_ime_position(Point2(), wid);+ DisplayServer::get_singleton()->window_set_ime_position(Point2i(), wid);+ DisplayServer::get_singleton()->window_set_ime_rect(Rect2i(), wid);DisplayServer::get_singleton()->window_set_ime_active(false, wid);}@@ -4044,19 +4045,25 @@ void TextEdit::_update_ime_window_position() {if (wid == DisplayServerEnums::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_IME)) {return;}- if (!editable) {+ if (!editable || carets.is_empty()) {DisplayServer::get_singleton()->window_set_ime_active(false, wid);return;}- DisplayServer::get_singleton()->window_set_ime_active(true, wid);- Point2 pos = get_global_position() + get_caret_draw_pos();+ const Caret &caret = carets[0];+ float h = theme_cache.font->get_height(theme_cache.font_size);+ Rect2i rect = Rect2i(Vector2i(0, caret.draw_pos.y - h) + get_global_position(), Size2i(get_size().width, h));+ Point2 pos = get_global_position() + caret.draw_pos;if (get_window()->get_embedder()) {pos += get_viewport()->get_popup_base_transform().get_origin();+ rect.position += get_viewport()->get_popup_base_transform().get_origin();}// Take into account the window's transform.pos = get_window()->get_screen_transform().xform(pos);+ rect = get_window()->get_screen_transform().xform(rect);// The window will move to the updated position the next time the IME is updated, not immediately.+ DisplayServer::get_singleton()->window_set_ime_active(true, wid);DisplayServer::get_singleton()->window_set_ime_position(pos, wid);+ DisplayServer::get_singleton()->window_set_ime_rect(rect, wid);}void TextEdit::_update_ime_text() {diff --git a/scene/main/window.cpp b/scene/main/window.cppindex b3a61cd5e94..ef0140ee146 100644--- a/scene/main/window.cpp+++ b/scene/main/window.cpp@@ -730,6 +730,13 @@ void Window::set_ime_position(const Point2i &p_pos) {}}+void Window::set_ime_rect(const Rect2i &p_rect) {+ ERR_MAIN_THREAD_GUARD;+ if (window_id != DisplayServerEnums::INVALID_WINDOW_ID) {+ DisplayServer::get_singleton()->window_set_ime_rect(p_rect, window_id);+ }+}+bool Window::is_embedded() const {ERR_READ_THREAD_GUARD_V(false);return get_embedder() != nullptr;@@ -3563,6 +3570,7 @@ void Window::_bind_methods() {ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &Window::set_ime_active);ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &Window::set_ime_position);+ ClassDB::bind_method(D_METHOD("set_ime_rect", "rect"), &Window::set_ime_rect);ClassDB::bind_method(D_METHOD("is_embedded"), &Window::is_embedded);diff --git a/scene/main/window.h b/scene/main/window.hindex a82decca1ea..d8a29fe52fc 100644--- a/scene/main/window.h+++ b/scene/main/window.h@@ -396,6 +396,7 @@ public:void set_ime_active(bool p_active);void set_ime_position(const Point2i &p_pos);+ void set_ime_rect(const Rect2i &p_rect);bool is_embedded() const;Viewport *get_embedder() const;diff --git a/servers/display/display_server.cpp b/servers/display/display_server.cppindex 789ae06ad34..d363550e2b1 100644--- a/servers/display/display_server.cpp+++ b/servers/display/display_server.cpp@@ -1554,6 +1554,7 @@ void DisplayServer::_bind_methods() {ClassDB::bind_method(D_METHOD("window_set_ime_active", "active", "window_id"), &DisplayServer::window_set_ime_active, DEFVAL(DisplayServerEnums::MAIN_WINDOW_ID));ClassDB::bind_method(D_METHOD("window_set_ime_position", "position", "window_id"), &DisplayServer::window_set_ime_position, DEFVAL(DisplayServerEnums::MAIN_WINDOW_ID));+ ClassDB::bind_method(D_METHOD("window_set_ime_rect", "rect", "window_id"), &DisplayServer::window_set_ime_rect, DEFVAL(DisplayServerEnums::MAIN_WINDOW_ID));ClassDB::bind_method(D_METHOD("window_set_vsync_mode", "vsync_mode", "window_id"), &DisplayServer::window_set_vsync_mode, DEFVAL(DisplayServerEnums::MAIN_WINDOW_ID));ClassDB::bind_method(D_METHOD("window_get_vsync_mode", "window_id"), &DisplayServer::window_get_vsync_mode, DEFVAL(DisplayServerEnums::MAIN_WINDOW_ID));diff --git a/servers/display/display_server.h b/servers/display/display_server.hindex 2c064aaff22..b17639deea3 100644--- a/servers/display/display_server.h+++ b/servers/display/display_server.h@@ -472,6 +472,7 @@ public:virtual void window_set_ime_active(const bool p_active, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID);virtual void window_set_ime_position(const Point2i &p_pos, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID);+ virtual void window_set_ime_rect(const Rect2i &p_rect, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID) {}virtual bool window_maximize_on_title_dbl_click() const { return false; }virtual bool window_minimize_on_title_dbl_click() const { return false; }diff --git a/servers/display/display_server_headless.h b/servers/display/display_server_headless.hindex d5c13fd231c..25a87537279 100644--- a/servers/display/display_server_headless.h+++ b/servers/display/display_server_headless.h@@ -142,6 +142,7 @@ public:void window_set_ime_active(const bool p_active, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID) override {}void window_set_ime_position(const Point2i &p_pos, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID) override {}+ void window_set_ime_rect(const Rect2i &p_rect, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID) override {}int64_t window_get_native_handle(DisplayServerEnums::HandleType p_handle_type, DisplayServerEnums::WindowID p_window = DisplayServerEnums::MAIN_WINDOW_ID) const override { return 0; }