Skip to content

Commit

Permalink
Merge pull request #629 from reupen/optimise-playlist-view-search-2-0
Browse files Browse the repository at this point in the history
Optimise typing in playlist view to jump to items in foobar2000 2.0
  • Loading branch information
reupen authored Nov 21, 2022
2 parents 50ea7a1 + 04b7ba9 commit 9b630ce
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

This requires the SVG services component. Text is not supported.

- Performance when typing into the playlist view to jump to an item was improved
in foobar2000 2.0. [[#629](https://github.com/reupen/columns_ui/pull/629)]

### Bug fixes

- A bug where ampersands didn’t render correctly in tab names in the Playlist
Expand Down
49 changes: 49 additions & 0 deletions foo_ui_columns/ng_playlist/ng_playlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,55 @@ void PlaylistView::s_redraw_all()
RedrawWindow(window->get_wnd(), nullptr, nullptr, RDW_INVALIDATE);
}

const char* PlaylistView::PlaylistViewSearchContext::get_item_text(size_t index)
{
constexpr size_t max_batch_size = 1000;

if (!m_start_index) {
m_playlist_manager->activeplaylist_get_all_items(m_tracks);
m_items.resize(m_tracks.size());
GetLocalTime(&m_systemtime);
m_start_index = index;
} else if (m_items[index]) {
return m_items[index]->c_str();
}

const auto batch_end_index
= std::min(index + max_batch_size, index < *m_start_index ? *m_start_index : m_tracks.size());

const auto batch_size = batch_end_index - index;
const auto batch_tracks = pfc::list_partial_ref_t(m_tracks, index, batch_size);

const bool has_global_variables = m_global_script.is_valid();

m_metadb->queryMulti_(batch_tracks, [this, index, has_global_variables](size_t offset, const metadb_v2_rec_t& rec) {
metadb_handle_v2::ptr track;
track &= m_tracks[index + offset];

GlobalVariableList global_variables;
DateTitleformatHook tf_hook_date(&m_systemtime);
PlaylistNameTitleformatHook tf_hook_playlist_name;

if (has_global_variables) {
SetGlobalTitleformatHook<true, false> tf_hook_set_global(global_variables);
SplitterTitleformatHook tf_hook(&tf_hook_set_global, &tf_hook_date, &tf_hook_playlist_name);
pfc::string8 _;
track->formatTitle_v2(rec, &tf_hook, _, m_global_script, nullptr);
}

std::string title;
mmh::StringAdaptor adapted_title(title);

SetGlobalTitleformatHook<false, true> tf_hook_get_global(global_variables);
SplitterTitleformatHook tf_hook(
has_global_variables ? &tf_hook_get_global : nullptr, &tf_hook_date, &tf_hook_playlist_name);
track->formatTitle_v2(rec, &tf_hook, adapted_title, m_column_script, nullptr);
m_items[index + offset] = std::move(title);
});

return m_items[index]->c_str();
}

void PlaylistView::s_create_message_window()
{
uie::container_window_v3_config config(L"{columns_ui_playlist_view_message_window_goJRO8xwg7s}", false);
Expand Down
30 changes: 30 additions & 0 deletions foo_ui_columns/ng_playlist/ng_playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,27 @@ class PlaylistView
size_t get_group_count() { return m_groups.size(); }
};

class PlaylistViewSearchContext : public ListViewSearchContextBase {
public:
explicit PlaylistViewSearchContext(titleformat_object::ptr global_script, titleformat_object::ptr column_script)
: m_global_script(std::move(global_script))
, m_column_script(std::move(column_script))
{
}

const char* get_item_text(size_t index) override;

private:
SYSTEMTIME m_systemtime;
metadb_handle_list m_tracks;
std::vector<std::optional<std::string>> m_items;
std::optional<size_t> m_start_index;
playlist_manager_v4::ptr m_playlist_manager{playlist_manager_v4::get()};
metadb_v2::ptr m_metadb{metadb_v2::get()};
titleformat_object::ptr m_global_script{};
titleformat_object::ptr m_column_script{};
};

static void s_create_message_window();
static void s_destroy_message_window();

Expand Down Expand Up @@ -464,6 +485,15 @@ class PlaylistView

void notify_update_item_data(size_t index) override;

std::unique_ptr<ListViewSearchContextBase> create_search_context() override
{
if (!static_api_test_t<metadb_v2>() || m_column_data.size() == 0) {
return ListViewPanelBase::create_search_context();
}

return std::make_unique<PlaylistViewSearchContext>(m_script_global, m_column_data[0].m_display_script);
}

const style_data_t& get_style_data(size_t index);
void get_insert_items(size_t start, size_t count, InsertItemsContainer& items);
void flush_items();
Expand Down
2 changes: 1 addition & 1 deletion ui_helpers

0 comments on commit 9b630ce

Please sign in to comment.