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_);
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<FileLoadView>(".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 */

View File

@ -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"; };

View File

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