core: Add FocusTracker.get_as_edit_text method

This is a utility method which simplifies getting the current focus
as an EditText object, which is being done frequently throughout Ruffle.
This commit is contained in:
Kamil Jarosz 2024-04-09 22:04:48 +02:00 committed by Adrian Wielgosik
parent 31c9be3a35
commit f94a31c5c2
3 changed files with 12 additions and 16 deletions

View File

@ -23,8 +23,7 @@ pub fn get_begin_index<'gc>(
if let Some(selection) = activation
.context
.focus_tracker
.get()
.and_then(|o| o.as_edit_text())
.get_as_edit_text()
.and_then(EditText::selection)
{
Ok(selection.start().into())
@ -41,8 +40,7 @@ pub fn get_end_index<'gc>(
if let Some(selection) = activation
.context
.focus_tracker
.get()
.and_then(|o| o.as_edit_text())
.get_as_edit_text()
.and_then(EditText::selection)
{
Ok(selection.end().into())
@ -59,8 +57,7 @@ pub fn get_caret_index<'gc>(
if let Some(selection) = activation
.context
.focus_tracker
.get()
.and_then(|o| o.as_edit_text())
.get_as_edit_text()
.and_then(EditText::selection)
{
Ok(selection.to().into())
@ -78,12 +75,7 @@ pub fn set_selection<'gc>(
return Ok(Value::Undefined);
}
if let Some(edit_box) = activation
.context
.focus_tracker
.get()
.and_then(|o| o.as_edit_text())
{
if let Some(edit_box) = activation.context.focus_tracker.get_as_edit_text() {
let start = args
.get(0)
.map(|v| v.coerce_to_i32(activation))

View File

@ -1,10 +1,10 @@
use crate::avm1::Avm1;
use crate::avm1::Value;
use crate::context::{RenderContext, UpdateContext};
use crate::display_object::TInteractiveObject;
pub use crate::display_object::{
DisplayObject, TDisplayObject, TDisplayObjectContainer, TextSelection,
};
use crate::display_object::{EditText, TInteractiveObject};
use crate::drawing::Drawing;
use either::Either;
use gc_arena::barrier::unlock;
@ -60,6 +60,10 @@ impl<'gc> FocusTracker<'gc> {
self.0.focus.get()
}
pub fn get_as_edit_text(&self) -> Option<EditText<'gc>> {
self.get().and_then(|o| o.as_edit_text())
}
pub fn set(
&self,
focused_element: Option<DisplayObject<'gc>>,
@ -99,7 +103,7 @@ impl<'gc> FocusTracker<'gc> {
}
// This applies even if the focused element hasn't changed.
if let Some(text_field) = focused_element.and_then(|e| e.as_edit_text()) {
if let Some(text_field) = self.get_as_edit_text() {
if text_field.is_editable() {
if !text_field.movie().is_action_script_3() {
let length = text_field.text_length();

View File

@ -1060,12 +1060,12 @@ impl Player {
// keyPress events take precedence over text input.
if !key_press_handled {
if let PlayerEvent::TextInput { codepoint } = event {
if let Some(text) = context.focus_tracker.get().and_then(|o| o.as_edit_text()) {
if let Some(text) = context.focus_tracker.get_as_edit_text() {
text.text_input(codepoint, context);
}
}
if let PlayerEvent::TextControl { code } = event {
if let Some(text) = context.focus_tracker.get().and_then(|o| o.as_edit_text()) {
if let Some(text) = context.focus_tracker.get_as_edit_text() {
text.text_control_input(code, context);
}
}