TaskbarWindow.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "TaskbarWindow.h"
  2. #include "TaskbarWidget.h"
  3. #include <LibGUI/GWindow.h>
  4. #include <LibGUI/GDesktop.h>
  5. #include <LibGUI/GEventLoop.h>
  6. #include <LibGUI/GButton.h>
  7. #include <WindowServer/WSAPITypes.h>
  8. #include <stdio.h>
  9. TaskbarWindow::TaskbarWindow()
  10. {
  11. set_window_type(GWindowType::Taskbar);
  12. set_title("Taskbar");
  13. set_should_exit_event_loop_on_close(true);
  14. on_screen_rect_change(GDesktop::the().rect());
  15. GDesktop::the().on_rect_change = [this] (const Rect& rect) { on_screen_rect_change(rect); };
  16. auto* widget = new TaskbarWidget(m_window_list);
  17. set_main_widget(widget);
  18. m_window_list.aid_create_button = [this] {
  19. return create_button();
  20. };
  21. }
  22. TaskbarWindow::~TaskbarWindow()
  23. {
  24. }
  25. void TaskbarWindow::on_screen_rect_change(const Rect& rect)
  26. {
  27. Rect new_rect { rect.x(), rect.bottom() - taskbar_height() + 1, rect.width(), taskbar_height() };
  28. set_rect(new_rect);
  29. }
  30. GButton* TaskbarWindow::create_button()
  31. {
  32. auto* button = new GButton(main_widget());
  33. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  34. button->set_preferred_size({ 140, 22 });
  35. button->set_checkable(true);
  36. button->set_text_alignment(TextAlignment::CenterLeft);
  37. return button;
  38. }
  39. static bool should_include_window(GWindowType window_type)
  40. {
  41. return window_type == GWindowType::Normal;
  42. }
  43. void TaskbarWindow::wm_event(GWMEvent& event)
  44. {
  45. WindowIdentifier identifier { event.client_id(), event.window_id() };
  46. switch (event.type()) {
  47. case GEvent::WM_WindowRemoved: {
  48. auto& removed_event = static_cast<GWMWindowRemovedEvent&>(event);
  49. printf("WM_WindowRemoved: client_id=%d, window_id=%d\n",
  50. removed_event.client_id(),
  51. removed_event.window_id()
  52. );
  53. m_window_list.remove_window(identifier);
  54. update();
  55. break;
  56. }
  57. case GEvent::WM_WindowStateChanged: {
  58. auto& changed_event = static_cast<GWMWindowStateChangedEvent&>(event);
  59. printf("WM_WindowStateChanged: client_id=%d, window_id=%d, title=%s, rect=%s, is_active=%u\n",
  60. changed_event.client_id(),
  61. changed_event.window_id(),
  62. changed_event.title().characters(),
  63. changed_event.rect().to_string().characters(),
  64. changed_event.is_active()
  65. );
  66. if (!should_include_window(changed_event.window_type()))
  67. break;
  68. auto& window = m_window_list.ensure_window(identifier);
  69. window.set_title(changed_event.title());
  70. window.set_rect(changed_event.rect());
  71. window.set_active(changed_event.is_active());
  72. window.button()->set_caption(changed_event.title());
  73. window.button()->set_checked(changed_event.is_active());
  74. break;
  75. }
  76. default:
  77. break;
  78. }
  79. }