OpacitySlider.cpp 8.0 KB

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