diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index 70b7457c0f..2a274417f5 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -793,32 +793,29 @@ void HackStudioWidget::initialize_debugger() } dbgln("Debugger stopped at source position: {}:{}", source_position.value().file_path, source_position.value().line_number); - Core::EventLoop::main().post_event( - *window(), - make( - [this, source_position, ®s](auto&) { - m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path); - if (m_current_editor_in_execution) - m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1); - m_debug_info_widget->update_state(*Debugger::the().session(), regs); - m_debug_info_widget->set_debug_actions_enabled(true); - m_disassembly_widget->update_state(*Debugger::the().session(), regs); - HackStudioWidget::reveal_action_tab(*m_debug_info_widget); - })); + deferred_invoke([this, source_position, ®s] { + m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path); + if (m_current_editor_in_execution) + m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1); + m_debug_info_widget->update_state(*Debugger::the().session(), regs); + m_debug_info_widget->set_debug_actions_enabled(true); + m_disassembly_widget->update_state(*Debugger::the().session(), regs); + HackStudioWidget::reveal_action_tab(*m_debug_info_widget); + }); Core::EventLoop::wake(); return Debugger::HasControlPassedToUser::Yes; }, [this]() { - Core::EventLoop::main().post_event(*window(), make([this](auto&) { + deferred_invoke([this] { m_debug_info_widget->set_debug_actions_enabled(false); if (m_current_editor_in_execution) m_current_editor_in_execution->editor().clear_execution_position(); - })); + }); Core::EventLoop::wake(); }, [this]() { - Core::EventLoop::main().post_event(*window(), make([this](auto&) { + deferred_invoke([this] { m_debug_info_widget->set_debug_actions_enabled(false); if (m_current_editor_in_execution) m_current_editor_in_execution->editor().clear_execution_position(); @@ -834,7 +831,7 @@ void HackStudioWidget::initialize_debugger() HackStudioWidget::hide_action_tabs(); GUI::MessageBox::show(window(), "Program Exited", "Debugger", GUI::MessageBox::Type::Information); - })); + }); Core::EventLoop::wake(); }); } diff --git a/Userland/Libraries/LibCore/DeferredInvocationContext.h b/Userland/Libraries/LibCore/DeferredInvocationContext.h new file mode 100644 index 0000000000..b0e0e19c2b --- /dev/null +++ b/Userland/Libraries/LibCore/DeferredInvocationContext.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2018-2020, sin-ack + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Core { + +class DeferredInvocationContext final : public Core::Object { + C_OBJECT(DeferredInvocationContext) +private: + DeferredInvocationContext() { } +}; + +} diff --git a/Userland/Libraries/LibCore/Event.h b/Userland/Libraries/LibCore/Event.h index 4de780a5f9..bc7045704b 100644 --- a/Userland/Libraries/LibCore/Event.h +++ b/Userland/Libraries/LibCore/Event.h @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace Core { @@ -50,14 +51,16 @@ class DeferredInvocationEvent : public Event { friend class EventLoop; public: - DeferredInvocationEvent(Function invokee) + DeferredInvocationEvent(NonnullRefPtr context, Function invokee) : Event(Event::Type::DeferredInvoke) + , m_context(move(context)) , m_invokee(move(invokee)) { } private: - Function m_invokee; + NonnullRefPtr m_context; + Function m_invokee; }; class TimerEvent final : public Event { diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp index 436aba6ee5..081c34acdb 100644 --- a/Userland/Libraries/LibCore/EventLoop.cpp +++ b/Userland/Libraries/LibCore/EventLoop.cpp @@ -394,7 +394,7 @@ void EventLoop::pump(WaitMode mode) } } else if (event.type() == Event::Type::DeferredInvoke) { dbgln_if(DEFERRED_INVOKE_DEBUG, "DeferredInvoke: receiver = {}", *receiver); - static_cast(event).m_invokee(*receiver); + static_cast(event).m_invokee(); } else { NonnullRefPtr protector(*receiver); receiver->dispatch_event(event); diff --git a/Userland/Libraries/LibCore/EventLoop.h b/Userland/Libraries/LibCore/EventLoop.h index 116d23c023..047de38234 100644 --- a/Userland/Libraries/LibCore/EventLoop.h +++ b/Userland/Libraries/LibCore/EventLoop.h @@ -11,9 +11,12 @@ #include #include #include +#include #include #include #include +#include +#include #include #include #include @@ -76,6 +79,12 @@ public: static bool has_been_instantiated(); + void deferred_invoke(Function invokee) + { + auto context = DeferredInvocationContext::construct(); + post_event(context, make(context, move(invokee))); + } + private: void wait_for_event(WaitMode); Optional