Skip to content

Commit

Permalink
page/Page: add method GetSelectedSong()
Browse files Browse the repository at this point in the history
Allows creating generic versions of some commands.
  • Loading branch information
MaxKellermann committed Sep 9, 2024
1 parent b8fd771 commit b01d1ee
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 164 deletions.
6 changes: 0 additions & 6 deletions src/FileBrowserPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,6 @@ FileBrowserPage::OnCommand(struct mpdclient &c, Command cmd)
ChangeToParent(c);
return true;

case Command::LOCATE:
/* don't let browser_cmd() evaluate the locate command
- it's a no-op, and by the way, leads to a
segmentation fault in the current implementation */
return false;

case Command::SCREEN_UPDATE:
Reload(c);
screen_browser_sync_highlights(*filelist, c.playlist);
Expand Down
33 changes: 0 additions & 33 deletions src/FileListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#include "config.h"
#include "FileListPage.hxx"
#include "FileBrowserPage.hxx"
#include "SongPage.hxx"
#include "EditPlaylistPage.hxx"
#include "LyricsPage.hxx"
#include "Command.hxx"
#include "screen_status.hxx"
#include "screen_find.hxx"
Expand Down Expand Up @@ -397,29 +395,6 @@ FileListPage::OnCommand(struct mpdclient &c, Command cmd)
SchedulePaint();
return true;

#ifdef ENABLE_SONG_SCREEN
case Command::SCREEN_SONG:
if (const auto *song = GetSelectedSong()) {
screen_song_switch(screen, c, *song);
return true;
} else
return false;

#endif

#ifdef ENABLE_LYRICS_SCREEN
case Command::SCREEN_LYRICS:
if (const auto *song = GetSelectedSong()) {
screen_lyrics_switch(screen, c, *song, false);
return true;
} else
return false;

#endif
case Command::SCREEN_SWAP:
screen.Swap(c, GetSelectedSong());
return true;

default:
break;
}
Expand Down Expand Up @@ -455,14 +430,6 @@ FileListPage::OnCommand(struct mpdclient &c, Command cmd)
HandleSelectAll(c);
return true;

case Command::LOCATE:
if (const auto *song = GetSelectedSong()) {
screen_file_goto_song(screen, c, *song);
return true;
} else
return false;


default:
break;
}
Expand Down
5 changes: 2 additions & 3 deletions src/FileListPage.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ protected:
[[gnu::pure]]
const struct mpd_entity *GetSelectedEntity() const noexcept;

[[gnu::pure]]
const struct mpd_song *GetSelectedSong() const noexcept;

[[gnu::pure]]
FileListEntry *GetIndex(unsigned i) const noexcept;

Expand Down Expand Up @@ -76,6 +73,8 @@ public:
bool OnMouse(struct mpdclient &c, Point p,
mmask_t bstate) override;
#endif

const struct mpd_song *GetSelectedSong() const noexcept override;
};

#ifndef NCMPC_MINI
Expand Down
27 changes: 4 additions & 23 deletions src/LyricsPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "LyricsLoader.hxx"
#include "PageMeta.hxx"
#include "screen_status.hxx"
#include "FileBrowserPage.hxx"
#include "SongPage.hxx"
#include "i18n.h"
#include "Command.hxx"
#include "Options.hxx"
Expand Down Expand Up @@ -105,6 +103,10 @@ class LyricsPage final : public TextPage, PluginResponseHandler {
bool OnCommand(struct mpdclient &c, Command cmd) override;
std::string_view GetTitle(std::span<char> buffer) const noexcept override;

const struct mpd_song *GetSelectedSong() const noexcept override {
return song;
}

private:
/* virtual methods from class PluginResponseHandler */
void OnPluginSuccess(const char *plugin_name,
Expand Down Expand Up @@ -417,27 +419,6 @@ LyricsPage::OnCommand(struct mpdclient &c, Command cmd)
Reload();
return true;

#ifdef ENABLE_SONG_SCREEN
case Command::SCREEN_SONG:
if (song != nullptr) {
screen_song_switch(screen, c, *song);
return true;
}

break;
#endif
case Command::SCREEN_SWAP:
screen.Swap(c, song);
return true;

case Command::LOCATE:
if (song != nullptr) {
screen_file_goto_song(screen, c, *song);
return true;
}

return false;

default:
break;
}
Expand Down
47 changes: 1 addition & 46 deletions src/QueuePage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "QueuePage.hxx"
#include "PageMeta.hxx"
#include "FileBrowserPage.hxx"
#include "screen_status.hxx"
#include "screen_find.hxx"
#include "save_playlist.hxx"
Expand All @@ -20,8 +19,6 @@
#include "time_format.hxx"
#include "screen.hxx"
#include "screen_utils.hxx"
#include "SongPage.hxx"
#include "LyricsPage.hxx"
#include "db_completion.hxx"
#include "page/ListPage.hxx"
#include "ui/ListRenderer.hxx"
Expand Down Expand Up @@ -85,9 +82,6 @@ class QueuePage final : public ListPage, ListRenderer, ListText {
}

private:
[[gnu::pure]]
const struct mpd_song *GetSelectedSong() const noexcept;

void SaveSelection() noexcept;
void RestoreSelection() noexcept;

Expand Down Expand Up @@ -135,6 +129,7 @@ class QueuePage final : public ListPage, ListRenderer, ListText {
#endif

std::string_view GetTitle(std::span<char> buffer) const noexcept override;
const struct mpd_song *GetSelectedSong() const noexcept override;
};

const struct mpd_song *
Expand Down Expand Up @@ -559,38 +554,6 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
SchedulePaint();
return true;

#ifdef ENABLE_SONG_SCREEN
case Command::SCREEN_SONG:
if (GetSelectedSong() != nullptr) {
screen_song_switch(screen, c, *GetSelectedSong());
return true;
}

break;
#endif

#ifdef ENABLE_LYRICS_SCREEN
case Command::SCREEN_LYRICS:
if (lw.GetCursorIndex() < playlist->size()) {
struct mpd_song &selected = (*playlist)[lw.GetCursorIndex()];
bool follow = false;

if (&selected == c.GetPlayingSong())
follow = true;

screen_lyrics_switch(screen, c, selected, follow);
return true;
}

break;
#endif
case Command::SCREEN_SWAP:
if (!playlist->empty())
screen.Swap(c, &(*playlist)[lw.GetCursorIndex()]);
else
screen.Swap(c, nullptr);
return true;

default:
break;
}
Expand Down Expand Up @@ -670,14 +633,6 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
return true;
}

