forked from mirrors/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:
@@ -73,7 +73,6 @@ InReaction CGUI::HandleEvent(const SDL_Event_* ev)
|
|||||||
|
|
||||||
if (m_GlobalHotkeys.count(hotkey) && ev->ev.type == SDL_HOTKEYDOWN)
|
if (m_GlobalHotkeys.count(hotkey) && ev->ev.type == SDL_HOTKEYDOWN)
|
||||||
{
|
{
|
||||||
HotkeyInputHandler(ev);
|
|
||||||
ret = IN_HANDLED;
|
ret = IN_HANDLED;
|
||||||
|
|
||||||
JSContext* cx = m_ScriptInterface->GetContext();
|
JSContext* cx = m_ScriptInterface->GetContext();
|
||||||
|
|||||||
@@ -55,17 +55,20 @@ public:
|
|||||||
|
|
||||||
void test_hotkeysState()
|
void test_hotkeysState()
|
||||||
{
|
{
|
||||||
|
|
||||||
// Load up a fake test hotkey when pressing 'a'.
|
// Load up a fake test hotkey when pressing 'a'.
|
||||||
const char* test_hotkey_name = "hotkey.test";
|
const char* test_hotkey_name = "hotkey.test";
|
||||||
g_ConfigDB.SetValueString(CFG_USER, test_hotkey_name, "A");
|
g_ConfigDB.SetValueString(CFG_USER, test_hotkey_name, "A");
|
||||||
LoadHotkeys();
|
LoadHotkeys();
|
||||||
|
|
||||||
// Load up a test page.
|
// Load up a test page.
|
||||||
JS::RootedValue val(g_GUI->GetScriptInterface()->GetContext());
|
const ScriptInterface& scriptInterface = *(g_GUI->GetScriptInterface());
|
||||||
g_GUI->GetScriptInterface()->Eval("({})", &val);
|
JSContext* cx = scriptInterface.GetContext();
|
||||||
std::shared_ptr<ScriptInterface::StructuredClone> data = g_GUI->GetScriptInterface()->WriteStructuredClone(JS::NullHandleValue);
|
JSAutoRequest rq(cx);
|
||||||
g_GUI->PushPage(L"page_hotkey.xml", data, JS::UndefinedHandleValue);
|
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'.
|
// Press 'a'.
|
||||||
SDL_Event_ hotkeyNotification;
|
SDL_Event_ hotkeyNotification;
|
||||||
@@ -79,16 +82,22 @@ public:
|
|||||||
while (in_poll_event(&ev))
|
while (in_poll_event(&ev))
|
||||||
in_dispatch_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.
|
// Ensure that our hotkey state was synchronised with the event itself.
|
||||||
bool hotkey_pressed_value = false;
|
bool hotkey_pressed_value = false;
|
||||||
JS::RootedValue js_hotkey_pressed_value(g_GUI->GetActiveGUI()->GetScriptInterface()->GetContext());
|
JS::RootedValue js_hotkey_pressed_value(pageScriptInterface.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);
|
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);
|
TS_ASSERT_EQUALS(hotkey_pressed_value, true);
|
||||||
|
|
||||||
hotkey_pressed_value = false;
|
hotkey_pressed_value = false;
|
||||||
g_GUI->GetActiveGUI()->GetScriptInterface()->Eval("state_after", &js_hotkey_pressed_value);
|
pageScriptInterface.GetProperty(global, "state_after", &js_hotkey_pressed_value);
|
||||||
g_GUI->GetActiveGUI()->GetScriptInterface()->FromJSVal(g_GUI->GetActiveGUI()->GetScriptInterface()->GetContext(), js_hotkey_pressed_value, hotkey_pressed_value);
|
ScriptInterface::FromJSVal(pcx, js_hotkey_pressed_value, hotkey_pressed_value);
|
||||||
TS_ASSERT_EQUALS(hotkey_pressed_value, true);
|
TS_ASSERT_EQUALS(hotkey_pressed_value, true);
|
||||||
|
|
||||||
hotkeyNotification.ev.type = SDL_KEYUP;
|
hotkeyNotification.ev.type = SDL_KEYUP;
|
||||||
@@ -97,13 +106,13 @@ public:
|
|||||||
in_dispatch_event(&ev);
|
in_dispatch_event(&ev);
|
||||||
|
|
||||||
hotkey_pressed_value = true;
|
hotkey_pressed_value = true;
|
||||||
g_GUI->GetActiveGUI()->GetScriptInterface()->Eval("state_before", &js_hotkey_pressed_value);
|
pageScriptInterface.GetProperty(global, "state_before", &js_hotkey_pressed_value);
|
||||||
g_GUI->GetActiveGUI()->GetScriptInterface()->FromJSVal(g_GUI->GetActiveGUI()->GetScriptInterface()->GetContext(), js_hotkey_pressed_value, hotkey_pressed_value);
|
ScriptInterface::FromJSVal(pcx, js_hotkey_pressed_value, hotkey_pressed_value);
|
||||||
TS_ASSERT_EQUALS(hotkey_pressed_value, false);
|
TS_ASSERT_EQUALS(hotkey_pressed_value, false);
|
||||||
|
|
||||||
hotkey_pressed_value = true;
|
hotkey_pressed_value = true;
|
||||||
g_GUI->GetActiveGUI()->GetScriptInterface()->Eval("state_after", &js_hotkey_pressed_value);
|
pageScriptInterface.GetProperty(global, "state_after", &js_hotkey_pressed_value);
|
||||||
g_GUI->GetActiveGUI()->GetScriptInterface()->FromJSVal(g_GUI->GetActiveGUI()->GetScriptInterface()->GetContext(), js_hotkey_pressed_value, hotkey_pressed_value);
|
ScriptInterface::FromJSVal(pcx, js_hotkey_pressed_value, hotkey_pressed_value);
|
||||||
TS_ASSERT_EQUALS(hotkey_pressed_value, false);
|
TS_ASSERT_EQUALS(hotkey_pressed_value, false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user