Application.cpp 9.2 KB

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