Bug 1826326 [wpt PR 39360] - Get rid of ResourceWarnings produced by the tests, a...
[gecko.git] / widget / windows / OSKInputPaneManager.cpp
blob293a84cd28c8008898978ba6de719c863f5c48c4
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"
10 #include "nsDebug.h"
12 #ifndef __MINGW32__
13 # include <inputpaneinterop.h>
14 # include <windows.ui.viewmanagement.h>
15 # include <wrl.h>
17 using namespace ABI::Windows::UI::ViewManagement;
18 using namespace Microsoft::WRL;
19 using namespace Microsoft::WRL::Wrappers;
20 #endif
22 namespace mozilla {
23 namespace widget {
25 #ifndef __MINGW32__
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))) {
32 return nullptr;
35 ComPtr<IInputPane> inputPane;
36 hr = inputPaneInterop->GetForWindow(aHwnd, IID_PPV_ARGS(&inputPane));
37 if (NS_WARN_IF(FAILED(hr))) {
38 return nullptr;
41 ComPtr<IInputPane2> inputPane2;
42 inputPane.As(&inputPane2);
43 return inputPane2;
46 # ifdef DEBUG
47 static bool IsInputPaneVisible(ComPtr<IInputPane2>& aInputPane2) {
48 ComPtr<IInputPaneControl> inputPaneControl;
49 aInputPane2.As(&inputPaneControl);
50 if (NS_WARN_IF(!inputPaneControl)) {
51 return false;
53 boolean visible = false;
54 inputPaneControl->get_Visible(&visible);
55 return visible;
58 static bool IsForegroundApp() {
59 HWND foregroundWnd = ::GetForegroundWindow();
60 if (!foregroundWnd) {
61 return false;
63 DWORD pid;
64 ::GetWindowThreadProcessId(foregroundWnd, &pid);
65 return pid == ::GetCurrentProcessId();
67 # endif
68 #endif
70 // static
71 void OSKInputPaneManager::ShowOnScreenKeyboard(HWND aWnd) {
72 #ifndef __MINGW32__
73 ComPtr<IInputPane2> inputPane2 = GetInputPane(aWnd);
74 if (!inputPane2) {
75 return;
77 boolean result;
78 inputPane2->TryShow(&result);
79 NS_WARNING_ASSERTION(
80 result || !IsForegroundApp() || IsInputPaneVisible(inputPane2),
81 "IInputPane2::TryShow is failure");
82 #endif
85 // static
86 void OSKInputPaneManager::DismissOnScreenKeyboard(HWND aWnd) {
87 #ifndef __MINGW32__
88 ComPtr<IInputPane2> inputPane2 = GetInputPane(aWnd);
89 if (!inputPane2) {
90 return;
92 boolean result;
93 inputPane2->TryHide(&result);
94 NS_WARNING_ASSERTION(
95 result || !IsForegroundApp() || !IsInputPaneVisible(inputPane2),
96 "IInputPane2::TryHide is failure");
97 #endif
100 } // namespace widget
101 } // namespace mozilla