From 3793b7c6bd26f7dee98cdc7fe869f52793431d8a Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 2 Jul 2023 21:38:11 -0700 Subject: [PATCH] LibWeb: Move helper to toggle a media element's playback to the element This will be needed elsewhere. --- .../LibWeb/HTML/HTMLMediaElement.cpp | 17 +++++++++++++++++ .../Libraries/LibWeb/HTML/HTMLMediaElement.h | 1 + .../LibWeb/Painting/MediaPaintable.cpp | 19 ++----------------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp index a347c1d6b5..7df28b1934 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp @@ -366,6 +366,23 @@ WebIDL::ExceptionOr HTMLMediaElement::pause() return {}; } +WebIDL::ExceptionOr HTMLMediaElement::toggle_playback() +{ + // FIXME: This runs from outside the context of any user script, so we do not have a running execution + // context. This pushes one to allow the promise creation hook to run. + auto& environment_settings = document().relevant_settings_object(); + environment_settings.prepare_to_run_script(); + + ScopeGuard guard { [&] { environment_settings.clean_up_after_running_script(); } }; + + if (potentially_playing()) + TRY(pause()); + else + TRY(play()); + + return {}; +} + // https://html.spec.whatwg.org/multipage/media.html#dom-media-volume WebIDL::ExceptionOr HTMLMediaElement::set_volume(double volume) { diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h index f3b3fc560e..57606b9be9 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h @@ -84,6 +84,7 @@ public: bool potentially_playing() const; WebIDL::ExceptionOr> play(); WebIDL::ExceptionOr pause(); + WebIDL::ExceptionOr toggle_playback(); double volume() const { return m_volume; } WebIDL::ExceptionOr set_volume(double); diff --git a/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp b/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp index ab950f96ca..a8d423d9cc 100644 --- a/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp @@ -310,24 +310,9 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mouseup(Badge WebIDL::ExceptionOr { - if (media_element.paused()) - TRY(media_element.play()); - else - TRY(media_element.pause()); - return {}; - }; - if (cached_layout_boxes.control_box_rect.has_value() && cached_layout_boxes.control_box_rect->contains(position)) { if (cached_layout_boxes.playback_button_rect.has_value() && cached_layout_boxes.playback_button_rect->contains(position)) { - toggle_playback().release_value_but_fixme_should_propagate_errors(); + media_element.toggle_playback().release_value_but_fixme_should_propagate_errors(); return DispatchEventOfSameName::Yes; } @@ -339,7 +324,7 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mouseup(Badge