TaskbarWindow.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "TaskbarWindow.h"
  2. #include <LibGUI/GWindow.h>
  3. #include <LibGUI/GDesktop.h>
  4. #include <LibGUI/GEventLoop.h>
  5. #include <LibGUI/GBoxLayout.h>
  6. #include <LibGUI/GButton.h>
  7. #include <LibGUI/GFrame.h>
  8. #include <WindowServer/WSAPITypes.h>
  9. #include <stdio.h>
  10. //#define EVENT_DEBUG
  11. TaskbarWindow::TaskbarWindow()
  12. {
  13. set_window_type(GWindowType::Taskbar);
  14. set_title("Taskbar");
  15. set_should_exit_event_loop_on_close(true);
  16. on_screen_rect_change(GDesktop::the().rect());
  17. GDesktop::the().on_rect_change = [this] (const Rect& rect) { on_screen_rect_change(rect); };
  18. auto* widget = new GFrame;
  19. widget->set_fill_with_background_color(true);
  20. widget->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  21. widget->layout()->set_margins({ 3, 2, 3, 2 });
  22. widget->layout()->set_spacing(3);
  23. widget->set_frame_thickness(1);
  24. widget->set_frame_shape(FrameShape::Panel);
  25. widget->set_frame_shadow(FrameShadow::Raised);
  26. set_main_widget(widget);
  27. m_window_list.aid_create_button = [this] {
  28. return create_button();
  29. };
  30. }
  31. TaskbarWindow::~TaskbarWindow()
  32. {
  33. }
  34. void TaskbarWindow::on_screen_rect_change(const Rect& rect)
  35. {
  36. Rect new_rect { rect.x(), rect.bottom() - taskbar_height() + 1, rect.width(), taskbar_height() };
  37. set_rect(new_rect);
  38. }
  39. GButton* TaskbarWindow::create_button()
  40. {
  41. auto* button = new GButton(main_widget());
  42. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  43. button->set_preferred_size({ 140, 22 });
  44. button->set_checkable(true);
  45. button->set_text_alignment(TextAlignment::CenterLeft);
  46. return button;
  47. }
  48. static bool should_include_window(GWindowType window_type)
  49. {
  50. return window_type == GWindowType::Normal;
  51. }
  52. void TaskbarWindow::wm_event(GWMEvent& event)
  53. {
  54. WindowIdentifier identifier { event.client_id(), event.window_id() };
  55. switch (event.type()) {
  56. case GEvent::WM_WindowRemoved: {
  57. #ifdef EVENT_DEBUG
  58. auto& removed_event = static_cast<GWMWindowRemovedEvent&>(event);
  59. dbgprintf("WM_WindowRemoved: client_id=%d, window_id=%d\n",
  60. removed_event.client_id(),
  61. removed_event.window_id()
  62. );
  63. #endif
  64. m_window_list.remove_window(identifier);
  65. update();
  66. break;
  67. }
  68. case GEvent::WM_WindowRectChanged: {
  69. #ifdef EVENT_DEBUG
  70. auto& changed_event = static_cast<GWMWindowRectChangedEvent&>(event);
  71. dbgprintf("WM_WindowRectChanged: client_id=%d, window_id=%d, rect=%s\n",
  72. changed_event.client_id(),
  73. changed_event.window_id(),
  74. changed_event.rect().to_string().characters()
  75. );
  76. #endif
  77. break;
  78. }
  79. case GEvent::WM_WindowIconChanged: {
  80. auto& changed_event = static_cast<GWMWindowIconChangedEvent&>(event);
  81. #ifdef EVENT_DEBUG
  82. dbgprintf("WM_WindowIconChanged: client_id=%d, window_id=%d, icon_path=%s\n",
  83. changed_event.client_id(),
  84. changed_event.window_id(),
  85. changed_event.icon_path().characters()
  86. );
  87. #endif
  88. if (auto* window = m_window_list.window(identifier)) {
  89. window->set_icon_path(changed_event.icon_path());
  90. window->button()->set_icon(window->icon());
  91. }
  92. break;
  93. }
  94. case GEvent::WM_WindowStateChanged: {
  95. auto& changed_event = static_cast<GWMWindowStateChangedEvent&>(event);
  96. #ifdef EVENT_DEBUG
  97. dbgprintf("WM_WindowStateChanged: client_id=%d, window_id=%d, title=%s, rect=%s, is_active=%u, is_minimized=%u\n",
  98. changed_event.client_id(),
  99. changed_event.window_id(),
  100. changed_event.title().characters(),
  101. changed_event.rect().to_string().characters(),
  102. changed_event.is_active(),
  103. changed_event.is_minimized()
  104. );
  105. #endif
  106. if (!should_include_window(changed_event.window_type()))
  107. break;
  108. auto& window = m_window_list.ensure_window(identifier);
  109. window.set_title(changed_event.title());
  110. window.set_rect(changed_event.rect());
  111. window.set_active(changed_event.is_active());
  112. window.set_minimized(changed_event.is_minimized());
  113. if (window.is_minimized()) {
  114. window.button()->set_foreground_color(Color::DarkGray);
  115. window.button()->set_caption(String::format("[%s]", changed_event.title().characters()));
  116. } else {
  117. window.button()->set_foreground_color(Color::Black);
  118. window.button()->set_caption(changed_event.title());
  119. }
  120. window.button()->set_checked(changed_event.is_active());
  121. break;
  122. }
  123. default:
  124. break;
  125. }
  126. }