TaskbarWindow.cpp 8.3 KB

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