LibWeb: Return EventResult in EventHandler::focus_next/previous_element

No functional changes.
This commit is contained in:
Jelle Raaijmakers 2025-06-13 13:06:32 +02:00 committed by Alexander Kalenik
commit e4586abc18
Notes: github-actions[bot] 2025-06-13 15:40:38 +00:00
2 changed files with 17 additions and 19 deletions

View file

@ -989,24 +989,24 @@ EventResult EventHandler::handle_drag_and_drop_event(DragEvent::Type type, CSSPi
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
bool EventHandler::focus_next_element() EventResult EventHandler::focus_next_element()
{ {
if (!m_navigable->active_document()) if (!m_navigable->active_document())
return false; return EventResult::Dropped;
if (!m_navigable->active_document()->is_fully_active()) if (!m_navigable->active_document()->is_fully_active())
return false; return EventResult::Dropped;
auto set_focus_to_first_focusable_element = [&]() { auto set_focus_to_first_focusable_element = [&] {
auto* element = m_navigable->active_document()->first_child_of_type<DOM::Element>(); auto* element = m_navigable->active_document()->first_child_of_type<DOM::Element>();
for (; element; element = element->next_element_in_pre_order()) { for (; element; element = element->next_element_in_pre_order()) {
if (element->is_focusable()) { if (element->is_focusable()) {
m_navigable->active_document()->set_focused_element(element); m_navigable->active_document()->set_focused_element(element);
return true; return EventResult::Handled;
} }
} }
return false; return EventResult::Dropped;
}; };
auto* element = m_navigable->active_document()->focused_element(); auto* element = m_navigable->active_document()->focused_element();
@ -1020,28 +1020,28 @@ bool EventHandler::focus_next_element()
return set_focus_to_first_focusable_element(); return set_focus_to_first_focusable_element();
m_navigable->active_document()->set_focused_element(element); m_navigable->active_document()->set_focused_element(element);
return true; return EventResult::Handled;
} }
bool EventHandler::focus_previous_element() EventResult EventHandler::focus_previous_element()
{ {
if (!m_navigable->active_document()) if (!m_navigable->active_document())
return false; return EventResult::Dropped;
if (!m_navigable->active_document()->is_fully_active()) if (!m_navigable->active_document()->is_fully_active())
return false; return EventResult::Dropped;
auto set_focus_to_last_focusable_element = [&]() { auto set_focus_to_last_focusable_element = [&] {
// FIXME: This often returns the HTML element itself, which has no previous sibling. // FIXME: This often returns the HTML element itself, which has no previous sibling.
auto* element = m_navigable->active_document()->last_child_of_type<DOM::Element>(); auto* element = m_navigable->active_document()->last_child_of_type<DOM::Element>();
for (; element; element = element->previous_element_in_pre_order()) { for (; element; element = element->previous_element_in_pre_order()) {
if (element->is_focusable()) { if (element->is_focusable()) {
m_navigable->active_document()->set_focused_element(element); m_navigable->active_document()->set_focused_element(element);
return true; return EventResult::Handled;
} }
} }
return false; return EventResult::Dropped;
}; };
auto* element = m_navigable->active_document()->focused_element(); auto* element = m_navigable->active_document()->focused_element();
@ -1055,7 +1055,7 @@ bool EventHandler::focus_previous_element()
return set_focus_to_last_focusable_element(); return set_focus_to_last_focusable_element();
m_navigable->active_document()->set_focused_element(element); m_navigable->active_document()->set_focused_element(element);
return true; return EventResult::Handled;
} }
constexpr bool should_ignore_keydown_event(u32 code_point, u32 modifiers) constexpr bool should_ignore_keydown_event(u32 code_point, u32 modifiers)
@ -1177,9 +1177,7 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
if (!(modifiers & UIEvents::KeyModifier::Mod_Ctrl)) { if (!(modifiers & UIEvents::KeyModifier::Mod_Ctrl)) {
if (key == UIEvents::KeyCode::Key_Tab) { if (key == UIEvents::KeyCode::Key_Tab) {
if (modifiers & UIEvents::KeyModifier::Mod_Shift) return modifiers & UIEvents::KeyModifier::Mod_Shift ? focus_previous_element() : focus_next_element();
return focus_previous_element() ? EventResult::Handled : EventResult::Dropped;
return focus_next_element() ? EventResult::Handled : EventResult::Dropped;
} }
} }

View file

@ -46,8 +46,8 @@ public:
Unicode::Segmenter& word_segmenter(); Unicode::Segmenter& word_segmenter();
private: private:
bool focus_next_element(); EventResult focus_next_element();
bool focus_previous_element(); EventResult focus_previous_element();
EventResult fire_keyboard_event(FlyString const& event_name, HTML::Navigable&, UIEvents::KeyCode, unsigned modifiers, u32 code_point, bool repeat); EventResult fire_keyboard_event(FlyString const& event_name, HTML::Navigable&, UIEvents::KeyCode, unsigned modifiers, u32 code_point, bool repeat);
[[nodiscard]] EventResult input_event(FlyString const& event_name, FlyString const& input_type, HTML::Navigable&, u32 code_point); [[nodiscard]] EventResult input_event(FlyString const& event_name, FlyString const& input_type, HTML::Navigable&, u32 code_point);