Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

Search keyboard: up / down go to the item under the focused one

Rafael Abud Ferreira4b86f3bbcf61
diff --git a/src/app/cli_main.cpp b/src/app/cli_main.cpp
index 09afde7..187fb47 100644
--- a/src/app/cli_main.cpp
+++ b/src/app/cli_main.cpp
@@ -694,6 +694,67 @@ int main(int argc, char** argv) {
check(app.screen() == ui::Screen::Error, "other errors: no list, no Settings");
save("region_plain_error");
return failures ? 1 : 0;
+ } else if (cmd == "ui-search" && argi < argc) {
+ // Offline: the search keyboard's up / down go to the item under the
+ // focused one on screen (the rows hold 6, 3 and a few items).
+ ui::Fonts fonts;
+ if (!fonts.load("assets/fonts")) return 1;
+ ui::ImageCache images([] {});
+ ui::AppUi app(fonts, images);
+ ui::Canvas canvas(1920, 1080);
+ auto press = [&](auto set) {
+ ui::NavInput n;
+ set(n);
+ app.handle(n);
+ };
+ auto down = [&] { press([](ui::NavInput& n) { n.down = true; }); };
+ auto up = [&] { press([](ui::NavInput& n) { n.up = true; }); };
+ auto right = [&] { press([](ui::NavInput& n) { n.right = true; }); };
+ int failures = 0;
+ auto expect = [&](int row, int col, const char* what) {
+ auto [r, c] = app.searchKeyFocus();
+ bool ok = r == row && c == col;
+ std::printf("%s: %s (at %d,%d)\n", ok ? "ok" : "FAILED", what, r, c);
+ failures += !ok;
+ };
+ auto save = [&](const std::string& name) {
+ app.render(canvas, 1000);
+ std::string path = std::string(argv[argi]) + "/" + name + ".png";
+ stbi_write_png(path.c_str(), canvas.width(), canvas.height(), 4, canvas.data(), canvas.width() * 4);
+ };
+ // Game Pass: one filter on the first row, four on the second.
+ app.showFilteredSearch(ui::Tab::GamePass, false, 0, "", 0, -1);
+ for (int i = 0; i < 8; ++i) up(); // from the row of lists to the first letters
+ for (int i = 0; i < 5; ++i) right();
+ expect(0, 5, "letter F");
+ for (int i = 0; i < 6; ++i) down();
+ expect(6, 2, "F, down to the wide keys: Clear");
+ down();
+ expect(7, 0, "Clear, down: the only filter of the first row");
+ down();
+ expect(8, 1, "the centred filter, down: the one under it (not the leftmost)");
+ save("search_gamepass");
+ up();
+ up();
+ expect(6, 1, "up twice: Delete, over the filter");
+ // Your games: three filters, then four.
+ app.showFilteredSearch(ui::Tab::Library, false, 0, "", 0, -1);
+ for (int i = 0; i < 2; ++i) up();
+ expect(6, 0, "Space");
+ right();
+ right();
+ down();
+ expect(7, 2, "Clear, down: the rightmost filter");
+ down();
+ expect(8, 3, "rightmost filter, down: the rightmost list");
+ save("search_library");
+ up();
+ expect(7, 2, "up: back to it");
+ up();
+ expect(6, 2, "up: Clear");
+ up();
+ expect(5, 5, "up: the letter over Clear");
+ return failures ? 1 : 0;
} else if (cmd == "ui-settings" && argi < argc) {
// Offline: Settings with the region list open, then the language list.
ui::Fonts fonts;
diff --git a/src/ui/app_ui.cpp b/src/ui/app_ui.cpp
index 8a3222d..699f7e6 100644
--- a/src/ui/app_ui.cpp
+++ b/src/ui/app_ui.cpp
@@ -70,6 +70,9 @@ constexpr int kKeyW = 90, kKeyH = 62, kKeyGap = 10;
constexpr int kKeysY = 262;
constexpr int kWideCols[3] = {0, 3, 5}; // first column of Space, Delete, Clear
constexpr int kWideSpan[3] = {3, 2, 1};
+constexpr int kKeysW = kKeyCols * (kKeyW + kKeyGap) - kKeyGap;
+constexpr int kChipGap = 10;
+int chipWidth(int count) { return (kKeysW - kChipGap * (count - 1)) / std::max(1, count); }
// Lower case, accents dropped (Latin-1 range), anything else a space: what
// the search compares.
@@ -1706,6 +1709,26 @@ void AppUi::pressFilter(Filter f) {
dirty_ = true;
}
+std::pair<int, int> AppUi::searchKeyFocus() const {
+ std::lock_guard<std::mutex> lock(mutex_);
+ return {keyRow_, keyCol_};
+}
+
+std::vector<int> AppUi::keyCenters(int row) const {
+ // Caller holds mutex_.
+ std::vector<int> out;
+ if (row < kKeyRows) {
+ for (int k = 0; k < kKeyCols; ++k) out.push_back(kMargin + k * (kKeyW + kKeyGap) + kKeyW / 2);
+ } else if (row == kKeyRows) {
+ for (int k = 0; k < 3; ++k)
+ out.push_back(kMargin + kWideCols[k] * (kKeyW + kKeyGap) + (kWideSpan[k] * kKeyW + (kWideSpan[k] - 1) * kKeyGap) / 2);
+ } else {
+ int n = static_cast<int>(filterRow(row - kKeyRows - 1).size()), w = chipWidth(n);
+ for (int k = 0; k < n; ++k) out.push_back(kMargin + k * (w + kChipGap) + w / 2);
+ }
+ return out;
+}
+
void AppUi::handleSearchKeys(const NavInput& in) {
// Caller holds mutex_. keyRow_ == kKeyRows is the row of wide keys, where
// keyCol_ is 0..2 (Space, Delete, Clear); kKeyRows + 1 and + 2 the two
@@ -1723,15 +1746,17 @@ void AppUi::handleSearchKeys(const NavInput& in) {
}
bool wide = keyRow_ == kKeyRows, filters = keyRow_ > kKeyRows;
int filterCount = filters ? static_cast<int>(filterRow(keyRow_ - kKeyRows - 1).size()) : 0;
- if (in.up && keyRow_ > 0) {
- if (wide) keyCol_ = kWideCols[keyCol_];
- if (keyRow_ == kKeyRows + 1) keyCol_ = std::min(keyCol_, 2);
- --keyRow_;
- if (keyRow_ == kKeyRows + 1) keyCol_ = std::min(keyCol_, static_cast<int>(filterRow(0).size()) - 1);
- } else if (in.down && keyRow_ < kKeyRows + 2) {
- ++keyRow_;
- if (keyRow_ == kKeyRows) keyCol_ = keyCol_ < 3 ? 0 : keyCol_ < 5 ? 1 : 2;
- else keyCol_ = std::min(keyCol_, static_cast<int>(filterRow(keyRow_ - kKeyRows - 1).size()) - 1);
+ if ((in.up && keyRow_ > 0) || (in.down && keyRow_ < kKeyRows + 2)) {
+ // To the item on the next row whose middle is nearest this one's
+ // (the rows have 6, 3 and a few items of different widths).
+ auto from = keyCenters(keyRow_);
+ int x = from[static_cast<size_t>(std::clamp(keyCol_, 0, static_cast<int>(from.size()) - 1))];
+ keyRow_ += in.up ? -1 : 1;
+ auto to = keyCenters(keyRow_);
+ int best = 0;
+ for (int k = 1; k < static_cast<int>(to.size()); ++k)
+ if (std::abs(to[static_cast<size_t>(k)] - x) < std::abs(to[static_cast<size_t>(best)] - x)) best = k;
+ keyCol_ = best;
} else if (in.left && keyCol_ > 0) {
--keyCol_;
} else if (in.right) {
@@ -2647,13 +2672,13 @@ void AppUi::drawSearch(Canvas& c, uint64_t nowMs) {
c.fillRect({0, kH - 80, kW, 80}, withAlpha(kBg, 245));
// The filters, two rows under the keys (over the fade): green when on;
// the lists with a chevron.
- const int kbW2 = kKeyCols * (kKeyW + kKeyGap) - kKeyGap, gap = 10, px = 18, fh = 50;
+ const int gap = kChipGap, px = 18, fh = 50;
const int fy0 = kKeysY + (kKeyRows + 1) * (kKeyH + kKeyGap) + 6;
Rect openChip{};
for (int row = 0; row < 2; ++row) {
auto chips = filterRow(row);
int fy = fy0 + row * (fh + 10);
- int chipW = (kbW2 - gap * (static_cast<int>(chips.size()) - 1)) / static_cast<int>(chips.size());
+ int chipW = chipWidth(static_cast<int>(chips.size()));
for (size_t k = 0; k < chips.size(); ++k) {
Filter f = chips[k];
bool on = filterOn(f);
diff --git a/src/ui/app_ui.h b/src/ui/app_ui.h
index 258bcc9..146e0ce 100644
--- a/src/ui/app_ui.h
+++ b/src/ui/app_ui.h
@@ -273,6 +273,8 @@ public:
// `language` as their lists' index, `genre` "" for all), optionally with
// `openList` (0 mode, 1 genre, 2 language) open; and the first results.
void showFilteredSearch(Tab tab, bool cheapest, int mode, const std::string& genre, int language, int openList = -1);
+ // The search keyboard's focus (row, column), for the tests.
+ std::pair<int, int> searchKeyFocus() const;
std::vector<std::pair<std::string, PriceInfo>> searchResults(size_t max) const;
enum class Filter { Free, Cheapest, Console, Mode, Genre, Publisher, Language };
void chooseFilter(Filter f, int index);
@@ -349,6 +351,10 @@ private:
bool anyFilter() const;
void handleHome(const NavInput& in, UiEvent& ev);
void handleSearchKeys(const NavInput& in);
+ // The x of the middle of each item on a row of the search keyboard
+ // (letters, wide keys, the two rows of filters), as drawn: up / down
+ // go to the nearest one on screen.
+ std::vector<int> keyCenters(int row) const;
void runSearch();
void openDetails(const GameTile& tile);
void refreshDetail();