From dbed1c0fa5599b1b96c221b89b10f1e350351645 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Thu, 6 Feb 2025 16:40:27 +0100 Subject: refactor: file_browser::item -> file_browser::entry --- app/mac/impl/file-browser-posix.cc | 16 ++++++++-------- app/mac/impl/file-browser-posix.hh | 2 +- lib/nes/app/file-browser.hh | 17 +++++++++-------- lib/nes/app/ui/CMakeLists.txt | 2 ++ lib/nes/app/ui/screen-browser.cc | 27 +++++++++++++-------------- lib/nes/app/ui/screen-browser.hh | 2 +- lib/nes/app/ui/screen-settings.cc | 5 +++++ lib/nes/app/ui/screen-settings.hh | 16 ++++++++++++++++ 8 files changed, 55 insertions(+), 32 deletions(-) create mode 100644 lib/nes/app/ui/screen-settings.cc create mode 100644 lib/nes/app/ui/screen-settings.hh diff --git a/app/mac/impl/file-browser-posix.cc b/app/mac/impl/file-browser-posix.cc index 8cde6b7..500ce83 100644 --- a/app/mac/impl/file-browser-posix.cc +++ b/app/mac/impl/file-browser-posix.cc @@ -39,31 +39,31 @@ namespace nes::app::mac } } - auto file_browser_posix::read_next(item* out_item) -> bool + auto file_browser_posix::read_next(entry* out_entry) -> bool { if (!it_) { return false; } while (true) { errno = 0; - auto const entry = readdir(it_); - if (!entry) + auto const ent = readdir(it_); + if (!ent) { if (errno) { perror("file_browser_posix: readdir"); } return false; } - auto const name = std::string_view{ entry->d_name }; + auto const name = std::string_view{ ent->d_name }; if (name.length() > max_name_length) { continue; } if (name[0] == '.') { continue; } - auto type = item_type::directory; - if (entry->d_type == DT_DIR) { type = item_type::directory; } - else if (entry->d_type == DT_REG) { type = item_type::file; } + auto type = entry_type{}; + if (ent->d_type == DT_DIR) { type = entry_type::directory; } + else if (ent->d_type == DT_REG) { type = entry_type::file; } else { continue; } it_pos_ += 1; - if (out_item) { *out_item = item{ type, name }; } + if (out_entry) { *out_entry = entry{ type, name }; } return true; } } diff --git a/app/mac/impl/file-browser-posix.hh b/app/mac/impl/file-browser-posix.hh index 4962907..b60ce2b 100644 --- a/app/mac/impl/file-browser-posix.hh +++ b/app/mac/impl/file-browser-posix.hh @@ -19,7 +19,7 @@ namespace nes::app::mac auto get_path() const -> path_view override { return path_.get_components(); } auto seek(u32) -> void override; - auto read_next(item* out_item) -> bool override; + auto read_next(entry* out_entry) -> bool override; auto navigate_up() -> status override; auto navigate(std::string_view) -> status override; auto load(std::string_view, span, u32* out_length) -> status override; diff --git a/lib/nes/app/file-browser.hh b/lib/nes/app/file-browser.hh index 1fc7616..3465000 100644 --- a/lib/nes/app/file-browser.hh +++ b/lib/nes/app/file-browser.hh @@ -18,27 +18,28 @@ namespace nes::app public: static constexpr auto max_name_length = u32{ 64 }; - enum class item_type + enum class entry_type { directory, file, }; - class item + /// An entry in a directory. + class entry { - item_type type_{ item_type::directory }; + entry_type type_{ entry_type::directory }; char name_[max_name_length + 1]{}; public: - explicit item() = default; + explicit entry() = default; - explicit item(item_type const type, std::string_view const name) + explicit entry(entry_type const type, std::string_view const name) : type_{ type } { std::copy_n(name.begin(), std::min(static_cast(name.length()), max_name_length), name_); } - auto get_type() const -> item_type { return type_; } + auto get_type() const -> entry_type { return type_; } auto get_name() const -> std::string_view { return name_; } }; @@ -55,8 +56,8 @@ namespace nes::app /// Go to directory entry at the given index. virtual auto seek(u32) -> void = 0; - /// Read the directory entry at the current index. - virtual auto read_next(item* out_item) -> bool = 0; + /// Read the directory entry at the current index and move the index forward by 1. + virtual auto read_next(entry* out_entry) -> bool = 0; /// Change the current working directory to the parent directory. virtual auto navigate_up() -> status = 0; diff --git a/lib/nes/app/ui/CMakeLists.txt b/lib/nes/app/ui/CMakeLists.txt index 37720c1..c43bb13 100644 --- a/lib/nes/app/ui/CMakeLists.txt +++ b/lib/nes/app/ui/CMakeLists.txt @@ -9,6 +9,8 @@ target_sources( screen-browser.cc screen-freeze.hh screen-freeze.cc + screen-settings.hh + screen-settings.cc screen-error.hh screen-error.cc screen-confirm-quit.hh diff --git a/lib/nes/app/ui/screen-browser.cc b/lib/nes/app/ui/screen-browser.cc index 4643e1c..1b515bd 100644 --- a/lib/nes/app/ui/screen-browser.cc +++ b/lib/nes/app/ui/screen-browser.cc @@ -8,16 +8,15 @@ namespace nes::app { auto screen_browser::selection_impl::render_item( - renderer& renderer, item const& item, i32 const x, i32 const y, u32 const width, - color const color) const -> void + renderer& renderer, item const& item, i32 const x, i32 const y, u32 const width, color const c) const -> void { - switch (item.base.get_type()) + switch (item.entry.get_type()) { - case file_browser::item_type::file: renderer.render_mask_tile(x, y, tiles::icon_cartridge, color); break; - case file_browser::item_type::directory: renderer.render_mask_tile(x, y, tiles::icon_directory, color); break; + case file_browser::entry_type::file: renderer.render_mask_tile(x, y, tiles::icon_cartridge, c); break; + case file_browser::entry_type::directory: renderer.render_mask_tile(x, y, tiles::icon_directory, c); break; } - renderer.render_text(x + 2, y, item.base.get_name(), color, text_attributes{} + renderer.render_text(x + 2, y, item.entry.get_name(), c, text_attributes{} .set_ellipsize_mode(ellipsize_mode::end) .set_max_width(width - 2)); } @@ -31,7 +30,7 @@ namespace nes::app if (page == 0) { items[count].is_parent = true; - items[count].base = file_browser::item{ file_browser::item_type::directory, "[Parent Directory]" }; + items[count].entry = file_browser::entry{ file_browser::entry_type::directory, "[Parent Directory]" }; count += 1; } else @@ -44,11 +43,11 @@ namespace nes::app file_browser_.seek(position); while (count < page_size) { - auto item = file_browser::item{}; - if (!file_browser_.read_next(&item)) { break; } + auto entry = file_browser::entry{}; + if (!file_browser_.read_next(&entry)) { break; } items[count].is_parent = false; - items[count].base = item; + items[count].entry = entry; count += 1; } @@ -125,18 +124,18 @@ namespace nes::app refresh(); if (navigation_steps_ > 0) { navigation_steps_ -= 1; } } - else if (selected->base.get_type() == file_browser::item_type::directory) + else if (selected->entry.get_type() == file_browser::entry_type::directory) { - if (auto const s = file_browser_.navigate(selected->base.get_name()); s != status::success) + if (auto const s = file_browser_.navigate(selected->entry.get_name()); s != status::success) { return action::show_error("Unable to navigate to directory", s); } refresh(); navigation_steps_ += 1; } - else if (selected->base.get_type() == file_browser::item_type::file) + else if (selected->entry.get_type() == file_browser::entry_type::file) { - return action::launch_game(selected->base.get_name()); + return action::launch_game(selected->entry.get_name()); } } } diff --git a/lib/nes/app/ui/screen-browser.hh b/lib/nes/app/ui/screen-browser.hh index 0ccf0b6..4ba6e73 100644 --- a/lib/nes/app/ui/screen-browser.hh +++ b/lib/nes/app/ui/screen-browser.hh @@ -16,7 +16,7 @@ namespace nes::app struct item { bool is_parent{ false }; - file_browser::item base{}; + file_browser::entry entry{}; }; class selection_impl final : public selection diff --git a/lib/nes/app/ui/screen-settings.cc b/lib/nes/app/ui/screen-settings.cc new file mode 100644 index 0000000..4d32662 --- /dev/null +++ b/lib/nes/app/ui/screen-settings.cc @@ -0,0 +1,5 @@ +// +// Created by Hannes Schulze on 06.02.25. +// + +#include "screen-settings.hh" diff --git a/lib/nes/app/ui/screen-settings.hh b/lib/nes/app/ui/screen-settings.hh new file mode 100644 index 0000000..8f510ca --- /dev/null +++ b/lib/nes/app/ui/screen-settings.hh @@ -0,0 +1,16 @@ +// +// Created by Hannes Schulze on 06.02.25. +// + +#ifndef SCREEN_SETTINGS_HH +#define SCREEN_SETTINGS_HH + + + +class screen_settings { + +}; + + + +#endif //SCREEN_SETTINGS_HH -- cgit v1.3.1