1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sts=2 sw=2 et cin: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #define NTDDI_VERSION NTDDI_WIN10_RS1
9 #include "OSKInputPaneManager.h"
13 # include <inputpaneinterop.h>
14 # include <windows.ui.viewmanagement.h>
17 using namespace ABI::Windows::UI::ViewManagement
;
18 using namespace Microsoft::WRL
;
19 using namespace Microsoft::WRL::Wrappers
;
26 static ComPtr
<IInputPane2
> GetInputPane(HWND aHwnd
) {
27 ComPtr
<IInputPaneInterop
> inputPaneInterop
;
28 HRESULT hr
= RoGetActivationFactory(
29 HStringReference(RuntimeClass_Windows_UI_ViewManagement_InputPane
).Get(),
30 IID_PPV_ARGS(&inputPaneInterop
));
31 if (NS_WARN_IF(FAILED(hr
))) {
35 ComPtr
<IInputPane
> inputPane
;
36 hr
= inputPaneInterop
->GetForWindow(aHwnd
, IID_PPV_ARGS(&inputPane
));
37 if (NS_WARN_IF(FAILED(hr
))) {
41 ComPtr
<IInputPane2
> inputPane2
;
42 inputPane
.As(&inputPane2
);
47 static bool IsInputPaneVisible(ComPtr
<IInputPane2
>& aInputPane2
) {
48 ComPtr
<IInputPaneControl
> inputPaneControl
;
49 aInputPane2
.As(&inputPaneControl
);
50 if (NS_WARN_IF(!inputPaneControl
)) {
53 boolean visible
= false;
54 inputPaneControl
->get_Visible(&visible
);
58 static bool IsForegroundApp() {
59 HWND foregroundWnd
= ::GetForegroundWindow();
64 ::GetWindowThreadProcessId(foregroundWnd
, &pid
);
65 return pid
== ::GetCurrentProcessId();
71 void OSKInputPaneManager::ShowOnScreenKeyboard(HWND aWnd
) {
73 ComPtr
<IInputPane2
> inputPane2
= GetInputPane(aWnd
);
78 inputPane2
->TryShow(&result
);
80 result
|| !IsForegroundApp() || IsInputPaneVisible(inputPane2
),
81 "IInputPane2::TryShow is failure");
86 void OSKInputPaneManager::DismissOnScreenKeyboard(HWND aWnd
) {
88 ComPtr
<IInputPane2
> inputPane2
= GetInputPane(aWnd
);
93 inputPane2
->TryHide(&result
);
95 result
|| !IsForegroundApp() || !IsInputPaneVisible(inputPane2
),
96 "IInputPane2::TryHide is failure");
100 } // namespace widget
101 } // namespace mozilla