Dialog.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <LibGfx/Palette.h>
  12. namespace GUI {
  13. Dialog::Dialog(Window* parent_window, ScreenPosition screen_position)
  14. : Window(parent_window)
  15. , m_screen_position(screen_position)
  16. {
  17. set_modal(true);
  18. set_minimizable(false);
  19. }
  20. Dialog::ExecResult Dialog::exec()
  21. {
  22. VERIFY(!m_event_loop);
  23. m_event_loop = make<Core::EventLoop>();
  24. auto desktop_rect = Desktop::the().rect();
  25. auto window_rect = rect();
  26. auto top_align = [](Gfx::Rect<int>& rect) { rect.set_y(32); };
  27. auto bottom_align = [this, desktop_rect](Gfx::Rect<int>& rect) { rect.set_y(desktop_rect.height() - Desktop::the().taskbar_height() - height() - 12); };
  28. auto left_align = [](Gfx::Rect<int>& rect) { rect.set_x(12); };
  29. auto right_align = [this, desktop_rect](Gfx::Rect<int>& rect) { rect.set_x(desktop_rect.width() - width() - 12); };
  30. switch (m_screen_position) {
  31. case ScreenPosition::CenterWithinParent:
  32. if (parent() && is<Window>(parent())) {
  33. auto& parent_window = *static_cast<Window*>(parent());
  34. if (parent_window.is_visible()) {
  35. // Check the dialog's positiom against the Desktop's rect and reposition it to be entirely visible.
  36. // If the dialog is larger than the desktop's rect just center it.
  37. window_rect.center_within(parent_window.rect());
  38. if (window_rect.size().width() < desktop_rect.size().width() && window_rect.size().height() < desktop_rect.size().height()) {
  39. auto taskbar_top_y = desktop_rect.bottom() - Desktop::the().taskbar_height();
  40. auto palette = GUI::Application::the()->palette();
  41. auto border_thickness = palette.window_border_thickness();
  42. auto top_border_title_thickness = border_thickness + palette.window_title_height();
  43. if (window_rect.top() < top_border_title_thickness) {
  44. window_rect.set_y(top_border_title_thickness);
  45. }
  46. if (window_rect.right() + border_thickness > desktop_rect.right()) {
  47. window_rect.translate_by((window_rect.right() + border_thickness - desktop_rect.right()) * -1, 0);
  48. }
  49. if (window_rect.bottom() + border_thickness > taskbar_top_y) {
  50. window_rect.translate_by(0, (window_rect.bottom() + border_thickness - taskbar_top_y) * -1);
  51. }
  52. if (window_rect.left() - border_thickness < 0) {
  53. window_rect.set_x(0 + border_thickness);
  54. }
  55. }
  56. break;
  57. }
  58. }
  59. [[fallthrough]]; // Fall back to `Center` if parent window is invalid or not visible
  60. case ScreenPosition::Center:
  61. window_rect.center_within(desktop_rect);
  62. break;
  63. case ScreenPosition::CenterLeft:
  64. left_align(window_rect);
  65. window_rect.center_vertically_within(desktop_rect);
  66. break;
  67. case ScreenPosition::CenterRight:
  68. right_align(window_rect);
  69. window_rect.center_vertically_within(desktop_rect);
  70. break;
  71. case ScreenPosition::TopLeft:
  72. left_align(window_rect);
  73. top_align(window_rect);
  74. break;
  75. case ScreenPosition::TopCenter:
  76. window_rect.center_horizontally_within(desktop_rect);
  77. top_align(window_rect);
  78. break;
  79. case ScreenPosition::TopRight:
  80. right_align(window_rect);
  81. top_align(window_rect);
  82. break;
  83. case ScreenPosition::BottomLeft:
  84. left_align(window_rect);
  85. bottom_align(window_rect);
  86. break;
  87. case ScreenPosition::BottomCenter:
  88. window_rect.center_horizontally_within(desktop_rect);
  89. bottom_align(window_rect);
  90. break;
  91. case ScreenPosition::BottomRight:
  92. right_align(window_rect);
  93. bottom_align(window_rect);
  94. break;
  95. }
  96. set_rect(window_rect);
  97. show();
  98. auto result = m_event_loop->exec();
  99. m_event_loop = nullptr;
  100. dbgln("{}: Event loop returned with result {}", *this, result);
  101. remove_from_parent();
  102. return static_cast<ExecResult>(result);
  103. }
  104. void Dialog::done(ExecResult result)
  105. {
  106. if (!m_event_loop)
  107. return;
  108. m_result = result;
  109. dbgln("{}: Quit event loop with result {}", *this, to_underlying(result));
  110. m_event_loop->quit(to_underlying(result));
  111. }
  112. void Dialog::event(Core::Event& event)
  113. {
  114. if (event.type() == Event::KeyUp || event.type() == Event::KeyDown) {
  115. auto& key_event = static_cast<KeyEvent&>(event);
  116. if (key_event.key() == KeyCode::Key_Escape) {
  117. done(ExecResult::Cancel);
  118. event.accept();
  119. return;
  120. }
  121. }
  122. Window::event(event);
  123. }
  124. void Dialog::close()
  125. {
  126. Window::close();
  127. done(ExecResult::Cancel);
  128. }
  129. }