Auto merge of #28308 - philip-lamb:phil-ime-textentry, r=jdm

Improve IME messaging to embedder with insertion point index and mult…

…iline flag.

<!-- Please describe your changes on the following line: -->
This improves handling of IME requests in the embedder by passing the location of the insertion point along with the current text, and a boolean flag 'multiline' (true for HTML textarea, false otherwise) which allows the embedder to be more clever about handling of the 'enter' or 'return' keys.

Tested and working in an embedding example.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [x] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
bors-servo 2021-03-29 23:10:17 -04:00 committed by GitHub
commit 94e337f22f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 16 deletions

View file

@ -197,7 +197,10 @@ pub enum EmbedderMsg {
/// Open interface to request permission specified by prompt.
PromptPermission(PermissionPrompt, IpcSender<PermissionRequest>),
/// Request to present an IME to the user when an editable element is focused.
ShowIME(InputMethodType, Option<String>, DeviceIntRect),
/// If the input is text, the second parameter defines the pre-existing string
/// text content and the zero-based index into the string locating the insertion point.
/// bool is true for multi-line and false otherwise.
ShowIME(InputMethodType, Option<(String, i32)>, bool, DeviceIntRect),
/// Request to hide the IME when the editable element is blurred.
HideIME,
/// Servo has shut down

View file

@ -1122,16 +1122,29 @@ impl Document {
Point2D::new(rect.origin.x.to_px(), rect.origin.y.to_px()),
Size2D::new(rect.size.width.to_px(), rect.size.height.to_px()),
);
let text = if let Some(input) = elem.downcast::<HTMLInputElement>() {
Some((&input.Value()).to_string())
let (text, multiline) = if let Some(input) = elem.downcast::<HTMLInputElement>() {
(
Some((
(&input.Value()).to_string(),
input.GetSelectionEnd().unwrap_or(0) as i32,
)),
false,
)
} else if let Some(textarea) = elem.downcast::<HTMLTextAreaElement>() {
Some((&textarea.Value()).to_string())
(
Some((
(&textarea.Value()).to_string(),
textarea.GetSelectionEnd().unwrap_or(0) as i32,
)),
true,
)
} else {
None
(None, false)
};
self.send_to_embedder(EmbedderMsg::ShowIME(
kind,
text,
multiline,
DeviceIntRect::from_untyped(&rect),
));
}