Skip to content

Commit

Permalink
LibWeb: Scroll page and nav history with keyboard
Browse files Browse the repository at this point in the history
This patch implements basic keyboard functionality for page scrolling
and history traversal.

(cherry picked from commit 1759b82114febe82b1da4ea35c2d3657555f631b)
  • Loading branch information
alec3660 authored and nico committed Oct 19, 2024
1 parent c62dd41 commit b890d7c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Userland/Libraries/LibWeb/Page/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,45 @@ bool EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u32 code
}
}

// FIXME: Implement scroll by line and by page instead of approximating the behavior of other browsers.
auto arrow_key_scroll_distance = 100;
auto page_scroll_distance = document->window()->inner_height() - (document->window()->outer_height() - document->window()->inner_height());

switch (key) {
case UIEvents::KeyCode::Key_Up:
case UIEvents::KeyCode::Key_Down:
if (modifiers && modifiers != UIEvents::KeyModifier::Mod_Ctrl)
break;
if (modifiers)
key == UIEvents::KeyCode::Key_Up ? document->scroll_to_the_beginning_of_the_document() : document->window()->scroll_by(0, INT64_MAX);
else
document->window()->scroll_by(0, key == UIEvents::KeyCode::Key_Up ? -arrow_key_scroll_distance : arrow_key_scroll_distance);
return true;
case UIEvents::KeyCode::Key_Left:
case UIEvents::KeyCode::Key_Right:
if (modifiers > UIEvents::KeyModifier::Mod_Alt && modifiers != (UIEvents::KeyModifier::Mod_Alt | UIEvents::KeyModifier::Mod_AltGr))
break;
if (modifiers)
document->page().traverse_the_history_by_delta(key == UIEvents::KeyCode::Key_Left ? -1 : 1);
else
document->window()->scroll_by(key == UIEvents::KeyCode::Key_Left ? -arrow_key_scroll_distance : arrow_key_scroll_distance, 0);
return true;
case UIEvents::KeyCode::Key_PageUp:
case UIEvents::KeyCode::Key_PageDown:
if (modifiers > UIEvents::KeyModifier::Mod_None)
break;
document->window()->scroll_by(0, key == UIEvents::KeyCode::Key_PageUp ? -page_scroll_distance : page_scroll_distance);
return true;
case UIEvents::KeyCode::Key_Home:
document->scroll_to_the_beginning_of_the_document();
return true;
case UIEvents::KeyCode::Key_End:
document->window()->scroll_by(0, INT64_MAX);
return true;
default:
break;
}

// FIXME: Work out and implement the difference between this and keydown.
return !fire_keyboard_event(UIEvents::EventNames::keypress, m_navigable, key, modifiers, code_point);
}
Expand Down

0 comments on commit b890d7c

Please sign in to comment.