TaskbarWindow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "TaskbarWindow.h"
  27. #include "TaskbarButton.h"
  28. #include <AK/SharedBuffer.h>
  29. #include <LibCore/ConfigFile.h>
  30. #include <LibCore/StandardPaths.h>
  31. #include <LibGUI/BoxLayout.h>
  32. #include <LibGUI/Button.h>
  33. #include <LibGUI/Desktop.h>
  34. #include <LibGUI/Frame.h>
  35. #include <LibGUI/Painter.h>
  36. #include <LibGUI/Window.h>
  37. #include <LibGUI/WindowServerConnection.h>
  38. #include <LibGfx/Palette.h>
  39. #include <serenity.h>
  40. #include <stdio.h>
  41. //#define EVENT_DEBUG
  42. class TaskbarWidget final : public GUI::Widget {
  43. C_OBJECT(TaskbarWidget);
  44. public:
  45. virtual ~TaskbarWidget() override { }
  46. private:
  47. TaskbarWidget() { }
  48. virtual void paint_event(GUI::PaintEvent& event) override
  49. {
  50. GUI::Painter painter(*this);
  51. painter.add_clip_rect(event.rect());
  52. painter.fill_rect(rect(), palette().button());
  53. painter.draw_line({ 0, 1 }, { width() - 1, 1 }, palette().threed_highlight());
  54. }
  55. virtual void did_layout() override
  56. {
  57. WindowList::the().for_each_window([&](auto& window) {
  58. if (auto* button = window.button())
  59. static_cast<TaskbarButton*>(button)->update_taskbar_rect();
  60. });
  61. }
  62. };
  63. TaskbarWindow::TaskbarWindow()
  64. {
  65. set_window_type(GUI::WindowType::Taskbar);
  66. set_title("Taskbar");
  67. on_screen_rect_change(GUI::Desktop::the().rect());
  68. GUI::Desktop::the().on_rect_change = [this](const Gfx::IntRect& rect) { on_screen_rect_change(rect); };
  69. auto& widget = set_main_widget<TaskbarWidget>();
  70. widget.set_layout<GUI::HorizontalBoxLayout>();
  71. widget.layout()->set_margins({ 3, 2, 3, 2 });
  72. widget.layout()->set_spacing(3);
  73. m_default_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png");
  74. create_quick_launch_bar();
  75. }
  76. TaskbarWindow::~TaskbarWindow()
  77. {
  78. }
  79. void TaskbarWindow::create_quick_launch_bar()
  80. {
  81. auto& quick_launch_bar = main_widget()->add<GUI::Frame>();
  82. quick_launch_bar.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  83. quick_launch_bar.set_layout<GUI::HorizontalBoxLayout>();
  84. quick_launch_bar.layout()->set_spacing(0);
  85. quick_launch_bar.layout()->set_margins({ 3, 0, 3, 0 });
  86. quick_launch_bar.set_frame_thickness(0);
  87. int total_width = 6;
  88. bool first = true;
  89. auto config = Core::ConfigFile::get_for_app("Taskbar");
  90. constexpr const char* quick_launch = "QuickLaunch";
  91. // FIXME: Core::ConfigFile does not keep the order of the entries.
  92. for (auto& name : config->keys(quick_launch)) {
  93. auto af_name = config->read_entry(quick_launch, name);
  94. ASSERT(!af_name.is_null());
  95. auto af_path = String::format("/res/apps/%s", af_name.characters());
  96. auto af = Core::ConfigFile::open(af_path);
  97. auto app_executable = af->read_entry("App", "Executable");
  98. auto app_name = af->read_entry("App", "Name");
  99. auto app_icon_path = af->read_entry("Icons", "16x16");
  100. auto& button = quick_launch_bar.add<GUI::Button>();
  101. button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  102. button.set_preferred_size(24, 24);
  103. button.set_button_style(Gfx::ButtonStyle::CoolBar);
  104. button.set_icon(Gfx::Bitmap::load_from_file(app_icon_path));
  105. button.set_tooltip(app_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. ASSERT_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.preferred_width();
  127. }
  128. quick_launch_bar.set_preferred_size(total_width, 24);
  129. }
  130. void TaskbarWindow::on_screen_rect_change(const Gfx::IntRect& rect)
  131. {
  132. Gfx::IntRect new_rect { rect.x(), rect.bottom() - taskbar_height() + 1, rect.width(), taskbar_height() };
  133. set_rect(new_rect);
  134. }
  135. NonnullRefPtr<GUI::Button> TaskbarWindow::create_button(const WindowIdentifier& identifier)
  136. {
  137. auto& button = main_widget()->add<TaskbarButton>(identifier);
  138. button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  139. button.set_preferred_size(140, 22);
  140. button.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  141. button.set_icon(*m_default_icon);
  142. return button;
  143. }
  144. static bool should_include_window(GUI::WindowType window_type, bool is_frameless)
  145. {
  146. return window_type == GUI::WindowType::Normal && !is_frameless;
  147. }
  148. void TaskbarWindow::add_window_button(::Window& window, const WindowIdentifier& identifier)
  149. {
  150. if (window.button())
  151. return;
  152. window.set_button(create_button(identifier));
  153. auto* button = window.button();
  154. button->on_click = [window = &window, identifier, button](auto) {
  155. // We need to look at the button's checked state here to figure
  156. // out if the application is active or not. That's because this
  157. // button's window may not actually be active when a modal window
  158. // is displayed, in which case window->is_active() would return
  159. // false because window is the modal window's owner (which is not
  160. // active)
  161. if (window->is_minimized() || !button->is_checked()) {
  162. GUI::WindowServerConnection::the().post_message(Messages::WindowServer::WM_SetActiveWindow(identifier.client_id(), identifier.window_id()));
  163. } else {
  164. GUI::WindowServerConnection::the().post_message(Messages::WindowServer::WM_SetWindowMinimized(identifier.client_id(), identifier.window_id(), true));
  165. }
  166. };
  167. }
  168. void TaskbarWindow::remove_window_button(::Window& window, bool was_removed)
  169. {
  170. auto* button = window.button();
  171. if (!button)
  172. return;
  173. if (!was_removed)
  174. static_cast<TaskbarButton*>(button)->clear_taskbar_rect();
  175. window.set_button(nullptr);
  176. button->remove_from_parent();
  177. }
  178. void TaskbarWindow::update_window_button(::Window& window, bool show_as_active)
  179. {
  180. auto* button = window.button();
  181. if (!button)
  182. return;
  183. if (window.is_minimized()) {
  184. button->set_foreground_color(Color::DarkGray);
  185. } else {
  186. button->set_foreground_color(Color::Black);
  187. }
  188. button->set_text(window.title());
  189. button->set_checked(show_as_active);
  190. }
  191. ::Window* TaskbarWindow::find_window_owner(::Window& window) const
  192. {
  193. if (!window.is_modal())
  194. return &window;
  195. ::Window* parent = nullptr;
  196. auto* current_window = &window;
  197. while (current_window) {
  198. parent = WindowList::the().find_parent(*current_window);
  199. if (!parent || !parent->is_modal())
  200. break;
  201. current_window = parent;
  202. }
  203. return parent;
  204. }
  205. void TaskbarWindow::wm_event(GUI::WMEvent& event)
  206. {
  207. WindowIdentifier identifier { event.client_id(), event.window_id() };
  208. switch (event.type()) {
  209. case GUI::Event::WM_WindowRemoved: {
  210. #ifdef EVENT_DEBUG
  211. auto& removed_event = static_cast<GUI::WMWindowRemovedEvent&>(event);
  212. dbgprintf("WM_WindowRemoved: client_id=%d, window_id=%d\n",
  213. removed_event.client_id(),
  214. removed_event.window_id());
  215. #endif
  216. if (auto* window = WindowList::the().window(identifier))
  217. remove_window_button(*window, true);
  218. WindowList::the().remove_window(identifier);
  219. update();
  220. break;
  221. }
  222. case GUI::Event::WM_WindowRectChanged: {
  223. #ifdef EVENT_DEBUG
  224. auto& changed_event = static_cast<GUI::WMWindowRectChangedEvent&>(event);
  225. dbgprintf("WM_WindowRectChanged: client_id=%d, window_id=%d, rect=%s\n",
  226. changed_event.client_id(),
  227. changed_event.window_id(),
  228. changed_event.rect().to_string().characters());
  229. #endif
  230. break;
  231. }
  232. case GUI::Event::WM_WindowIconBitmapChanged: {
  233. auto& changed_event = static_cast<GUI::WMWindowIconBitmapChangedEvent&>(event);
  234. #ifdef EVENT_DEBUG
  235. dbgprintf("WM_WindowIconBitmapChanged: client_id=%d, window_id=%d, icon_buffer_id=%d\n",
  236. changed_event.client_id(),
  237. changed_event.window_id(),
  238. changed_event.icon_buffer_id());
  239. #endif
  240. if (auto* window = WindowList::the().window(identifier)) {
  241. auto buffer = SharedBuffer::create_from_shbuf_id(changed_event.icon_buffer_id());
  242. ASSERT(buffer);
  243. if (window->button())
  244. window->button()->set_icon(Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *buffer, changed_event.icon_size()));
  245. }
  246. break;
  247. }
  248. case GUI::Event::WM_WindowStateChanged: {
  249. auto& changed_event = static_cast<GUI::WMWindowStateChangedEvent&>(event);
  250. #ifdef EVENT_DEBUG
  251. dbgprintf("WM_WindowStateChanged: client_id=%d, window_id=%d, title=%s, rect=%s, is_active=%u, is_minimized=%u\n",
  252. changed_event.client_id(),
  253. changed_event.window_id(),
  254. changed_event.title().characters(),
  255. changed_event.rect().to_string().characters(),
  256. changed_event.is_active(),
  257. changed_event.is_minimized());
  258. #endif
  259. if (!should_include_window(changed_event.window_type(), changed_event.is_frameless()))
  260. break;
  261. auto& window = WindowList::the().ensure_window(identifier);
  262. window.set_parent_identifier({ changed_event.parent_client_id(), changed_event.parent_window_id() });
  263. if (!window.is_modal())
  264. add_window_button(window, identifier);
  265. else
  266. remove_window_button(window, false);
  267. window.set_title(changed_event.title());
  268. window.set_rect(changed_event.rect());
  269. window.set_modal(changed_event.is_modal());
  270. window.set_active(changed_event.is_active());
  271. window.set_minimized(changed_event.is_minimized());
  272. window.set_progress(changed_event.progress());
  273. auto* window_owner = find_window_owner(window);
  274. if (window_owner == &window) {
  275. update_window_button(window, window.is_active());
  276. } else if (window_owner) {
  277. // check the window owner's button if the modal's window button
  278. // would have been checked
  279. ASSERT(window.is_modal());
  280. update_window_button(*window_owner, window.is_active());
  281. }
  282. break;
  283. }
  284. default:
  285. break;
  286. }
  287. }