Application.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/NeverDestroyed.h>
  7. #include <LibCore/EventLoop.h>
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/Clipboard.h>
  11. #include <LibGUI/Desktop.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/Menubar.h>
  14. #include <LibGUI/Painter.h>
  15. #include <LibGUI/Window.h>
  16. #include <LibGUI/WindowServerConnection.h>
  17. #include <LibGfx/Font.h>
  18. #include <LibGfx/Palette.h>
  19. namespace GUI {
  20. class Application::TooltipWindow final : public Window {
  21. C_OBJECT(TooltipWindow);
  22. public:
  23. void set_tooltip(const String& tooltip)
  24. {
  25. m_label->set_text(Gfx::parse_ampersand_string(tooltip));
  26. int tooltip_width = m_label->min_width() + 10;
  27. Gfx::IntRect desktop_rect = Desktop::the().rect();
  28. if (tooltip_width > desktop_rect.width())
  29. tooltip_width = desktop_rect.width();
  30. set_rect(rect().x(), rect().y(), tooltip_width, m_label->font().glyph_height() + 8);
  31. }
  32. private:
  33. TooltipWindow()
  34. {
  35. set_window_type(WindowType::Tooltip);
  36. m_label = set_main_widget<Label>();
  37. m_label->set_background_role(Gfx::ColorRole::Tooltip);
  38. m_label->set_foreground_role(Gfx::ColorRole::TooltipText);
  39. m_label->set_fill_with_background_color(true);
  40. m_label->set_frame_thickness(1);
  41. m_label->set_frame_shape(Gfx::FrameShape::Container);
  42. m_label->set_frame_shadow(Gfx::FrameShadow::Plain);
  43. m_label->set_autosize(true);
  44. }
  45. RefPtr<Label> m_label;
  46. };
  47. static NeverDestroyed<WeakPtr<Application>> s_the;
  48. Application* Application::the()
  49. {
  50. // NOTE: If we don't explicitly call revoke_weak_ptrs() in the
  51. // ~Application destructor, we would have to change this to
  52. // return s_the->strong_ref().ptr();
  53. // This is because this is using the unsafe operator*/operator->
  54. // that do not have the ability to check the ref count!
  55. return *s_the;
  56. }
  57. Application::Application(int argc, char** argv)
  58. {
  59. VERIFY(!*s_the);
  60. *s_the = *this;
  61. m_event_loop = make<Core::EventLoop>();
  62. WindowServerConnection::the();
  63. Clipboard::initialize({});
  64. if (argc > 0)
  65. m_invoked_as = argv[0];
  66. if (getenv("GUI_FOCUS_DEBUG"))
  67. m_focus_debugging_enabled = true;
  68. if (getenv("GUI_DND_DEBUG"))
  69. m_dnd_debugging_enabled = true;
  70. for (int i = 1; i < argc; i++) {
  71. String arg(argv[i]);
  72. m_args.append(move(arg));
  73. }
  74. m_tooltip_show_timer = Core::Timer::create_single_shot(700, [this] {
  75. tooltip_show_timer_did_fire();
  76. });
  77. m_tooltip_hide_timer = Core::Timer::create_single_shot(50, [this] {
  78. tooltip_hide_timer_did_fire();
  79. });
  80. }
  81. Application::~Application()
  82. {
  83. revoke_weak_ptrs();
  84. }
  85. int Application::exec()
  86. {
  87. return m_event_loop->exec();
  88. }
  89. void Application::quit(int exit_code)
  90. {
  91. m_event_loop->quit(exit_code);
  92. }
  93. void Application::register_global_shortcut_action(Badge<Action>, Action& action)
  94. {
  95. m_global_shortcut_actions.set(action.shortcut(), &action);
  96. }
  97. void Application::unregister_global_shortcut_action(Badge<Action>, Action& action)
  98. {
  99. m_global_shortcut_actions.remove(action.shortcut());
  100. }
  101. Action* Application::action_for_key_event(const KeyEvent& event)
  102. {
  103. auto it = m_global_shortcut_actions.find(Shortcut(event.modifiers(), (KeyCode)event.key()));
  104. if (it == m_global_shortcut_actions.end())
  105. return nullptr;
  106. return (*it).value;
  107. }
  108. void Application::show_tooltip(String tooltip, const Widget* tooltip_source_widget)
  109. {
  110. m_tooltip_source_widget = tooltip_source_widget;
  111. if (!m_tooltip_window) {
  112. m_tooltip_window = TooltipWindow::construct();
  113. m_tooltip_window->set_double_buffering_enabled(false);
  114. }
  115. m_tooltip_window->set_tooltip(move(tooltip));
  116. if (m_tooltip_window->is_visible()) {
  117. tooltip_show_timer_did_fire();
  118. m_tooltip_show_timer->stop();
  119. m_tooltip_hide_timer->stop();
  120. } else {
  121. m_tooltip_show_timer->restart();
  122. m_tooltip_hide_timer->stop();
  123. }
  124. }
  125. void Application::hide_tooltip()
  126. {
  127. m_tooltip_show_timer->stop();
  128. m_tooltip_hide_timer->start();
  129. }
  130. void Application::did_create_window(Badge<Window>)
  131. {
  132. if (m_event_loop->was_exit_requested())
  133. m_event_loop->unquit();
  134. }
  135. void Application::did_delete_last_window(Badge<Window>)
  136. {
  137. if (m_quit_when_last_window_deleted)
  138. m_event_loop->quit(0);
  139. }
  140. void Application::set_system_palette(Core::AnonymousBuffer& buffer)
  141. {
  142. if (!m_system_palette)
  143. m_system_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(buffer);
  144. else
  145. m_system_palette->replace_internal_buffer({}, buffer);
  146. if (!m_palette)
  147. m_palette = m_system_palette;
  148. }
  149. void Application::set_palette(const Palette& palette)
  150. {
  151. m_palette = palette.impl();
  152. }
  153. Gfx::Palette Application::palette() const
  154. {
  155. return Palette(*m_palette);
  156. }
  157. void Application::tooltip_show_timer_did_fire()
  158. {
  159. VERIFY(m_tooltip_window);
  160. Gfx::IntRect desktop_rect = Desktop::the().rect();
  161. const int margin = 30;
  162. Gfx::IntPoint adjusted_pos = WindowServerConnection::the().get_global_cursor_position();
  163. adjusted_pos.translate_by(0, 18);
  164. if (adjusted_pos.x() + m_tooltip_window->width() >= desktop_rect.width() - margin) {
  165. adjusted_pos = adjusted_pos.translated(-m_tooltip_window->width(), 0);
  166. }
  167. if (adjusted_pos.y() + m_tooltip_window->height() >= desktop_rect.height() - margin) {
  168. adjusted_pos = adjusted_pos.translated(0, -(m_tooltip_window->height() * 2));
  169. }
  170. if (adjusted_pos.x() < 0)
  171. adjusted_pos.set_x(0);
  172. m_tooltip_window->move_to(adjusted_pos);
  173. m_tooltip_window->show();
  174. }
  175. void Application::tooltip_hide_timer_did_fire()
  176. {
  177. m_tooltip_source_widget = nullptr;
  178. if (m_tooltip_window)
  179. m_tooltip_window->hide();
  180. }
  181. void Application::window_did_become_active(Badge<Window>, Window& window)
  182. {
  183. m_active_window = window.make_weak_ptr<Window>();
  184. }
  185. void Application::window_did_become_inactive(Badge<Window>, Window& window)
  186. {
  187. if (m_active_window.ptr() != &window)
  188. return;
  189. m_active_window = nullptr;
  190. }
  191. void Application::set_pending_drop_widget(Widget* widget)
  192. {
  193. if (m_pending_drop_widget == widget)
  194. return;
  195. if (m_pending_drop_widget)
  196. m_pending_drop_widget->update();
  197. m_pending_drop_widget = widget;
  198. if (m_pending_drop_widget)
  199. m_pending_drop_widget->update();
  200. }
  201. void Application::set_drag_hovered_widget_impl(Widget* widget, const Gfx::IntPoint& position, Vector<String> mime_types)
  202. {
  203. if (widget == m_drag_hovered_widget)
  204. return;
  205. if (m_drag_hovered_widget) {
  206. Event leave_event(Event::DragLeave);
  207. m_drag_hovered_widget->dispatch_event(leave_event, m_drag_hovered_widget->window());
  208. }
  209. set_pending_drop_widget(nullptr);
  210. m_drag_hovered_widget = widget;
  211. if (m_drag_hovered_widget) {
  212. DragEvent enter_event(Event::DragEnter, position, move(mime_types));
  213. enter_event.ignore();
  214. m_drag_hovered_widget->dispatch_event(enter_event, m_drag_hovered_widget->window());
  215. if (enter_event.is_accepted())
  216. set_pending_drop_widget(m_drag_hovered_widget);
  217. }
  218. }
  219. void Application::notify_drag_cancelled(Badge<WindowServerConnection>)
  220. {
  221. set_drag_hovered_widget_impl(nullptr);
  222. }
  223. void Application::event(Core::Event& event)
  224. {
  225. if (event.type() == GUI::Event::ActionEnter || event.type() == GUI::Event::ActionLeave) {
  226. auto& action_event = static_cast<ActionEvent&>(event);
  227. auto& action = action_event.action();
  228. if (action_event.type() == GUI::Event::ActionEnter) {
  229. if (on_action_enter)
  230. on_action_enter(action);
  231. } else {
  232. if (on_action_leave)
  233. on_action_leave(action);
  234. }
  235. }
  236. Object::event(event);
  237. }
  238. }