OpacitySlider.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. * Copyright (c) 2023, networkException <networkexception@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibGUI/OpacitySlider.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibGfx/StylePainter.h>
  12. REGISTER_WIDGET(GUI, HorizontalOpacitySlider)
  13. REGISTER_WIDGET(GUI, VerticalOpacitySlider)
  14. namespace GUI {
  15. OpacitySlider::OpacitySlider(Gfx::Orientation orientation)
  16. : AbstractSlider(orientation)
  17. {
  18. set_min(0);
  19. set_max(100);
  20. set_value(100);
  21. set_preferred_size(SpecialDimension::Fit);
  22. }
  23. Gfx::IntRect OpacitySlider::frame_inner_rect() const
  24. {
  25. return rect().shrunken(4, 4);
  26. }
  27. void OpacitySlider::paint_event(PaintEvent& event)
  28. {
  29. GUI::Painter painter(*this);
  30. painter.add_clip_rect(event.rect());
  31. auto inner_rect = frame_inner_rect();
  32. // Grid pattern
  33. Gfx::StylePainter::paint_transparency_grid(painter, inner_rect, palette());
  34. // Alpha gradient
  35. for (int pos = inner_rect.first_edge_for_orientation(orientation()); pos <= inner_rect.last_edge_for_orientation(orientation()); ++pos) {
  36. float relative_offset = (float)pos / (float)inner_rect.primary_size_for_orientation(orientation());
  37. float alpha = relative_offset * 255.0f;
  38. Color color = m_base_color.with_alpha(static_cast<u8>(AK::min(alpha, UINT8_MAX)));
  39. if (orientation() == Gfx::Orientation::Horizontal) {
  40. painter.fill_rect({ pos, inner_rect.top(), 1, inner_rect.height() }, color);
  41. } else {
  42. painter.fill_rect({ inner_rect.left(), pos, inner_rect.right(), 1 }, color);
  43. }
  44. }
  45. constexpr int notch_size = 3;
  46. if (orientation() == Gfx::Orientation::Horizontal) {
  47. int notch_y_top = inner_rect.top() + notch_size;
  48. int notch_y_bottom = inner_rect.bottom() - notch_size;
  49. int notch_x = inner_rect.left() + ((float)value() / (float)max() * (float)inner_rect.width());
  50. // Top notch
  51. painter.set_pixel(notch_x, notch_y_top, palette().threed_shadow2());
  52. for (int i = notch_size; i >= 0; --i) {
  53. painter.set_pixel(notch_x - (i + 1), notch_y_top - i - 1, palette().threed_highlight());
  54. for (int j = 0; j < i * 2; ++j) {
  55. painter.set_pixel(notch_x - (i + 1) + j + 1, notch_y_top - i - 1, palette().button());
  56. }
  57. painter.set_pixel(notch_x + (i + 0), notch_y_top - i - 1, palette().threed_shadow1());
  58. painter.set_pixel(notch_x + (i + 1), notch_y_top - i - 1, palette().threed_shadow2());
  59. }
  60. // Bottom notch
  61. painter.set_pixel(notch_x, notch_y_bottom, palette().threed_shadow2());
  62. for (int i = 0; i < notch_size; ++i) {
  63. painter.set_pixel(notch_x - (i + 1), notch_y_bottom + i + 1, palette().threed_highlight());
  64. for (int j = 0; j < i * 2; ++j) {
  65. painter.set_pixel(notch_x - (i + 1) + j + 1, notch_y_bottom + i + 1, palette().button());
  66. }
  67. painter.set_pixel(notch_x + (i + 0), notch_y_bottom + i + 1, palette().threed_shadow1());
  68. painter.set_pixel(notch_x + (i + 1), notch_y_bottom + i + 1, palette().threed_shadow2());
  69. }
  70. // Hairline
  71. // NOTE: If we're in the whiter part of the gradient, the notch is painted as shadow between the notches.
  72. // If we're in the darker part, the notch is painted as highlight.
  73. // We adjust the hairline's x position so it lines up with the shadow/highlight of the notches.
  74. u8 h = ((float)value() / (float)max()) * 255.0f;
  75. if (h < 128)
  76. painter.draw_line({ notch_x, notch_y_top }, { notch_x, notch_y_bottom }, Color(h, h, h, 255));
  77. else
  78. painter.draw_line({ notch_x - 1, notch_y_top }, { notch_x - 1, notch_y_bottom }, Color(h, h, h, 255));
  79. } else {
  80. int notch_x_left = inner_rect.left() + notch_size;
  81. int notch_x_right = inner_rect.right() - notch_size;
  82. int notch_y = inner_rect.top() + ((float)value() / (float)max() * (float)inner_rect.height());
  83. // Left notch
  84. painter.set_pixel(notch_x_left, notch_y, palette().threed_shadow2());
  85. for (int i = notch_size; i >= 0; --i) {
  86. painter.set_pixel(notch_x_left - i - 1, notch_y - (i + 1), palette().threed_highlight());
  87. for (int j = 0; j < i * 2; ++j) {
  88. painter.set_pixel(notch_x_left - i - 1, notch_y - (i + 1) + j + 1, palette().button());
  89. }
  90. painter.set_pixel(notch_x_left - i - 1, notch_y + (i + 0), palette().threed_shadow1());
  91. painter.set_pixel(notch_x_left - i - 1, notch_y + (i + 1), palette().threed_shadow2());
  92. }
  93. // Bottom notch
  94. painter.set_pixel(notch_x_right, notch_y, palette().threed_shadow2());
  95. for (int i = 0; i < notch_size; ++i) {
  96. painter.set_pixel(notch_x_right + i + 1, notch_y - (i + 1), palette().threed_highlight());
  97. for (int j = 0; j < i * 2; ++j) {
  98. painter.set_pixel(notch_x_right + i + 1, notch_y - (i + 1) + j + 1, palette().button());
  99. }
  100. painter.set_pixel(notch_x_right + i + 1, notch_y + (i + 0), palette().threed_shadow1());
  101. painter.set_pixel(notch_x_right + i + 1, notch_y + (i + 1), palette().threed_shadow2());
  102. }
  103. // Hairline
  104. // NOTE: See above
  105. u8 h = ((float)value() / (float)max()) * 255.0f;
  106. if (h < 128)
  107. painter.draw_line({ notch_x_left, notch_y }, { notch_x_right, notch_y }, Color(h, h, h, 255));
  108. else
  109. painter.draw_line({ notch_x_left, notch_y - 1 }, { notch_x_right, notch_y - 1 }, Color(h, h, h, 255));
  110. }
  111. // Text label
  112. // FIXME: better support text in vertical orientation, either by having a vertical option for draw_text, or only showing when there is enough space
  113. auto percent_text = DeprecatedString::formatted("{}%", (int)((float)value() / (float)max() * 100.0f));
  114. painter.draw_text(inner_rect.translated(1, 1), percent_text, Gfx::TextAlignment::Center, Color::Black);
  115. painter.draw_text(inner_rect, percent_text, Gfx::TextAlignment::Center, Color::White);
  116. // Frame
  117. Gfx::StylePainter::paint_frame(painter, rect(), palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  118. }
  119. int OpacitySlider::value_at(Gfx::IntPoint position) const
  120. {
  121. auto inner_rect = frame_inner_rect();
  122. auto relevant_position = position.primary_offset_for_orientation(orientation()),
  123. begin_position = inner_rect.first_edge_for_orientation(orientation()),
  124. end_position = inner_rect.last_edge_for_orientation(orientation());
  125. if (relevant_position < begin_position)
  126. return min();
  127. if (relevant_position > end_position)
  128. return max();
  129. float relative_offset = (float)(relevant_position - begin_position) / (float)inner_rect.primary_size_for_orientation(orientation());
  130. int range = max() - min();
  131. return min() + (int)(relative_offset * (float)range);
  132. }
  133. void OpacitySlider::set_base_color(Gfx::Color base_color)
  134. {
  135. m_base_color = base_color;
  136. update();
  137. }
  138. void OpacitySlider::mousedown_event(MouseEvent& event)
  139. {
  140. if (event.button() == MouseButton::Primary) {
  141. m_dragging = true;
  142. set_value(value_at(event.position()));
  143. return;
  144. }
  145. AbstractSlider::mousedown_event(event);
  146. }
  147. void OpacitySlider::mousemove_event(MouseEvent& event)
  148. {
  149. if (m_dragging) {
  150. set_value(value_at(event.position()));
  151. return;
  152. }
  153. AbstractSlider::mousemove_event(event);
  154. }
  155. void OpacitySlider::mouseup_event(MouseEvent& event)
  156. {
  157. if (event.button() == MouseButton::Primary) {
  158. m_dragging = false;
  159. return;
  160. }
  161. AbstractSlider::mouseup_event(event);
  162. }
  163. void OpacitySlider::mousewheel_event(MouseEvent& event)
  164. {
  165. decrease_slider_by(event.wheel_delta_y());
  166. }
  167. Optional<UISize> OpacitySlider::calculated_min_size() const
  168. {
  169. if (orientation() == Gfx::Orientation::Vertical)
  170. return { { 22, 40 } };
  171. return { { 40, 22 } };
  172. }
  173. Optional<UISize> OpacitySlider::calculated_preferred_size() const
  174. {
  175. if (orientation() == Gfx::Orientation::Vertical)
  176. return { { SpecialDimension::Shrink, SpecialDimension::OpportunisticGrow } };
  177. return { { SpecialDimension::OpportunisticGrow, SpecialDimension::Shrink } };
  178. }
  179. }