case Command::LOCATE:
if (GetSelectedSong() != nullptr) {
screen_file_goto_song(screen, c, *GetSelectedSong());
return true;
}

break;

default:
break;
}
Expand Down
45 changes: 4 additions & 41 deletions src/SongPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "SongPage.hxx"
#include "PageMeta.hxx"
#include "FileBrowserPage.hxx"
#include "LyricsPage.hxx"
#include "screen_find.hxx"
#include "Command.hxx"
#include "i18n.h"
Expand Down Expand Up @@ -148,6 +146,10 @@ class SongPage final : public ListPage, ListText {
bool OnCommand(struct mpdclient &c, Command cmd) override;
std::string_view GetTitle(std::span<char> buffer) const noexcept override;

const struct mpd_song *GetSelectedSong() const noexcept override {
return selected_song != nullptr ? selected_song : played_song;
}

private:
/* virtual methods from class ListText */
std::string_view GetListItemText(std::span<char> buffer,
Expand Down Expand Up @@ -478,45 +480,6 @@ SongPage::OnCommand(struct mpdclient &c, Command cmd)
if (ListPage::OnCommand(c, cmd))
return true;

switch(cmd) {
case Command::LOCATE:
if (selected_song != nullptr) {
screen_file_goto_song(screen, c, *selected_song);
return true;
}
if (played_song != nullptr) {
screen_file_goto_song(screen, c, *played_song);
return true;
}

return false;

#ifdef ENABLE_LYRICS_SCREEN
case Command::SCREEN_LYRICS:
if (selected_song != nullptr) {
screen_lyrics_switch(screen, c, *selected_song, false);
return true;
}
if (played_song != nullptr) {
screen_lyrics_switch(screen, c, *played_song, true);
return true;
}
return false;

#endif

case Command::SCREEN_SWAP:
if (selected_song != nullptr)
screen.Swap(c, selected_song);
else
// No need to check if this is null - we'd pass null anyway
screen.Swap(c, played_song);
return true;

default:
break;
}

if (screen_find(screen, lw, cmd, *this)) {
SchedulePaint();
return true;
Expand Down
11 changes: 11 additions & 0 deletions src/page/Page.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <string_view>

enum class Command : unsigned;
struct mpd_song;
struct mpdclient;
struct Window;
class PageContainer;
Expand Down Expand Up @@ -106,4 +107,14 @@ public:

[[gnu::pure]]
virtual std::string_view GetTitle(std::span<char> buffer) const noexcept = 0;

/**
* Returns a pointer to the #mpd_song that is currently
* selected on this page. The pointed-to object is owned by
* this #Page instance and may be invalidated at any time.
*/
[[gnu::pure]]
virtual const struct mpd_song *GetSelectedSong() const noexcept {
return nullptr;
}
};
8 changes: 8 additions & 0 deletions src/page/ProxyPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ ProxyPage::GetTitle(std::span<char> buffer) const noexcept
: std::string_view{};
}

const struct mpd_song *
ProxyPage::GetSelectedSong() const noexcept
{
return current_page != nullptr
? current_page->GetSelectedSong()
: nullptr;
}

void
ProxyPage::SchedulePaint(Page &page) noexcept
{
Expand Down
1 change: 1 addition & 0 deletions src/page/ProxyPage.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public:
#endif

std::string_view GetTitle(std::span<char> buffer) const noexcept override;
const struct mpd_song *GetSelectedSong() const noexcept override;

// virtual methods from PageContainer
void SchedulePaint(Page &page) noexcept override;
Expand Down
Loading

0 comments on commit b01d1ee

Please sign in to comment.