refactor: use a FixedFlatMap for v8 converting ui::MenuSourceType (#40786)

This commit is contained in:
Charles Kerr 2023-12-20 17:44:17 -06:00 коммит произвёл GitHub
Родитель ab2a4fd836
Коммит 7995c56fb0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 23 добавлений и 64 удалений

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

@ -69,80 +69,39 @@ namespace {
namespace gin {
static constexpr auto MenuSourceTypes =
base::MakeFixedFlatMap<base::StringPiece, ui::MenuSourceType>({
{"adjustSelection", ui::MENU_SOURCE_ADJUST_SELECTION},
{"adjustSelectionReset", ui::MENU_SOURCE_ADJUST_SELECTION_RESET},
{"keyboard", ui::MENU_SOURCE_KEYBOARD},
{"longPress", ui::MENU_SOURCE_LONG_PRESS},
{"longTap", ui::MENU_SOURCE_LONG_TAP},
{"mouse", ui::MENU_SOURCE_MOUSE},
{"none", ui::MENU_SOURCE_NONE},
{"stylus", ui::MENU_SOURCE_STYLUS},
{"touch", ui::MENU_SOURCE_TOUCH},
{"touchHandle", ui::MENU_SOURCE_TOUCH_HANDLE},
{"touchMenu", ui::MENU_SOURCE_TOUCH_EDIT_MENU},
});
// let us know when upstream changes & we need to update MenuSourceTypes
static_assert(std::size(MenuSourceTypes) == ui::MENU_SOURCE_TYPE_LAST + 1U);
// static
v8::Local<v8::Value> Converter<ui::MenuSourceType>::ToV8(
v8::Isolate* isolate,
const ui::MenuSourceType& in) {
switch (in) {
case ui::MENU_SOURCE_MOUSE:
return StringToV8(isolate, "mouse");
case ui::MENU_SOURCE_KEYBOARD:
return StringToV8(isolate, "keyboard");
case ui::MENU_SOURCE_TOUCH:
return StringToV8(isolate, "touch");
case ui::MENU_SOURCE_TOUCH_EDIT_MENU:
return StringToV8(isolate, "touchMenu");
case ui::MENU_SOURCE_LONG_PRESS:
return StringToV8(isolate, "longPress");
case ui::MENU_SOURCE_LONG_TAP:
return StringToV8(isolate, "longTap");
case ui::MENU_SOURCE_TOUCH_HANDLE:
return StringToV8(isolate, "touchHandle");
case ui::MENU_SOURCE_STYLUS:
return StringToV8(isolate, "stylus");
case ui::MENU_SOURCE_ADJUST_SELECTION:
return StringToV8(isolate, "adjustSelection");
case ui::MENU_SOURCE_ADJUST_SELECTION_RESET:
return StringToV8(isolate, "adjustSelectionReset");
case ui::MENU_SOURCE_NONE:
return StringToV8(isolate, "none");
}
for (auto const& [key, val] : MenuSourceTypes)
if (in == val)
return StringToV8(isolate, key);
return {};
}
// static
bool Converter<ui::MenuSourceType>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
ui::MenuSourceType* out) {
std::string type;
if (!ConvertFromV8(isolate, val, &type))
return false;
if (type == "mouse") {
*out = ui::MENU_SOURCE_MOUSE;
return true;
} else if (type == "keyboard") {
*out = ui::MENU_SOURCE_KEYBOARD;
return true;
} else if (type == "touch") {
*out = ui::MENU_SOURCE_TOUCH;
return true;
} else if (type == "touchMenu") {
*out = ui::MENU_SOURCE_TOUCH_EDIT_MENU;
return true;
} else if (type == "longPress") {
*out = ui::MENU_SOURCE_LONG_PRESS;
return true;
} else if (type == "longTap") {
*out = ui::MENU_SOURCE_LONG_TAP;
return true;
} else if (type == "touchHandle") {
*out = ui::MENU_SOURCE_TOUCH_HANDLE;
return true;
} else if (type == "stylus") {
*out = ui::MENU_SOURCE_STYLUS;
return true;
} else if (type == "adjustSelection") {
*out = ui::MENU_SOURCE_ADJUST_SELECTION;
return true;
} else if (type == "adjustSelectionReset") {
*out = ui::MENU_SOURCE_ADJUST_SELECTION_RESET;
return true;
} else if (type == "none") {
*out = ui::MENU_SOURCE_NONE;
return true;
}
return false;
return FromV8WithLookup(isolate, val, MenuSourceTypes, out);
}
// static