TaskbarWindow.cpp 8.4 KB

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