TaskbarButton.cpp 4.9 KB

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