From 7b1541d2e84224f5920a59f8309f9ea23357e81d Mon Sep 17 00:00:00 2001 From: Kyle Reed <3761006+kallanreed@users.noreply.github.com> Date: Wed, 28 Jun 2023 12:50:11 -0700 Subject: [PATCH] work around calling focus from ctor path. (#1207) --- firmware/application/apps/ui_playlist.cpp | 31 +++++++++++++---------- firmware/application/apps/ui_playlist.hpp | 3 ++- firmware/common/ui_widget.cpp | 2 ++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/firmware/application/apps/ui_playlist.cpp b/firmware/application/apps/ui_playlist.cpp index f74e18f0..8e1f6644 100644 --- a/firmware/application/apps/ui_playlist.cpp +++ b/firmware/application/apps/ui_playlist.cpp @@ -108,6 +108,11 @@ void PlaylistView::on_file_changed(const fs::path& new_file_path) { load_file(playlist_path_); update_ui(); + + // TODO: fix in UI framework with 'try_focus()'? + // Hack around focus getting called by ctor before parent is set. + if (parent()) + button_play.focus(); } void PlaylistView::open_file(bool prompt_save) { @@ -126,7 +131,6 @@ void PlaylistView::open_file(bool prompt_save) { open_view->push_dir(u"PLAYLIST"); open_view->on_changed = [this](fs::path new_file_path) { on_file_changed(new_file_path); - button_play.focus(); }; } @@ -162,9 +166,14 @@ void PlaylistView::save_file(bool show_dialogs) { } void PlaylistView::add_entry(fs::path&& path) { - if (playlist_path_.empty()) + if (playlist_path_.empty()) { playlist_path_ = next_filename_matching_pattern(u"/PLAYLIST/PLAY_????.PPL"); + // Hack around focus getting called by ctor before parent is set. + if (parent()) + button_play.focus(); + } + auto entry = load_entry(std::move(path)); if (entry) { playlist_db_.emplace_back(*std::move(entry)); @@ -400,11 +409,7 @@ PlaylistView::PlaylistView( auto open_view = nav_.push(".C16"); open_view->push_dir(u"CAPTURES"); open_view->on_changed = [this](fs::path path) { - // Set focus to play only on the first "add". - auto set_focus = playlist_path_.empty(); add_entry(std::move(path)); - if (set_focus) - button_play.focus(); }; }; @@ -472,17 +477,17 @@ void PlaylistView::set_parent_rect(Rect new_parent_rect) { waterfall.set_parent_rect(waterfall_rect); } -void PlaylistView::on_hide() { - stop(); - waterfall.on_hide(); - View::on_hide(); -} - -void PlaylistView::on_show() { +void PlaylistView::focus() { if (playlist_path_.empty()) button_add.focus(); else button_play.focus(); } +void PlaylistView::on_hide() { + stop(); + waterfall.on_hide(); + View::on_hide(); +} + } /* namespace ui */ diff --git a/firmware/application/apps/ui_playlist.hpp b/firmware/application/apps/ui_playlist.hpp index 87877762..f1c796d9 100644 --- a/firmware/application/apps/ui_playlist.hpp +++ b/firmware/application/apps/ui_playlist.hpp @@ -48,9 +48,10 @@ class PlaylistView : public View { PlaylistView(NavigationView& nav, const std::filesystem::path& path); ~PlaylistView(); + // Following 2 called by 'NavigationView::update_view' after view is created. void set_parent_rect(Rect new_parent_rect) override; + void focus() override; void on_hide() override; - void on_show() override; std::string title() const override { return "Replay"; }; diff --git a/firmware/common/ui_widget.cpp b/firmware/common/ui_widget.cpp index 92a1cd0e..84f0d0f0 100644 --- a/firmware/common/ui_widget.cpp +++ b/firmware/common/ui_widget.cpp @@ -177,6 +177,8 @@ const std::vector& Widget::children() const { } Context& Widget::context() const { + chDbgAssert(parent_, "parent_ is null", + "Check that parent isn't null before deref."); return parent()->context(); }