OpacitySlider.cpp 8.2 KB

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