1
0
forked from 0ad/0ad

Improve js glue code in D1839/61e3f1ec0d (hotkey state fix) and deal with rebase leftovers.

The code could be improved by using more modern code.

Reviewed By: elexis
Differential Revision: https://code.wildfiregames.com/D2295
This was SVN commit r22963.
This commit is contained in:
wraitii 2019-09-22 07:38:13 +00:00
parent d96ac8ea24
commit d1bcce55db
5 changed files with 23 additions and 15 deletions

View File

@ -73,7 +73,6 @@ InReaction CGUI::HandleEvent(const SDL_Event_* ev)
if (m_GlobalHotkeys.count(hotkey) && ev->ev.type == SDL_HOTKEYDOWN)
{
HotkeyInputHandler(ev);
ret = IN_HANDLED;
JSContext* cx = m_ScriptInterface->GetContext();

View File

@ -55,17 +55,20 @@ public:
void test_hotkeysState()
{
// Load up a fake test hotkey when pressing 'a'.
const char* test_hotkey_name = "hotkey.test";
g_ConfigDB.SetValueString(CFG_USER, test_hotkey_name, "A");
LoadHotkeys();
// Load up a test page.
JS::RootedValue val(g_GUI->GetScriptInterface()->GetContext());
g_GUI->GetScriptInterface()->Eval("({})", &val);
std::shared_ptr<ScriptInterface::StructuredClone> data = g_GUI->GetScriptInterface()->WriteStructuredClone(JS::NullHandleValue);
g_GUI->PushPage(L"page_hotkey.xml", data, JS::UndefinedHandleValue);
const ScriptInterface& scriptInterface = *(g_GUI->GetScriptInterface());
JSContext* cx = scriptInterface.GetContext();
JSAutoRequest rq(cx);
JS::RootedValue val(cx);
scriptInterface.CreateObject(cx, &val);
std::shared_ptr<ScriptInterface::StructuredClone> data = scriptInterface.WriteStructuredClone(JS::NullHandleValue);
g_GUI->PushPage(L"hotkey/page_hotkey.xml", data, JS::UndefinedHandleValue);
// Press 'a'.
SDL_Event_ hotkeyNotification;
@ -79,16 +82,22 @@ public:
while (in_poll_event(&ev))
in_dispatch_event(&ev);
const ScriptInterface& pageScriptInterface = *(g_GUI->GetActiveGUI()->GetScriptInterface());
JSContext* pcx = pageScriptInterface.GetContext();
JSAutoRequest pagerq(pcx);
JS::RootedValue global(pcx, pageScriptInterface.GetGlobalObject());
// Ensure that our hotkey state was synchronised with the event itself.
bool hotkey_pressed_value = false;
JS::RootedValue js_hotkey_pressed_value(g_GUI->GetActiveGUI()->GetScriptInterface()->GetContext());
g_GUI->GetActiveGUI()->GetScriptInterface()->Eval("state_before", &js_hotkey_pressed_value);
g_GUI->GetActiveGUI()->GetScriptInterface()->FromJSVal(g_GUI->GetActiveGUI()->GetScriptInterface()->GetContext(), js_hotkey_pressed_value, hotkey_pressed_value);
JS::RootedValue js_hotkey_pressed_value(pageScriptInterface.GetContext());
pageScriptInterface.GetProperty(global, "state_before", &js_hotkey_pressed_value);
ScriptInterface::FromJSVal(pcx, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, true);
hotkey_pressed_value = false;
g_GUI->GetActiveGUI()->GetScriptInterface()->Eval("state_after", &js_hotkey_pressed_value);
g_GUI->GetActiveGUI()->GetScriptInterface()->FromJSVal(g_GUI->GetActiveGUI()->GetScriptInterface()->GetContext(), js_hotkey_pressed_value, hotkey_pressed_value);
pageScriptInterface.GetProperty(global, "state_after", &js_hotkey_pressed_value);
ScriptInterface::FromJSVal(pcx, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, true);
hotkeyNotification.ev.type = SDL_KEYUP;
@ -97,13 +106,13 @@ public:
in_dispatch_event(&ev);
hotkey_pressed_value = true;
g_GUI->GetActiveGUI()->GetScriptInterface()->Eval("state_before", &js_hotkey_pressed_value);
g_GUI->GetActiveGUI()->GetScriptInterface()->FromJSVal(g_GUI->GetActiveGUI()->GetScriptInterface()->GetContext(), js_hotkey_pressed_value, hotkey_pressed_value);
pageScriptInterface.GetProperty(global, "state_before", &js_hotkey_pressed_value);
ScriptInterface::FromJSVal(pcx, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, false);
hotkey_pressed_value = true;
g_GUI->GetActiveGUI()->GetScriptInterface()->Eval("state_after", &js_hotkey_pressed_value);
g_GUI->GetActiveGUI()->GetScriptInterface()->FromJSVal(g_GUI->GetActiveGUI()->GetScriptInterface()->GetContext(), js_hotkey_pressed_value, hotkey_pressed_value);
pageScriptInterface.GetProperty(global, "state_after", &js_hotkey_pressed_value);
ScriptInterface::FromJSVal(pcx, js_hotkey_pressed_value, hotkey_pressed_value);
TS_ASSERT_EQUALS(hotkey_pressed_value, false);
}