@@ -17,7 +17,7 @@ use crate::display_object::interactive::{
17
17
} ;
18
18
use crate :: display_object:: { DisplayObjectBase , DisplayObjectPtr , TDisplayObject } ;
19
19
use crate :: drawing:: Drawing ;
20
- use crate :: events:: { ButtonKeyCode , ClipEvent , ClipEventResult , KeyCode , TextControlCode } ;
20
+ use crate :: events:: { ClipEvent , ClipEventResult , TextControlCode } ;
21
21
use crate :: font:: { round_down_to_pixel, Glyph , TextRenderSettings } ;
22
22
use crate :: html:: { BoxBounds , FormatSpans , LayoutBox , LayoutContent , LayoutMetrics , TextFormat } ;
23
23
use crate :: prelude:: * ;
@@ -1203,6 +1203,46 @@ impl<'gc> EditText<'gc> {
1203
1203
let mut changed = false ;
1204
1204
let is_selectable = self . is_selectable ( ) ;
1205
1205
match control_code {
1206
+ TextControlCode :: MoveLeft => {
1207
+ let new_pos = if selection. is_caret ( ) && selection. to > 0 {
1208
+ string_utils:: prev_char_boundary ( & self . text ( ) , selection. to )
1209
+ } else {
1210
+ selection. start ( )
1211
+ } ;
1212
+ self . set_selection (
1213
+ Some ( TextSelection :: for_position ( new_pos) ) ,
1214
+ context. gc_context ,
1215
+ ) ;
1216
+ }
1217
+ TextControlCode :: MoveRight => {
1218
+ let new_pos = if selection. is_caret ( ) && selection. to < self . text ( ) . len ( ) {
1219
+ string_utils:: next_char_boundary ( & self . text ( ) , selection. to )
1220
+ } else {
1221
+ selection. end ( )
1222
+ } ;
1223
+ self . set_selection (
1224
+ Some ( TextSelection :: for_position ( new_pos) ) ,
1225
+ context. gc_context ,
1226
+ ) ;
1227
+ }
1228
+ TextControlCode :: SelectLeft => {
1229
+ if is_selectable && selection. to > 0 {
1230
+ let new_pos = string_utils:: prev_char_boundary ( & self . text ( ) , selection. to ) ;
1231
+ self . set_selection (
1232
+ Some ( TextSelection :: for_range ( selection. from , new_pos) ) ,
1233
+ context. gc_context ,
1234
+ ) ;
1235
+ }
1236
+ }
1237
+ TextControlCode :: SelectRight => {
1238
+ if is_selectable && selection. to < self . text ( ) . len ( ) {
1239
+ let new_pos = string_utils:: next_char_boundary ( & self . text ( ) , selection. to ) ;
1240
+ self . set_selection (
1241
+ Some ( TextSelection :: for_range ( selection. from , new_pos) ) ,
1242
+ context. gc_context ,
1243
+ )
1244
+ }
1245
+ }
1206
1246
TextControlCode :: SelectAll => {
1207
1247
if is_selectable {
1208
1248
self . set_selection (
@@ -1222,11 +1262,16 @@ impl<'gc> EditText<'gc> {
1222
1262
// TODO: To match Flash Player, we should truncate pasted text that is longer than max_chars
1223
1263
// instead of canceling the paste action entirely
1224
1264
if text. len ( ) <= self . available_chars ( ) {
1225
- self . replace_text ( selection. start ( ) , selection. end ( ) , & WString :: from_utf8 ( text) , context) ;
1226
- let new_start = selection. start ( ) + text. len ( ) ;
1265
+ self . replace_text (
1266
+ selection. start ( ) ,
1267
+ selection. end ( ) ,
1268
+ & WString :: from_utf8 ( text) ,
1269
+ context,
1270
+ ) ;
1271
+ let new_pos = selection. start ( ) + text. len ( ) ;
1227
1272
if is_selectable {
1228
1273
self . set_selection (
1229
- Some ( TextSelection :: for_position ( new_start ) ) ,
1274
+ Some ( TextSelection :: for_position ( new_pos ) ) ,
1230
1275
context. gc_context ,
1231
1276
) ;
1232
1277
} else {
@@ -1243,7 +1288,12 @@ impl<'gc> EditText<'gc> {
1243
1288
let text = & self . text ( ) [ selection. start ( ) ..selection. end ( ) ] ;
1244
1289
context. ui . set_clipboard_content ( text. to_string ( ) ) ;
1245
1290
1246
- self . replace_text ( selection. start ( ) , selection. end ( ) , WStr :: empty ( ) , context) ;
1291
+ self . replace_text (
1292
+ selection. start ( ) ,
1293
+ selection. end ( ) ,
1294
+ WStr :: empty ( ) ,
1295
+ context,
1296
+ ) ;
1247
1297
if is_selectable {
1248
1298
self . set_selection (
1249
1299
Some ( TextSelection :: for_position ( selection. start ( ) ) ) ,
@@ -1322,9 +1372,9 @@ impl<'gc> EditText<'gc> {
1322
1372
& WString :: from_char ( character) ,
1323
1373
context,
1324
1374
) ;
1325
- let new_start = selection. start ( ) + character. len_utf8 ( ) ;
1375
+ let new_pos = selection. start ( ) + character. len_utf8 ( ) ;
1326
1376
self . set_selection (
1327
- Some ( TextSelection :: for_position ( new_start ) ) ,
1377
+ Some ( TextSelection :: for_position ( new_pos ) ) ,
1328
1378
context. gc_context ,
1329
1379
) ;
1330
1380
changed = true ;
@@ -1345,61 +1395,6 @@ impl<'gc> EditText<'gc> {
1345
1395
}
1346
1396
}
1347
1397
1348
- /// Listens for keyboard text control commands.
1349
- ///
1350
- /// TODO: Add explicit text control events (#4452).
1351
- pub fn handle_text_control_event (
1352
- self ,
1353
- context : & mut UpdateContext < ' _ , ' gc > ,
1354
- event : ClipEvent ,
1355
- ) -> ClipEventResult {
1356
- if let ClipEvent :: KeyPress { key_code } = event {
1357
- let mut edit_text = self . 0 . write ( context. gc_context ) ;
1358
- let selection = edit_text. selection ;
1359
- if let Some ( mut selection) = selection {
1360
- let text = edit_text. text_spans . text ( ) ;
1361
- let length = text. len ( ) ;
1362
- match key_code {
1363
- ButtonKeyCode :: Left => {
1364
- if ( context. input . is_key_down ( KeyCode :: Shift ) || selection. is_caret ( ) )
1365
- && selection. to > 0
1366
- {
1367
- selection. to = string_utils:: prev_char_boundary ( text, selection. to ) ;
1368
- if !context. input . is_key_down ( KeyCode :: Shift ) {
1369
- selection. from = selection. to ;
1370
- }
1371
- } else if !context. input . is_key_down ( KeyCode :: Shift ) {
1372
- selection. to = selection. start ( ) ;
1373
- selection. from = selection. to ;
1374
- }
1375
- selection. clamp ( length) ;
1376
- edit_text. selection = Some ( selection) ;
1377
- return ClipEventResult :: Handled ;
1378
- }
1379
- ButtonKeyCode :: Right => {
1380
- if ( context. input . is_key_down ( KeyCode :: Shift ) || selection. is_caret ( ) )
1381
- && selection. to < length
1382
- {
1383
- selection. to = string_utils:: next_char_boundary ( text, selection. to ) ;
1384
- if !context. input . is_key_down ( KeyCode :: Shift ) {
1385
- selection. from = selection. to ;
1386
- }
1387
- } else if !context. input . is_key_down ( KeyCode :: Shift ) {
1388
- selection. to = selection. end ( ) ;
1389
- selection. from = selection. to ;
1390
- }
1391
- selection. clamp ( length) ;
1392
- edit_text. selection = Some ( selection) ;
1393
- return ClipEventResult :: Handled ;
1394
- }
1395
- _ => ( ) ,
1396
- }
1397
- }
1398
- }
1399
-
1400
- ClipEventResult :: NotHandled
1401
- }
1402
-
1403
1398
fn initialize_as_broadcaster ( & self , activation : & mut Avm1Activation < ' _ , ' gc > ) {
1404
1399
if let Avm1Value :: Object ( object) = self . object ( ) {
1405
1400
activation. context . avm1 . broadcaster_functions ( ) . initialize (
@@ -2043,7 +2038,7 @@ impl TextSelection {
2043
2038
self . from . min ( self . to )
2044
2039
}
2045
2040
2046
- /// The "end" part of the range is the smallest (closest to 0) part of this selection range.
2041
+ /// The "end" part of the range is the largest (farthest from 0) part of this selection range.
2047
2042
pub fn end ( & self ) -> usize {
2048
2043
self . from . max ( self . to )
2049
2044
}
0 commit comments