From b64daafc2bd8e4ff1906dc51930b98d5195550ba Mon Sep 17 00:00:00 2001 From: James Teh Date: Thu, 14 Jul 2022 00:49:46 +0000 Subject: [PATCH] Bug 1757126 part 2: Move KeyBinding to base Accessible header. r=morgan We'll need this to unify AccessKey. Differential Revision: https://phabricator.services.mozilla.com/D151200 --- accessible/basetypes/Accessible.cpp | 81 ++++++++++++++++++++++++++ accessible/basetypes/Accessible.h | 54 +++++++++++++++++ accessible/generic/LocalAccessible.cpp | 81 -------------------------- accessible/generic/LocalAccessible.h | 54 ----------------- 4 files changed, 135 insertions(+), 135 deletions(-) diff --git a/accessible/basetypes/Accessible.cpp b/accessible/basetypes/Accessible.cpp index cab9ff6a69a6..8dd3c631eee2 100644 --- a/accessible/basetypes/Accessible.cpp +++ b/accessible/basetypes/Accessible.cpp @@ -9,6 +9,7 @@ #include "nsAccUtils.h" #include "States.h" #include "mozilla/a11y/HyperTextAccessibleBase.h" +#include "mozilla/BasicEvents.h" #include "mozilla/Components.h" #include "nsIStringBundle.h" @@ -473,3 +474,83 @@ nsAtom* Accessible::LandmarkRole() const { ? roleMapEntry->roleAtom : nullptr; } + +//////////////////////////////////////////////////////////////////////////////// +// KeyBinding class + +// static +uint32_t KeyBinding::AccelModifier() { + switch (WidgetInputEvent::AccelModifier()) { + case MODIFIER_ALT: + return kAlt; + case MODIFIER_CONTROL: + return kControl; + case MODIFIER_META: + return kMeta; + case MODIFIER_OS: + return kOS; + default: + MOZ_CRASH("Handle the new result of WidgetInputEvent::AccelModifier()"); + return 0; + } +} + +void KeyBinding::ToPlatformFormat(nsAString& aValue) const { + nsCOMPtr keyStringBundle; + nsCOMPtr stringBundleService = + mozilla::components::StringBundle::Service(); + if (stringBundleService) { + stringBundleService->CreateBundle( + "chrome://global-platform/locale/platformKeys.properties", + getter_AddRefs(keyStringBundle)); + } + + if (!keyStringBundle) return; + + nsAutoString separator; + keyStringBundle->GetStringFromName("MODIFIER_SEPARATOR", separator); + + nsAutoString modifierName; + if (mModifierMask & kControl) { + keyStringBundle->GetStringFromName("VK_CONTROL", modifierName); + + aValue.Append(modifierName); + aValue.Append(separator); + } + + if (mModifierMask & kAlt) { + keyStringBundle->GetStringFromName("VK_ALT", modifierName); + + aValue.Append(modifierName); + aValue.Append(separator); + } + + if (mModifierMask & kShift) { + keyStringBundle->GetStringFromName("VK_SHIFT", modifierName); + + aValue.Append(modifierName); + aValue.Append(separator); + } + + if (mModifierMask & kMeta) { + keyStringBundle->GetStringFromName("VK_META", modifierName); + + aValue.Append(modifierName); + aValue.Append(separator); + } + + aValue.Append(mKey); +} + +void KeyBinding::ToAtkFormat(nsAString& aValue) const { + nsAutoString modifierName; + if (mModifierMask & kControl) aValue.AppendLiteral(""); + + if (mModifierMask & kAlt) aValue.AppendLiteral(""); + + if (mModifierMask & kShift) aValue.AppendLiteral(""); + + if (mModifierMask & kMeta) aValue.AppendLiteral(""); + + aValue.Append(mKey); +} diff --git a/accessible/basetypes/Accessible.h b/accessible/basetypes/Accessible.h index 6628e1b334c8..3e945a3edf29 100644 --- a/accessible/basetypes/Accessible.h +++ b/accessible/basetypes/Accessible.h @@ -68,6 +68,60 @@ struct GroupPos { int32_t setSize; }; +/** + * Represent key binding associated with accessible (such as access key and + * global keyboard shortcuts). + */ +class KeyBinding { + public: + /** + * Modifier mask values. + */ + static const uint32_t kShift = 1; + static const uint32_t kControl = 2; + static const uint32_t kAlt = 4; + static const uint32_t kMeta = 8; + static const uint32_t kOS = 16; + + static uint32_t AccelModifier(); + + KeyBinding() : mKey(0), mModifierMask(0) {} + KeyBinding(uint32_t aKey, uint32_t aModifierMask) + : mKey(aKey), mModifierMask(aModifierMask) {} + + inline bool IsEmpty() const { return !mKey; } + inline uint32_t Key() const { return mKey; } + inline uint32_t ModifierMask() const { return mModifierMask; } + + enum Format { ePlatformFormat, eAtkFormat }; + + /** + * Return formatted string for this key binding depending on the given format. + */ + inline void ToString(nsAString& aValue, + Format aFormat = ePlatformFormat) const { + aValue.Truncate(); + AppendToString(aValue, aFormat); + } + inline void AppendToString(nsAString& aValue, + Format aFormat = ePlatformFormat) const { + if (mKey) { + if (aFormat == ePlatformFormat) { + ToPlatformFormat(aValue); + } else { + ToAtkFormat(aValue); + } + } + } + + private: + void ToPlatformFormat(nsAString& aValue) const; + void ToAtkFormat(nsAString& aValue) const; + + uint32_t mKey; + uint32_t mModifierMask; +}; + class Accessible { protected: Accessible(); diff --git a/accessible/generic/LocalAccessible.cpp b/accessible/generic/LocalAccessible.cpp index 951b79656892..43a202eb4e7a 100644 --- a/accessible/generic/LocalAccessible.cpp +++ b/accessible/generic/LocalAccessible.cpp @@ -52,7 +52,6 @@ #include "nsDeckFrame.h" #include "nsLayoutUtils.h" -#include "nsIStringBundle.h" #include "nsPresContext.h" #include "nsIFrame.h" #include "nsTextFrame.h" @@ -3793,86 +3792,6 @@ void LocalAccessible::StaticAsserts() const { "LocalAccessible::mContextFlags was oversized by eLastContextFlag!"); } -//////////////////////////////////////////////////////////////////////////////// -// KeyBinding class - -// static -uint32_t KeyBinding::AccelModifier() { - switch (WidgetInputEvent::AccelModifier()) { - case MODIFIER_ALT: - return kAlt; - case MODIFIER_CONTROL: - return kControl; - case MODIFIER_META: - return kMeta; - case MODIFIER_OS: - return kOS; - default: - MOZ_CRASH("Handle the new result of WidgetInputEvent::AccelModifier()"); - return 0; - } -} - -void KeyBinding::ToPlatformFormat(nsAString& aValue) const { - nsCOMPtr keyStringBundle; - nsCOMPtr stringBundleService = - mozilla::components::StringBundle::Service(); - if (stringBundleService) { - stringBundleService->CreateBundle( - "chrome://global-platform/locale/platformKeys.properties", - getter_AddRefs(keyStringBundle)); - } - - if (!keyStringBundle) return; - - nsAutoString separator; - keyStringBundle->GetStringFromName("MODIFIER_SEPARATOR", separator); - - nsAutoString modifierName; - if (mModifierMask & kControl) { - keyStringBundle->GetStringFromName("VK_CONTROL", modifierName); - - aValue.Append(modifierName); - aValue.Append(separator); - } - - if (mModifierMask & kAlt) { - keyStringBundle->GetStringFromName("VK_ALT", modifierName); - - aValue.Append(modifierName); - aValue.Append(separator); - } - - if (mModifierMask & kShift) { - keyStringBundle->GetStringFromName("VK_SHIFT", modifierName); - - aValue.Append(modifierName); - aValue.Append(separator); - } - - if (mModifierMask & kMeta) { - keyStringBundle->GetStringFromName("VK_META", modifierName); - - aValue.Append(modifierName); - aValue.Append(separator); - } - - aValue.Append(mKey); -} - -void KeyBinding::ToAtkFormat(nsAString& aValue) const { - nsAutoString modifierName; - if (mModifierMask & kControl) aValue.AppendLiteral(""); - - if (mModifierMask & kAlt) aValue.AppendLiteral(""); - - if (mModifierMask & kShift) aValue.AppendLiteral(""); - - if (mModifierMask & kMeta) aValue.AppendLiteral(""); - - aValue.Append(mKey); -} - TableAccessibleBase* LocalAccessible::AsTableBase() { if (StaticPrefs::accessibility_cache_enabled_AtStartup() && IsTable() && !mContent->IsXULElement()) { diff --git a/accessible/generic/LocalAccessible.h b/accessible/generic/LocalAccessible.h index 851ec28f2011..2a6e7ee96bb2 100644 --- a/accessible/generic/LocalAccessible.h +++ b/accessible/generic/LocalAccessible.h @@ -1076,60 +1076,6 @@ inline LocalAccessible* Accessible::AsLocal() { return IsLocal() ? static_cast(this) : nullptr; } -/** - * Represent key binding associated with accessible (such as access key and - * global keyboard shortcuts). - */ -class KeyBinding { - public: - /** - * Modifier mask values. - */ - static const uint32_t kShift = 1; - static const uint32_t kControl = 2; - static const uint32_t kAlt = 4; - static const uint32_t kMeta = 8; - static const uint32_t kOS = 16; - - static uint32_t AccelModifier(); - - KeyBinding() : mKey(0), mModifierMask(0) {} - KeyBinding(uint32_t aKey, uint32_t aModifierMask) - : mKey(aKey), mModifierMask(aModifierMask) {} - - inline bool IsEmpty() const { return !mKey; } - inline uint32_t Key() const { return mKey; } - inline uint32_t ModifierMask() const { return mModifierMask; } - - enum Format { ePlatformFormat, eAtkFormat }; - - /** - * Return formatted string for this key binding depending on the given format. - */ - inline void ToString(nsAString& aValue, - Format aFormat = ePlatformFormat) const { - aValue.Truncate(); - AppendToString(aValue, aFormat); - } - inline void AppendToString(nsAString& aValue, - Format aFormat = ePlatformFormat) const { - if (mKey) { - if (aFormat == ePlatformFormat) { - ToPlatformFormat(aValue); - } else { - ToAtkFormat(aValue); - } - } - } - - private: - void ToPlatformFormat(nsAString& aValue) const; - void ToAtkFormat(nsAString& aValue) const; - - uint32_t mKey; - uint32_t mModifierMask; -}; - } // namespace a11y } // namespace mozilla