work around calling focus from ctor path. (#1207)

This commit is contained in:
Kyle Reed 2023-06-28 12:50:11 -07:00 committed by GitHub
parent 830fea63ed
commit 7b1541d2e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View File

@ -108,6 +108,11 @@ void PlaylistView::on_file_changed(const fs::path& new_file_path) {
load_file(playlist_path_); load_file(playlist_path_);
update_ui(); 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) { 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->push_dir(u"PLAYLIST");
open_view->on_changed = [this](fs::path new_file_path) { open_view->on_changed = [this](fs::path new_file_path) {
on_file_changed(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) { 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"); 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)); auto entry = load_entry(std::move(path));
if (entry) { if (entry) {
playlist_db_.emplace_back(*std::move(entry)); playlist_db_.emplace_back(*std::move(entry));
@ -400,11 +409,7 @@ PlaylistView::PlaylistView(
auto open_view = nav_.push<FileLoadView>(".C16"); auto open_view = nav_.push<FileLoadView>(".C16");
open_view->push_dir(u"CAPTURES"); open_view->push_dir(u"CAPTURES");
open_view->on_changed = [this](fs::path path) { 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)); 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); waterfall.set_parent_rect(waterfall_rect);
} }
void PlaylistView::on_hide() { void PlaylistView::focus() {
stop();
waterfall.on_hide();
View::on_hide();
}
void PlaylistView::on_show() {
if (playlist_path_.empty()) if (playlist_path_.empty())
button_add.focus(); button_add.focus();
else else
button_play.focus(); button_play.focus();
} }
void PlaylistView::on_hide() {
stop();
waterfall.on_hide();
View::on_hide();
}
} /* namespace ui */ } /* namespace ui */

View File

@ -48,9 +48,10 @@ class PlaylistView : public View {
PlaylistView(NavigationView& nav, const std::filesystem::path& path); PlaylistView(NavigationView& nav, const std::filesystem::path& path);
~PlaylistView(); ~PlaylistView();
// Following 2 called by 'NavigationView::update_view' after view is created.
void set_parent_rect(Rect new_parent_rect) override; void set_parent_rect(Rect new_parent_rect) override;
void focus() override;
void on_hide() override; void on_hide() override;
void on_show() override;
std::string title() const override { return "Replay"; }; std::string title() const override { return "Replay"; };

View File

@ -177,6 +177,8 @@ const std::vector<Widget*>& Widget::children() const {
} }
Context& Widget::context() const { Context& Widget::context() const {
chDbgAssert(parent_, "parent_ is null",
"Check that parent isn't null before deref.");
return parent()->context(); return parent()->context();
} }