Dialog.cpp 5.0 KB

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