TaskbarButton.cpp 4.9 KB

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