Skip to content

Commit

Permalink
screen: click on title bar switches to page
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 12, 2024
1 parent cbac948 commit cb49e95
Show file tree
Hide file tree
Showing 5 changed files with 64 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 title bar switches to page
* click on progress bar seeks current song
* fix tab completion bug

Expand Down
40 changes: 40 additions & 0 deletions src/TabBar.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "GlobalBindings.hxx"
#include "i18n.h"
#include "ui/Window.hxx"
#include "util/LocaleString.hxx"

static void
PaintPageTab(const Window window, Command cmd, std::string_view label, bool selected) noexcept
Expand Down Expand Up @@ -55,3 +56,42 @@ PaintTabBar(const Window window, const PageMeta &current_page_meta,
page == &current_page_meta);
}
}

static unsigned
GetTabWidth(Command cmd, std::string_view label) noexcept
{
unsigned width = 3 + StringWidthMB(label);

const char *key = GetGlobalKeyBindings().GetFirstKeyName(cmd);
if (key != nullptr)
width += StringWidthMB(key);

return width;
}

const PageMeta *
GetTabAtX(const PageMeta &current_page_meta,
std::string_view current_page_title,
unsigned x) noexcept
{
for (unsigned i = 0;; ++i) {
const auto *page = GetPageMeta(i);
if (page == nullptr)
break;

std::string_view title{};
if (page == &current_page_meta)
title = current_page_title;

if (title.data() == nullptr)
title = my_gettext(page->title);

const unsigned tab_width = GetTabWidth(page->command, title);
if (x < tab_width)
return page;

x -= tab_width;
}

return nullptr;
}
6 changes: 6 additions & 0 deletions src/TabBar.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ struct PageMeta;
void
PaintTabBar(Window window, const PageMeta &current_page_meta,
std::string_view current_page_title) noexcept;

[[gnu::pure]]
const PageMeta *
GetTabAtX(const PageMeta &current_page_meta,
std::string_view current_page_title,
unsigned x) noexcept;
16 changes: 16 additions & 0 deletions src/screen.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include "util/ScopeExit.hxx"
#include "util/StringAPI.hxx"

#ifdef HAVE_GETMOUSE
#include "TabBar.hxx"
#endif

#include <mpd/client.h>

ScreenManager::PageMap::iterator
Expand Down Expand Up @@ -305,6 +309,17 @@ ScreenManager::OnCommand(struct mpdclient &c, DelayedSeek &seek, Command cmd)

#ifdef HAVE_GETMOUSE

inline void
ScreenManager::OnTitleBarMouse(struct mpdclient &c, int x, mmask_t bstate)
{
if (options.welcome_screen_list && (bstate & BUTTON1_CLICKED)) {
const auto title = GetCurrentPage().GetTitle({buf, buf_size});
const auto *page = GetTabAtX(GetCurrentPageMeta(), title, x);
if (page != nullptr)
Switch(*page, c);
}
}

inline void
ScreenManager::OnProgressBarMouse(struct mpdclient &c, DelayedSeek &seek,
int x, mmask_t bstate)
Expand Down Expand Up @@ -339,6 +354,7 @@ ScreenManager::OnMouse(struct mpdclient &c, DelayedSeek &seek,
if (options.show_title_bar) {
const int title_height = title_bar.GetHeight();
if (p.y < title_height) {
OnTitleBarMouse(c, p.x, bstate);
return;
}

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

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

0 comments on commit cb49e95

Please sign in to comment.