Skip to content

Commit ee95692

Browse files
n0samuDinnerbone
authored andcommitted
core: Fix TextField.maxChars handling and address other reviews
1 parent 4e579b7 commit ee95692

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

core/src/display_object/edit_text.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,19 +1174,21 @@ impl<'gc> EditText<'gc> {
11741174
None
11751175
}
11761176

1177+
/// The number of characters that currently can be inserted, considering `TextField.maxChars`
1178+
/// constraint, current text length, and current text selection length.
11771179
fn available_chars(self) -> usize {
11781180
let read = self.0.read();
11791181
let max_chars = read.max_chars;
11801182
if max_chars == 0 {
11811183
usize::MAX
11821184
} else {
1183-
let text_len = read.text_spans.text().len();
1185+
let text_len = read.text_spans.text().len() as i32;
11841186
let selection_len = if let Some(selection) = self.selection() {
1185-
selection.end() - selection.start()
1187+
(selection.end() - selection.start()) as i32
11861188
} else {
11871189
0
11881190
};
1189-
max_chars.max(0) as usize - (text_len - selection_len)
1191+
0.max(max_chars.max(0) - (text_len - selection_len)) as usize
11901192
}
11911193
}
11921194

core/src/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<'gc> ClipEvent<'gc> {
336336
/// Control inputs to a text field
337337
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
338338
pub enum TextControlCode {
339-
// TODO: Extend this
339+
// TODO: Add control codes for Ctrl+Arrows and Home/End keys
340340
MoveLeft,
341341
MoveRight,
342342
SelectLeft,

desktop/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ fn winit_key_to_char(key_code: VirtualKeyCode, is_shift_down: bool) -> Option<ch
915915

916916
/// Converts a `VirtualKeyCode` and `ModifiersState` to a Ruffle `TextControlCode`.
917917
/// Returns `None` if there is no match.
918+
/// TODO: Handle Ctrl+Arrows and Home/End keys
918919
fn winit_to_ruffle_text_control(
919920
key: VirtualKeyCode,
920921
modifiers: ModifiersState,

web/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -843,12 +843,12 @@ impl Ruffle {
843843
let is_ctrl_cmd = js_event.ctrl_key() || js_event.meta_key();
844844
core.handle_event(PlayerEvent::KeyDown { key_code, key_char });
845845

846-
if let Some(control_code) = web_to_text_control(
846+
if let Some(control_code) = web_to_ruffle_text_control(
847847
&js_event.key(),
848848
is_ctrl_cmd,
849849
js_event.shift_key(),
850850
) {
851-
core.handle_event(PlayerEvent::TextControl { code: control_code })
851+
core.handle_event(PlayerEvent::TextControl { code: control_code });
852852
} else if let Some(codepoint) = key_char {
853853
core.handle_event(PlayerEvent::TextInput { codepoint });
854854
}
@@ -1748,14 +1748,20 @@ fn web_key_to_codepoint(key: &str) -> Option<char> {
17481748
}
17491749
}
17501750

1751-
pub fn web_to_text_control(key: &str, ctrl_key: bool, shift_key: bool) -> Option<TextControlCode> {
1751+
/// Convert a web `KeyboardEvent.key` value to a Ruffle `TextControlCode`,
1752+
/// given the states of the modifier keys. Return `None` if there is no match.
1753+
/// TODO: Handle Ctrl+Arrows and Home/End keys
1754+
pub fn web_to_ruffle_text_control(
1755+
key: &str,
1756+
ctrl_key: bool,
1757+
shift_key: bool,
1758+
) -> Option<TextControlCode> {
17521759
let mut chars = key.chars();
17531760
let (c1, c2) = (chars.next(), chars.next());
17541761
if c2.is_none() {
17551762
// Single character.
17561763
if ctrl_key {
17571764
match c1 {
1758-
// TODO: Extend this
17591765
Some('a') => Some(TextControlCode::SelectAll),
17601766
Some('c') => Some(TextControlCode::Copy),
17611767
Some('v') => Some(TextControlCode::Paste),

0 commit comments

Comments
 (0)