Skip to content

Commit

Permalink
screen: click on progress bar seeks current song
Browse files Browse the repository at this point in the history
Closes #135
  • Loading branch information
MaxKellermann committed Sep 11, 2024
1 parent b302aad commit cec104c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ncmpc 0.50 - not yet released
* lyrics/google: fix partial loading of lyrics
* support MPD 0.22 tag "label" (requires libmpdclient 2.17)
* fix config file overriding MPD_HOST environment variable
* click on progress bar seeks current song

ncmpc 0.49 - (2023-08-04)
* fix UI freeze if lyrics plugin is stuck
Expand Down
9 changes: 9 additions & 0 deletions src/ProgressBar.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public:

bool Set(unsigned current, unsigned max) noexcept;

int ValueAtX(int x) const noexcept {
if (max == 0 || x < 0)
return -1;

const unsigned ux = static_cast<unsigned>(x);
const unsigned window_width = window.GetWidth();
return ux * max / window_width;
}

void Paint() const noexcept;

private:
Expand Down
37 changes: 37 additions & 0 deletions src/screen.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,33 @@ ScreenManager::OnCommand(struct mpdclient &c, DelayedSeek &seek, Command cmd)

#ifdef HAVE_GETMOUSE

inline void
ScreenManager::OnProgressBarMouse(struct mpdclient &c, DelayedSeek &seek,
int x, mmask_t bstate)
{
if (bstate & BUTTON1_CLICKED) {
if (!c.IsReady() || !c.playing_or_paused)
return;

const int time = progress_bar.ValueAtX(x);
if (time < 0)
return;

const int id = c.GetCurrentSongId();
if (id < 0)
return;

seek.Cancel();

struct mpd_connection *connection = c.GetConnection();
if (connection == nullptr)
return;

if (!mpd_run_seek_id(connection, id, time))
c.HandleError();
}
}

void
ScreenManager::OnMouse(struct mpdclient &c, DelayedSeek &seek,
Point p, mmask_t bstate)
Expand All @@ -318,6 +345,16 @@ ScreenManager::OnMouse(struct mpdclient &c, DelayedSeek &seek,
p.y -= title_height;
}

if (int main_height = main_window.GetSize().height; p.y >= main_height) {
p.y -= main_height;

if (p.y == 0) {
OnProgressBarMouse(c, seek, p.x, bstate);
}

return;
}

if (GetCurrentPage().OnMouse(c, p, bstate))
return;

Expand Down
2 changes: 2 additions & 0 deletions src/screen.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public:
void OnCommand(struct mpdclient &c, DelayedSeek &seek, Command cmd);

#ifdef HAVE_GETMOUSE
void OnProgressBarMouse(struct mpdclient &c, DelayedSeek &seek,
int x, mmask_t bstate);
void OnMouse(struct mpdclient &c, DelayedSeek &seek,
Point p, mmask_t bstate);
#endif
Expand Down

0 comments on commit cec104c

Please sign in to comment.