Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Add basic support for Tab key behavior #15542

Merged
merged 8 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions core/src/avm1/globals/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {
"scale9Grid" => property(button_getter!(scale_9_grid), button_setter!(set_scale_9_grid); DONT_DELETE | DONT_ENUM | VERSION_8);
"filters" => property(button_getter!(filters), button_setter!(set_filters); DONT_DELETE | DONT_ENUM | VERSION_8);
"cacheAsBitmap" => property(button_getter!(cache_as_bitmap), button_setter!(set_cache_as_bitmap); DONT_DELETE | DONT_ENUM | VERSION_8);
// NOTE: `tabEnabled` is not a built-in property of Button.
"tabIndex" => property(button_getter!(tab_index), button_setter!(set_tab_index); VERSION_6);
};

pub fn create_proto<'gc>(
Expand Down Expand Up @@ -173,3 +175,36 @@ fn set_scale_9_grid<'gc>(
};
Ok(())
}

fn tab_index<'gc>(
this: Avm1Button<'gc>,
_activation: &mut Activation<'_, 'gc>,
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(index) = this.tab_index_value() {
Ok(index.into())
} else {
Ok(Value::Undefined)
}
}

fn set_tab_index<'gc>(
this: Avm1Button<'gc>,
activation: &mut Activation<'_, 'gc>,
value: Value<'gc>,
) -> Result<(), Error<'gc>> {
match value {
Value::Undefined | Value::Null => {
this.set_tab_index_value(&mut activation.context, None);
}
Value::Bool(_) | Value::Number(_) => {
// FIXME This coercion is not perfect, as it wraps
// instead of falling back to MIN, as FP does
let i32_value = value.coerce_to_i32(activation)?;
this.set_tab_index_value(&mut activation.context, Some(i32_value));
}
_ => {
this.set_tab_index_value(&mut activation.context, Some(i32::MIN));
}
};
Ok(())
}
36 changes: 36 additions & 0 deletions core/src/avm1/globals/movie_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {
"transform" => property(mc_getter!(transform), mc_setter!(set_transform); DONT_ENUM | VERSION_8);
"useHandCursor" => bool(true; DONT_ENUM);
// NOTE: `focusEnabled` is not a built-in property of MovieClip.
// NOTE: `tabEnabled` is not a built-in property of MovieClip.
// NOTE: `tabIndex` is not enumerable in MovieClip, contrary to Button and TextField
"tabIndex" => property(mc_getter!(tab_index), mc_setter!(set_tab_index); DONT_ENUM | VERSION_6);
};

/// Implements `MovieClip`
Expand Down Expand Up @@ -1819,3 +1822,36 @@ fn set_filters<'gc>(
this.set_filters(activation.context.gc_context, filters);
Ok(())
}

fn tab_index<'gc>(
this: MovieClip<'gc>,
_activation: &mut Activation<'_, 'gc>,
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(index) = this.tab_index_value() {
Ok(index.into())
} else {
Ok(Value::Undefined)
}
}

fn set_tab_index<'gc>(
this: MovieClip<'gc>,
activation: &mut Activation<'_, 'gc>,
value: Value<'gc>,
) -> Result<(), Error<'gc>> {
match value {
Value::Undefined | Value::Null => {
this.set_tab_index_value(&mut activation.context, None);
}
Value::Bool(_) | Value::Number(_) => {
// FIXME This coercion is not perfect, as it wraps
// instead of falling back to MIN, as FP does
let i32_value = value.coerce_to_i32(activation)?;
this.set_tab_index_value(&mut activation.context, Some(i32_value));
}
_ => {
this.set_tab_index_value(&mut activation.context, Some(i32::MIN));
}
};
Ok(())
}
31 changes: 31 additions & 0 deletions core/src/avm1/globals/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {
"gridFitType" => property(tf_getter!(grid_fit_type), tf_setter!(set_grid_fit_type));
"sharpness" => property(tf_getter!(sharpness), tf_setter!(set_sharpness));
"thickness" => property(tf_getter!(thickness), tf_setter!(set_thickness));
// NOTE: `tabEnabled` is not a built-in property of TextField.
"tabIndex" => property(tf_getter!(tab_index), tf_setter!(set_tab_index); VERSION_6);
};

