Application.cpp 8.2 KB

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