Dialog.cpp 3.3 KB

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