1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:28:12 +00:00

LibWeb: Move helper to toggle a media element's playback to the element

This will be needed elsewhere.
This commit is contained in:
Timothy Flynn 2023-07-02 21:38:11 -07:00 committed by Andreas Kling
parent 435ced70b8
commit 3793b7c6bd
3 changed files with 20 additions and 17 deletions

View file

@ -366,6 +366,23 @@ WebIDL::ExceptionOr<void> HTMLMediaElement::pause()
return {};
}
WebIDL::ExceptionOr<void> 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<void> HTMLMediaElement::set_volume(double volume)
{

View file

@ -84,6 +84,7 @@ public:
bool potentially_playing() const;
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> play();
WebIDL::ExceptionOr<void> pause();
WebIDL::ExceptionOr<void> toggle_playback();
double volume() const { return m_volume; }
WebIDL::ExceptionOr<void> set_volume(double);

View file

@ -310,24 +310,9 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mouseup(Badge<Eve
if (button != GUI::MouseButton::Primary)
return DispatchEventOfSameName::Yes;
// 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(); } };
auto toggle_playback = [&]() -> WebIDL::ExceptionOr<void> {
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<Eve
return DispatchEventOfSameName::No;
}
toggle_playback().release_value_but_fixme_should_propagate_errors();
media_element.toggle_playback().release_value_but_fixme_should_propagate_errors();
return DispatchEventOfSameName::Yes;
}