Application.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/NeverDestroyed.h>
  7. #include <LibConfig/Client.h>
  8. #include <LibCore/EventLoop.h>
  9. #include <LibGUI/Action.h>
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/Clipboard.h>
  12. #include <LibGUI/ConnectionToWindowServer.h>
  13. #include <LibGUI/Desktop.h>
  14. #include <LibGUI/Label.h>
  15. #include <LibGUI/Menubar.h>
  16. #include <LibGUI/Painter.h>
  17. #include <LibGUI/Window.h>
  18. #include <LibGfx/Font/Font.h>
  19. #include <LibGfx/Palette.h>
  20. namespace GUI {
  21. class Application::TooltipWindow final : public Window {
  22. C_OBJECT(TooltipWindow);
  23. public:
  24. void set_tooltip(String tooltip)
  25. {
  26. m_label->set_text(move(tooltip));
  27. int tooltip_width = m_label->effective_min_size().width().as_int() + 10;
  28. int line_count = m_label->text().count("\n"sv);
  29. int font_size = m_label->font().pixel_size_rounded_up();
  30. int tooltip_height = font_size * (1 + line_count) + ((font_size + 1) / 2) * line_count + 8;
  31. Gfx::IntRect desktop_rect = Desktop::the().rect();
  32. if (tooltip_width > desktop_rect.width())
  33. tooltip_width = desktop_rect.width();
  34. set_rect(rect().x(), rect().y(), tooltip_width, tooltip_height);
  35. }
  36. private:
  37. TooltipWindow()
  38. {
  39. set_window_type(WindowType::Tooltip);
  40. set_obey_widget_min_size(false);
  41. m_label = set_main_widget<Label>();
  42. m_label->set_background_role(Gfx::ColorRole::Tooltip);
  43. m_label->set_foreground_role(Gfx::ColorRole::TooltipText);
  44. m_label->set_fill_with_background_color(true);
  45. m_label->set_frame_style(Gfx::FrameStyle::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. ErrorOr<NonnullRefPtr<Application>> Application::create(Main::Arguments const& arguments)
  61. {
  62. if (*s_the)
  63. return Error::from_string_literal("An Application has already been created for this process!");
  64. auto application = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Application {}));
  65. *s_the = *application;
  66. application->m_event_loop = TRY(try_make<Core::EventLoop>());
  67. ConnectionToWindowServer::the();
  68. TRY(Clipboard::initialize({}));
  69. if (arguments.argc > 0)
  70. application->m_invoked_as = arguments.argv[0];
  71. if (getenv("GUI_FOCUS_DEBUG"))
  72. application->m_focus_debugging_enabled = true;
  73. if (getenv("GUI_HOVER_DEBUG"))
  74. application->m_hover_debugging_enabled = true;
  75. if (getenv("GUI_DND_DEBUG"))
  76. application->m_dnd_debugging_enabled = true;
  77. if (!arguments.strings.is_empty()) {
  78. for (auto arg : arguments.strings.slice(1))
  79. TRY(application->m_args.try_append(arg));
  80. }
  81. application->m_tooltip_show_timer = TRY(Core::Timer::create_single_shot(700, [weak_application = application->make_weak_ptr<Application>()] {
  82. weak_application->request_tooltip_show();
  83. }));
  84. application->m_tooltip_hide_timer = TRY(Core::Timer::create_single_shot(50, [weak_application = application->make_weak_ptr<Application>()] {
  85. weak_application->tooltip_hide_timer_did_fire();
  86. }));
  87. return application;
  88. }
  89. static bool s_in_teardown;
  90. bool Application::in_teardown()
  91. {
  92. return s_in_teardown;
  93. }
  94. Application::~Application()
  95. {
  96. s_in_teardown = true;
  97. revoke_weak_ptrs();
  98. }
  99. int Application::exec()
  100. {
  101. return m_event_loop->exec();
  102. }
  103. void Application::quit(int exit_code)
  104. {
  105. m_event_loop->quit(exit_code);
  106. }
  107. void Application::register_global_shortcut_action(Badge<Action>, Action& action)
  108. {
  109. m_global_shortcut_actions.set(action.shortcut(), &action);
  110. m_global_shortcut_actions.set(action.alternate_shortcut(), &action);
  111. }
  112. void Application::unregister_global_shortcut_action(Badge<Action>, Action& action)
  113. {
  114. m_global_shortcut_actions.remove(action.shortcut());
  115. m_global_shortcut_actions.remove(action.alternate_shortcut());
  116. }
  117. Action* Application::action_for_shortcut(Shortcut const& shortcut) const
  118. {
  119. auto it = m_global_shortcut_actions.find(shortcut);
  120. if (it == m_global_shortcut_actions.end())
  121. return nullptr;
  122. return (*it).value;
  123. }
  124. void Application::show_tooltip(String tooltip, Widget const* tooltip_source_widget)
  125. {
  126. if (!Desktop::the().system_effects().tooltips())
  127. return;
  128. m_tooltip_source_widget = tooltip_source_widget;
  129. if (!m_tooltip_window) {
  130. m_tooltip_window = TooltipWindow::construct();
  131. m_tooltip_window->set_double_buffering_enabled(false);
  132. }
  133. m_tooltip_window->set_tooltip(move(tooltip));
  134. if (m_tooltip_window->is_visible()) {
  135. request_tooltip_show();
  136. m_tooltip_show_timer->stop();
  137. m_tooltip_hide_timer->stop();
  138. } else {
  139. m_tooltip_show_timer->restart();
  140. m_tooltip_hide_timer->stop();
  141. }
  142. }
  143. void Application::show_tooltip_immediately(String tooltip, Widget const* tooltip_source_widget)
  144. {
  145. if (!Desktop::the().system_effects().tooltips())
  146. return;
  147. m_tooltip_source_widget = tooltip_source_widget;
  148. if (!m_tooltip_window) {
  149. m_tooltip_window = TooltipWindow::construct();
  150. m_tooltip_window->set_double_buffering_enabled(false);
  151. }
  152. m_tooltip_window->set_tooltip(move(tooltip));
  153. request_tooltip_show();
  154. m_tooltip_show_timer->stop();
  155. m_tooltip_hide_timer->stop();
  156. }
  157. void Application::hide_tooltip()
  158. {
  159. m_tooltip_show_timer->stop();
  160. m_tooltip_hide_timer->start();
  161. }
  162. void Application::did_create_window(Badge<Window>)
  163. {
  164. if (m_event_loop->was_exit_requested())
  165. m_event_loop->unquit();
  166. }
  167. void Application::did_delete_last_window(Badge<Window>)
  168. {
  169. if (m_quit_when_last_window_deleted)
  170. m_event_loop->quit(0);
  171. }
  172. void Application::set_system_palette(Core::AnonymousBuffer& buffer)
  173. {
  174. if (!m_system_palette)
  175. m_system_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(buffer);
  176. else
  177. m_system_palette->replace_internal_buffer({}, buffer);
  178. if (!m_palette)
  179. m_palette = m_system_palette;
  180. }
  181. void Application::set_palette(Palette& palette)
  182. {
  183. m_palette = palette.impl();
  184. }
  185. Gfx::Palette Application::palette() const
  186. {
  187. return Palette(*m_palette);
  188. }
  189. void Application::request_tooltip_show()
  190. {
  191. VERIFY(m_tooltip_window);
  192. Gfx::IntRect desktop_rect = Desktop::the().rect();
  193. int const margin = 30;
  194. Gfx::IntPoint adjusted_pos = ConnectionToWindowServer::the().get_global_cursor_position();
  195. adjusted_pos.translate_by(0, 14);
  196. if (adjusted_pos.x() + m_tooltip_window->width() >= desktop_rect.width() - margin) {
  197. adjusted_pos = adjusted_pos.translated(-m_tooltip_window->width(), 0);
  198. }
  199. if (adjusted_pos.y() + m_tooltip_window->height() >= desktop_rect.height() - margin) {
  200. adjusted_pos = adjusted_pos.translated(0, -(m_tooltip_window->height() * 2));
  201. }
  202. if (adjusted_pos.x() < 0)
  203. adjusted_pos.set_x(0);
  204. m_tooltip_window->move_to(adjusted_pos);
  205. m_tooltip_window->show();
  206. }
  207. void Application::tooltip_hide_timer_did_fire()
  208. {
  209. m_tooltip_source_widget = nullptr;
  210. if (m_tooltip_window)
  211. m_tooltip_window->hide();
  212. }
  213. void Application::window_did_become_active(Badge<Window>, Window& window)
  214. {
  215. m_active_window = window.make_weak_ptr<Window>();
  216. window.update();
  217. }
  218. void Application::window_did_become_inactive(Badge<Window>, Window& window)
  219. {
  220. if (m_active_window.ptr() != &window)
  221. return;
  222. window.update();
  223. m_active_window = nullptr;
  224. }
  225. void Application::set_pending_drop_widget(Widget* widget)
  226. {
  227. if (m_pending_drop_widget == widget)
  228. return;
  229. if (m_pending_drop_widget)
  230. m_pending_drop_widget->update();
  231. m_pending_drop_widget = widget;
  232. if (m_pending_drop_widget)
  233. m_pending_drop_widget->update();
  234. }
  235. void Application::set_drag_hovered_widget_impl(Widget* widget, Gfx::IntPoint position, Vector<String> mime_types)
  236. {
  237. if (widget == m_drag_hovered_widget)
  238. return;
  239. if (m_drag_hovered_widget) {
  240. Event leave_event(Event::DragLeave);
  241. m_drag_hovered_widget->dispatch_event(leave_event, m_drag_hovered_widget->window());
  242. }
  243. set_pending_drop_widget(nullptr);
  244. m_drag_hovered_widget = widget;
  245. if (m_drag_hovered_widget) {
  246. DragEvent enter_event(Event::DragEnter, position, move(mime_types));
  247. enter_event.ignore();
  248. m_drag_hovered_widget->dispatch_event(enter_event, m_drag_hovered_widget->window());
  249. if (enter_event.is_accepted())
  250. set_pending_drop_widget(m_drag_hovered_widget);
  251. ConnectionToWindowServer::the().async_set_accepts_drag(enter_event.is_accepted());
  252. }
  253. }
  254. void Application::notify_drag_cancelled(Badge<ConnectionToWindowServer>)
  255. {
  256. set_drag_hovered_widget_impl(nullptr);
  257. }
  258. void Application::event(Core::Event& event)
  259. {
  260. if (event.type() == GUI::Event::ActionEnter || event.type() == GUI::Event::ActionLeave) {
  261. auto& action_event = static_cast<ActionEvent&>(event);
  262. auto& action = action_event.action();
  263. if (action_event.type() == GUI::Event::ActionEnter) {
  264. if (on_action_enter)
  265. on_action_enter(action);
  266. } else {
  267. if (on_action_leave)
  268. on_action_leave(action);
  269. }
  270. }
  271. if (event.type() == GUI::Event::ThemeChange) {
  272. if (on_theme_change)
  273. on_theme_change();
  274. }
  275. EventReceiver::event(event);
  276. }
  277. void Application::set_config_domain(String config_domain)
  278. {
  279. m_config_domain = move(config_domain);
  280. }
  281. void Application::register_recent_file_actions(Badge<GUI::Menu>, Vector<NonnullRefPtr<GUI::Action>> actions)
  282. {
  283. m_recent_file_actions = move(actions);
  284. update_recent_file_actions();
  285. }
  286. void Application::update_recent_file_actions()
  287. {
  288. VERIFY(!m_config_domain.is_empty());
  289. size_t number_of_recently_open_files = 0;
  290. auto update_action = [&](size_t index) {
  291. auto& action = m_recent_file_actions[index];
  292. char buffer = static_cast<char>('0' + index);
  293. auto key = StringView(&buffer, 1);
  294. auto path = Config::read_string(m_config_domain, "RecentFiles"sv, key);
  295. if (path.is_empty()) {
  296. action->set_visible(false);
  297. action->set_enabled(false);
  298. } else {
  299. action->set_visible(true);
  300. action->set_enabled(true);
  301. action->set_text(path);
  302. action->set_status_tip(String::formatted("Open {}", path).release_value_but_fixme_should_propagate_errors());
  303. ++number_of_recently_open_files;
  304. }
  305. };
  306. for (size_t i = 0; i < max_recently_open_files(); ++i)
  307. update_action(i);
  308. // Hide or show the "(No recently open files)" placeholder.
  309. m_recent_file_actions.last()->set_visible(number_of_recently_open_files == 0);
  310. }
  311. void Application::set_most_recently_open_file(String new_path)
  312. {
  313. Vector<DeprecatedString> new_recent_files_list;
  314. for (size_t i = 0; i < max_recently_open_files(); ++i) {
  315. static_assert(max_recently_open_files() < 10);
  316. char buffer = static_cast<char>('0' + i);
  317. auto key = StringView(&buffer, 1);
  318. new_recent_files_list.append(Config::read_string(m_config_domain, "RecentFiles"sv, key));
  319. }
  320. new_recent_files_list.remove_all_matching([&](auto& existing_path) {
  321. return existing_path.view() == new_path;
  322. });
  323. new_recent_files_list.prepend(new_path.to_deprecated_string());
  324. for (size_t i = 0; i < max_recently_open_files(); ++i) {
  325. auto& path = new_recent_files_list[i];
  326. char buffer = static_cast<char>('0' + i);
  327. auto key = StringView(&buffer, 1);
  328. Config::write_string(
  329. m_config_domain,
  330. "RecentFiles"sv,
  331. key,
  332. path);
  333. }
  334. update_recent_file_actions();
  335. }
  336. }