TaskbarWindow.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/UserInfo.h>
  31. #include <LibGUI/BoxLayout.h>
  32. #include <LibGUI/Button.h>
  33. #include <LibGUI/Desktop.h>
  34. #include <LibGUI/Frame.h>
  35. #include <LibGUI/Window.h>
  36. #include <stdio.h>
  37. //#define EVENT_DEBUG
  38. TaskbarWindow::TaskbarWindow()
  39. {
  40. set_window_type(GUI::WindowType::Taskbar);
  41. set_title("Taskbar");
  42. on_screen_rect_change(GUI::Desktop::the().rect());
  43. GUI::Desktop::the().on_rect_change = [this](const Gfx::Rect& rect) { on_screen_rect_change(rect); };
  44. auto& widget = set_main_widget<GUI::Frame>();
  45. widget.set_fill_with_background_color(true);
  46. widget.set_layout<GUI::HorizontalBoxLayout>();
  47. widget.layout()->set_margins({ 3, 2, 3, 2 });
  48. widget.layout()->set_spacing(3);
  49. widget.set_frame_thickness(1);
  50. widget.set_frame_shape(Gfx::FrameShape::Panel);
  51. widget.set_frame_shadow(Gfx::FrameShadow::Raised);
  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<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] {
  90. pid_t pid = fork();
  91. if (pid < 0) {
  92. perror("fork");
  93. } else if (pid == 0) {
  94. if (chdir(get_current_user_home_path().characters()) < 0) {
  95. perror("chdir");
  96. exit(1);
  97. }
  98. execl(app_executable.characters(), app_executable.characters(), nullptr);
  99. perror("execl");
  100. ASSERT_NOT_REACHED();
  101. }
  102. };
  103. if (!first)
  104. total_width += 3;
  105. first = false;
  106. total_width += 22;
  107. }
  108. quick_launch_bar.set_preferred_size(total_width, 22);
  109. }
  110. void TaskbarWindow::on_screen_rect_change(const Gfx::Rect& rect)
  111. {
  112. Gfx::Rect new_rect { rect.x(), rect.bottom() - taskbar_height() + 1, rect.width(), taskbar_height() };
  113. set_rect(new_rect);
  114. }
  115. NonnullRefPtr<GUI::Button> TaskbarWindow::create_button(const WindowIdentifier& identifier)
  116. {
  117. auto& button = main_widget()->add<TaskbarButton>(identifier);
  118. button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  119. button.set_preferred_size(140, 22);
  120. button.set_checkable(true);
  121. button.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  122. return button;
  123. }
  124. static bool should_include_window(GUI::WindowType window_type)
  125. {
  126. return window_type == GUI::WindowType::Normal;
  127. }
  128. void TaskbarWindow::wm_event(GUI::WMEvent& event)
  129. {
  130. WindowIdentifier identifier { event.client_id(), event.window_id() };
  131. switch (event.type()) {
  132. case GUI::Event::WM_WindowRemoved: {
  133. #ifdef EVENT_DEBUG
  134. auto& removed_event = static_cast<GUI::WMWindowRemovedEvent&>(event);
  135. dbgprintf("WM_WindowRemoved: client_id=%d, window_id=%d\n",
  136. removed_event.client_id(),
  137. removed_event.window_id());
  138. #endif
  139. WindowList::the().remove_window(identifier);
  140. update();
  141. break;
  142. }
  143. case GUI::Event::WM_WindowRectChanged: {
  144. #ifdef EVENT_DEBUG
  145. auto& changed_event = static_cast<GUI::WMWindowRectChangedEvent&>(event);
  146. dbgprintf("WM_WindowRectChanged: client_id=%d, window_id=%d, rect=%s\n",
  147. changed_event.client_id(),
  148. changed_event.window_id(),
  149. changed_event.rect().to_string().characters());
  150. #endif
  151. break;
  152. }
  153. case GUI::Event::WM_WindowIconBitmapChanged: {
  154. auto& changed_event = static_cast<GUI::WMWindowIconBitmapChangedEvent&>(event);
  155. #ifdef EVENT_DEBUG
  156. dbgprintf("WM_WindowIconBitmapChanged: client_id=%d, window_id=%d, icon_buffer_id=%d\n",
  157. changed_event.client_id(),
  158. changed_event.window_id(),
  159. changed_event.icon_buffer_id());
  160. #endif
  161. if (auto* window = WindowList::the().window(identifier)) {
  162. auto buffer = SharedBuffer::create_from_shbuf_id(changed_event.icon_buffer_id());
  163. ASSERT(buffer);
  164. window->button()->set_icon(Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *buffer, changed_event.icon_size()));
  165. }
  166. break;
  167. }
  168. case GUI::Event::WM_WindowStateChanged: {
  169. auto& changed_event = static_cast<GUI::WMWindowStateChangedEvent&>(event);
  170. #ifdef EVENT_DEBUG
  171. dbgprintf("WM_WindowStateChanged: client_id=%d, window_id=%d, title=%s, rect=%s, is_active=%u, is_minimized=%u\n",
  172. changed_event.client_id(),
  173. changed_event.window_id(),
  174. changed_event.title().characters(),
  175. changed_event.rect().to_string().characters(),
  176. changed_event.is_active(),
  177. changed_event.is_minimized());
  178. #endif
  179. if (!should_include_window(changed_event.window_type()))
  180. break;
  181. auto& window = WindowList::the().ensure_window(identifier);
  182. window.set_title(changed_event.title());
  183. window.set_rect(changed_event.rect());
  184. window.set_active(changed_event.is_active());
  185. window.set_minimized(changed_event.is_minimized());
  186. if (window.is_minimized()) {
  187. window.button()->set_foreground_color(Color::DarkGray);
  188. window.button()->set_text(String::format("[%s]", changed_event.title().characters()));
  189. } else {
  190. window.button()->set_foreground_color(Color::Black);
  191. window.button()->set_text(changed_event.title());
  192. }
  193. window.button()->set_checked(changed_event.is_active());
  194. break;
  195. }
  196. default:
  197. break;
  198. }
  199. }