Qt: Open host input method on imgui text edit

This should open virtual keyboards on devices which do not have a
physical keyboard.
This commit is contained in:
Connor McLaughlin 2022-09-03 22:29:36 +10:00 committed by refractionpcsx2
parent c136bb4ea8
commit 1f5d672cbf
3 changed files with 37 additions and 2 deletions

View File

@ -52,6 +52,7 @@
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QtWidgets/QMessageBox> #include <QtWidgets/QMessageBox>
#include <QtGui/QClipboard> #include <QtGui/QClipboard>
#include <QtGui/QInputMethod>
#include "fmt/core.h" #include "fmt/core.h"
@ -1407,6 +1408,20 @@ bool Host::CopyTextToClipboard(const std::string_view& text)
return true; return true;
} }
void Host::BeginTextInput()
{
QInputMethod* method = qApp->inputMethod();
if (method)
QMetaObject::invokeMethod(method, "show", Qt::QueuedConnection);
}
void Host::EndTextInput()
{
QInputMethod* method = qApp->inputMethod();
if (method)
QMetaObject::invokeMethod(method, "hide", Qt::QueuedConnection);
}
void Host::OnInputDeviceConnected(const std::string_view& identifier, const std::string_view& device_name) void Host::OnInputDeviceConnected(const std::string_view& identifier, const std::string_view& device_name)
{ {
emit g_emu_thread->onInputDeviceConnected( emit g_emu_thread->onInputDeviceConnected(

View File

@ -82,6 +82,7 @@ static Common::Timer s_last_render_time;
// cached copies of WantCaptureKeyboard/Mouse, used to know when to dispatch events // cached copies of WantCaptureKeyboard/Mouse, used to know when to dispatch events
static std::atomic_bool s_imgui_wants_keyboard{false}; static std::atomic_bool s_imgui_wants_keyboard{false};
static std::atomic_bool s_imgui_wants_mouse{false}; static std::atomic_bool s_imgui_wants_mouse{false};
static std::atomic_bool s_imgui_wants_text{false};
// mapping of host key -> imgui key // mapping of host key -> imgui key
static std::unordered_map<u32, ImGuiKey> s_imgui_key_map; static std::unordered_map<u32, ImGuiKey> s_imgui_key_map;
@ -225,6 +226,16 @@ void ImGuiManager::NewFrame()
ImGui::GetCurrentWindowRead()->Flags |= ImGuiWindowFlags_NoNavInputs; ImGui::GetCurrentWindowRead()->Flags |= ImGuiWindowFlags_NoNavInputs;
s_imgui_wants_keyboard.store(io.WantCaptureKeyboard, std::memory_order_relaxed); s_imgui_wants_keyboard.store(io.WantCaptureKeyboard, std::memory_order_relaxed);
s_imgui_wants_mouse.store(io.WantCaptureMouse, std::memory_order_release); s_imgui_wants_mouse.store(io.WantCaptureMouse, std::memory_order_release);
const bool want_text_input = io.WantTextInput;
if (s_imgui_wants_text.load(std::memory_order_relaxed) != want_text_input)
{
s_imgui_wants_text.store(want_text_input, std::memory_order_release);
if (want_text_input)
Host::BeginTextInput();
else
Host::EndTextInput();
}
#endif #endif
} }
@ -875,12 +886,12 @@ ImFont* ImGuiManager::GetLargeFont()
bool ImGuiManager::WantsTextInput() bool ImGuiManager::WantsTextInput()
{ {
return s_imgui_wants_keyboard.load(std::memory_order_acquire); return s_imgui_wants_text.load(std::memory_order_acquire);
} }
void ImGuiManager::AddTextInput(std::string str) void ImGuiManager::AddTextInput(std::string str)
{ {
if (!s_imgui_wants_keyboard.load(std::memory_order_acquire)) if (!s_imgui_wants_text.load(std::memory_order_acquire))
return; return;
// Has to go through the CPU -> GS thread :( // Has to go through the CPU -> GS thread :(

View File

@ -90,3 +90,12 @@ namespace ImGuiManager
#endif #endif
} // namespace ImGuiManager } // namespace ImGuiManager
namespace Host
{
/// Called by ImGuiManager when the cursor enters a text field. The host may choose to open an on-screen
/// keyboard for devices without a physical keyboard.
void BeginTextInput();
/// Called by ImGuiManager when the cursor leaves a text field.
void EndTextInput();
}