TaskbarButton.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "TaskbarButton.h"
  7. #include "WindowList.h"
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGUI/WindowManagerServerConnection.h>
  11. #include <LibGUI/WindowServerConnection.h>
  12. #include <LibGfx/Font.h>
  13. #include <LibGfx/FontDatabase.h>
  14. #include <LibGfx/Palette.h>
  15. #include <LibGfx/StylePainter.h>
  16. TaskbarButton::TaskbarButton(const WindowIdentifier& identifier)
  17. : m_identifier(identifier)
  18. {
  19. }
  20. TaskbarButton::~TaskbarButton()
  21. {
  22. }
  23. void TaskbarButton::context_menu_event(GUI::ContextMenuEvent&)
  24. {
  25. GUI::WindowManagerServerConnection::the().async_popup_window_menu(
  26. m_identifier.client_id(),
  27. m_identifier.window_id(),
  28. screen_relative_rect().location());
  29. }
  30. void TaskbarButton::update_taskbar_rect()
  31. {
  32. GUI::WindowManagerServerConnection::the().async_set_window_taskbar_rect(
  33. m_identifier.client_id(),
  34. m_identifier.window_id(),
  35. screen_relative_rect());
  36. }
  37. void TaskbarButton::clear_taskbar_rect()
  38. {
  39. GUI::WindowManagerServerConnection::the().async_set_window_taskbar_rect(
  40. m_identifier.client_id(),
  41. m_identifier.window_id(),
  42. {});
  43. }
  44. void TaskbarButton::resize_event(GUI::ResizeEvent& event)
  45. {
  46. update_taskbar_rect();
  47. return GUI::Button::resize_event(event);
  48. }
  49. static void paint_custom_progressbar(GUI::Painter& painter, const Gfx::IntRect& rect, const Gfx::IntRect& text_rect, const Palette& palette, int min, int max, int value, const StringView& text, const Gfx::Font& font, Gfx::TextAlignment text_alignment)
  50. {
  51. float range_size = max - min;
  52. float progress = (value - min) / range_size;
  53. float progress_width = progress * rect.width();
  54. Gfx::IntRect progress_rect { rect.x(), rect.y(), (int)progress_width, rect.height() };
  55. {
  56. Gfx::PainterStateSaver saver(painter);
  57. painter.add_clip_rect(progress_rect);
  58. Color start_color = palette.active_window_border1();
  59. Color end_color = palette.active_window_border2();
  60. painter.fill_rect_with_gradient(rect, start_color, end_color);
  61. if (!text.is_null()) {
  62. painter.draw_text(text_rect.translated(1, 1), text, font, text_alignment, palette.base_text(), Gfx::TextElision::Right);
  63. painter.draw_text(text_rect, text, font, text_alignment, palette.base_text().inverted(), Gfx::TextElision::Right);
  64. }
  65. }
  66. Gfx::IntRect hole_rect { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() };
  67. hole_rect.translate_by(rect.location());
  68. hole_rect.set_right_without_resize(rect.right());
  69. Gfx::PainterStateSaver saver(painter);
  70. painter.add_clip_rect(hole_rect);
  71. if (!text.is_null())
  72. painter.draw_text(text_rect, text, font, text_alignment, palette.base_text(), Gfx::TextElision::Right);
  73. }
  74. void TaskbarButton::paint_event(GUI::PaintEvent& event)
  75. {
  76. VERIFY(icon());
  77. auto& icon = *this->icon();
  78. auto& font = is_checked() ? this->font().bold_variant() : this->font();
  79. auto& window = WindowList::the().ensure_window(m_identifier);
  80. GUI::Painter painter(*this);
  81. painter.add_clip_rect(event.rect());
  82. Gfx::StylePainter::paint_button(painter, rect(), palette(), button_style(), is_being_pressed(), is_hovered(), is_checked(), is_enabled());
  83. if (text().is_empty())
  84. return;
  85. auto content_rect = rect().shrunken(8, 2);
  86. auto icon_location = content_rect.center().translated(-(icon.width() / 2), -(icon.height() / 2));
  87. if (!text().is_empty())
  88. icon_location.set_x(content_rect.x());
  89. if (!text().is_empty()) {
  90. content_rect.translate_by(icon.width() + 4, 0);
  91. content_rect.set_width(content_rect.width() - icon.width() - 4);
  92. }
  93. Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
  94. if (text_rect.width() > content_rect.width())
  95. text_rect.set_width(content_rect.width());
  96. text_rect.align_within(content_rect, text_alignment());
  97. if (is_being_pressed() || is_checked()) {
  98. text_rect.translate_by(1, 1);
  99. icon_location.translate_by(1, 1);
  100. }
  101. if (window.progress().has_value()) {
  102. auto adjusted_rect = rect().shrunken(4, 4);
  103. if (is_being_pressed() || is_checked()) {
  104. adjusted_rect.set_height(adjusted_rect.height() + 1);
  105. }
  106. paint_custom_progressbar(painter, adjusted_rect, text_rect, palette(), 0, 100, window.progress().value(), text(), font, text_alignment());
  107. }
  108. if (is_enabled()) {
  109. if (is_hovered())
  110. painter.blit_brightened(icon_location, icon, icon.rect());
  111. else
  112. painter.blit(icon_location, icon, icon.rect());
  113. } else {
  114. painter.blit_disabled(icon_location, icon, icon.rect(), palette());
  115. }
  116. if (!window.progress().has_value())
  117. paint_text(painter, text_rect, font, text_alignment());
  118. }