TaskbarWindow.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "TaskbarWindow.h"
  8. #include "ClockWidget.h"
  9. #include "TaskbarButton.h"
  10. #include <AK/Debug.h>
  11. #include <LibCore/ConfigFile.h>
  12. #include <LibCore/StandardPaths.h>
  13. #include <LibGUI/BoxLayout.h>
  14. #include <LibGUI/Button.h>
  15. #include <LibGUI/Desktop.h>
  16. #include <LibGUI/Frame.h>
  17. #include <LibGUI/Icon.h>
  18. #include <LibGUI/Menu.h>
  19. #include <LibGUI/Painter.h>
  20. #include <LibGUI/Window.h>
  21. #include <LibGUI/WindowManagerServerConnection.h>
  22. #include <LibGUI/WindowServerConnection.h>
  23. #include <LibGfx/FontDatabase.h>
  24. #include <LibGfx/Palette.h>
  25. #include <serenity.h>
  26. #include <stdio.h>
  27. class TaskbarWidget final : public GUI::Widget {
  28. C_OBJECT(TaskbarWidget);
  29. public:
  30. virtual ~TaskbarWidget() override { }
  31. private:
  32. TaskbarWidget() { }
  33. virtual void paint_event(GUI::PaintEvent& event) override
  34. {
  35. GUI::Painter painter(*this);
  36. painter.add_clip_rect(event.rect());
  37. painter.fill_rect(rect(), palette().button());
  38. painter.draw_line({ 0, 1 }, { width() - 1, 1 }, palette().threed_highlight());
  39. }
  40. virtual void did_layout() override
  41. {
  42. WindowList::the().for_each_window([&](auto& window) {
  43. if (auto* button = window.button())
  44. static_cast<TaskbarButton*>(button)->update_taskbar_rect();
  45. });
  46. }
  47. };
  48. TaskbarWindow::TaskbarWindow(NonnullRefPtr<GUI::Menu> start_menu)
  49. : m_start_menu(move(start_menu))
  50. {
  51. set_window_type(GUI::WindowType::Taskbar);
  52. set_title("Taskbar");
  53. on_screen_rects_change(GUI::Desktop::the().rects(), GUI::Desktop::the().main_screen_index());
  54. auto& main_widget = set_main_widget<TaskbarWidget>();
  55. main_widget.set_layout<GUI::HorizontalBoxLayout>();
  56. main_widget.layout()->set_margins({ 3, 3, 3, 1 });
  57. m_start_button = GUI::Button::construct("Serenity");
  58. m_start_button->set_font(Gfx::FontDatabase::default_font().bold_variant());
  59. m_start_button->set_icon_spacing(0);
  60. m_start_button->set_fixed_size(80, 21);
  61. auto app_icon = GUI::Icon::default_icon("ladyball");
  62. m_start_button->set_icon(app_icon.bitmap_for_size(16));
  63. m_start_button->set_menu(m_start_menu);
  64. main_widget.add_child(*m_start_button);
  65. create_quick_launch_bar();
  66. m_task_button_container = main_widget.add<GUI::Widget>();
  67. m_task_button_container->set_layout<GUI::HorizontalBoxLayout>();
  68. m_task_button_container->layout()->set_spacing(3);
  69. m_default_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png");
  70. m_applet_area_container = main_widget.add<GUI::Frame>();
  71. m_applet_area_container->set_frame_thickness(1);
  72. m_applet_area_container->set_frame_shape(Gfx::FrameShape::Box);
  73. m_applet_area_container->set_frame_shadow(Gfx::FrameShadow::Sunken);
  74. main_widget.add<Taskbar::ClockWidget>();
  75. auto af_path = String::formatted("{}/{}", Desktop::AppFile::APP_FILES_DIRECTORY, "Assistant.af");
  76. m_assistant_app_file = Desktop::AppFile::open(af_path);
  77. }
  78. TaskbarWindow::~TaskbarWindow()
  79. {
  80. }
  81. void TaskbarWindow::create_quick_launch_bar()
  82. {
  83. auto& quick_launch_bar = main_widget()->add<GUI::Frame>();
  84. quick_launch_bar.set_layout<GUI::HorizontalBoxLayout>();
  85. quick_launch_bar.layout()->set_spacing(0);
  86. quick_launch_bar.layout()->set_margins({ 3, 0, 3, 0 });
  87. quick_launch_bar.set_frame_thickness(0);
  88. int total_width = 6;
  89. bool first = true;
  90. auto config = Core::ConfigFile::get_for_app("Taskbar");
  91. constexpr const char* quick_launch = "QuickLaunch";
  92. // FIXME: Core::ConfigFile does not keep the order of the entries.
  93. for (auto& name : config->keys(quick_launch)) {
  94. auto af_name = config->read_entry(quick_launch, name);
  95. auto af_path = String::formatted("{}/{}", Desktop::AppFile::APP_FILES_DIRECTORY, af_name);
  96. auto af = Desktop::AppFile::open(af_path);
  97. if (!af->is_valid())
  98. continue;
  99. auto app_executable = af->executable();
  100. const int button_size = 24;
  101. auto& button = quick_launch_bar.add<GUI::Button>();
  102. button.set_fixed_size(button_size, button_size);
  103. button.set_button_style(Gfx::ButtonStyle::Coolbar);
  104. button.set_icon(af->icon().bitmap_for_size(16));
  105. button.set_tooltip(af->name());
  106. button.on_click = [app_executable](auto) {
  107. pid_t pid = fork();
  108. if (pid < 0) {
  109. perror("fork");
  110. } else if (pid == 0) {
  111. if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
  112. perror("chdir");
  113. exit(1);
  114. }
  115. execl(app_executable.characters(), app_executable.characters(), nullptr);
  116. perror("execl");
  117. VERIFY_NOT_REACHED();
  118. } else {
  119. if (disown(pid) < 0)
  120. perror("disown");
  121. }
  122. };
  123. if (!first)
  124. total_width += quick_launch_bar.layout()->spacing();
  125. first = false;
  126. total_width += button_size;
  127. }
  128. quick_launch_bar.set_fixed_size(total_width, 24);
  129. }
  130. void TaskbarWindow::on_screen_rects_change(const Vector<Gfx::IntRect, 4>& rects, size_t main_screen_index)
  131. {
  132. const auto& rect = rects[main_screen_index];
  133. Gfx::IntRect new_rect { rect.x(), rect.bottom() - taskbar_height() + 1, rect.width(), taskbar_height() };
  134. set_rect(new_rect);
  135. update_applet_area();
  136. }
  137. void TaskbarWindow::update_applet_area()
  138. {
  139. // NOTE: Widget layout is normally lazy, but here we have to force it right away so we can tell
  140. // WindowServer where to place the applet area window.
  141. if (!main_widget())
  142. return;
  143. main_widget()->do_layout();
  144. Gfx::IntRect new_rect { {}, m_applet_area_size };
  145. new_rect.center_within(m_applet_area_container->screen_relative_rect());
  146. GUI::WindowManagerServerConnection::the().async_set_applet_area_position(new_rect.location());
  147. }
  148. NonnullRefPtr<GUI::Button> TaskbarWindow::create_button(const WindowIdentifier& identifier)
  149. {
  150. auto& button = m_task_button_container->add<TaskbarButton>(identifier);
  151. button.set_min_size(20, 21);
  152. button.set_max_size(140, 21);
  153. button.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  154. button.set_icon(*m_default_icon);
  155. return button;
  156. }
  157. void TaskbarWindow::add_window_button(::Window& window, const WindowIdentifier& identifier)
  158. {
  159. if (window.button())
  160. return;
  161. window.set_button(create_button(identifier));
  162. auto* button = window.button();
  163. button->on_click = [window = &window, identifier, button](auto) {
  164. // We need to look at the button's checked state here to figure
  165. // out if the application is active or not. That's because this
  166. // button's window may not actually be active when a modal window
  167. // is displayed, in which case window->is_active() would return
  168. // false because window is the modal window's owner (which is not
  169. // active)
  170. if (window->is_minimized() || !button->is_checked()) {
  171. GUI::WindowManagerServerConnection::the().async_set_active_window(identifier.client_id(), identifier.window_id());
  172. } else {
  173. GUI::WindowManagerServerConnection::the().async_set_window_minimized(identifier.client_id(), identifier.window_id(), true);
  174. }
  175. };
  176. }
  177. void TaskbarWindow::remove_window_button(::Window& window, bool was_removed)
  178. {
  179. auto* button = window.button();
  180. if (!button)
  181. return;
  182. if (!was_removed)
  183. static_cast<TaskbarButton*>(button)->clear_taskbar_rect();
  184. window.set_button(nullptr);
  185. button->remove_from_parent();
  186. }
  187. void TaskbarWindow::update_window_button(::Window& window, bool show_as_active)
  188. {
  189. auto* button = window.button();
  190. if (!button)
  191. return;
  192. button->set_text(window.title());
  193. button->set_tooltip(window.title());
  194. button->set_checked(show_as_active);
  195. button->set_visible(is_window_on_current_virtual_desktop(window));
  196. }
  197. ::Window* TaskbarWindow::find_window_owner(::Window& window) const
  198. {
  199. if (!window.is_modal())
  200. return &window;
  201. ::Window* parent = nullptr;
  202. auto* current_window = &window;
  203. while (current_window) {
  204. parent = WindowList::the().find_parent(*current_window);
  205. if (!parent || !parent->is_modal())
  206. break;
  207. current_window = parent;
  208. }
  209. return parent;
  210. }
  211. void TaskbarWindow::wm_event(GUI::WMEvent& event)
  212. {
  213. WindowIdentifier identifier { event.client_id(), event.window_id() };
  214. switch (event.type()) {
  215. case GUI::Event::WM_WindowRemoved: {
  216. if constexpr (EVENT_DEBUG) {
  217. auto& removed_event = static_cast<GUI::WMWindowRemovedEvent&>(event);
  218. dbgln("WM_WindowRemoved: client_id={}, window_id={}",
  219. removed_event.client_id(),
  220. removed_event.window_id());
  221. }
  222. if (auto* window = WindowList::the().window(identifier))
  223. remove_window_button(*window, true);
  224. WindowList::the().remove_window(identifier);
  225. update();
  226. break;
  227. }
  228. case GUI::Event::WM_WindowRectChanged: {
  229. if constexpr (EVENT_DEBUG) {
  230. auto& changed_event = static_cast<GUI::WMWindowRectChangedEvent&>(event);
  231. dbgln("WM_WindowRectChanged: client_id={}, window_id={}, rect={}",
  232. changed_event.client_id(),
  233. changed_event.window_id(),
  234. changed_event.rect());
  235. }
  236. break;
  237. }
  238. case GUI::Event::WM_WindowIconBitmapChanged: {
  239. auto& changed_event = static_cast<GUI::WMWindowIconBitmapChangedEvent&>(event);
  240. if (auto* window = WindowList::the().window(identifier)) {
  241. if (window->button())
  242. window->button()->set_icon(changed_event.bitmap());
  243. }
  244. break;
  245. }
  246. case GUI::Event::WM_WindowStateChanged: {
  247. auto& changed_event = static_cast<GUI::WMWindowStateChangedEvent&>(event);
  248. if constexpr (EVENT_DEBUG) {
  249. dbgln("WM_WindowStateChanged: client_id={}, window_id={}, title={}, rect={}, is_active={}, is_minimized={}",
  250. changed_event.client_id(),
  251. changed_event.window_id(),
  252. changed_event.title(),
  253. changed_event.rect(),
  254. changed_event.is_active(),
  255. changed_event.is_minimized());
  256. }
  257. if (changed_event.window_type() != GUI::WindowType::Normal || changed_event.is_frameless()) {
  258. if (auto* window = WindowList::the().window(identifier))
  259. remove_window_button(*window, false);
  260. break;
  261. }
  262. auto& window = WindowList::the().ensure_window(identifier);
  263. window.set_parent_identifier({ changed_event.parent_client_id(), changed_event.parent_window_id() });
  264. if (!window.is_modal())
  265. add_window_button(window, identifier);
  266. else
  267. remove_window_button(window, false);
  268. window.set_title(changed_event.title());
  269. window.set_rect(changed_event.rect());
  270. window.set_modal(changed_event.is_modal());
  271. window.set_active(changed_event.is_active());
  272. window.set_minimized(changed_event.is_minimized());
  273. window.set_progress(changed_event.progress());
  274. window.set_virtual_desktop(changed_event.virtual_desktop_row(), changed_event.virtual_desktop_column());
  275. auto* window_owner = find_window_owner(window);
  276. if (window_owner == &window) {
  277. update_window_button(window, window.is_active());
  278. } else if (window_owner) {
  279. // check the window owner's button if the modal's window button
  280. // would have been checked
  281. VERIFY(window.is_modal());
  282. update_window_button(*window_owner, window.is_active());
  283. }
  284. break;
  285. }
  286. case GUI::Event::WM_AppletAreaSizeChanged: {
  287. auto& changed_event = static_cast<GUI::WMAppletAreaSizeChangedEvent&>(event);
  288. m_applet_area_size = changed_event.size();
  289. m_applet_area_container->set_fixed_size(changed_event.size().width() + 8, 21);
  290. update_applet_area();
  291. break;
  292. }
  293. case GUI::Event::WM_SuperKeyPressed: {
  294. if (m_start_menu->is_visible()) {
  295. m_start_menu->dismiss();
  296. } else {
  297. m_start_menu->popup(m_start_button->screen_relative_rect().top_left());
  298. }
  299. break;
  300. }
  301. case GUI::Event::WM_SuperSpaceKeyPressed: {
  302. if (!m_assistant_app_file->spawn())
  303. warnln("failed to spawn 'Assistant' when requested via Super+Space");
  304. break;
  305. }
  306. case GUI::Event::WM_VirtualDesktopChanged: {
  307. auto& changed_event = static_cast<GUI::WMVirtualDesktopChangedEvent&>(event);
  308. virtual_desktop_change_event(changed_event.current_row(), changed_event.current_column());
  309. }
  310. default:
  311. break;
  312. }
  313. }
  314. void TaskbarWindow::screen_rects_change_event(GUI::ScreenRectsChangeEvent& event)
  315. {
  316. on_screen_rects_change(event.rects(), event.main_screen_index());
  317. }
  318. bool TaskbarWindow::is_window_on_current_virtual_desktop(::Window& window) const
  319. {
  320. return window.virtual_desktop_row() == m_current_virtual_desktop_row && window.virtual_desktop_column() == m_current_virtual_desktop_column;
  321. }
  322. void TaskbarWindow::virtual_desktop_change_event(unsigned current_row, unsigned current_column)
  323. {
  324. m_current_virtual_desktop_row = current_row;
  325. m_current_virtual_desktop_column = current_column;
  326. WindowList::the().for_each_window([&](auto& window) {
  327. if (auto* button = window.button())
  328. button->set_visible(is_window_on_current_virtual_desktop(window));
  329. });
  330. }