TaskbarWindow.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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, 1, 1, 3 });
  57. m_start_button = GUI::Button::construct("Serenity");
  58. set_start_button_font(Gfx::FontDatabase::default_font().bold_variant());
  59. m_start_button->set_icon_spacing(0);
  60. auto app_icon = GUI::Icon::default_icon("ladyball");
  61. m_start_button->set_icon(app_icon.bitmap_for_size(16));
  62. m_start_button->set_menu(m_start_menu);
  63. main_widget.add_child(*m_start_button);
  64. create_quick_launch_bar();
  65. m_task_button_container = main_widget.add<GUI::Widget>();
  66. m_task_button_container->set_layout<GUI::HorizontalBoxLayout>();
  67. m_task_button_container->layout()->set_spacing(3);
  68. m_default_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window.png");
  69. m_applet_area_container = main_widget.add<GUI::Frame>();
  70. m_applet_area_container->set_frame_thickness(1);
  71. m_applet_area_container->set_frame_shape(Gfx::FrameShape::Box);
  72. m_applet_area_container->set_frame_shadow(Gfx::FrameShadow::Sunken);
  73. main_widget.add<Taskbar::ClockWidget>();
  74. m_show_desktop_button = GUI::Button::construct();
  75. m_show_desktop_button->set_tooltip("Show Desktop");
  76. m_show_desktop_button->set_icon(GUI::Icon::default_icon("desktop").bitmap_for_size(16));
  77. m_show_desktop_button->set_button_style(Gfx::ButtonStyle::Coolbar);
  78. m_show_desktop_button->set_fixed_size(24, 24);
  79. m_show_desktop_button->on_click = TaskbarWindow::show_desktop_button_clicked;
  80. main_widget.add_child(*m_show_desktop_button);
  81. auto af_path = String::formatted("{}/{}", Desktop::AppFile::APP_FILES_DIRECTORY, "Assistant.af");
  82. m_assistant_app_file = Desktop::AppFile::open(af_path);
  83. }
  84. TaskbarWindow::~TaskbarWindow()
  85. {
  86. }
  87. void TaskbarWindow::show_desktop_button_clicked(unsigned)
  88. {
  89. GUI::WindowManagerServerConnection::the().async_toggle_show_desktop();
  90. }
  91. void TaskbarWindow::create_quick_launch_bar()
  92. {
  93. auto& quick_launch_bar = main_widget()->add<GUI::Frame>();
  94. quick_launch_bar.set_shrink_to_fit(true);
  95. quick_launch_bar.set_layout<GUI::HorizontalBoxLayout>();
  96. quick_launch_bar.layout()->set_spacing(0);
  97. quick_launch_bar.set_frame_thickness(0);
  98. auto config = Core::ConfigFile::open_for_app("Taskbar");
  99. constexpr const char* quick_launch = "QuickLaunch";
  100. // FIXME: Core::ConfigFile does not keep the order of the entries.
  101. for (auto& name : config->keys(quick_launch)) {
  102. auto af_name = config->read_entry(quick_launch, name);
  103. auto af_path = String::formatted("{}/{}", Desktop::AppFile::APP_FILES_DIRECTORY, af_name);
  104. auto af = Desktop::AppFile::open(af_path);
  105. if (!af->is_valid())
  106. continue;
  107. auto app_executable = af->executable();
  108. auto app_run_in_terminal = af->run_in_terminal();
  109. const int button_size = 24;
  110. auto& button = quick_launch_bar.add<GUI::Button>();
  111. button.set_fixed_size(button_size, button_size);
  112. button.set_button_style(Gfx::ButtonStyle::Coolbar);
  113. button.set_icon(af->icon().bitmap_for_size(16));
  114. button.set_tooltip(af->name());
  115. button.on_click = [app_executable, app_run_in_terminal](auto) {
  116. pid_t pid = fork();
  117. if (pid < 0) {
  118. perror("fork");
  119. } else if (pid == 0) {
  120. if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
  121. perror("chdir");
  122. exit(1);
  123. }
  124. if (app_run_in_terminal)
  125. execl("/bin/Terminal", "Terminal", "-e", app_executable.characters(), nullptr);
  126. else
  127. execl(app_executable.characters(), app_executable.characters(), nullptr);
  128. perror("execl");
  129. VERIFY_NOT_REACHED();
  130. } else {
  131. if (disown(pid) < 0)
  132. perror("disown");
  133. }
  134. };
  135. }
  136. quick_launch_bar.set_fixed_height(24);
  137. }
  138. void TaskbarWindow::on_screen_rects_change(const Vector<Gfx::IntRect, 4>& rects, size_t main_screen_index)
  139. {
  140. const auto& rect = rects[main_screen_index];
  141. Gfx::IntRect new_rect { rect.x(), rect.bottom() - taskbar_height() + 1, rect.width(), taskbar_height() };
  142. set_rect(new_rect);
  143. update_applet_area();
  144. }
  145. void TaskbarWindow::update_applet_area()
  146. {
  147. // NOTE: Widget layout is normally lazy, but here we have to force it right away so we can tell
  148. // WindowServer where to place the applet area window.
  149. if (!main_widget())
  150. return;
  151. main_widget()->do_layout();
  152. Gfx::IntRect new_rect { {}, m_applet_area_size };
  153. new_rect.center_within(m_applet_area_container->screen_relative_rect());
  154. GUI::WindowManagerServerConnection::the().async_set_applet_area_position(new_rect.location());
  155. }
  156. NonnullRefPtr<GUI::Button> TaskbarWindow::create_button(const WindowIdentifier& identifier)
  157. {
  158. auto& button = m_task_button_container->add<TaskbarButton>(identifier);
  159. button.set_min_size(20, 21);
  160. button.set_max_size(140, 21);
  161. button.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  162. button.set_icon(*m_default_icon);
  163. return button;
  164. }
  165. void TaskbarWindow::add_window_button(::Window& window, const WindowIdentifier& identifier)
  166. {
  167. if (window.button())
  168. return;
  169. window.set_button(create_button(identifier));
  170. auto* button = window.button();
  171. button->on_click = [window = &window, identifier, button](auto) {
  172. // We need to look at the button's checked state here to figure
  173. // out if the application is active or not. That's because this
  174. // button's window may not actually be active when a modal window
  175. // is displayed, in which case window->is_active() would return
  176. // false because window is the modal window's owner (which is not
  177. // active)
  178. if (window->is_minimized() || !button->is_checked()) {
  179. GUI::WindowManagerServerConnection::the().async_set_active_window(identifier.client_id(), identifier.window_id());
  180. } else {
  181. GUI::WindowManagerServerConnection::the().async_set_window_minimized(identifier.client_id(), identifier.window_id(), true);
  182. }
  183. };
  184. }
  185. void TaskbarWindow::remove_window_button(::Window& window, bool was_removed)
  186. {
  187. auto* button = window.button();
  188. if (!button)
  189. return;
  190. if (!was_removed)
  191. static_cast<TaskbarButton*>(button)->clear_taskbar_rect();
  192. window.set_button(nullptr);
  193. button->remove_from_parent();
  194. }
  195. void TaskbarWindow::update_window_button(::Window& window, bool show_as_active)
  196. {
  197. auto* button = window.button();
  198. if (!button)
  199. return;
  200. button->set_text(window.title());
  201. button->set_tooltip(window.title());
  202. button->set_checked(show_as_active);
  203. button->set_visible(is_window_on_current_virtual_desktop(window));
  204. }
  205. ::Window* TaskbarWindow::find_window_owner(::Window& window) const
  206. {
  207. if (!window.is_modal())
  208. return &window;
  209. ::Window* parent = nullptr;
  210. auto* current_window = &window;
  211. while (current_window) {
  212. parent = WindowList::the().find_parent(*current_window);
  213. if (!parent || !parent->is_modal())
  214. break;
  215. current_window = parent;
  216. }
  217. return parent;
  218. }
  219. void TaskbarWindow::event(Core::Event& event)
  220. {
  221. switch (event.type()) {
  222. case GUI::Event::MouseDown: {
  223. // If the cursor is at the edge/corner of the screen but technically not within the start button (or other taskbar buttons),
  224. // we adjust it so that the nearest button ends up being clicked anyways.
  225. auto& mouse_event = static_cast<GUI::MouseEvent&>(event);
  226. const int ADJUSTMENT = 4;
  227. auto adjusted_x = AK::clamp(mouse_event.x(), ADJUSTMENT, width() - ADJUSTMENT);
  228. auto adjusted_y = AK::min(mouse_event.y(), height() - ADJUSTMENT);
  229. Gfx::IntPoint adjusted_point = { adjusted_x, adjusted_y };
  230. if (adjusted_point != mouse_event.position()) {
  231. GUI::WindowServerConnection::the().async_set_global_cursor_position(position() + adjusted_point);
  232. GUI::MouseEvent adjusted_event = { (GUI::Event::Type)mouse_event.type(), adjusted_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta() };
  233. Window::event(adjusted_event);
  234. return;
  235. }
  236. break;
  237. }
  238. case GUI::Event::FontsChange:
  239. set_start_button_font(Gfx::FontDatabase::default_font().bold_variant());
  240. break;
  241. }
  242. Window::event(event);
  243. }
  244. void TaskbarWindow::wm_event(GUI::WMEvent& event)
  245. {
  246. WindowIdentifier identifier { event.client_id(), event.window_id() };
  247. switch (event.type()) {
  248. case GUI::Event::WM_WindowRemoved: {
  249. if constexpr (EVENT_DEBUG) {
  250. auto& removed_event = static_cast<GUI::WMWindowRemovedEvent&>(event);
  251. dbgln("WM_WindowRemoved: client_id={}, window_id={}",
  252. removed_event.client_id(),
  253. removed_event.window_id());
  254. }
  255. if (auto* window = WindowList::the().window(identifier))
  256. remove_window_button(*window, true);
  257. WindowList::the().remove_window(identifier);
  258. update();
  259. break;
  260. }
  261. case GUI::Event::WM_WindowRectChanged: {
  262. if constexpr (EVENT_DEBUG) {
  263. auto& changed_event = static_cast<GUI::WMWindowRectChangedEvent&>(event);
  264. dbgln("WM_WindowRectChanged: client_id={}, window_id={}, rect={}",
  265. changed_event.client_id(),
  266. changed_event.window_id(),
  267. changed_event.rect());
  268. }
  269. break;
  270. }
  271. case GUI::Event::WM_WindowIconBitmapChanged: {
  272. auto& changed_event = static_cast<GUI::WMWindowIconBitmapChangedEvent&>(event);
  273. if (auto* window = WindowList::the().window(identifier)) {
  274. if (window->button()) {
  275. auto icon = changed_event.bitmap();
  276. if (icon->height() != taskbar_icon_size() || icon->width() != taskbar_icon_size()) {
  277. auto sw = taskbar_icon_size() / (float)icon->width();
  278. auto sh = taskbar_icon_size() / (float)icon->height();
  279. auto scaled_bitmap = icon->scaled(sw, sh);
  280. window->button()->set_icon(move(scaled_bitmap));
  281. } else {
  282. window->button()->set_icon(icon);
  283. }
  284. }
  285. }
  286. break;
  287. }
  288. case GUI::Event::WM_WindowStateChanged: {
  289. auto& changed_event = static_cast<GUI::WMWindowStateChangedEvent&>(event);
  290. if constexpr (EVENT_DEBUG) {
  291. dbgln("WM_WindowStateChanged: client_id={}, window_id={}, title={}, rect={}, is_active={}, is_minimized={}",
  292. changed_event.client_id(),
  293. changed_event.window_id(),
  294. changed_event.title(),
  295. changed_event.rect(),
  296. changed_event.is_active(),
  297. changed_event.is_minimized());
  298. }
  299. if (changed_event.window_type() != GUI::WindowType::Normal || changed_event.is_frameless()) {
  300. if (auto* window = WindowList::the().window(identifier))
  301. remove_window_button(*window, false);
  302. break;
  303. }
  304. auto& window = WindowList::the().ensure_window(identifier);
  305. window.set_parent_identifier({ changed_event.parent_client_id(), changed_event.parent_window_id() });
  306. if (!window.is_modal())
  307. add_window_button(window, identifier);
  308. else
  309. remove_window_button(window, false);
  310. window.set_title(changed_event.title());
  311. window.set_rect(changed_event.rect());
  312. window.set_modal(changed_event.is_modal());
  313. window.set_active(changed_event.is_active());
  314. window.set_minimized(changed_event.is_minimized());
  315. window.set_progress(changed_event.progress());
  316. window.set_virtual_desktop(changed_event.virtual_desktop_row(), changed_event.virtual_desktop_column());
  317. auto* window_owner = find_window_owner(window);
  318. if (window_owner == &window) {
  319. update_window_button(window, window.is_active());
  320. } else if (window_owner) {
  321. // check the window owner's button if the modal's window button
  322. // would have been checked
  323. VERIFY(window.is_modal());
  324. update_window_button(*window_owner, window.is_active());
  325. }
  326. break;
  327. }
  328. case GUI::Event::WM_AppletAreaSizeChanged: {
  329. auto& changed_event = static_cast<GUI::WMAppletAreaSizeChangedEvent&>(event);
  330. m_applet_area_size = changed_event.size();
  331. m_applet_area_container->set_fixed_size(changed_event.size().width() + 8, 21);
  332. update_applet_area();
  333. break;
  334. }
  335. case GUI::Event::WM_SuperKeyPressed: {
  336. if (m_start_menu->is_visible()) {
  337. m_start_menu->dismiss();
  338. } else {
  339. m_start_menu->popup(m_start_button->screen_relative_rect().top_left());
  340. }
  341. break;
  342. }
  343. case GUI::Event::WM_SuperSpaceKeyPressed: {
  344. if (!m_assistant_app_file->spawn())
  345. warnln("failed to spawn 'Assistant' when requested via Super+Space");
  346. break;
  347. }
  348. case GUI::Event::WM_VirtualDesktopChanged: {
  349. auto& changed_event = static_cast<GUI::WMVirtualDesktopChangedEvent&>(event);
  350. virtual_desktop_change_event(changed_event.current_row(), changed_event.current_column());
  351. break;
  352. }
  353. default:
  354. break;
  355. }
  356. }
  357. void TaskbarWindow::screen_rects_change_event(GUI::ScreenRectsChangeEvent& event)
  358. {
  359. on_screen_rects_change(event.rects(), event.main_screen_index());
  360. }
  361. bool TaskbarWindow::is_window_on_current_virtual_desktop(::Window& window) const
  362. {
  363. return window.virtual_desktop_row() == m_current_virtual_desktop_row && window.virtual_desktop_column() == m_current_virtual_desktop_column;
  364. }
  365. void TaskbarWindow::virtual_desktop_change_event(unsigned current_row, unsigned current_column)
  366. {
  367. m_current_virtual_desktop_row = current_row;
  368. m_current_virtual_desktop_column = current_column;
  369. WindowList::the().for_each_window([&](auto& window) {
  370. if (auto* button = window.button())
  371. button->set_visible(is_window_on_current_virtual_desktop(window));
  372. });
  373. }
  374. void TaskbarWindow::set_start_button_font(Gfx::Font const& font)
  375. {
  376. m_start_button->set_font(font);
  377. m_start_button->set_fixed_size(font.width(m_start_button->text()) + 30, 21);
  378. }