VideoPaintable.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <AK/NumberFormat.h>
  8. #include <LibGUI/Event.h>
  9. #include <LibGfx/AntiAliasingPainter.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/HTML/HTMLMediaElement.h>
  12. #include <LibWeb/HTML/HTMLVideoElement.h>
  13. #include <LibWeb/Layout/VideoBox.h>
  14. #include <LibWeb/Painting/BorderRadiusCornerClipper.h>
  15. #include <LibWeb/Painting/InputColors.h>
  16. #include <LibWeb/Painting/VideoPaintable.h>
  17. namespace Web::Painting {
  18. static constexpr auto control_box_color = Gfx::Color::from_rgb(0x26'26'26);
  19. static constexpr auto control_highlight_color = Gfx::Color::from_rgb(0x1d'99'f3);
  20. static constexpr Gfx::Color control_button_color(bool is_hovered)
  21. {
  22. if (!is_hovered)
  23. return Color::White;
  24. return control_highlight_color;
  25. }
  26. JS::NonnullGCPtr<VideoPaintable> VideoPaintable::create(Layout::VideoBox const& layout_box)
  27. {
  28. return layout_box.heap().allocate_without_realm<VideoPaintable>(layout_box);
  29. }
  30. VideoPaintable::VideoPaintable(Layout::VideoBox const& layout_box)
  31. : PaintableBox(layout_box)
  32. {
  33. }
  34. Layout::VideoBox& VideoPaintable::layout_box()
  35. {
  36. return static_cast<Layout::VideoBox&>(layout_node());
  37. }
  38. Layout::VideoBox const& VideoPaintable::layout_box() const
  39. {
  40. return static_cast<Layout::VideoBox const&>(layout_node());
  41. }
  42. void VideoPaintable::paint(PaintContext& context, PaintPhase phase) const
  43. {
  44. if (!is_visible())
  45. return;
  46. // FIXME: This should be done at a different level.
  47. if (is_out_of_view(context))
  48. return;
  49. PaintableBox::paint(context, phase);
  50. if (phase != PaintPhase::Foreground)
  51. return;
  52. auto video_rect = context.rounded_device_rect(absolute_rect());
  53. context.painter().add_clip_rect(video_rect.to_type<int>());
  54. ScopedCornerRadiusClip corner_clip { context, context.painter(), video_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
  55. auto const& video_element = layout_box().dom_node();
  56. auto const& layout_mouse_position = video_element.layout_mouse_position({});
  57. Optional<DevicePixelPoint> mouse_position;
  58. if (layout_mouse_position.has_value() && document().hovered_node() == &video_element)
  59. mouse_position = context.rounded_device_point(*layout_mouse_position);
  60. auto paint_user_agent_controls = video_element.has_attribute(HTML::AttributeNames::controls);
  61. if (auto const& bitmap = layout_box().dom_node().current_frame()) {
  62. context.painter().draw_scaled_bitmap(video_rect.to_type<int>(), *bitmap, bitmap->rect(), 1.0f, to_gfx_scaling_mode(computed_values().image_rendering()));
  63. if (paint_user_agent_controls)
  64. paint_loaded_video_controls(context, video_element, video_rect, mouse_position);
  65. } else {
  66. auto input_colors = compute_input_colors(context.palette(), computed_values().accent_color());
  67. context.painter().fill_rect(video_rect.to_type<int>(), input_colors.light_gray);
  68. if (paint_user_agent_controls)
  69. paint_placeholder_video_controls(context, video_rect, mouse_position);
  70. }
  71. }
  72. void VideoPaintable::paint_loaded_video_controls(PaintContext& context, HTML::HTMLVideoElement const& video_element, DevicePixelRect video_rect, Optional<DevicePixelPoint> const& mouse_position) const
  73. {
  74. auto maximum_control_box_size = context.rounded_device_pixels(30);
  75. auto playback_padding = context.rounded_device_pixels(5);
  76. auto is_hovered = document().hovered_node() == &video_element;
  77. auto is_paused = video_element.paused();
  78. if (!is_hovered && !is_paused)
  79. return;
  80. auto control_box_rect = video_rect;
  81. if (control_box_rect.height() > maximum_control_box_size)
  82. control_box_rect.take_from_top(control_box_rect.height() - maximum_control_box_size);
  83. context.painter().fill_rect(control_box_rect.to_type<int>(), control_box_color.with_alpha(0xd0));
  84. layout_box().dom_node().cached_layout_boxes({}).control_box_rect = context.scale_to_css_rect(control_box_rect);
  85. control_box_rect = paint_control_bar_playback_button(context, video_element, control_box_rect, mouse_position);
  86. control_box_rect.take_from_left(playback_padding);
  87. control_box_rect = paint_control_bar_timeline(context, video_element, control_box_rect, mouse_position);
  88. control_box_rect.take_from_left(playback_padding);
  89. control_box_rect = paint_control_bar_timestamp(context, video_element, control_box_rect);
  90. control_box_rect.take_from_left(playback_padding);
  91. }
  92. DevicePixelRect VideoPaintable::paint_control_bar_playback_button(PaintContext& context, HTML::HTMLVideoElement const& video_element, DevicePixelRect control_box_rect, Optional<DevicePixelPoint> const& mouse_position) const
  93. {
  94. auto maximum_playback_button_size = context.rounded_device_pixels(15);
  95. auto maximum_playback_button_offset_x = context.rounded_device_pixels(15);
  96. auto playback_button_size = min(maximum_playback_button_size, control_box_rect.height() / 2);
  97. auto playback_button_offset_x = min(maximum_playback_button_offset_x, control_box_rect.width());
  98. auto playback_button_offset_y = (control_box_rect.height() - playback_button_size) / 2;
  99. auto playback_button_location = control_box_rect.top_left().translated(playback_button_offset_x, playback_button_offset_y);
  100. auto playback_button_hover_rect = DevicePixelRect {
  101. control_box_rect.top_left(),
  102. { playback_button_size + playback_button_offset_x * 2, control_box_rect.height() }
  103. };
  104. layout_box().dom_node().cached_layout_boxes({}).playback_button_rect = context.scale_to_css_rect(playback_button_hover_rect);
  105. auto playback_button_is_hovered = mouse_position.has_value() && playback_button_hover_rect.contains(*mouse_position);
  106. auto playback_button_color = control_button_color(playback_button_is_hovered);
  107. if (video_element.paused()) {
  108. Array<Gfx::IntPoint, 3> play_button_coordinates { {
  109. { 0, 0 },
  110. { static_cast<int>(playback_button_size), static_cast<int>(playback_button_size) / 2 },
  111. { 0, static_cast<int>(playback_button_size) },
  112. } };
  113. context.painter().draw_triangle(playback_button_location.to_type<int>(), play_button_coordinates, playback_button_color);
  114. } else {
  115. DevicePixelRect pause_button_left_rect {
  116. playback_button_location,
  117. { maximum_playback_button_size / 3, playback_button_size }
  118. };
  119. DevicePixelRect pause_button_right_rect {
  120. playback_button_location.translated(maximum_playback_button_size * 2 / 3, 0),
  121. { maximum_playback_button_size / 3, playback_button_size }
  122. };
  123. context.painter().fill_rect(pause_button_left_rect.to_type<int>(), playback_button_color);
  124. context.painter().fill_rect(pause_button_right_rect.to_type<int>(), playback_button_color);
  125. }
  126. control_box_rect.take_from_left(playback_button_hover_rect.width());
  127. return control_box_rect;
  128. }
  129. DevicePixelRect VideoPaintable::paint_control_bar_timeline(PaintContext& context, HTML::HTMLVideoElement const& video_element, DevicePixelRect control_box_rect, Optional<DevicePixelPoint> const& mouse_position) const
  130. {
  131. auto maximum_timeline_button_size = context.rounded_device_pixels(16);
  132. auto timeline_rect = control_box_rect;
  133. timeline_rect.set_width(min(control_box_rect.width() * 6 / 10, timeline_rect.width()));
  134. layout_box().dom_node().cached_layout_boxes({}).timeline_rect = context.scale_to_css_rect(timeline_rect);
  135. auto playback_percentage = video_element.current_time() / video_element.duration();
  136. auto playback_position = static_cast<double>(static_cast<int>(timeline_rect.width())) * playback_percentage;
  137. auto timeline_button_size = min(maximum_timeline_button_size, timeline_rect.height() / 2);
  138. auto timeline_button_offset_x = static_cast<DevicePixels>(round(playback_position));
  139. Gfx::AntiAliasingPainter painter { context.painter() };
  140. auto playback_timelime_scrub_rect = timeline_rect;
  141. playback_timelime_scrub_rect.shrink(0, timeline_rect.height() - timeline_button_size / 2);
  142. auto timeline_past_rect = playback_timelime_scrub_rect;
  143. timeline_past_rect.set_width(timeline_button_offset_x);
  144. painter.fill_rect_with_rounded_corners(timeline_past_rect.to_type<int>(), control_highlight_color.lightened(), 4);
  145. auto timeline_future_rect = playback_timelime_scrub_rect;
  146. timeline_future_rect.take_from_left(timeline_button_offset_x);
  147. painter.fill_rect_with_rounded_corners(timeline_future_rect.to_type<int>(), Color::Black, 4);
  148. auto timeline_button_rect = timeline_rect;
  149. timeline_button_rect.shrink(timeline_rect.width() - timeline_button_size, timeline_rect.height() - timeline_button_size);
  150. timeline_button_rect.set_x(timeline_rect.x() + timeline_button_offset_x - timeline_button_size / 2);
  151. auto timeline_is_hovered = mouse_position.has_value() && timeline_rect.contains(*mouse_position);
  152. auto timeline_color = control_button_color(timeline_is_hovered);
  153. painter.fill_ellipse(timeline_button_rect.to_type<int>(), timeline_color);
  154. control_box_rect.take_from_left(timeline_rect.width() + timeline_button_size / 2);
  155. return control_box_rect;
  156. }
  157. DevicePixelRect VideoPaintable::paint_control_bar_timestamp(PaintContext& context, HTML::HTMLVideoElement const& video_element, DevicePixelRect control_box_rect) const
  158. {
  159. auto current_time = human_readable_digital_time(round(video_element.current_time()));
  160. auto duration = human_readable_digital_time(round(video_element.duration()));
  161. auto timestamp = String::formatted("{} / {}", current_time, duration).release_value_but_fixme_should_propagate_errors();
  162. auto timestamp_size = static_cast<DevicePixels::Type>(ceilf(context.painter().font().width(timestamp)));
  163. if (timestamp_size > control_box_rect.width())
  164. return control_box_rect;
  165. auto timestamp_rect = control_box_rect;
  166. timestamp_rect.set_width(timestamp_size);
  167. context.painter().draw_text(timestamp_rect.to_type<int>(), timestamp, Gfx::TextAlignment::CenterLeft, Color::White);
  168. control_box_rect.take_from_left(timestamp_rect.width());
  169. return control_box_rect;
  170. }
  171. void VideoPaintable::paint_placeholder_video_controls(PaintContext& context, DevicePixelRect video_rect, Optional<DevicePixelPoint> const& mouse_position) const
  172. {
  173. auto maximum_control_box_size = context.rounded_device_pixels(100);
  174. auto maximum_playback_button_size = context.rounded_device_pixels(40);
  175. auto center = video_rect.center();
  176. auto control_box_size = min(maximum_control_box_size, min(video_rect.width(), video_rect.height()) * 4 / 5);
  177. auto control_box_offset_x = control_box_size / 2;
  178. auto control_box_offset_y = control_box_size / 2;
  179. auto control_box_location = center.translated(-control_box_offset_x, -control_box_offset_y);
  180. DevicePixelRect control_box_rect { control_box_location, { control_box_size, control_box_size } };
  181. auto playback_button_size = min(maximum_playback_button_size, min(video_rect.width(), video_rect.height()) * 2 / 5);
  182. auto playback_button_offset_x = playback_button_size / 2;
  183. auto playback_button_offset_y = playback_button_size / 2;
  184. // We want to center the play button on its center of mass, which is not the midpoint of its vertices.
  185. // To do so, reduce its desired x offset by a factor of tan(30 degrees) / 2 (about 0.288685).
  186. playback_button_offset_x -= 0.288685f * static_cast<float>(static_cast<DevicePixels::Type>(playback_button_offset_x));
  187. auto playback_button_location = center.translated(-playback_button_offset_x, -playback_button_offset_y);
  188. Array<Gfx::IntPoint, 3> play_button_coordinates { {
  189. { 0, 0 },
  190. { static_cast<int>(playback_button_size), static_cast<int>(playback_button_size) / 2 },
  191. { 0, static_cast<int>(playback_button_size) },
  192. } };
  193. auto playback_button_is_hovered = mouse_position.has_value() && control_box_rect.contains(*mouse_position);
  194. auto playback_button_color = control_button_color(playback_button_is_hovered);
  195. Gfx::AntiAliasingPainter painter { context.painter() };
  196. painter.fill_ellipse(control_box_rect.to_type<int>(), control_box_color);
  197. context.painter().draw_triangle(playback_button_location.to_type<int>(), play_button_coordinates, playback_button_color);
  198. }
  199. VideoPaintable::DispatchEventOfSameName VideoPaintable::handle_mouseup(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  200. {
  201. if (button != GUI::MouseButton::Primary)
  202. return DispatchEventOfSameName::No;
  203. auto& video_element = layout_box().dom_node();
  204. auto const& cached_layout_boxes = video_element.cached_layout_boxes({});
  205. // FIXME: This runs from outside the context of any user script, so we do not have a running execution
  206. // context. This pushes one to allow the promise creation hook to run.
  207. auto& environment_settings = document().relevant_settings_object();
  208. environment_settings.prepare_to_run_script();
  209. ScopeGuard guard { [&] { environment_settings.clean_up_after_running_script(); } };
  210. auto toggle_playback = [&]() -> WebIDL::ExceptionOr<void> {
  211. if (video_element.paused())
  212. TRY(video_element.play());
  213. else
  214. TRY(video_element.pause());
  215. return {};
  216. };
  217. if (cached_layout_boxes.control_box_rect.has_value() && cached_layout_boxes.control_box_rect->contains(position)) {
  218. if (cached_layout_boxes.playback_button_rect.has_value() && cached_layout_boxes.playback_button_rect->contains(position)) {
  219. toggle_playback().release_value_but_fixme_should_propagate_errors();
  220. return DispatchEventOfSameName::Yes;
  221. }
  222. if (cached_layout_boxes.timeline_rect.has_value() && cached_layout_boxes.timeline_rect->contains(position)) {
  223. auto x_offset = position.x() - cached_layout_boxes.timeline_rect->x();
  224. auto x_percentage = static_cast<float>(x_offset) / static_cast<float>(cached_layout_boxes.timeline_rect->width());
  225. auto position = static_cast<double>(x_percentage) * video_element.duration();
  226. video_element.set_current_time(position);
  227. return DispatchEventOfSameName::Yes;
  228. }
  229. return DispatchEventOfSameName::No;
  230. }
  231. toggle_playback().release_value_but_fixme_should_propagate_errors();
  232. return DispatchEventOfSameName::Yes;
  233. }
  234. VideoPaintable::DispatchEventOfSameName VideoPaintable::handle_mousemove(Badge<EventHandler>, CSSPixelPoint position, unsigned, unsigned)
  235. {
  236. auto& video_element = layout_box().dom_node();
  237. if (absolute_rect().contains(position)) {
  238. video_element.set_layout_mouse_position({}, position);
  239. return DispatchEventOfSameName::Yes;
  240. }
  241. video_element.set_layout_mouse_position({}, {});
  242. return DispatchEventOfSameName::No;
  243. }
  244. }