summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb/Page/EventHandler.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-08-14 19:40:37 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-15 00:05:45 +0200
commit01022eb5d69753d55edcbd538ae8a5a3aabf199c (patch)
tree1b1a87d5f4f8533b2e5ddfbc6c6a6c3672167512 /Libraries/LibWeb/Page/EventHandler.cpp
parent5939af14d4b8bf8763e094bb6805765319288466 (diff)
downloadserenity-01022eb5d69753d55edcbd538ae8a5a3aabf199c.zip
LibWeb: Allow focusing individual (focusable) elements with Tab key
You can now cycle through focusable elements (currently only hyperlinks are focusable) with the Tab key. The focus outline is rendered in a new FocusOutline paint phase.
Diffstat (limited to 'Libraries/LibWeb/Page/EventHandler.cpp')
-rw-r--r--Libraries/LibWeb/Page/EventHandler.cpp35
1 files changed, 34 insertions, 1 deletions
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 748ff0f31a..e7281864cc 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -237,8 +237,41 @@ void EventHandler::dump_selection(const char* event_name) const
#endif
}
-bool EventHandler::handle_keydown(KeyCode key, unsigned, u32 code_point)
+bool EventHandler::focus_next_element()
{
+ if (!m_frame.document())
+ return false;
+ auto* element = m_frame.document()->focused_element();
+ if (!element) {
+ element = m_frame.document()->first_child_of_type<DOM::Element>();
+ if (element && element->is_focusable()) {
+ m_frame.document()->set_focused_element(element);
+ return true;
+ }
+ }
+
+ for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
+ ;
+
+ m_frame.document()->set_focused_element(element);
+ return element;
+}
+
+bool EventHandler::focus_previous_element()
+{
+ // FIXME: Implement Shift-Tab cycling backwards through focusable elements!
+ return false;
+}
+
+bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
+{
+ if (key == KeyCode::Key_Tab) {
+ if (modifiers & KeyModifier::Mod_Shift)
+ return focus_previous_element();
+ else
+ return focus_next_element();
+ }
+
if (m_frame.cursor_position().node() && m_frame.cursor_position().node()->is_editable()) {
// FIXME: Support backspacing across DOM node boundaries.
if (key == KeyCode::Key_Backspace && m_frame.cursor_position().offset() > 0) {