TaskbarWindow.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/Desktop.h>
  33. #include <LibGUI/Frame.h>
  34. #include <LibGUI/Window.h>
  35. #include <stdio.h>
  36. //#define EVENT_DEBUG
  37. TaskbarWindow::TaskbarWindow()
  38. {
  39. set_window_type(GUI::WindowType::Taskbar);
  40. set_title("Taskbar");
  41. on_screen_rect_change(GUI::Desktop::the().rect());
  42. GUI::Desktop::the().on_rect_change = [this](const Gfx::Rect& rect) { on_screen_rect_change(rect); };
  43. auto widget = GUI::Frame::construct();
  44. widget->set_fill_with_background_color(true);
  45. widget->set_layout(make<GUI::HorizontalBoxLayout>());
  46. widget->layout()->set_margins({ 3, 2, 3, 2 });
  47. widget->layout()->set_spacing(3);
  48. widget->set_frame_thickness(1);
  49. widget->set_frame_shape(Gfx::FrameShape::Panel);
  50. widget->set_frame_shadow(Gfx::FrameShadow::Raised);
  51. set_main_widget(widget);
  52. WindowList::the().aid_create_button = [this](auto& identifier) {
  53. return create_button(identifier);
  54. };
  55. create_quick_launch_bar();
  56. }
  57. TaskbarWindow::~TaskbarWindow()
  58. {
  59. }
  60. void TaskbarWindow::create_quick_launch_bar()
  61. {
  62. auto quick_launch_bar = main_widget()->add<GUI::Frame>();
  63. quick_launch_bar->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  64. quick_launch_bar->set_layout(make<GUI::HorizontalBoxLayout>());
  65. quick_launch_bar->layout()->set_spacing(3);
  66. quick_launch_bar->layout()->set_margins({ 3, 0, 3, 0 });
  67. quick_launch_bar->set_frame_thickness(1);
  68. quick_launch_bar->set_frame_shape(Gfx::FrameShape::Container);
  69. quick_launch_bar->set_frame_shadow(Gfx::FrameShadow::Raised);
  70. int total_width = 6;
  71. bool first = true;
  72. auto config = Core::ConfigFile::get_for_app("Taskbar");
  73. constexpr const char* quick_launch = "QuickLaunch";
  74. // FIXME: Core::ConfigFile does not keep the order of the entries.
  75. for (auto& name : config->keys(quick_launch)) {
  76. auto af_name = config->read_entry(quick_launch, name);
  77. ASSERT(!af_name.is_null());
  78. auto af_path = String::format("/res/apps/%s", af_name.characters());
  79. auto af = Core::ConfigFile::open(af_path);
  80. auto app_executable = af->read_entry("App", "Executable");
  81. auto app_icon_path = af->read_entry("Icons", "16x16");
  82. auto button = quick_launch_bar->add<GUI::Button>();
  83. button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  84. button->set_preferred_size(22, 22);
  85. button->set_button_style(Gfx::ButtonStyle::CoolBar);
  86. button->set_icon(Gfx::Bitmap::load_from_file(app_icon_path));
  87. // FIXME: the tooltip ends up outside the screen rect.
  88. button->set_tooltip(name);
  89. button->on_click = [app_executable](auto&) {
  90. pid_t pid = fork();
  91. if (pid < 0) {
  92. perror("fork");
  93. } else if (pid == 0) {
  94. execl(app_executable.characters(), app_executable.characters(), nullptr);
  95. perror("execl");
  96. ASSERT_NOT_REACHED();
  97. }
  98. };
  99. if (!first)
  100. total_width += 3;
  101. first = false;
  102. total_width += 22;
  103. }
  104. quick_launch_bar->set_preferred_size(total_width, 22);
  105. }
  106. void TaskbarWindow::on_screen_rect_change(const Gfx::Rect& rect)
  107. {
  108. Gfx::Rect new_rect { rect.x(), rect.bottom() - taskbar_height() + 1, rect.width(), taskbar_height() };
  109. set_rect(new_rect);
  110. }
  111. NonnullRefPtr<GUI::Button> TaskbarWindow::create_button(const WindowIdentifier& identifier)
  112. {
  113. auto button = main_widget()->add<TaskbarButton>(identifier);
  114. button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  115. button->set_preferred_size(140, 22);
  116. button->set_checkable(true);
  117. button->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  118. return button;
  119. }
  120. static bool should_include_window(GUI::WindowType window_type)
  121. {
  122. return window_type == GUI::WindowType::Normal;
  123. }
  124. void TaskbarWindow::wm_event(GUI::WMEvent& event)
  125. {
  126. WindowIdentifier identifier { event.client_id(), event.window_id() };
  127. switch (event.type()) {
  128. case GUI::Event::WM_WindowRemoved: {
  129. #ifdef EVENT_DEBUG
  130. auto& removed_event = static_cast<GUI::WMWindowRemovedEvent&>(event);
  131. dbgprintf("WM_WindowRemoved: client_id=%d, window_id=%d\n",
  132. removed_event.client_id(),
  133. removed_event.window_id());
  134. #endif
  135. WindowList::the().remove_window(identifier);
  136. update();
  137. break;
  138. }
  139. case GUI::Event::WM_WindowRectChanged: {
  140. #ifdef EVENT_DEBUG
  141. auto& changed_event = static_cast<GUI::WMWindowRectChangedEvent&>(event);
  142. dbgprintf("WM_WindowRectChanged: client_id=%d, window_id=%d, rect=%s\n",
  143. changed_event.client_id(),
  144. changed_event.window_id(),
  145. changed_event.rect().to_string().characters());
  146. #endif
  147. break;
  148. }
  149. case GUI::Event::WM_WindowIconBitmapChanged: {
  150. auto& changed_event = static_cast<GUI::WMWindowIconBitmapChangedEvent&>(event);
  151. #ifdef EVENT_DEBUG
  152. dbgprintf("WM_WindowIconBitmapChanged: client_id=%d, window_id=%d, icon_buffer_id=%d\n",
  153. changed_event.client_id(),
  154. changed_event.window_id(),
  155. changed_event.icon_buffer_id());
  156. #endif
  157. if (auto* window = WindowList::the().window(identifier)) {
  158. auto buffer = SharedBuffer::create_from_shbuf_id(changed_event.icon_buffer_id());
  159. ASSERT(buffer);
  160. window->button()->set_icon(Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *buffer, changed_event.icon_size()));
  161. }
  162. break;
  163. }
  164. case GUI::Event::WM_WindowStateChanged: {
  165. auto& changed_event = static_cast<GUI::WMWindowStateChangedEvent&>(event);
  166. #ifdef EVENT_DEBUG
  167. dbgprintf("WM_WindowStateChanged: client_id=%d, window_id=%d, title=%s, rect=%s, is_active=%u, is_minimized=%u\n",
  168. changed_event.client_id(),
  169. changed_event.window_id(),
  170. changed_event.title().characters(),
  171. changed_event.rect().to_string().characters(),
  172. changed_event.is_active(),
  173. changed_event.is_minimized());
  174. #endif
  175. if (!should_include_window(changed_event.window_type()))
  176. break;
  177. auto& window = WindowList::the().ensure_window(identifier);
  178. window.set_title(changed_event.title());
  179. window.set_rect(changed_event.rect());
  180. window.set_active(changed_event.is_active());
  181. window.set_minimized(changed_event.is_minimized());
  182. if (window.is_minimized()) {
  183. window.button()->set_foreground_color(Color::DarkGray);
  184. window.button()->set_text(String::format("[%s]", changed_event.title().characters()));
  185. } else {
  186. window.button()->set_foreground_color(Color::Black);
  187. window.button()->set_text(changed_event.title());
  188. }
  189. window.button()->set_checked(changed_event.is_active());
  190. break;
  191. }
  192. default:
  193. break;
  194. }
  195. }