mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:58: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:
parent
435ced70b8
commit
3793b7c6bd
3 changed files with 20 additions and 17 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue