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
This commit is contained in:
James Teh 2022-07-14 05:48:16 +00:00
Родитель 52a9b98ce9
Коммит 2d82055fe8
4 изменённых файлов: 135 добавлений и 135 удалений

Просмотреть файл

@ -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<nsIStringBundle> keyStringBundle;
nsCOMPtr<nsIStringBundleService> 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("<Control>");
if (mModifierMask & kAlt) aValue.AppendLiteral("<Alt>");
if (mModifierMask & kShift) aValue.AppendLiteral("<Shift>");
if (mModifierMask & kMeta) aValue.AppendLiteral("<Meta>");
aValue.Append(mKey);
}

Просмотреть файл

@ -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();

Просмотреть файл

@ -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<nsIStringBundle> keyStringBundle;
nsCOMPtr<nsIStringBundleService> 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("<Control>");
if (mModifierMask & kAlt) aValue.AppendLiteral("<Alt>");
if (mModifierMask & kShift) aValue.AppendLiteral("<Shift>");
if (mModifierMask & kMeta) aValue.AppendLiteral("<Meta>");
aValue.Append(mKey);
}
TableAccessibleBase* LocalAccessible::AsTableBase() {
if (StaticPrefs::accessibility_cache_enabled_AtStartup() && IsTable() &&
!mContent->IsXULElement()) {

Просмотреть файл

@ -1076,60 +1076,6 @@ inline LocalAccessible* Accessible::AsLocal() {
return IsLocal() ? static_cast<LocalAccessible*>(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