GApplication.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <LibGUI/GApplication.h>
  2. #include <LibGUI/GEventLoop.h>
  3. #include <LibGUI/GMenuBar.h>
  4. #include <LibGUI/GAction.h>
  5. static GApplication* s_the;
  6. GApplication& GApplication::the()
  7. {
  8. ASSERT(s_the);
  9. return *s_the;
  10. }
  11. GApplication::GApplication(int argc, char** argv)
  12. {
  13. (void)argc;
  14. (void)argv;
  15. ASSERT(!s_the);
  16. s_the = this;
  17. m_event_loop = make<GEventLoop>();
  18. }
  19. GApplication::~GApplication()
  20. {
  21. }
  22. int GApplication::exec()
  23. {
  24. return m_event_loop->exec();
  25. }
  26. void GApplication::quit(int exit_code)
  27. {
  28. m_event_loop->quit(exit_code);
  29. }
  30. void GApplication::set_menubar(OwnPtr<GMenuBar>&& menubar)
  31. {
  32. if (m_menubar)
  33. m_menubar->notify_removed_from_application(Badge<GApplication>());
  34. m_menubar = move(menubar);
  35. if (m_menubar)
  36. m_menubar->notify_added_to_application(Badge<GApplication>());
  37. }
  38. void GApplication::register_shortcut_action(Badge<GAction>, GAction& action)
  39. {
  40. m_shortcut_actions.set(action.shortcut(), &action);
  41. }
  42. void GApplication::unregister_shortcut_action(Badge<GAction>, GAction& action)
  43. {
  44. m_shortcut_actions.remove(action.shortcut());
  45. }
  46. GAction* GApplication::action_for_key_event(const GKeyEvent& event)
  47. {
  48. auto it = m_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key()));
  49. if (it == m_shortcut_actions.end())
  50. return nullptr;
  51. return (*it).value;
  52. }