Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: ui/base/win/osk_display_manager.cc

Issue 1986153005: The on screen keyboard on Windows 8+ should not obscure the input field. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix more winclang build errors Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/base/win/osk_display_manager.h"
6
7 #include <windows.h>
8 #include <shellapi.h>
9 #include <shlobj.h>
10 #include <shobjidl.h> // Must be before propkey.
11
12 #include "base/bind.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/string_util.h"
17 #include "base/win/registry.h"
18 #include "base/win/scoped_co_mem.h"
19 #include "base/win/win_util.h"
20 #include "base/win/windows_version.h"
21 #include "ui/display/win/dpi.h"
22 #include "ui/gfx/geometry/dip_util.h"
23
24 namespace ui {
25
26 static const int kCheckOSKDelayMs = 1000;
27 static const wchar_t kOSKClassName[] = L"IPTip_Main_Window";
28
29 const wchar_t kWindows8OSKRegPath[] =
30 L"Software\\Classes\\CLSID\\{054AAE20-4BEA-4347-8A35-64A533254A9D}"
31 L"\\LocalServer32";
32
33 // OnScreenKeyboardDetector member definitions.
34 OnScreenKeyboardDetector::OnScreenKeyboardDetector()
35 : main_window_(nullptr),
36 osk_visible_notification_received_(false),
37 keyboard_detector_factory_(this) {
38 }
39
40 OnScreenKeyboardDetector::~OnScreenKeyboardDetector() {
41 }
42
43 void OnScreenKeyboardDetector::DetectKeyboard(HWND main_window) {
44 main_window_ = main_window;
45 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
46 base::Bind(&OnScreenKeyboardDetector::CheckOSKState,
47 keyboard_detector_factory_.GetWeakPtr(), true),
48 base::TimeDelta::FromMilliseconds(kCheckOSKDelayMs));
49 }
50
51 bool OnScreenKeyboardDetector::DismissKeyboard() {
52 // We dismiss the virtual keyboard by generating the ESC keystroke
53 // programmatically.
54 HWND osk = ::FindWindow(kOSKClassName, nullptr);
55 if (::IsWindow(osk) && ::IsWindowEnabled(osk)) {
56 HandleKeyboardHidden();
57 PostMessage(osk, WM_SYSCOMMAND, SC_CLOSE, 0);
58 return true;
59 }
60 return false;
61 }
62
63 void OnScreenKeyboardDetector::AddObserver(
64 OnScreenKeyboardObserver* observer) {
65 observers_.AddObserver(observer);
66 }
67
68 void OnScreenKeyboardDetector::RemoveObserver(
69 OnScreenKeyboardObserver* observer) {
70 observers_.RemoveObserver(observer);
71 }
72
73 void OnScreenKeyboardDetector::CheckOSKState(bool check_for_activation) {
74 HWND osk = ::FindWindow(kOSKClassName, nullptr);
75 if (!::IsWindow(osk))
76 return;
77
78 RECT osk_rect = {};
79 ::GetWindowRect(osk, &osk_rect);
80
81 osk_rect_dip_ = gfx::ConvertRectToDIP(display::win::GetDPIScale(),
82 gfx::Rect(osk_rect));
83
84 if (check_for_activation) {
85 if (::IsWindowVisible(osk) && ::IsWindowEnabled(osk)) {
86 if (!osk_visible_notification_received_)
87 HandleKeyboardVisible();
88 } else {
89 DVLOG(1) << "OSK did not come up in 1 second. Something wrong.";
90 }
91 } else {
92 // Three cases here.
93 // 1. OSK was hidden because the user dismissed it.
94 // 2. We are no longer in the foreground.
95 // 3. The OSK is still visible.
96 // In the first case we just have to notify the observers that the OSK was
97 // hidden.
98 // In the second case we need to dismiss the OSK which internally will
99 // notify the observers about the OSK being hidden.
100 if (!::IsWindowEnabled(osk)) {
101 if (osk_visible_notification_received_) {
102 if (main_window_ == ::GetForegroundWindow()) {
103 DVLOG(1) << "OSK window hidden while we are in the foreground.";
104 HandleKeyboardHidden();
105 }
106 }
107 } else if (main_window_ != ::GetForegroundWindow()) {
108 if (osk_visible_notification_received_) {
109 DVLOG(1) << "We are no longer in the foreground. Dismising OSK.";
110 DismissKeyboard();
111 }
112 } else {
113 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
114 base::Bind(&OnScreenKeyboardDetector::CheckOSKState,
115 keyboard_detector_factory_.GetWeakPtr(), false),
116 base::TimeDelta::FromMilliseconds(kCheckOSKDelayMs));
117 }
118 }
119 }
120
121 void OnScreenKeyboardDetector::HandleKeyboardVisible() {
122 DCHECK(!osk_visible_notification_received_);
123 osk_visible_notification_received_ = true;
124
125 FOR_EACH_OBSERVER(OnScreenKeyboardObserver, observers_,
126 OnKeyboardVisible(osk_rect_dip_));
127
128 // Now that the keyboard is visible, run the task to detect if it was hidden.
129 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
130 base::Bind(&OnScreenKeyboardDetector::CheckOSKState,
131 keyboard_detector_factory_.GetWeakPtr(), false),
132 base::TimeDelta::FromMilliseconds(kCheckOSKDelayMs));
133 }
134
135 void OnScreenKeyboardDetector::HandleKeyboardHidden() {
136 osk_visible_notification_received_ = false;
137 FOR_EACH_OBSERVER(OnScreenKeyboardObserver, observers_,
138 OnKeyboardHidden(osk_rect_dip_));
139 ClearObservers();
140 }
141
142 void OnScreenKeyboardDetector::ClearObservers() {
143 base::ObserverListBase<OnScreenKeyboardObserver>::Iterator iter(&observers_);
144 for (OnScreenKeyboardObserver* observer = iter.GetNext(); observer;
145 observer = iter.GetNext()) {
146 RemoveObserver(observer);
147 }
148 }
149
150 // OnScreenKeyboardDisplayManager member definitions.
151 OnScreenKeyboardDisplayManager::OnScreenKeyboardDisplayManager() {
152 }
153
154 OnScreenKeyboardDisplayManager::~OnScreenKeyboardDisplayManager() {
155 }
156
157 OnScreenKeyboardDisplayManager* OnScreenKeyboardDisplayManager::GetInstance() {
158 return base::Singleton<OnScreenKeyboardDisplayManager,
159 base::LeakySingletonTraits<OnScreenKeyboardDisplayManager>>::get();
160 }
161
162 bool OnScreenKeyboardDisplayManager::DisplayVirtualKeyboard(
163 OnScreenKeyboardObserver* observer) {
164 if (base::win::GetVersion() < base::win::VERSION_WIN8)
165 return false;
166
167 if (base::win::IsKeyboardPresentOnSlate(nullptr))
168 return false;
169
170 if (osk_path_.empty()) {
171 // We need to launch TabTip.exe from the location specified under the
172 // LocalServer32 key for the {{054AAE20-4BEA-4347-8A35-64A533254A9D}}
173 // CLSID.
174 // TabTip.exe is typically found at
175 // c:\program files\common files\microsoft shared\ink on English Windows.
176 // We don't want to launch TabTip.exe from
177 // c:\program files (x86)\common files\microsoft shared\ink. This path is
178 // normally found on 64 bit Windows.
179 base::win::RegKey key(HKEY_LOCAL_MACHINE, kWindows8OSKRegPath,
180 KEY_READ | KEY_WOW64_64KEY);
181 DWORD osk_path_length = 1024;
182 if (key.ReadValue(NULL,
183 base::WriteInto(&osk_path_, osk_path_length),
184 &osk_path_length,
185 NULL) != ERROR_SUCCESS) {
186 DLOG(WARNING) << "Failed to read on screen keyboard path from registry";
187 return false;
188 }
189
190 osk_path_.resize(base::string16::traits_type::length(osk_path_.c_str()));
191
192 osk_path_ = base::ToLowerASCII(osk_path_);
193
194 size_t common_program_files_offset =
195 osk_path_.find(L"%commonprogramfiles%");
196 // Typically the path to TabTip.exe read from the registry will start with
197 // %CommonProgramFiles% which needs to be replaced with the corrsponding
198 // expanded string.
199 // If the path does not begin with %CommonProgramFiles% we use it as is.
200 if (common_program_files_offset != base::string16::npos) {
201 // Preserve the beginning quote in the path.
202 osk_path_.erase(common_program_files_offset,
203 wcslen(L"%commonprogramfiles%"));
204 // The path read from the registry contains the %CommonProgramFiles%
205 // environment variable prefix. On 64 bit Windows the SHGetKnownFolderPath
206 // function returns the common program files path with the X86 suffix for
207 // the FOLDERID_ProgramFilesCommon value.
208 // To get the correct path to TabTip.exe we first read the environment
209 // variable CommonProgramW6432 which points to the desired common
210 // files path. Failing that we fallback to the SHGetKnownFolderPath API.
211
212 // We then replace the %CommonProgramFiles% value with the actual common
213 // files path found in the process.
214 base::string16 common_program_files_path;
215 DWORD buffer_size =
216 GetEnvironmentVariable(L"CommonProgramW6432", nullptr, 0);
217 if (buffer_size) {
218 GetEnvironmentVariable(L"CommonProgramW6432",
219 base::WriteInto(&common_program_files_path,
220 buffer_size),
221 buffer_size);
222 DCHECK(!common_program_files_path.empty());
223 } else {
224 base::win::ScopedCoMem<wchar_t> common_program_files;
225 if (FAILED(SHGetKnownFolderPath(FOLDERID_ProgramFilesCommon,
226 0,
227 nullptr,
228 &common_program_files))) {
229 return false;
230 }
231 common_program_files_path = common_program_files;
232 }
233
234 osk_path_.insert(common_program_files_offset, common_program_files_path);
235 }
236 }
237
238 HINSTANCE ret = ::ShellExecuteW(nullptr,
239 L"",
240 osk_path_.c_str(),
241 nullptr,
242 nullptr,
243 SW_SHOW);
244
245 bool success = reinterpret_cast<intptr_t>(ret) > 32;
246 if (success) {
247 keyboard_detector_.reset(new OnScreenKeyboardDetector);
ncarter (slow) 2016/05/19 17:45:55 Is this the right behavior, if keyboard_detector_
ananta 2016/05/19 19:33:38 In the ideal case we want a list of detectors. How
248 if (observer)
249 keyboard_detector_->AddObserver(observer);
250 keyboard_detector_->DetectKeyboard(::GetForegroundWindow());
251 }
252 return success;
253 }
254
255 bool OnScreenKeyboardDisplayManager::DismissVirtualKeyboard() {
256 if (base::win::GetVersion() < base::win::VERSION_WIN8)
257 return false;
258
259 DCHECK(keyboard_detector_.get());
ncarter (slow) 2016/05/19 17:45:55 It seems like we'll crash if DisplayVirtualKeyboar
260 bool ret = keyboard_detector_->DismissKeyboard();
261 return ret;
262 }
263
264 bool DisplayVirtualKeyboard() {
265 DCHECK(OnScreenKeyboardDisplayManager::GetInstance());
266 return OnScreenKeyboardDisplayManager::
267 GetInstance()->DisplayVirtualKeyboard(nullptr); // !keyboard_observer.
268 }
269
270 bool DismissVirtualKeyboard() {
271 DCHECK(OnScreenKeyboardDisplayManager::GetInstance());
272 return OnScreenKeyboardDisplayManager::
273 GetInstance()->DismissVirtualKeyboard(); // !keyboard_observer.
ncarter (slow) 2016/05/19 17:45:55 This comment seems out of date.
ananta 2016/05/19 19:33:38 Done.
274 }
275
276 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698