MediaPaintable.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/BrowsingContext.h>
  10. #include <LibWeb/HTML/HTMLAudioElement.h>
  11. #include <LibWeb/HTML/HTMLVideoElement.h>
  12. #include <LibWeb/Layout/ReplacedBox.h>
  13. #include <LibWeb/Page/EventHandler.h>
  14. #include <LibWeb/Page/Page.h>
  15. #include <LibWeb/Painting/MediaPaintable.h>
  16. #include <LibWeb/UIEvents/MouseButton.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. MediaPaintable::MediaPaintable(Layout::ReplacedBox const& layout_box)
  27. : PaintableBox(layout_box)
  28. {
  29. }
  30. Optional<DevicePixelPoint> MediaPaintable::mouse_position(PaintContext& context, HTML::HTMLMediaElement const& media_element)
  31. {
  32. auto const& layout_mouse_position = media_element.layout_mouse_position({});
  33. if (layout_mouse_position.has_value() && media_element.document().hovered_node() == &media_element)
  34. return context.rounded_device_point(*layout_mouse_position);
  35. return {};
  36. }
  37. void MediaPaintable::fill_triangle(DisplayListRecorder& painter, Gfx::IntPoint location, Array<Gfx::IntPoint, 3> coordinates, Color color)
  38. {
  39. Gfx::Path path;
  40. path.move_to((coordinates[0] + location).to_type<float>());
  41. path.line_to((coordinates[1] + location).to_type<float>());
  42. path.line_to((coordinates[2] + location).to_type<float>());
  43. path.close();
  44. painter.fill_path({
  45. .path = path,
  46. .color = color,
  47. .winding_rule = Gfx::WindingRule::EvenOdd,
  48. });
  49. }
  50. void MediaPaintable::paint_media_controls(PaintContext& context, HTML::HTMLMediaElement const& media_element, DevicePixelRect media_rect, Optional<DevicePixelPoint> const& mouse_position) const
  51. {
  52. auto components = compute_control_bar_components(context, media_element, media_rect);
  53. context.display_list_recorder().fill_rect(components.control_box_rect.to_type<int>(), control_box_color.with_alpha(0xd0));
  54. paint_control_bar_playback_button(context, media_element, components, mouse_position);
  55. paint_control_bar_timeline(context, media_element, components);
  56. paint_control_bar_timestamp(context, components);
  57. paint_control_bar_speaker(context, media_element, components, mouse_position);
  58. paint_control_bar_volume(context, media_element, components, mouse_position);
  59. }
  60. MediaPaintable::Components MediaPaintable::compute_control_bar_components(PaintContext& context, HTML::HTMLMediaElement const& media_element, DevicePixelRect media_rect) const
  61. {
  62. auto maximum_control_box_height = context.rounded_device_pixels(40);
  63. auto component_padding = context.rounded_device_pixels(5);
  64. Components components {};
  65. components.control_box_rect = media_rect;
  66. if (components.control_box_rect.height() > maximum_control_box_height)
  67. components.control_box_rect.take_from_top(components.control_box_rect.height() - maximum_control_box_height);
  68. auto remaining_rect = components.control_box_rect;
  69. remaining_rect.shrink(component_padding * 2, 0);
  70. auto timeline_rect_height = context.rounded_device_pixels(8);
  71. if ((timeline_rect_height * 3) <= components.control_box_rect.height()) {
  72. components.timeline_rect = components.control_box_rect;
  73. components.timeline_rect.set_height(timeline_rect_height);
  74. remaining_rect.take_from_top(timeline_rect_height);
  75. }
  76. auto playback_button_rect_width = min(context.rounded_device_pixels(40), remaining_rect.width());
  77. components.playback_button_rect = remaining_rect;
  78. components.playback_button_rect.set_width(playback_button_rect_width);
  79. remaining_rect.take_from_left(playback_button_rect_width);
  80. components.speaker_button_size = context.rounded_device_pixels(30);
  81. if (components.speaker_button_size <= remaining_rect.width()) {
  82. components.volume_button_size = context.rounded_device_pixels(16);
  83. if ((components.speaker_button_size + components.volume_button_size * 3) <= remaining_rect.width()) {
  84. auto volume_width = min(context.rounded_device_pixels(60), remaining_rect.width() - components.speaker_button_size);
  85. components.volume_rect = remaining_rect;
  86. components.volume_rect.take_from_left(remaining_rect.width() - volume_width);
  87. remaining_rect.take_from_right(volume_width);
  88. components.volume_scrub_rect = components.volume_rect.shrunken(components.volume_button_size, components.volume_rect.height() - components.volume_button_size / 2);
  89. }
  90. components.speaker_button_rect = remaining_rect;
  91. components.speaker_button_rect.take_from_left(remaining_rect.width() - components.speaker_button_size);
  92. remaining_rect.take_from_right(components.speaker_button_size + component_padding);
  93. }
  94. auto display_time = human_readable_digital_time(round(media_element.layout_display_time({})));
  95. auto duration = human_readable_digital_time(isnan(media_element.duration()) ? 0 : round(media_element.duration()));
  96. components.timestamp = String::formatted("{} / {}", display_time, duration).release_value_but_fixme_should_propagate_errors();
  97. components.timestamp_font = layout_node().scaled_font(context);
  98. auto timestamp_size = DevicePixels { static_cast<DevicePixels::Type>(ceilf(components.timestamp_font->width(components.timestamp))) };
  99. if (timestamp_size <= remaining_rect.width()) {
  100. components.timestamp_rect = remaining_rect;
  101. components.timestamp_rect.take_from_right(remaining_rect.width() - timestamp_size);
  102. remaining_rect.take_from_left(timestamp_size + component_padding);
  103. }
  104. media_element.cached_layout_boxes({}).control_box_rect = context.scale_to_css_rect(components.control_box_rect);
  105. media_element.cached_layout_boxes({}).playback_button_rect = context.scale_to_css_rect(components.playback_button_rect);
  106. media_element.cached_layout_boxes({}).timeline_rect = context.scale_to_css_rect(components.timeline_rect);
  107. media_element.cached_layout_boxes({}).speaker_button_rect = context.scale_to_css_rect(components.speaker_button_rect);
  108. media_element.cached_layout_boxes({}).volume_rect = context.scale_to_css_rect(components.volume_rect);
  109. media_element.cached_layout_boxes({}).volume_scrub_rect = context.scale_to_css_rect(components.volume_scrub_rect);
  110. return components;
  111. }
  112. void MediaPaintable::paint_control_bar_playback_button(PaintContext& context, HTML::HTMLMediaElement const& media_element, Components const& components, Optional<DevicePixelPoint> const& mouse_position)
  113. {
  114. auto playback_button_size = components.playback_button_rect.width() * 4 / 10;
  115. auto playback_button_offset_x = (components.playback_button_rect.width() - playback_button_size) / 2;
  116. auto playback_button_offset_y = (components.playback_button_rect.height() - playback_button_size) / 2;
  117. auto playback_button_location = components.playback_button_rect.top_left().translated(playback_button_offset_x, playback_button_offset_y);
  118. auto playback_button_is_hovered = rect_is_hovered(media_element, components.playback_button_rect, mouse_position);
  119. auto playback_button_color = control_button_color(playback_button_is_hovered);
  120. if (media_element.paused()) {
  121. Array<Gfx::IntPoint, 3> play_button_coordinates { {
  122. { 0, 0 },
  123. { static_cast<int>(playback_button_size), static_cast<int>(playback_button_size) / 2 },
  124. { 0, static_cast<int>(playback_button_size) },
  125. } };
  126. fill_triangle(context.display_list_recorder(), playback_button_location.to_type<int>(), play_button_coordinates, playback_button_color);
  127. } else {
  128. DevicePixelRect pause_button_left_rect {
  129. playback_button_location,
  130. { playback_button_size / 3, playback_button_size }
  131. };
  132. DevicePixelRect pause_button_right_rect {
  133. playback_button_location.translated(playback_button_size * 2 / 3, 0),
  134. { playback_button_size / 3, playback_button_size }
  135. };
  136. context.display_list_recorder().fill_rect(pause_button_left_rect.to_type<int>(), playback_button_color);
  137. context.display_list_recorder().fill_rect(pause_button_right_rect.to_type<int>(), playback_button_color);
  138. }
  139. }
  140. void MediaPaintable::paint_control_bar_timeline(PaintContext& context, HTML::HTMLMediaElement const& media_element, Components const& components)
  141. {
  142. if (components.timeline_rect.is_empty())
  143. return;
  144. auto playback_percentage = isnan(media_element.duration()) ? 0.0 : media_element.layout_display_time({}) / media_element.duration();
  145. auto playback_position = static_cast<double>(static_cast<int>(components.timeline_rect.width())) * playback_percentage;
  146. auto timeline_button_offset_x = static_cast<DevicePixels>(round(playback_position));
  147. auto timeline_past_rect = components.timeline_rect;
  148. timeline_past_rect.set_width(timeline_button_offset_x);
  149. context.display_list_recorder().fill_rect(timeline_past_rect.to_type<int>(), control_highlight_color.lightened());
  150. auto timeline_future_rect = components.timeline_rect;
  151. timeline_future_rect.take_from_left(timeline_button_offset_x);
  152. context.display_list_recorder().fill_rect(timeline_future_rect.to_type<int>(), Color::Black);
  153. }
  154. void MediaPaintable::paint_control_bar_timestamp(PaintContext& context, Components const& components)
  155. {
  156. if (components.timestamp_rect.is_empty())
  157. return;
  158. context.display_list_recorder().draw_text(components.timestamp_rect.to_type<int>(), components.timestamp, *components.timestamp_font, Gfx::TextAlignment::CenterLeft, Color::White);
  159. }
  160. void MediaPaintable::paint_control_bar_speaker(PaintContext& context, HTML::HTMLMediaElement const& media_element, Components const& components, Optional<DevicePixelPoint> const& mouse_position)
  161. {
  162. if (components.speaker_button_rect.is_empty())
  163. return;
  164. auto speaker_button_width = context.rounded_device_pixels(20);
  165. auto speaker_button_height = context.rounded_device_pixels(15);
  166. auto speaker_button_offset_x = (components.speaker_button_rect.width() - speaker_button_width) / 2;
  167. auto speaker_button_offset_y = (components.speaker_button_rect.height() - speaker_button_height) / 2;
  168. auto speaker_button_location = components.speaker_button_rect.top_left().translated(speaker_button_offset_x, speaker_button_offset_y);
  169. auto device_point = [&](double x, double y) {
  170. auto position = context.rounded_device_point({ x, y }) + speaker_button_location;
  171. return position.to_type<DevicePixels::Type>().to_type<float>();
  172. };
  173. auto speaker_button_is_hovered = rect_is_hovered(media_element, components.speaker_button_rect, mouse_position);
  174. auto speaker_button_color = control_button_color(speaker_button_is_hovered);
  175. Gfx::Path path;
  176. path.move_to(device_point(0, 4));
  177. path.line_to(device_point(5, 4));
  178. path.line_to(device_point(11, 0));
  179. path.line_to(device_point(11, 15));
  180. path.line_to(device_point(5, 11));
  181. path.line_to(device_point(0, 11));
  182. path.line_to(device_point(0, 4));
  183. path.close();
  184. context.display_list_recorder().fill_path({ .path = path, .color = speaker_button_color, .winding_rule = Gfx::WindingRule::EvenOdd });
  185. path.clear();
  186. path.move_to(device_point(13, 3));
  187. path.quadratic_bezier_curve_to(device_point(16, 7.5), device_point(13, 12));
  188. path.move_to(device_point(14, 0));
  189. path.quadratic_bezier_curve_to(device_point(20, 7.5), device_point(14, 15));
  190. context.display_list_recorder().stroke_path({
  191. .cap_style = Gfx::Path::CapStyle::Round,
  192. .join_style = Gfx::Path::JoinStyle::Round,
  193. .miter_limit = 4,
  194. .dash_array = {},
  195. .dash_offset = 0,
  196. .path = path,
  197. .color = speaker_button_color,
  198. .thickness = 1,
  199. });
  200. if (media_element.muted()) {
  201. context.display_list_recorder().draw_line(device_point(0, 0).to_type<int>(), device_point(20, 15).to_type<int>(), Color::Red, 2);
  202. context.display_list_recorder().draw_line(device_point(0, 15).to_type<int>(), device_point(20, 0).to_type<int>(), Color::Red, 2);
  203. }
  204. }
  205. void MediaPaintable::paint_control_bar_volume(PaintContext& context, HTML::HTMLMediaElement const& media_element, Components const& components, Optional<DevicePixelPoint> const& mouse_position)
  206. {
  207. if (components.volume_rect.is_empty())
  208. return;
  209. auto volume_position = static_cast<double>(static_cast<int>(components.volume_scrub_rect.width())) * media_element.volume();
  210. auto volume_button_offset_x = static_cast<DevicePixels>(round(volume_position));
  211. auto volume_lower_rect = components.volume_scrub_rect;
  212. volume_lower_rect.set_width(volume_button_offset_x);
  213. context.display_list_recorder().fill_rect_with_rounded_corners(volume_lower_rect.to_type<int>(), control_highlight_color.lightened(), 4);
  214. auto volume_higher_rect = components.volume_scrub_rect;
  215. volume_higher_rect.take_from_left(volume_button_offset_x);
  216. context.display_list_recorder().fill_rect_with_rounded_corners(volume_higher_rect.to_type<int>(), Color::Black, 4);
  217. auto volume_button_rect = components.volume_scrub_rect;
  218. volume_button_rect.shrink(components.volume_scrub_rect.width() - components.volume_button_size, components.volume_scrub_rect.height() - components.volume_button_size);
  219. volume_button_rect.set_x(components.volume_scrub_rect.x() + volume_button_offset_x - components.volume_button_size / 2);
  220. auto volume_is_hovered = rect_is_hovered(media_element, components.volume_rect, mouse_position, HTML::HTMLMediaElement::MouseTrackingComponent::Volume);
  221. auto volume_color = control_button_color(volume_is_hovered);
  222. context.display_list_recorder().fill_ellipse(volume_button_rect.to_type<int>(), volume_color);
  223. }
  224. MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mousedown(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  225. {
  226. if (button != UIEvents::MouseButton::Primary)
  227. return DispatchEventOfSameName::Yes;
  228. auto& media_element = *verify_cast<HTML::HTMLMediaElement>(layout_node_with_style_and_box_metrics().dom_node());
  229. auto const& cached_layout_boxes = media_element.cached_layout_boxes({});
  230. auto position_adjusted_by_scroll_offset = position;
  231. position_adjusted_by_scroll_offset.translate_by(-cumulative_offset_of_enclosing_scroll_frame());
  232. if (cached_layout_boxes.timeline_rect.has_value() && cached_layout_boxes.timeline_rect->contains(position_adjusted_by_scroll_offset)) {
  233. media_element.set_layout_mouse_tracking_component({}, HTML::HTMLMediaElement::MouseTrackingComponent::Timeline);
  234. set_current_time(media_element, *cached_layout_boxes.timeline_rect, position_adjusted_by_scroll_offset, Temporary::Yes);
  235. } else if (cached_layout_boxes.volume_rect.has_value() && cached_layout_boxes.volume_rect->contains(position_adjusted_by_scroll_offset)) {
  236. media_element.set_layout_mouse_tracking_component({}, HTML::HTMLMediaElement::MouseTrackingComponent::Volume);
  237. set_volume(media_element, *cached_layout_boxes.volume_scrub_rect, position_adjusted_by_scroll_offset);
  238. }
  239. if (media_element.layout_mouse_tracking_component({}).has_value())
  240. const_cast<HTML::Navigable&>(*navigable()).event_handler().set_mouse_event_tracking_paintable(this);
  241. return DispatchEventOfSameName::Yes;
  242. }
  243. MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mouseup(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  244. {
  245. auto& media_element = *verify_cast<HTML::HTMLMediaElement>(layout_node_with_style_and_box_metrics().dom_node());
  246. auto const& cached_layout_boxes = media_element.cached_layout_boxes({});
  247. auto position_adjusted_by_scroll_offset = position;
  248. position_adjusted_by_scroll_offset.translate_by(-cumulative_offset_of_enclosing_scroll_frame());
  249. if (auto const& mouse_tracking_component = media_element.layout_mouse_tracking_component({}); mouse_tracking_component.has_value()) {
  250. switch (*mouse_tracking_component) {
  251. case HTML::HTMLMediaElement::MouseTrackingComponent::Timeline:
  252. set_current_time(media_element, *cached_layout_boxes.timeline_rect, position_adjusted_by_scroll_offset, Temporary::No);
  253. media_element.set_layout_display_time({}, {});
  254. break;
  255. case HTML::HTMLMediaElement::MouseTrackingComponent::Volume:
  256. document().page().client().page_did_stop_tooltip_override();
  257. break;
  258. }
  259. const_cast<HTML::Navigable&>(*navigable()).event_handler().set_mouse_event_tracking_paintable(nullptr);
  260. media_element.set_layout_mouse_tracking_component({}, {});
  261. return DispatchEventOfSameName::Yes;
  262. }
  263. if (button != UIEvents::MouseButton::Primary)
  264. return DispatchEventOfSameName::Yes;
  265. if (cached_layout_boxes.control_box_rect.has_value() && cached_layout_boxes.control_box_rect->contains(position_adjusted_by_scroll_offset)) {
  266. if (cached_layout_boxes.playback_button_rect.has_value() && cached_layout_boxes.playback_button_rect->contains(position_adjusted_by_scroll_offset)) {
  267. media_element.toggle_playback().release_value_but_fixme_should_propagate_errors();
  268. return DispatchEventOfSameName::Yes;
  269. }
  270. if (cached_layout_boxes.speaker_button_rect.has_value() && cached_layout_boxes.speaker_button_rect->contains(position_adjusted_by_scroll_offset)) {
  271. media_element.set_muted(!media_element.muted());
  272. return DispatchEventOfSameName::Yes;
  273. }
  274. if (cached_layout_boxes.timeline_rect.has_value() && cached_layout_boxes.timeline_rect->contains(position_adjusted_by_scroll_offset))
  275. return DispatchEventOfSameName::No;
  276. }
  277. media_element.toggle_playback().release_value_but_fixme_should_propagate_errors();
  278. return DispatchEventOfSameName::Yes;
  279. }
  280. MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mousemove(Badge<EventHandler>, CSSPixelPoint position, unsigned, unsigned)
  281. {
  282. auto& media_element = *verify_cast<HTML::HTMLMediaElement>(layout_node_with_style_and_box_metrics().dom_node());
  283. auto const& cached_layout_boxes = media_element.cached_layout_boxes({});
  284. auto position_adjusted_by_scroll_offset = position;
  285. auto scroll_offset = cumulative_offset_of_enclosing_scroll_frame();
  286. position_adjusted_by_scroll_offset.translate_by(-scroll_offset);
  287. if (auto const& mouse_tracking_component = media_element.layout_mouse_tracking_component({}); mouse_tracking_component.has_value()) {
  288. switch (*mouse_tracking_component) {
  289. case HTML::HTMLMediaElement::MouseTrackingComponent::Timeline:
  290. if (cached_layout_boxes.timeline_rect.has_value())
  291. set_current_time(media_element, *cached_layout_boxes.timeline_rect, position_adjusted_by_scroll_offset, Temporary::Yes);
  292. break;
  293. case HTML::HTMLMediaElement::MouseTrackingComponent::Volume:
  294. if (cached_layout_boxes.volume_rect.has_value()) {
  295. set_volume(media_element, *cached_layout_boxes.volume_scrub_rect, position_adjusted_by_scroll_offset);
  296. auto volume = static_cast<u8>(media_element.volume() * 100.0);
  297. document().page().client().page_did_request_tooltip_override({ position_adjusted_by_scroll_offset.x(), cached_layout_boxes.volume_scrub_rect->y() + scroll_offset.y() }, ByteString::formatted("{}%", volume));
  298. }
  299. break;
  300. }
  301. }
  302. if (absolute_rect().contains(position_adjusted_by_scroll_offset)) {
  303. media_element.set_layout_mouse_position({}, position_adjusted_by_scroll_offset);
  304. return DispatchEventOfSameName::Yes;
  305. }
  306. media_element.set_layout_mouse_position({}, {});
  307. return DispatchEventOfSameName::No;
  308. }
  309. void MediaPaintable::set_current_time(HTML::HTMLMediaElement& media_element, CSSPixelRect timeline_rect, CSSPixelPoint mouse_position, Temporary temporarily)
  310. {
  311. auto x_offset = mouse_position.x() - timeline_rect.x();
  312. x_offset = max(x_offset, 0);
  313. x_offset = min(x_offset, timeline_rect.width());
  314. auto x_percentage = static_cast<double>(x_offset) / static_cast<double>(timeline_rect.width());
  315. auto position = static_cast<double>(x_percentage) * media_element.duration();
  316. switch (temporarily) {
  317. case Temporary::Yes:
  318. media_element.set_layout_display_time({}, position);
  319. break;
  320. case Temporary::No:
  321. media_element.set_current_time(position);
  322. break;
  323. }
  324. }
  325. void MediaPaintable::set_volume(HTML::HTMLMediaElement& media_element, CSSPixelRect volume_rect, CSSPixelPoint mouse_position)
  326. {
  327. auto x_offset = mouse_position.x() - volume_rect.x();
  328. x_offset = max(x_offset, 0);
  329. x_offset = min(x_offset, volume_rect.width());
  330. auto volume = static_cast<double>(x_offset) / static_cast<double>(volume_rect.width());
  331. media_element.set_volume(volume).release_value_but_fixme_should_propagate_errors();
  332. }
  333. bool MediaPaintable::rect_is_hovered(HTML::HTMLMediaElement const& media_element, Optional<DevicePixelRect> const& rect, Optional<DevicePixelPoint> const& mouse_position, Optional<HTML::HTMLMediaElement::MouseTrackingComponent> const& allowed_mouse_tracking_component)
  334. {
  335. if (auto const& mouse_tracking_component = media_element.layout_mouse_tracking_component({}); mouse_tracking_component.has_value())
  336. return mouse_tracking_component == allowed_mouse_tracking_component;
  337. if (!rect.has_value() || !mouse_position.has_value())
  338. return false;
  339. return rect->contains(*mouse_position);
  340. }
  341. }