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

cpp AccessibilityUnit settings #2

Draft
wants to merge 3 commits into
base: tts-span
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ const Text: React.AbstractComponent<
<NativeVirtualText
{...restProps}
accessibilityState={_accessibilityState}
accessibilityUnit={{hours: 10}}
{...eventHandlersForText}
accessibilityLabel={ariaLabel ?? accessibilityLabel}
accessibilityRole={
Expand All @@ -243,6 +244,7 @@ const Text: React.AbstractComponent<
<NativeText
{...restProps}
{...eventHandlersForText}
accessibilityUnit={{hours: 10}}
disabled={_disabled}
selectable={_selectable}
accessible={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,6 @@ public void setAccessibilityCollectionItem(
view.setTag(R.id.accessibility_collection_item, accessibilityCollectionItem);
}

@ReactProp(name = "accessibilityUnit")
public void setAccessibilityUnit(@NonNull T view, @Nullable ReadableMap accessibilityUnit) {
Log.w("TESTING::BaseViewManager", "setAccessibilityUnit");
Log.w("TESTING::BaseViewManager", "accessibilityUnit: " + (accessibilityUnit));
}

@Override
@ReactProp(name = ViewProps.ACCESSIBILITY_STATE)
public void setViewState(@NonNull T view, @Nullable ReadableMap accessibilityState) {
Expand Down Expand Up @@ -325,6 +319,12 @@ public void setViewState(@NonNull T view, @Nullable ReadableMap accessibilitySta
}
}

@Override
@ReactProp(name = ViewProps.ACCESSIBILITY_UNIT)
public void setAccessibilityUnit(@NonNull T view, @Nullable ReadableMap accessibilityUnit) {
Log.w("TESTING::BaseViewManager", "setAccessibilityUnit");
}

private void updateViewContentDescription(@NonNull T view) {
final String accessibilityLabel = (String) view.getTag(R.id.accessibility_label);
final ReadableMap accessibilityState = (ReadableMap) view.getTag(R.id.accessibility_state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public void setAccessibilityCollectionItem(
@Override
public void setViewState(@NonNull T view, @Nullable ReadableMap accessibilityState) {}

@Override
public void setAccessibilityUnit(@NonNull T view, @Nullable ReadableMap accessibilityUnit) {}

@Override
public void setBackgroundColor(@NonNull T view, int backgroundColor) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public class ViewProps {
public static final String ACCESSIBILITY_LIVE_REGION = "accessibilityLiveRegion";
public static final String ACCESSIBILITY_ROLE = "accessibilityRole";
public static final String ACCESSIBILITY_STATE = "accessibilityState";
public static final String ACCESSIBILITY_UNIT = "accessibilityUnit";
public static final String ACCESSIBILITY_ACTIONS = "accessibilityActions";
public static final String ACCESSIBILITY_VALUE = "accessibilityValue";
public static final String ACCESSIBILITY_LABELLED_BY = "accessibilityLabelledBy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
import android.text.Spannable;
import android.text.TextUtils;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.BaseViewManager;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.Spacing;
Expand Down Expand Up @@ -48,6 +52,13 @@ public void setAccessible(ReactTextView view, boolean accessible) {
view.setFocusable(accessible);
}

@Override
@ReactProp(name = ViewProps.ACCESSIBILITY_UNIT)
public void setAccessibilityUnit(@NonNull T view, @Nullable ReadableMap accessibilityUnit) {
Log.w("TESTING::ReactTextAnchorViewManager", "setAccessibilityUnit");
Log.w("TESTING::ReactTextAnchorViewManager", "accessibilityUnit: " + (accessibilityUnit));
}

// maxLines can only be set in master view (block), doesn't really make sense to set in a span
@ReactProp(name = ViewProps.NUMBER_OF_LINES, defaultInt = ViewDefaults.NUMBER_OF_LINES)
public void setNumberOfLines(ReactTextView view, int numberOfLines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.LayoutDirection;
import android.util.Log;
import android.util.LruCache;
import android.view.View;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -230,6 +231,9 @@ private static Spannable createSpannableFromAttributedString(
// a new spannable will be wiped out
List<SetSpanOperation> ops = new ArrayList<>();

Log.w(
"TESTING::TextLayoutManagerMapBuffer",
"getMapBuffer(AS_KEY_FRAGMENTS): " + (attributedString.getMapBuffer(AS_KEY_FRAGMENTS)));
buildSpannableFromFragment(context, attributedString.getMapBuffer(AS_KEY_FRAGMENTS), sb, ops);

// TODO T31905686: add support for inline Images
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

package com.facebook.react.views.view
import android.util.Log;
import android.graphics.Color
import android.graphics.Rect
import androidx.core.view.ViewCompat
Expand Down Expand Up @@ -108,7 +107,6 @@ object ReactMapBufferPropSetter {
private const val UNDEF_COLOR = Int.MAX_VALUE

fun setProps(view: ReactViewGroup, viewManager: ReactViewManager, props: MapBuffer) {
Log.w("TESTING::ReactMapBufferPropSetter", "props: " + ( props ));
for (entry in props) {
when (entry.key) {
VP_ACCESSIBILITY_ACTIONS -> {
Expand All @@ -133,7 +131,7 @@ object ReactMapBufferPropSetter {
viewManager.accessibilityState(view, entry.mapBufferValue)
}
VP_ACCESSIBILITY_UNIT -> {
// viewManager.accessibilityUnit(view, entry.mapBufferValue)
viewManager.accessibilityUnit(view, entry.mapBufferValue)
}
VP_ACCESSIBILITY_VALUE -> {
viewManager.accessibilityValue(view, entry.stringValue)
Expand Down Expand Up @@ -284,7 +282,6 @@ object ReactMapBufferPropSetter {
}

private fun ReactViewManager.accessibilityUnit(view: ReactViewGroup, value: MapBuffer) {
Log.w("TESTING::ReactMapBufferPropSetter", "accessibilityUnit");
val accessibilityUnit = JavaOnlyMap()
accessibilityUnit.putString("hours", "10")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ void TextAttributes::apply(TextAttributes textAttributes) {
accessibilityRole = textAttributes.accessibilityRole.has_value()
? textAttributes.accessibilityRole
: accessibilityRole;
accessibilityUnit = textAttributes.accessibilityUnit.has_value()
? textAttributes.accessibilityUnit
: accessibilityUnit;
}

#pragma mark - Operators
Expand All @@ -122,6 +125,7 @@ bool TextAttributes::operator==(const TextAttributes &rhs) const {
isHighlighted,
layoutDirection,
accessibilityRole,
accessibilityUnit,
textTransform) ==
std::tie(
rhs.foregroundColor,
Expand All @@ -142,6 +146,7 @@ bool TextAttributes::operator==(const TextAttributes &rhs) const {
rhs.isHighlighted,
rhs.layoutDirection,
rhs.accessibilityRole,
rhs.accessibilityUnit,
rhs.textTransform) &&
floatEquality(opacity, rhs.opacity) &&
floatEquality(fontSize, rhs.fontSize) &&
Expand Down Expand Up @@ -209,6 +214,7 @@ SharedDebugStringConvertibleList TextAttributes::getDebugProps() const {
debugStringConvertibleItem("isHighlighted", isHighlighted),
debugStringConvertibleItem("layoutDirection", layoutDirection),
debugStringConvertibleItem("accessibilityRole", accessibilityRole),
debugStringConvertibleItem("accessibilityUnit", accessibilityUnit),
};
}
#endif
Expand Down
4 changes: 3 additions & 1 deletion ReactCommon/react/renderer/attributedstring/TextAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class TextAttributes : public DebugStringConvertible {
// construction.
std::optional<LayoutDirection> layoutDirection{};
std::optional<AccessibilityRole> accessibilityRole{};
std::optional<AccessibilityUnit1> accessibilityUnit{};

#pragma mark - Operations

Expand Down Expand Up @@ -131,7 +132,8 @@ struct hash<facebook::react::TextAttributes> {
textAttributes.textShadowColor,
textAttributes.isHighlighted,
textAttributes.layoutDirection,
textAttributes.accessibilityRole);
textAttributes.accessibilityRole,
textAttributes.accessibilityUnit);
}
};
} // namespace std
49 changes: 49 additions & 0 deletions ReactCommon/react/renderer/attributedstring/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,55 @@ inline void fromRawValue(
result = TextDecorationStyle::Solid;
}

inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
AccessibilityUnit &result) {
auto map = (butter::map<std::string, RawValue>)value;
auto hours = map.find("hours");
if (hours->second.hasType<int>()) {
result.hours = (int) hours->second;
}
}
/*
auto map = (butter::map<std::string, RawValue>)value;
auto selected = map.find("selected");
if (selected != map.end()) {
fromRawValue(context, selected->second, result.selected);
}
auto disabled = map.find("disabled");
if (disabled != map.end()) {
fromRawValue(context, disabled->second, result.disabled);
}
auto checked = map.find("checked");
if (checked != map.end()) {
if (checked->second.hasType<std::string>()) {
if ((std::string)checked->second == "mixed") {
result.checked = AccessibilityState::Mixed;
} else {
result.checked = AccessibilityState::None;
}
} else if (checked->second.hasType<bool>()) {
if ((bool)checked->second == true) {
result.checked = AccessibilityState::Checked;
} else {
result.checked = AccessibilityState::Unchecked;
}
} else {
result.checked = AccessibilityState::None;
}
}
auto busy = map.find("busy");
if (busy != map.end()) {
fromRawValue(context, busy->second, result.busy);
}
auto expanded = map.find("expanded");
if (expanded != map.end()) {
fromRawValue(context, expanded->second, result.expanded);
}
*/
}

inline std::string toString(const TextDecorationStyle &textDecorationStyle) {
switch (textDecorationStyle) {
case TextDecorationStyle::Solid:
Expand Down
25 changes: 25 additions & 0 deletions ReactCommon/react/renderer/attributedstring/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ enum class AccessibilityRole {
Toolbar,
};


struct AccessibilityUnit1 {
int hours;
};

constexpr bool operator==(
AccessibilityUnit1 const &lhs,
AccessibilityUnit1 const &rhs) {
return lhs.hours == rhs.hours;
}

constexpr bool operator!=(
AccessibilityUnit1 const &lhs,
AccessibilityUnit1 const &rhs) {
return !(rhs == lhs);
}


enum class TextTransform {
None,
Uppercase,
Expand Down Expand Up @@ -223,6 +241,13 @@ struct hash<facebook::react::AccessibilityRole> {
}
};

template <>
struct hash<facebook::react::AccessibilityUnit1> {
size_t operator()(const facebook::react::AccessibilityRole &v) const {
return hash<int>()(static_cast<int>(v));
}
};

template <>
struct hash<facebook::react::TextTransform> {
size_t operator()(const facebook::react::TextTransform &v) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ constexpr bool operator!=(
}

struct AccessibilityUnit {
std::optional<std::string> hours;
int hours;
};

constexpr bool operator==(
Expand All @@ -104,6 +104,7 @@ constexpr bool operator!=(
return !(rhs == lhs);
}


struct AccessibilityLabelledBy {
std::vector<std::string> value{};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ MapBuffer convertAccessibilityState(AccessibilityState const &state) {

MapBuffer convertAccessibilityUnit(AccessibilityUnit const &state) {
MapBufferBuilder builder(1);
builder.putString(ACCESSIBILITY_UNIT_HOURS, "10");
builder.putInt(ACCESSIBILITY_UNIT_HOURS, 10);
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ inline void fromRawValue(
AccessibilityUnit &result) {
auto map = (butter::map<std::string, RawValue>)value;
auto hours = map.find("hours");
if (hours != map.end()) {
fromRawValue(context, hours->second, result.hours);
if (hours->second.hasType<int>()) {
result.hours = (int) hours->second;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ class AccessibilityExample extends React.Component<{}> {
</View>
<View>
<Text accessible={true}>
My number is{' '}
My number is <View accessibilityUnit={{hours: '10'}} />
<Text
accessibilityRole="time"
accessibilityUnit={{hours: '10'}}
accessible={true}
style={{backgroundColor: 'red'}}>
17:00
Expand Down Expand Up @@ -413,21 +414,20 @@ class SwitchExample extends React.Component<
_onSwitchToggle = () => {
const switchState = !this.state.switchState;

console.log('switchState:', switchState);
this.setState({
switchState: switchState,
});
};

render(): React.Node {
return (
<TouchableOpacity
onPress={this._onSwitchToggle}
accessibilityLabel="element 12"
accessibilityRole="switch"
accessibilityState={{checked: this.state.switchState}}
accessible={true}>
<Text>Switch example</Text>
</TouchableOpacity>
<Text
onPress={() => console.log('onPress')}
disabled
accessibilityState={{disabled: true}}>
This is a Switch example
</Text>
);
}
}
Expand Down