LibWeb: Allow toggling playback of media elements with keyboard controls

This allows pausing/playing media elements with the space bar.
This commit is contained in:
Timothy Flynn 2023-07-02 21:40:46 -07:00 committed by Andreas Kling
parent 2c5c815f44
commit a4070b1452
3 changed files with 22 additions and 0 deletions

View file

@ -1857,6 +1857,20 @@ void HTMLMediaElement::reject_pending_play_promises(ReadonlySpan<JS::NonnullGCPt
environment_settings.clean_up_after_running_script();
}
WebIDL::ExceptionOr<void> HTMLMediaElement::handle_keydown(Badge<Web::EventHandler>, KeyCode key)
{
switch (key) {
case KeyCode::Key_Space:
TRY(toggle_playback());
break;
default:
break;
}
return {};
}
void HTMLMediaElement::set_layout_display_time(Badge<Painting::MediaPaintable>, Optional<double> display_time)
{
if (display_time.has_value() && !m_display_time.has_value()) {

View file

@ -11,6 +11,7 @@
#include <AK/Optional.h>
#include <AK/Time.h>
#include <AK/Variant.h>
#include <Kernel/API/KeyCode.h>
#include <LibGfx/Rect.h>
#include <LibJS/Heap/MarkedVector.h>
#include <LibJS/SafeFunction.h>
@ -99,6 +100,8 @@ public:
JS::NonnullGCPtr<AudioTrackList> audio_tracks() const { return *m_audio_tracks; }
JS::NonnullGCPtr<VideoTrackList> video_tracks() const { return *m_video_tracks; }
WebIDL::ExceptionOr<void> handle_keydown(Badge<Web::EventHandler>, KeyCode);
enum class MouseTrackingComponent {
Timeline,
Volume,

View file

@ -745,6 +745,11 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin
}
}
if (auto* element = m_browsing_context->active_document()->focused_element(); is<HTML::HTMLMediaElement>(element)) {
auto& media_element = static_cast<HTML::HTMLMediaElement&>(*element);
media_element.handle_keydown({}, key).release_value_but_fixme_should_propagate_errors();
}
if (m_browsing_context->cursor_position().is_valid() && m_browsing_context->cursor_position().node()->is_editable()) {
if (key == KeyCode::Key_Backspace) {
if (!m_browsing_context->decrement_cursor_position_offset()) {