UWP: support virtual keyboard

This commit is contained in:
Paul Rouget 2020-07-02 10:38:36 +02:00
parent 19b36bd795
commit 34265c872e
11 changed files with 143 additions and 43 deletions

View file

@ -43,10 +43,17 @@ void on_panic(const char *backtrace) {
throw hresult_error(E_FAIL, char2hstring(backtrace));
}
void on_ime_state_changed(bool aShow) {
sServo->Delegate().OnServoIMEStateChanged(aShow);
void on_ime_show(const char *text, int32_t x, int32_t y, int32_t width,
int32_t height) {
hstring htext = L"";
if (text != nullptr) {
htext = char2hstring(text);
}
sServo->Delegate().OnServoIMEShow(htext, x, y, width, height);
}
void on_ime_hide() { sServo->Delegate().OnServoIMEHide(); }
void set_clipboard_contents(const char *) {
// FIXME
}
@ -256,7 +263,8 @@ Servo::Servo(hstring args, GLsizei width, GLsizei height,
c.on_animating_changed = &on_animating_changed;
c.on_shutdown_complete = &on_shutdown_complete;
c.on_allow_navigation = &on_allow_navigation;
c.on_ime_state_changed = &on_ime_state_changed;
c.on_ime_show = &on_ime_show;
c.on_ime_hide = &on_ime_hide;
c.get_clipboard_contents = &get_clipboard_contents;
c.set_clipboard_contents = &set_clipboard_contents;
c.on_media_session_metadata = &on_media_session_metadata;

View file

@ -109,7 +109,9 @@ public:
virtual void OnServoURLChanged(hstring) = 0;
virtual bool OnServoAllowNavigation(hstring) = 0;
virtual void OnServoAnimatingChanged(bool) = 0;
virtual void OnServoIMEStateChanged(bool) = 0;
virtual void OnServoIMEShow(hstring text, int32_t x, int32_t y, int32_t width,
int32_t height) = 0;
virtual void OnServoIMEHide() = 0;
virtual void OnServoDevtoolsStarted(bool, const unsigned int, hstring) = 0;
virtual void OnServoMediaSessionMetadata(hstring, hstring, hstring) = 0;
virtual void OnServoMediaSessionPlaybackStateChange(int) = 0;

View file

@ -94,26 +94,37 @@ void ServoControl::InitializeTextController() {
auto manager = CoreTextServicesManager::GetForCurrentView();
mEditContext = manager.CreateEditContext();
mEditContext->InputPaneDisplayPolicy(CoreTextInputPaneDisplayPolicy::Manual);
mEditContext->InputScope(CoreTextInputScope::Text);
mEditContext->TextRequested([=](const auto &, const auto &e) {
e.Request().Text(L"");
CoreTextRange sel;
sel.StartCaretPosition = 0;
sel.EndCaretPosition = 0;
e.Request().Range() = sel;
e.Request().Text(*mFocusedInputText);
});
mEditContext->SelectionRequested([=](const auto &, const auto &) {});
mEditContext->LayoutRequested([=](const auto &, const auto &e) {
// Necessary to show the preview
e.Request().LayoutBounds().TextBounds(*mFocusedInputRect);
e.Request().LayoutBounds().ControlBounds(*mFocusedInputRect);
});
mEditContext->TextUpdating([=](const auto &, const auto &e) {
RunOnGLThread([=] {
auto keystr = *hstring2char((e.Text()));
mServo->KeyDown(keystr);
auto text = *hstring2char(e.Text());
size_t size = strlen(text);
for (int i = 0; i < size; i++) {
char letter[2];
memcpy(letter, &text[i], 1);
letter[1] = '\0';
mServo->KeyDown(letter);
mServo->KeyUp(letter);
}
});
e.Result(CoreTextTextUpdatingResult::Succeeded);
});
mEditContext->SelectionRequested([](const auto &, const auto &) {});
GotFocus(
[=](const auto &, const auto &) { mEditContext->NotifyFocusEnter(); });
LostFocus(
[=](const auto &, const auto &) { mEditContext->NotifyFocusLeave(); });
@ -126,6 +137,7 @@ void ServoControl::InitializeTextController() {
});
}
});
PreviewKeyUp([=](const auto &, const auto &e) {
auto keystr = KeyToString(e.Key());
if (keystr.has_value()) {
@ -489,13 +501,24 @@ void ServoControl::OnServoAnimatingChanged(bool animating) {
WakeConditionVariable(&mGLCondVar);
}
void ServoControl::OnServoIMEStateChanged(bool focused) {
void ServoControl::OnServoIMEHide() {
RunOnUIThread([=] { mInputPane->TryHide(); });
}
void ServoControl::OnServoIMEShow(hstring text, int32_t x, int32_t y,
int32_t width, int32_t height) {
RunOnUIThread([=] {
if (focused) {
mInputPane->TryShow();
} else {
mInputPane->TryHide();
}
mEditContext->NotifyFocusEnter();
// FIXME: The simpleservo on_ime_show callback comes with a input method
// type parameter that could be used to set the input scope here.
mEditContext->InputScope(CoreTextInputScope::Text);
// offset of the Servo SwapChainPanel.
auto transform = Panel().TransformToVisual(Window::Current().Content());
auto offset = transform.TransformPoint(Point(0, 0));
mFocusedInputRect =
Rect(x + offset.X, y + offset.Y, (float)width, (float)height);
mFocusedInputText = text;
mInputPane->TryShow();
});
}

View file

@ -173,7 +173,8 @@ struct ServoControl : ServoControlT<ServoControl>, public servo::ServoDelegate {
virtual void OnServoURLChanged(winrt::hstring);
virtual bool OnServoAllowNavigation(winrt::hstring);
virtual void OnServoAnimatingChanged(bool);
virtual void OnServoIMEStateChanged(bool);
virtual void OnServoIMEHide();
virtual void OnServoIMEShow(hstring text, int32_t, int32_t, int32_t, int32_t);
virtual void OnServoMediaSessionMetadata(winrt::hstring, winrt::hstring,
winrt::hstring);
virtual void OnServoMediaSessionPlaybackStateChange(int);
@ -281,6 +282,9 @@ private:
std::optional<Windows::UI::Text::Core::CoreTextEditContext> mEditContext;
std::optional<Windows::UI::ViewManagement::InputPane> mInputPane;
std::optional<Windows::Foundation::Rect> mFocusedInputRect;
std::optional<hstring> mFocusedInputText;
};
} // namespace winrt::ServoApp::implementation