TaskbarWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <LibDesktop/AppFile.h>
  32. #include <LibGUI/BoxLayout.h>
  33. #include <LibGUI/Button.h>
  34. #include <LibGUI/Desktop.h>
  35. #include <LibGUI/Frame.h>
  36. #include <LibGUI/Icon.h>
  37. #include <LibGUI/Painter.h>
  38. #include <LibGUI/Window.h>
  39. #include <LibGUI/WindowServerConnection.h>
  40. #include <LibGfx/Palette.h>
  41. #include <serenity.h>
  42. #include <stdio.h>
  43. //#define EVENT_DEBUG
  44. class TaskbarWidget final : public GUI::Widget {
  45. C_OBJECT(TaskbarWidget);
  46. public:
  47. virtual ~TaskbarWidget() override { }
  48. private:
  49. TaskbarWidget() { }
  50. virtual void paint_event(GUI::PaintEvent& event) override
  51. {
  52. GUI::Painter painter(*this);
  53. painter.add_clip_rect(event.rect());
  54. painter.fill_rect(rect(), palette().button());
  55. painter.draw_line({ 0, 1 }, { width() - 1, 1 }, palette().threed_highlight());
  56. }
  57. virtual void did_layout() override
  58. {
  59. WindowList::the().for_each_window([&](auto& window) {
  60. if (auto* button = window.button())
  61. static_cast<TaskbarButton*>(button)->update_taskbar_rect();
  62. });
  63. }
  64. };
  65. TaskbarWindow::TaskbarWindow()
  66. {
  67. set_window_type(GUI::WindowType::Taskbar);
  68. set_title("Taskbar");
  69. on_screen_rect_change(GUI::Desktop::the().rect());
  70. GUI::Desktop::the().on_rect_change = [this](const Gfx::IntRect& rect) { on_screen_rect_change(rect); };
  71. auto& widget = set_main_widget<TaskbarWidget>();
  72. widget.set_layout<GUI::HorizontalBoxLayout>();
  73. widget.layout()->set_margins({ 3, 2, 3, 2 });
  74. widget.layout()->set_spacing(3);
  75. m_default_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png");
  76. create_quick_launch_bar();
  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. 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_size;
  127. }
  128. quick_launch_bar.set_fixed_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_min_size(20, 23);
  139. button.set_max_size(140, 23);
  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. button->set_text(window.title());
  184. button->set_checked(show_as_active);
  185. }
  186. ::Window* TaskbarWindow::find_window_owner(::Window& window) const
  187. {
  188. if (!window.is_modal())
  189. return &window;
  190. ::Window* parent = nullptr;
  191. auto* current_window = &window;
  192. while (current_window) {
  193. parent = WindowList::the().find_parent(*current_window);
  194. if (!parent || !parent->is_modal())
  195. break;
  196. current_window = parent;
  197. }
  198. return parent;
  199. }
  200. void TaskbarWindow::wm_event(GUI::WMEvent& event)
  201. {
  202. WindowIdentifier identifier { event.client_id(), event.window_id() };
  203. switch (event.type()) {
  204. case GUI::Event::WM_WindowRemoved: {
  205. #ifdef EVENT_DEBUG
  206. auto& removed_event = static_cast<GUI::WMWindowRemovedEvent&>(event);
  207. dbgln("WM_WindowRemoved: client_id={}, window_id={}",
  208. removed_event.client_id(),
  209. removed_event.window_id());
  210. #endif
  211. if (auto* window = WindowList::the().window(identifier))
  212. remove_window_button(*window, true);
  213. WindowList::the().remove_window(identifier);
  214. update();
  215. break;
  216. }
  217. case GUI::Event::WM_WindowRectChanged: {
  218. #ifdef EVENT_DEBUG
  219. auto& changed_event = static_cast<GUI::WMWindowRectChangedEvent&>(event);
  220. dbgln("WM_WindowRectChanged: client_id={}, window_id={}, rect={}",
  221. changed_event.client_id(),
  222. changed_event.window_id(),
  223. changed_event.rect());
  224. #endif
  225. break;
  226. }
  227. case GUI::Event::WM_WindowIconBitmapChanged: {
  228. auto& changed_event = static_cast<GUI::WMWindowIconBitmapChangedEvent&>(event);
  229. #ifdef EVENT_DEBUG
  230. dbgln("WM_WindowIconBitmapChanged: client_id={}, window_id={}, icon_buffer_id={}",
  231. changed_event.client_id(),
  232. changed_event.window_id(),
  233. changed_event.icon_buffer_id());
  234. #endif
  235. if (auto* window = WindowList::the().window(identifier)) {
  236. auto buffer = SharedBuffer::create_from_shbuf_id(changed_event.icon_buffer_id());
  237. ASSERT(buffer);
  238. if (window->button())
  239. window->button()->set_icon(Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *buffer, changed_event.icon_size()));
  240. }
  241. break;
  242. }
  243. case GUI::Event::WM_WindowStateChanged: {
  244. auto& changed_event = static_cast<GUI::WMWindowStateChangedEvent&>(event);
  245. #ifdef EVENT_DEBUG
  246. dbgln("WM_WindowStateChanged: client_id={}, window_id={}, title={}, rect={}, is_active={}, is_minimized={}",
  247. changed_event.client_id(),
  248. changed_event.window_id(),
  249. changed_event.title(),
  250. changed_event.rect(),
  251. changed_event.is_active(),
  252. changed_event.is_minimized());
  253. #endif
  254. if (!should_include_window(changed_event.window_type(), changed_event.is_frameless()))
  255. break;
  256. auto& window = WindowList::the().ensure_window(identifier);
  257. window.set_parent_identifier({ changed_event.parent_client_id(), changed_event.parent_window_id() });
  258. if (!window.is_modal())
  259. add_window_button(window, identifier);
  260. else
  261. remove_window_button(window, false);
  262. window.set_title(changed_event.title());
  263. window.set_rect(changed_event.rect());
  264. window.set_modal(changed_event.is_modal());
  265. window.set_active(changed_event.is_active());
  266. window.set_minimized(changed_event.is_minimized());
  267. window.set_progress(changed_event.progress());
  268. auto* window_owner = find_window_owner(window);
  269. if (window_owner == &window) {
  270. update_window_button(window, window.is_active());
  271. } else if (window_owner) {
  272. // check the window owner's button if the modal's window button
  273. // would have been checked
  274. ASSERT(window.is_modal());
  275. update_window_button(*window_owner, window.is_active());
  276. }
  277. break;
  278. }
  279. default:
  280. break;
  281. }
  282. }