text: Make enter add a new line in multiline fields

This commit is contained in:
Kamil Jarosz 2024-02-03 10:58:07 +01:00 committed by Nathan Adams
parent b02a96373b
commit 50b5f29c0a
8 changed files with 57 additions and 2 deletions

View File

@ -243,6 +243,9 @@ fn get_line_data(layout: &[LayoutBox]) -> Vec<LineData> {
}
impl<'gc> EditText<'gc> {
// This seems to be OS-independent
const INPUT_NEWLINE: char = '\r';
/// Creates a new `EditText` from an SWF `DefineEditText` tag.
pub fn from_swf_tag(
context: &mut UpdateContext<'_, 'gc>,
@ -1370,6 +1373,9 @@ impl<'gc> EditText<'gc> {
let mut changed = false;
let is_selectable = self.is_selectable();
match control_code {
TextControlCode::Enter => {
self.text_input(Self::INPUT_NEWLINE, context);
}
TextControlCode::MoveLeft
| TextControlCode::MoveLeftWord
| TextControlCode::MoveLeftLine
@ -1529,7 +1535,6 @@ impl<'gc> EditText<'gc> {
changed = true;
}
}
_ => {}
}
if changed {
let mut activation = Avm1Activation::from_nothing(
@ -1645,12 +1650,16 @@ impl<'gc> EditText<'gc> {
pub fn text_input(self, character: char, context: &mut UpdateContext<'_, 'gc>) {
if self.0.read().flags.contains(EditTextFlag::READ_ONLY)
|| character.is_control()
|| (character.is_control() && character != Self::INPUT_NEWLINE)
|| self.available_chars() == 0
{
return;
}
if !self.is_multiline() && character == Self::INPUT_NEWLINE {
return;
}
let Some(selection) = self.selection() else {
return;
};

View File

@ -19,6 +19,7 @@ pub fn winit_to_ruffle_text_control(
let ctrl_cmd = modifiers.state().control_key()
|| (modifiers.state().super_key() && cfg!(target_os = "macos"));
match event.logical_key.as_ref() {
Key::Named(NamedKey::Enter) => Some(TextControlCode::Enter),
Key::Character("a") if ctrl_cmd => Some(TextControlCode::SelectAll),
Key::Character("c") if ctrl_cmd => Some(TextControlCode::Copy),
Key::Character("v") if ctrl_cmd => Some(TextControlCode::Paste),

View File

@ -0,0 +1,10 @@
[
{ "type": "TextInput", "codepoint": "a" },
{ "type": "TextControl", "code": "Enter" },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "KeyDown", "key_code": 27 },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "TextControl", "code": "Enter" },
{ "type": "TextInput", "codepoint": "a" },
{ "type": "KeyDown", "key_code": 27 }
]

View File

@ -0,0 +1,9 @@
text.onChanged: a
text.onChanged: aa
text: aa
text multiline:
textMultiline.onChanged: a
textMultiline.onChanged: a\r
textMultiline.onChanged: a\ra
text: aa
text multiline: a\ra

View File

@ -0,0 +1,24 @@
function escapeNewlines(text) {
return text
.split("\r").join("\\r")
.split("\n").join("\\n");
}
var listener = new Object();
listener.onKeyDown = function() {
if (Key.getCode() == 27) {
trace("text: " + escapeNewlines(text.text));
trace("text multiline: " + escapeNewlines(textMultiline.text));
Selection.setFocus(textMultiline);
}
};
Key.addListener(listener);
text.onChanged = function () {
trace("text.onChanged: " + escapeNewlines(text.text));
}
textMultiline.onChanged = function () {
trace("textMultiline.onChanged: " + escapeNewlines(textMultiline.text));
}
Selection.setFocus(text);

Binary file not shown.

View File

@ -0,0 +1 @@
num_frames = 1

View File

@ -1956,6 +1956,7 @@ pub fn web_to_ruffle_text_control(
}
} else {
match key {
"Enter" => Some(TextControlCode::Enter),
"Delete" if ctrl_key => Some(TextControlCode::DeleteWord),
"Delete" => Some(TextControlCode::Delete),
"Backspace" if ctrl_key => Some(TextControlCode::BackspaceWord),