/// Implements `TextField`
Expand All @@ -113,6 +115,7 @@ pub fn create_proto<'gc>(
define_properties_on(PROTO_DECLS, context, object, fn_proto);
object.into()
}

pub fn password<'gc>(
this: EditText<'gc>,
_activation: &mut Activation<'_, 'gc>,
Expand Down Expand Up @@ -911,3 +914,31 @@ fn set_restrict<'gc>(
};
Ok(())
}

pub fn tab_index<'gc>(
this: EditText<'gc>,
_activation: &mut Activation<'_, 'gc>,
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(index) = this.tab_index_value() {
Ok(index.into())
} else {
Ok(Value::Undefined)
}
}

pub fn set_tab_index<'gc>(
this: EditText<'gc>,
activation: &mut Activation<'_, 'gc>,
value: Value<'gc>,
) -> Result<(), Error<'gc>> {
match value {
Value::Undefined | Value::Null => {
this.set_tab_index_value(&mut activation.context, None);
}
_ => {
let u32_value = value.coerce_to_u32(activation)?;
this.set_tab_index_value(&mut activation.context, Some(u32_value));
}
};
Ok(())
}
42 changes: 41 additions & 1 deletion core/src/display_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1890,11 +1890,23 @@ pub trait TDisplayObject<'gc>:
}
}

/// Whether or not this clip may be focusable for keyboard input.
/// Whether this clip may be focusable for keyboard input.
fn is_focusable(&self, _context: &mut UpdateContext<'_, 'gc>) -> bool {
false
}

/// Whether this object is included in tab ordering.
fn is_tab_enabled(&self, _context: &mut UpdateContext<'_, 'gc>) -> bool {
false
}

/// Used to customize tab ordering.
/// When not `None`, a custom ordering is used, and
/// objects are ordered according to this value.
fn tab_index(&self) -> Option<i64> {
None
}

/// Whether this display object has been created by ActionScript 3.
/// When this flag is set, changes from SWF `RemoveObject` tags are
/// ignored.
Expand Down Expand Up @@ -2478,6 +2490,34 @@ pub trait TDisplayObject<'gc>:
}
}
}

/// Retrieve a named property from the AVM1 object.
///
/// This is required as some boolean properties in AVM1 can in fact hold any value.
fn get_avm1_boolean_property(
self,
context: &mut UpdateContext<'_, 'gc>,
name: &'static str,
default: bool,
) -> bool {
if let Avm1Value::Object(object) = self.object() {
let mut activation = Activation::from_nothing(
context.reborrow(),
Avm1ActivationIdentifier::root("[AVM1 Boolean Property]"),
self.avm1_root(),
);
if let Ok(value) = object.get(name, &mut activation) {
match value {
Avm1Value::Undefined => default,
_ => value.as_bool(activation.swf_version()),
}
} else {
default
}
} else {
false
}
}
}

pub enum DisplayObjectPtr {}
Expand Down
24 changes: 24 additions & 0 deletions core/src/display_object/avm1_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub struct Avm1ButtonData<'gc> {
object: Lock<Option<Object<'gc>>>,
initialized: Cell<bool>,
has_focus: Cell<bool>,
// TODO Consider moving this to InteractiveObject along with
// TextField's and MovieClip's tab indices after AVM2 analysis
tab_index: Cell<Option<i32>>,
Copy link
Collaborator

@adrian17 adrian17 Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you document all these places, that they might eventually need to be moved to InteractiveObject?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, added a TODO for each one

}

