Merge pull request #22657 from hrydgard/game-browser-restore-focus

Fixes for keyboard directory navigation on the games tab
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2026-10-19 12:05:39 +02:00
commit 99adff60f2
6 files changed
+89 -13

No files matched your search

+24
View File
@@ -17,6 +17,7 @@ extern bool focusForced;
static View *focusedView;
static bool focusMovementEnabled;
bool focusForced;
static bool scrollFocusedIntoView;
static std::function<void(UISound)> soundCallback;
@@ -105,6 +106,10 @@ View *GetFocusedView() {
return focusedView;
}
void ScrollFocusedViewIntoView() {
scrollFocusedIntoView = true;
}
void SetFocusedView(View *view, FocusFlags cause, bool force) {
if (focusedView) {
focusedView->FocusChanged(FocusFlags::LOST_FOCUS | cause);
@@ -252,6 +257,18 @@ static bool KeyToFocusMove(const KeyInput &key, FocusMove *move) {
KeyEventResult KeyEventToFocusMoves(const KeyInput &key) {
KeyEventResult retval = KeyEventResult::PASS_THROUGH;
// The OS's own repeats of a key we're already repeating (see ProcessHeldKeys) would double
// up with ours, in a text field for example.
if ((key.flags & KeyInputFlags::DOWN) && (key.flags & KeyInputFlags::IS_REPEAT) && !heldKeys.empty()) {
HeldKey hk;
hk.key = key.keyCode;
hk.deviceId = key.deviceId;
hk.triggerTime = 0.0; // irrelevant
hk.move = FocusMove::NEXT; // irrelevant, not part of the comparison
if (heldKeys.find(hk) != heldKeys.end()) {
return KeyEventResult::IGNORE_KEY;
}
}
// Ignore repeats for focus moves.
if ((key.flags & KeyInputFlags::DOWN) && !(key.flags & KeyInputFlags::IS_REPEAT)) {
FocusMove move;
@@ -450,6 +467,13 @@ DialogResult UpdateViewHierarchy(ViewGroup *root, bool canEnableFocusMovement) {
}
focusMoves.clear();
if (scrollFocusedIntoView) {
scrollFocusedIntoView = false;
if (focusedView) {
root->SubviewFocused(focusedView);
}
}
root->Update();
return DispatchEvents();
}
+3
View File
@@ -18,6 +18,9 @@ void EnableFocusMovement(bool enable);
bool IsFocusMovementEnabled();
View *GetFocusedView();
void SetFocusedView(View *view, FocusFlags cause, bool force = false);
// Scrolls the focused view into sight at the start of the next update, once it's been laid out.
// For focusing a view that was just created, since SetFocus doesn't scroll by itself.
void ScrollFocusedViewIntoView();
void RemoveQueuedEventsByEvent(Event *e);
void RemoveQueuedEventsByView(View * v);
+49 -9
View File
@@ -762,7 +762,7 @@ void GameBrowser::FocusGame(const Path &gamePath) {
}
}
// Otherwise, Refresh focuses it when it recreates the buttons.
focusGamePath_ = gamePath;
focusPath_ = gamePath;
refreshPending_ = true;
}
@@ -776,13 +776,27 @@ bool GameBrowser::Key(const KeyInput &input) {
// Alt+Up goes to the parent directory, as in file managers.
if ((input.flags & KeyInputFlags::DOWN) && (input.flags & KeyInputFlags::ModAlt) && input.keyCode == NKCODE_DPAD_UP && input.deviceId == DEVICE_ID_KEYBOARD) {
if ((browseFlags_ & BrowseFlags::NAVIGATE) && path_.CanNavigateUp()) {
// Like the ".." button.
path_.Navigate("..");
PathChanged();
NavigateUp();
return true;
}
}
// Backspace with no search going also goes to the parent directory, as in file managers. Never on
// a repeat, or holding it would climb to the root (or carry on up after clearing a search). Not
// every platform flags repeats, so this also tracks whether it's held.
if (input.keyCode == NKCODE_DEL && input.deviceId == DEVICE_ID_KEYBOARD) {
if (input.flags & KeyInputFlags::UP) {
backspaceHeld_ = false;
} else if (input.flags & KeyInputFlags::DOWN) {
const bool repeat = backspaceHeld_ || (input.flags & KeyInputFlags::IS_REPEAT);
backspaceHeld_ = true;
if (!repeat && search_.searchFilter.empty() && (browseFlags_ & BrowseFlags::NAVIGATE) && path_.CanNavigateUp()) {
NavigateUp();
return true;
}
}
}
bool retval = LinearLayout::Key(input);
if (retval) {
return true;
@@ -1122,10 +1136,11 @@ void GameBrowser::Refresh() {
}
}
DirButton *upButton = nullptr;
if (browseFlags_ & BrowseFlags::NAVIGATE) {
if (path_.CanNavigateUp()) {
gameList_->Add(new DirButton(Path(".."), lastViewMode_, new UI::LinearLayoutParams(UI::FILL_PARENT, UI::FILL_PARENT)))->
OnClick.Handle(this, &GameBrowser::NavigateClick);
upButton = gameList_->Add(new DirButton(Path(".."), lastViewMode_, new UI::LinearLayoutParams(UI::FILL_PARENT, UI::FILL_PARENT)));
upButton->OnClick.Handle(this, &GameBrowser::NavigateClick);
}
// Add any pinned paths before other directories.
@@ -1143,6 +1158,9 @@ void GameBrowser::Refresh() {
for (size_t i = 0; i < dirButtons.size(); i++) {
gameList_->Add(dirButtons[i])->OnClick.Handle(this, &GameBrowser::NavigateClick);
if (!focusPath_.empty() && dirButtons[i]->GetPath() == focusPath_ && dirButtons[i]->SetFocus(FocusFlags::CAUSE_RESTORE)) {
UI::ScrollFocusedViewIntoView();
}
}
for (size_t i = 0; i < gameButtons.size(); i++) {
@@ -1151,11 +1169,22 @@ void GameBrowser::Refresh() {
b->OnHoldClick.Handle(this, &GameBrowser::GameButtonHoldClick);
b->OnHighlight.Handle(this, &GameBrowser::GameButtonHighlight);
if (!focusGamePath_.empty() && b->GamePath() == focusGamePath_) {
b->SetFocus(FocusFlags::CAUSE_RESTORE);
if (!focusPath_.empty() && b->GamePath() == focusPath_ && b->SetFocus(FocusFlags::CAUSE_RESTORE)) {
UI::ScrollFocusedViewIntoView();
}
}
focusGamePath_.clear();
// A listing that's still loading has no buttons to focus yet.
if (!listingPending_) {
if (focusFirstItem_) {
// The folder's own contents rather than "..", which only shows up first if it's empty.
UI::View *first = !dirButtons.empty() ? (UI::View *)dirButtons[0] : !gameButtons.empty() ? (UI::View *)gameButtons[0] : upButton;
if (first && first->SetFocus(FocusFlags::CAUSE_RESTORE)) {
UI::ScrollFocusedViewIntoView();
}
}
focusFirstItem_ = false;
focusPath_.clear();
}
// Show a button to toggle pinning at the very end.
if ((browseFlags_ & BrowseFlags::PIN) && !path_.GetPath().empty()) {
@@ -1247,9 +1276,20 @@ void GameBrowser::NavigateClick(UI::EventParams &e) {
Path text = button->GetPath();
if (button->PathAbsolute()) {
path_.SetPath(text);
} else if (text.ToString() == "..") {
NavigateUp();
return;
} else {
path_.Navigate(text.ToString());
}
focusFirstItem_ = true;
PathChanged();
}
void GameBrowser::NavigateUp() {
// Focus the directory we came out of, in the parent's listing.
focusPath_ = path_.GetPath();
path_.Navigate("..");
PathChanged();
}
+6 -1
View File
@@ -65,6 +65,7 @@ private:
void GameButtonHoldClick(UI::EventParams &e);
void GameButtonHighlight(UI::EventParams &e);
void NavigateClick(UI::EventParams &e);
void NavigateUp();
void PathChanged();
void LayoutChange(UI::EventParams &e);
void LastClick(UI::EventParams &e);
@@ -85,7 +86,11 @@ private:
ViewSearch search_{};
Path focusGamePath_;
// A game or directory for Refresh to focus.
Path focusPath_;
// Set when entering a directory, for Refresh to focus its first entry.
bool focusFirstItem_ = false;
bool backspaceHeld_ = false;
bool listingPending_ = false;
bool refreshPending_ = false;
float lastScale_ = 1.0f;
+1 -2
View File
@@ -507,9 +507,8 @@ bool ViewSearch::Key(UI::ViewGroup *viewGroup, const KeyInput &input) {
// TODO: Restore focus state here.
UI::EnableFocusMovement(false);
}
} else {
// Empty search filter. Navigate upwards on backspace?
}
// With an empty filter, it's up to the owner (the game browser goes up a directory).
} else if (!searchFilter.empty() && input.keyCode == NKCODE_ESCAPE) {
searchFilter.clear();
ApplySearchFilter(viewGroup, false);
+6 -1
View File
@@ -264,8 +264,11 @@ namespace WindowsRawInput {
key.keyCode = GetTrueVKey(raw->data.keyboard);
if (key.keyCode) {
// Windows auto-repeats a held key as more key downs. Flag them, like other platforms do.
if (!keyboardKeysDown.insert(key.keyCode).second) {
key.flags |= KeyInputFlags::IS_REPEAT;
}
NativeKey(key);
keyboardKeysDown.insert(key.keyCode);
}
} else if (raw->data.keyboard.Message == WM_KEYUP || raw->data.keyboard.Message == WM_SYSKEYUP) {
// A key released while Alt is held comes as WM_SYSKEYUP.
@@ -434,6 +437,8 @@ namespace WindowsRawInput {
key.keyCode = *i;
NativeKey(key);
}
// Or the next press of one would count as a repeat.
keyboardKeysDown.clear();
focused = false;
}