mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
This updates the Win32 desktop embedder to support input method (abbreviated IM or IME) composing regions. In contrast to languages such as English, where keyboard input is managed keystroke-by-keystroke, languages such as Japanese require a multi-step input process wherein the user begins a composing sequence, during which point their keystrokes are captured by a system input method and converted into a text sequence. During composing, the user is able to edit the composing range and manage the conversion from keyboard input to text before eventually committing the text to the underlying text input field. To illustrate this, in Japanese, this sequence might look something like the following: 1. User types 'k'. The character 'k' is added to the composing region. Typically, the text 'k' will be inserted inline into the underlying text field but the composing range will be highlighted in some manner, frequently with a highlight or underline. 2. User types 'a'. The composing range is replaced with the phonetic kana character 'か' (ka). The composing range continues to be highlighted. 3. User types 'k'. The character 'k' is appended to the composing range such that the highlighted text is now 'かk' 4. User types 'u'. The trailing 'k' is replaced with the phonetic kana character 'く' (ku) such that the composing range now reads 'かく' The composing range continues to be highlighted. 5. The user presses the space bar to convert the kana characters to kanji. The composing range is replaced with '書く' (kaku: to write). 6. The user presses the space bar again to show other conversions. The user's configured input method (for example, ibus) pops up a completions menu populated with alternatives such as 各 (kaku: every), 描く (kaku: to draw), 核 (kaku: pit of a fruit, nucleus), 角 (kaku: angle), etc. 7. The user uses the arrow keys to navigate the completions menu and select the alternative to input. As they do, the inline composing region in the text field is updated. It continues to be highlighted or underlined. 8. The user hits enter to commit the composing region. The text is committed to the underlying text field and the visual highlighting is removed. 9. If the user presses another key, a new composing sequence begins. If a selection is present when composing begins, it is preserved until the first keypress of input is received, at which point the selection is deleted. If a composing sequence is aborted before the first keypress, the selection is preserved. Creating a new selection (with the mouse, for example) aborts composing and the composing region is automatically committed. A composing range and selection, both with an extent, are not permitted to co-exist. During composing, keyboard navigation via the arrow keys, or home and end (or equivalent shortcuts) is restricted to the composing range, as are deletions via backspace and the delete key. This patch adds two new private convenience methods, `editing_range` and `text_range`. The former returns the range for which editing is currently active -- the composing range, if composing, otherwise the full range of the text. The latter, returns a range from position 0 (inclusive) to `text_.length()` exclusive. Windows IME support revolves around two main UI windows: the composition window and the candidate window. The composition window is a system window overlaid within the current window bounds which renders the composing string. Flutter already renders this string itself, so we request that this window be hidden. The candidate window is a system-rendered dropdown that displays all possible conversions for the text in the composing region. Since the contents of this window are specific to the particular IME in use, and because the user may have installed one or more third-party IMEs, Flutter does not attempt to render this as a widget itself, but rather delegates to the system-rendered window. The lifecycle of IME composing begins follows the following event order: 1. WM_IME_SETCONTEXT: on window creation this event is received. We strip the ISC_SHOWUICOMPOSITIONWINDOW bit from the event lparam before passing it to DefWindowProc() in order to hide the composition window, which Flutter already renders itself. 2. WM_IME_STARTCOMPOSITION: triggered whenever the user begins inputting new text. We use this event to set Flutter's TextInputModel into composing mode. 3. WM_IME_COMPOSITION: triggered on each keypress as the user adds, replaces, or deletes text in the composing region, navigates with their cursor within the composing region, or selects a new conversion candidate from the candidates list. 4. WM_IME_ENDCOMPOSITION: triggered when the user has finished editing the text in the composing region and decides to commit or abort the composition. Additionally, the following IME-related events are emitted but not yet handled: * WM_INPUTLANGCHANGE: triggered whenever the user selects a new language using the system language selection menu. Since there some language-specific behaviours to IMEs, we may want to make use of this in the future. * WM_IME_NOTIFY: triggered to notify of various status events such as opening or closing the candidate window, setting the conversion mode, etc. None of these are relevant to Flutter at the moment. * WM_IME_REQUEST: triggered to notify of various commands/requests such as triggering reconversion of text, which should begin composition mode, insert the selected text into the composing region, and allow the user to select new alternative candidates for the text in question before re-committing their new selection. This patch doesn't support this feature, but it's an important feature that we should support in future.
201 lines
6.1 KiB
C++
201 lines
6.1 KiB
C++
// Copyright 2013 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "flutter/shell/platform/windows/win32_flutter_window.h"
|
|
|
|
#include <dwmapi.h>
|
|
#include <chrono>
|
|
#include <map>
|
|
|
|
namespace flutter {
|
|
|
|
namespace {
|
|
|
|
// The Windows DPI system is based on this
|
|
// constant for machines running at 100% scaling.
|
|
constexpr int base_dpi = 96;
|
|
|
|
// TODO: See if this can be queried from the OS; this value is chosen
|
|
// arbitrarily to get something that feels reasonable.
|
|
constexpr int kScrollOffsetMultiplier = 20;
|
|
|
|
// Maps a Flutter cursor name to an HCURSOR.
|
|
//
|
|
// Returns the arrow cursor for unknown constants.
|
|
//
|
|
// This map must be kept in sync with Flutter framework's
|
|
// rendering/mouse_cursor.dart.
|
|
static HCURSOR GetCursorByName(const std::string& cursor_name) {
|
|
static auto* cursors = new std::map<std::string, const wchar_t*>{
|
|
{"allScroll", IDC_SIZEALL},
|
|
{"basic", IDC_ARROW},
|
|
{"click", IDC_HAND},
|
|
{"forbidden", IDC_NO},
|
|
{"help", IDC_HELP},
|
|
{"move", IDC_SIZEALL},
|
|
{"none", nullptr},
|
|
{"noDrop", IDC_NO},
|
|
{"precise", IDC_CROSS},
|
|
{"progress", IDC_APPSTARTING},
|
|
{"text", IDC_IBEAM},
|
|
{"resizeColumn", IDC_SIZEWE},
|
|
{"resizeDown", IDC_SIZENS},
|
|
{"resizeDownLeft", IDC_SIZENESW},
|
|
{"resizeDownRight", IDC_SIZENWSE},
|
|
{"resizeLeft", IDC_SIZEWE},
|
|
{"resizeLeftRight", IDC_SIZEWE},
|
|
{"resizeRight", IDC_SIZEWE},
|
|
{"resizeRow", IDC_SIZENS},
|
|
{"resizeUp", IDC_SIZENS},
|
|
{"resizeUpDown", IDC_SIZENS},
|
|
{"resizeUpLeft", IDC_SIZENWSE},
|
|
{"resizeUpRight", IDC_SIZENESW},
|
|
{"resizeUpLeftDownRight", IDC_SIZENWSE},
|
|
{"resizeUpRightDownLeft", IDC_SIZENESW},
|
|
{"wait", IDC_WAIT},
|
|
};
|
|
const wchar_t* idc_name = IDC_ARROW;
|
|
auto it = cursors->find(cursor_name);
|
|
if (it != cursors->end()) {
|
|
idc_name = it->second;
|
|
}
|
|
return ::LoadCursor(nullptr, idc_name);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Win32FlutterWindow::Win32FlutterWindow(int width, int height)
|
|
: binding_handler_delegate_(nullptr) {
|
|
Win32Window::InitializeChild("FLUTTERVIEW", width, height);
|
|
current_cursor_ = ::LoadCursor(nullptr, IDC_ARROW);
|
|
}
|
|
|
|
Win32FlutterWindow::~Win32FlutterWindow() {}
|
|
|
|
void Win32FlutterWindow::SetView(WindowBindingHandlerDelegate* window) {
|
|
binding_handler_delegate_ = window;
|
|
}
|
|
|
|
WindowsRenderTarget Win32FlutterWindow::GetRenderTarget() {
|
|
return WindowsRenderTarget(GetWindowHandle());
|
|
}
|
|
|
|
float Win32FlutterWindow::GetDpiScale() {
|
|
return static_cast<float>(GetCurrentDPI()) / static_cast<float>(base_dpi);
|
|
}
|
|
|
|
PhysicalWindowBounds Win32FlutterWindow::GetPhysicalWindowBounds() {
|
|
return {GetCurrentWidth(), GetCurrentHeight()};
|
|
}
|
|
|
|
void Win32FlutterWindow::UpdateFlutterCursor(const std::string& cursor_name) {
|
|
current_cursor_ = GetCursorByName(cursor_name);
|
|
}
|
|
|
|
void Win32FlutterWindow::OnWindowResized() {
|
|
// Blocking the raster thread until DWM flushes alleviates glitches where
|
|
// previous size surface is stretched over current size view.
|
|
DwmFlush();
|
|
}
|
|
|
|
// Translates button codes from Win32 API to FlutterPointerMouseButtons.
|
|
static uint64_t ConvertWinButtonToFlutterButton(UINT button) {
|
|
switch (button) {
|
|
case WM_LBUTTONDOWN:
|
|
case WM_LBUTTONUP:
|
|
return kFlutterPointerButtonMousePrimary;
|
|
case WM_RBUTTONDOWN:
|
|
case WM_RBUTTONUP:
|
|
return kFlutterPointerButtonMouseSecondary;
|
|
case WM_MBUTTONDOWN:
|
|
case WM_MBUTTONUP:
|
|
return kFlutterPointerButtonMouseMiddle;
|
|
case XBUTTON1:
|
|
return kFlutterPointerButtonMouseBack;
|
|
case XBUTTON2:
|
|
return kFlutterPointerButtonMouseForward;
|
|
}
|
|
std::cerr << "Mouse button not recognized: " << button << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
void Win32FlutterWindow::OnDpiScale(unsigned int dpi){};
|
|
|
|
// When DesktopWindow notifies that a WM_Size message has come in
|
|
// lets FlutterEngine know about the new size.
|
|
void Win32FlutterWindow::OnResize(unsigned int width, unsigned int height) {
|
|
if (binding_handler_delegate_ != nullptr) {
|
|
binding_handler_delegate_->OnWindowSizeChanged(width, height);
|
|
}
|
|
}
|
|
|
|
void Win32FlutterWindow::OnPointerMove(double x, double y) {
|
|
binding_handler_delegate_->OnPointerMove(x, y);
|
|
}
|
|
|
|
void Win32FlutterWindow::OnPointerDown(double x, double y, UINT button) {
|
|
uint64_t flutter_button = ConvertWinButtonToFlutterButton(button);
|
|
if (flutter_button != 0) {
|
|
binding_handler_delegate_->OnPointerDown(
|
|
x, y, static_cast<FlutterPointerMouseButtons>(flutter_button));
|
|
}
|
|
}
|
|
|
|
void Win32FlutterWindow::OnPointerUp(double x, double y, UINT button) {
|
|
uint64_t flutter_button = ConvertWinButtonToFlutterButton(button);
|
|
if (flutter_button != 0) {
|
|
binding_handler_delegate_->OnPointerUp(
|
|
x, y, static_cast<FlutterPointerMouseButtons>(flutter_button));
|
|
}
|
|
}
|
|
|
|
void Win32FlutterWindow::OnPointerLeave() {
|
|
binding_handler_delegate_->OnPointerLeave();
|
|
}
|
|
|
|
void Win32FlutterWindow::OnSetCursor() {
|
|
::SetCursor(current_cursor_);
|
|
}
|
|
|
|
void Win32FlutterWindow::OnText(const std::u16string& text) {
|
|
binding_handler_delegate_->OnText(text);
|
|
}
|
|
|
|
bool Win32FlutterWindow::OnKey(int key,
|
|
int scancode,
|
|
int action,
|
|
char32_t character,
|
|
bool extended) {
|
|
return binding_handler_delegate_->OnKey(key, scancode, action, character,
|
|
extended);
|
|
}
|
|
|
|
void Win32FlutterWindow::OnComposeBegin() {
|
|
binding_handler_delegate_->OnComposeBegin();
|
|
}
|
|
|
|
void Win32FlutterWindow::OnComposeEnd() {
|
|
binding_handler_delegate_->OnComposeEnd();
|
|
}
|
|
|
|
void Win32FlutterWindow::OnComposeChange(const std::u16string& text,
|
|
int cursor_pos) {
|
|
binding_handler_delegate_->OnComposeChange(text, cursor_pos);
|
|
}
|
|
|
|
void Win32FlutterWindow::OnScroll(double delta_x, double delta_y) {
|
|
POINT point;
|
|
GetCursorPos(&point);
|
|
|
|
ScreenToClient(GetWindowHandle(), &point);
|
|
binding_handler_delegate_->OnScroll(point.x, point.y, delta_x, delta_y,
|
|
kScrollOffsetMultiplier);
|
|
}
|
|
|
|
void Win32FlutterWindow::UpdateCursorRect(const Rect& rect) {
|
|
text_input_manager_.UpdateCaretRect(rect);
|
|
}
|
|
|
|
} // namespace flutter
|