#[derive(Clone, Collect)]
Expand Down Expand Up @@ -99,6 +102,7 @@ impl<'gc> Avm1Button<'gc> {
ButtonTracking::Push
}),
has_focus: Cell::new(false),
tab_index: Cell::new(None),
},
))
}
Expand Down Expand Up @@ -241,6 +245,18 @@ impl<'gc> Avm1Button<'gc> {
fn use_hand_cursor(self, context: &mut UpdateContext<'_, 'gc>) -> bool {
self.get_boolean_property(context, "useHandCursor", true)
}

/// Get the value of `tabIndex` used in AS.
///
/// Do not confuse it with `tab_index`, which returns the value used for ordering.
pub fn tab_index_value(&self) -> Option<i32> {
self.0.tab_index.get()
}

/// Set the value of `tabIndex` used in AS.
pub fn set_tab_index_value(&self, _context: &mut UpdateContext<'_, 'gc>, value: Option<i32>) {
self.0.tab_index.set(value)
}
}

impl<'gc> TDisplayObject<'gc> for Avm1Button<'gc> {
Expand Down Expand Up @@ -406,6 +422,14 @@ impl<'gc> TDisplayObject<'gc> for Avm1Button<'gc> {
self.call_focus_handler(context, focused, other);
}

fn is_tab_enabled(&self, context: &mut UpdateContext<'_, 'gc>) -> bool {
self.get_avm1_boolean_property(context, "tabEnabled", true)
}

fn tab_index(&self) -> Option<i64> {
self.0.tab_index.get().map(|i| i as i64)
}

fn avm1_unload(&self, context: &mut UpdateContext<'_, 'gc>) {
let had_focus = self.0.has_focus.get();
if had_focus {
Expand Down
16 changes: 16 additions & 0 deletions core/src/display_object/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ pub trait TDisplayObjectContainer<'gc>:
RenderIter::from_container(self.into())
}

fn fill_tab_order(
&self,
tab_order: &mut Vec<DisplayObject<'gc>>,
context: &mut UpdateContext<'_, 'gc>,
) {
// TODO Add support for `tabChildren`
for child in self.iter_render_list() {
if child.is_tab_enabled(context) {
tab_order.push(child);
}
if let Some(container) = child.as_container() {
container.fill_tab_order(tab_order, context);
}
}
}

/// Renders the children of this container in render list order.
fn render_children(self, context: &mut RenderContext<'_, 'gc>) {
let mut clip_depth = 0;
Expand Down
26 changes: 26 additions & 0 deletions core/src/display_object/edit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ pub struct EditTextData<'gc> {
/// Restrict what characters the user may input.
#[collect(require_static)]
restrict: EditTextRestrict,

// TODO Consider moving this to InteractiveObject along with
// MovieClip's and Button's tab indices after AVM2 analysis
// NOTE: `tabIndex` is u32 in TextField, compared to i32 in Button and MovieClip
tab_index: Option<u32>,
}

impl<'gc> EditTextData<'gc> {
Expand Down Expand Up @@ -362,6 +367,7 @@ impl<'gc> EditText<'gc> {
mouse_wheel_enabled: true,
is_tlf: false,
restrict: EditTextRestrict::allow_all(),
tab_index: None,
},
));

Expand Down Expand Up @@ -2028,6 +2034,18 @@ impl<'gc> EditText<'gc> {
.contains(Position::from((position.x, position.y)))
})
}

/// Get the value of `tabIndex` used in AS.
///
/// Do not confuse it with `tab_index`, which returns the value used for ordering.
pub fn tab_index_value(&self) -> Option<u32> {
self.0.read().tab_index
}

/// Set the value of `tabIndex` used in AS.
pub fn set_tab_index_value(&self, context: &mut UpdateContext<'_, 'gc>, value: Option<u32>) {
self.0.write(context.gc()).tab_index = value;
}
}

impl<'gc> TDisplayObject<'gc> for EditText<'gc> {
Expand Down Expand Up @@ -2328,6 +2346,14 @@ impl<'gc> TDisplayObject<'gc> for EditText<'gc> {
// Even if this isn't selectable or editable, a script can focus on it manually.
true
}

fn is_tab_enabled(&self, context: &mut UpdateContext<'_, 'gc>) -> bool {
self.get_avm1_boolean_property(context, "tabEnabled", true)
}

fn tab_index(&self) -> Option<i64> {
self.0.read().tab_index.map(|i| i as i64)
}
}

impl<'gc> TInteractiveObject<'gc> for EditText<'gc> {
Expand Down
Loading
Loading