Application.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/NeverDestroyed.h>
  27. #include <LibCore/EventLoop.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/Application.h>
  30. #include <LibGUI/Clipboard.h>
  31. #include <LibGUI/Desktop.h>
  32. #include <LibGUI/Label.h>
  33. #include <LibGUI/MenuBar.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/Window.h>
  36. #include <LibGUI/WindowServerConnection.h>
  37. #include <LibGfx/Font.h>
  38. #include <LibGfx/Palette.h>
  39. namespace GUI {
  40. class Application::TooltipWindow final : public Window {
  41. C_OBJECT(TooltipWindow);
  42. public:
  43. void set_tooltip(String tooltip)
  44. {
  45. // FIXME: Add some kind of GUI::Label auto-sizing feature.
  46. int text_width = m_label->font().width(tooltip);
  47. set_rect(rect().x(), rect().y(), text_width + 10, m_label->font().glyph_height() + 8);
  48. m_label->set_text(move(tooltip));
  49. }
  50. private:
  51. TooltipWindow()
  52. {
  53. set_window_type(WindowType::Tooltip);
  54. m_label = set_main_widget<Label>();
  55. m_label->set_background_role(Gfx::ColorRole::Tooltip);
  56. m_label->set_foreground_role(Gfx::ColorRole::TooltipText);
  57. m_label->set_fill_with_background_color(true);
  58. m_label->set_frame_thickness(1);
  59. m_label->set_frame_shape(Gfx::FrameShape::Container);
  60. m_label->set_frame_shadow(Gfx::FrameShadow::Plain);
  61. }
  62. RefPtr<Label> m_label;
  63. };
  64. static NeverDestroyed<WeakPtr<Application>> s_the;
  65. Application* Application::the()
  66. {
  67. // NOTE: If we don't explicitly call revoke_weak_ptrs() in the
  68. // ~Application destructor, we would have to change this to
  69. // return s_the->strong_ref().ptr();
  70. // This is because this is using the unsafe operator*/operator->
  71. // that do not have the ability to check the ref count!
  72. return *s_the;
  73. }
  74. Application::Application(int argc, char** argv)
  75. {
  76. VERIFY(!*s_the);
  77. *s_the = *this;
  78. m_event_loop = make<Core::EventLoop>();
  79. WindowServerConnection::the();
  80. Clipboard::initialize({});
  81. if (argc > 0)
  82. m_invoked_as = argv[0];
  83. if (getenv("GUI_FOCUS_DEBUG"))
  84. m_focus_debugging_enabled = true;
  85. if (getenv("GUI_DND_DEBUG"))
  86. m_dnd_debugging_enabled = true;
  87. for (int i = 1; i < argc; i++) {
  88. String arg(argv[i]);
  89. m_args.append(move(arg));
  90. }
  91. m_tooltip_show_timer = Core::Timer::create_single_shot(700, [this] {
  92. tooltip_show_timer_did_fire();
  93. });
  94. m_tooltip_hide_timer = Core::Timer::create_single_shot(50, [this] {
  95. tooltip_hide_timer_did_fire();
  96. });
  97. }
  98. Application::~Application()
  99. {
  100. revoke_weak_ptrs();
  101. }
  102. int Application::exec()
  103. {
  104. return m_event_loop->exec();
  105. }
  106. void Application::quit(int exit_code)
  107. {
  108. m_event_loop->quit(exit_code);
  109. }
  110. void Application::set_menubar(RefPtr<MenuBar> menubar)
  111. {
  112. if (m_menubar)
  113. m_menubar->notify_removed_from_application({});
  114. m_menubar = move(menubar);
  115. if (m_menubar)
  116. m_menubar->notify_added_to_application({});
  117. }
  118. void Application::register_global_shortcut_action(Badge<Action>, Action& action)
  119. {
  120. m_global_shortcut_actions.set(action.shortcut(), &action);
  121. }
  122. void Application::unregister_global_shortcut_action(Badge<Action>, Action& action)
  123. {
  124. m_global_shortcut_actions.remove(action.shortcut());
  125. }
  126. Action* Application::action_for_key_event(const KeyEvent& event)
  127. {
  128. auto it = m_global_shortcut_actions.find(Shortcut(event.modifiers(), (KeyCode)event.key()));
  129. if (it == m_global_shortcut_actions.end())
  130. return nullptr;
  131. return (*it).value;
  132. }
  133. void Application::show_tooltip(String tooltip, const Widget* tooltip_source_widget)
  134. {
  135. m_tooltip_source_widget = tooltip_source_widget;
  136. if (!m_tooltip_window) {
  137. m_tooltip_window = TooltipWindow::construct();
  138. m_tooltip_window->set_double_buffering_enabled(false);
  139. }
  140. m_tooltip_window->set_tooltip(move(tooltip));
  141. if (m_tooltip_window->is_visible()) {
  142. tooltip_show_timer_did_fire();
  143. m_tooltip_show_timer->stop();
  144. m_tooltip_hide_timer->stop();
  145. } else {
  146. m_tooltip_show_timer->restart();
  147. m_tooltip_hide_timer->stop();
  148. }
  149. }
  150. void Application::hide_tooltip()
  151. {
  152. m_tooltip_show_timer->stop();
  153. m_tooltip_hide_timer->start();
  154. }
  155. void Application::did_create_window(Badge<Window>)
  156. {
  157. if (m_event_loop->was_exit_requested())
  158. m_event_loop->unquit();
  159. }
  160. void Application::did_delete_last_window(Badge<Window>)
  161. {
  162. if (m_quit_when_last_window_deleted)
  163. m_event_loop->quit(0);
  164. }
  165. void Application::set_system_palette(Core::AnonymousBuffer& buffer)
  166. {
  167. if (!m_system_palette)
  168. m_system_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(buffer);
  169. else
  170. m_system_palette->replace_internal_buffer({}, buffer);
  171. if (!m_palette)
  172. m_palette = m_system_palette;
  173. }
  174. void Application::set_palette(const Palette& palette)
  175. {
  176. m_palette = palette.impl();
  177. }
  178. Gfx::Palette Application::palette() const
  179. {
  180. return Palette(*m_palette);
  181. }
  182. void Application::tooltip_show_timer_did_fire()
  183. {
  184. VERIFY(m_tooltip_window);
  185. Gfx::IntRect desktop_rect = Desktop::the().rect();
  186. const int margin = 30;
  187. Gfx::IntPoint adjusted_pos = WindowServerConnection::the().send_sync<Messages::WindowServer::GetGlobalCursorPosition>()->position();
  188. adjusted_pos.move_by(0, 18);
  189. if (adjusted_pos.x() + m_tooltip_window->width() >= desktop_rect.width() - margin) {
  190. adjusted_pos = adjusted_pos.translated(-m_tooltip_window->width(), 0);
  191. }
  192. if (adjusted_pos.y() + m_tooltip_window->height() >= desktop_rect.height() - margin) {
  193. adjusted_pos = adjusted_pos.translated(0, -(m_tooltip_window->height() * 2));
  194. }
  195. m_tooltip_window->move_to(adjusted_pos);
  196. m_tooltip_window->show();
  197. }
  198. void Application::tooltip_hide_timer_did_fire()
  199. {
  200. m_tooltip_source_widget = nullptr;
  201. if (m_tooltip_window)
  202. m_tooltip_window->hide();
  203. }
  204. void Application::window_did_become_active(Badge<Window>, Window& window)
  205. {
  206. m_active_window = window.make_weak_ptr<Window>();
  207. }
  208. void Application::window_did_become_inactive(Badge<Window>, Window& window)
  209. {
  210. if (m_active_window.ptr() != &window)
  211. return;
  212. m_active_window = nullptr;
  213. }
  214. void Application::set_pending_drop_widget(Widget* widget)
  215. {
  216. if (m_pending_drop_widget == widget)
  217. return;
  218. if (m_pending_drop_widget)
  219. m_pending_drop_widget->update();
  220. m_pending_drop_widget = widget;
  221. if (m_pending_drop_widget)
  222. m_pending_drop_widget->update();
  223. }
  224. void Application::set_drag_hovered_widget_impl(Widget* widget, const Gfx::IntPoint& position, Vector<String> mime_types)
  225. {
  226. if (widget == m_drag_hovered_widget)
  227. return;
  228. if (m_drag_hovered_widget) {
  229. Event leave_event(Event::DragLeave);
  230. m_drag_hovered_widget->dispatch_event(leave_event, m_drag_hovered_widget->window());
  231. }
  232. set_pending_drop_widget(nullptr);
  233. m_drag_hovered_widget = widget;
  234. if (m_drag_hovered_widget) {
  235. DragEvent enter_event(Event::DragEnter, position, move(mime_types));
  236. enter_event.ignore();
  237. m_drag_hovered_widget->dispatch_event(enter_event, m_drag_hovered_widget->window());
  238. if (enter_event.is_accepted())
  239. set_pending_drop_widget(m_drag_hovered_widget);
  240. }
  241. }
  242. void Application::notify_drag_cancelled(Badge<WindowServerConnection>)
  243. {
  244. set_drag_hovered_widget_impl(nullptr);
  245. }
  246. }