Dialog.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/EventLoop.h>
  8. #include <LibGUI/Desktop.h>
  9. #include <LibGUI/Dialog.h>
  10. #include <LibGUI/Event.h>
  11. namespace GUI {
  12. Dialog::Dialog(Window* parent_window, ScreenPosition screen_position)
  13. : Window(parent_window)
  14. , m_screen_position(screen_position)
  15. {
  16. set_modal(true);
  17. set_minimizable(false);
  18. }
  19. Dialog::ExecResult Dialog::exec()
  20. {
  21. VERIFY(!m_event_loop);
  22. m_event_loop = make<Core::EventLoop>();
  23. auto desktop_rect = Desktop::the().rect();
  24. auto window_rect = rect();
  25. auto top_align = [](Gfx::Rect<int>& rect) { rect.set_y(32); };
  26. auto bottom_align = [this, desktop_rect](Gfx::Rect<int>& rect) { rect.set_y(desktop_rect.height() - Desktop::the().taskbar_height() - height() - 12); };
  27. auto left_align = [](Gfx::Rect<int>& rect) { rect.set_x(12); };
  28. auto right_align = [this, desktop_rect](Gfx::Rect<int>& rect) { rect.set_x(desktop_rect.width() - width() - 12); };
  29. switch (m_screen_position) {
  30. case CenterWithinParent:
  31. if (parent() && is<Window>(parent())) {
  32. auto& parent_window = *static_cast<Window*>(parent());
  33. if (parent_window.is_visible()) {
  34. window_rect.center_within(parent_window.rect());
  35. break;
  36. }
  37. }
  38. [[fallthrough]]; // Fall back to `Center` if parent window is invalid or not visible
  39. case Center:
  40. window_rect.center_within(desktop_rect);
  41. break;
  42. case CenterLeft:
  43. left_align(window_rect);
  44. window_rect.center_vertically_within(desktop_rect);
  45. break;
  46. case CenterRight:
  47. right_align(window_rect);
  48. window_rect.center_vertically_within(desktop_rect);
  49. break;
  50. case TopLeft:
  51. left_align(window_rect);
  52. top_align(window_rect);
  53. break;
  54. case TopCenter:
  55. window_rect.center_horizontally_within(desktop_rect);
  56. top_align(window_rect);
  57. break;
  58. case TopRight:
  59. right_align(window_rect);
  60. top_align(window_rect);
  61. break;
  62. case BottomLeft:
  63. left_align(window_rect);
  64. bottom_align(window_rect);
  65. break;
  66. case BottomCenter:
  67. window_rect.center_horizontally_within(desktop_rect);
  68. bottom_align(window_rect);
  69. break;
  70. case BottomRight:
  71. right_align(window_rect);
  72. bottom_align(window_rect);
  73. break;
  74. }
  75. set_rect(window_rect);
  76. show();
  77. auto result = m_event_loop->exec();
  78. m_event_loop = nullptr;
  79. dbgln("{}: Event loop returned with result {}", *this, result);
  80. remove_from_parent();
  81. return static_cast<ExecResult>(result);
  82. }
  83. void Dialog::done(ExecResult result)
  84. {
  85. if (!m_event_loop)
  86. return;
  87. m_result = result;
  88. dbgln("{}: Quit event loop with result {}", *this, to_underlying(result));
  89. m_event_loop->quit(to_underlying(result));
  90. }
  91. void Dialog::event(Core::Event& event)
  92. {
  93. if (event.type() == Event::KeyUp || event.type() == Event::KeyDown) {
  94. auto& key_event = static_cast<KeyEvent&>(event);
  95. if (key_event.key() == KeyCode::Key_Escape) {
  96. done(ExecResult::Cancel);
  97. event.accept();
  98. return;
  99. }
  100. }
  101. Window::event(event);
  102. }
  103. void Dialog::close()
  104. {
  105. Window::close();
  106. done(ExecResult::Cancel);
  107. }
  108. }