Skip to content

Commit

Permalink
ListCursor: add flag "highlight_cursor"
Browse files Browse the repository at this point in the history
On text-only pages without a cursor, don't center the line which was
found; instead, show the cursor temporarily.
  • Loading branch information
MaxKellermann committed Feb 1, 2021
1 parent 666485b commit 2bbc11d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ncmpc 0.44 - not yet released
* fix scrolling problems on help page
* highlight find results on help page
* don't attempt IPv6 connection if IPv6 routing is disabled

ncmpc 0.43 - (2021-01-27)
Expand Down
2 changes: 0 additions & 2 deletions src/HelpPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,6 @@ HelpPage::OnCommand(struct mpdclient &c, Command cmd)

lw.SetCursorFromOrigin(0);
if (screen_find(screen, lw, cmd, *this)) {
/* center the row */
lw.Center(lw.GetCursorIndex());
SetDirty();
return true;
}
Expand Down
22 changes: 22 additions & 0 deletions src/ListCursor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ListCursor::Reset() noexcept
{
selected = 0;
range_selection = false;
highlight_cursor = false;
range_base = 0;
start = 0;
}
Expand Down Expand Up @@ -78,6 +79,8 @@ ListCursor::SetLength(unsigned _length) noexcept
void
ListCursor::Center(unsigned n) noexcept
{
highlight_cursor = false;

if (n > GetHeight() / 2)
start = n - GetHeight() / 2;
else
Expand All @@ -94,6 +97,8 @@ ListCursor::Center(unsigned n) noexcept
void
ListCursor::ScrollTo(unsigned n) noexcept
{
highlight_cursor = false;

int new_start = start;

if (n < start + scroll_offset)
Expand Down Expand Up @@ -125,6 +130,7 @@ void
ListCursor::MoveCursor(unsigned n) noexcept
{
selected = n;
highlight_cursor = false;

CheckSelected();
CheckOrigin();
Expand Down Expand Up @@ -226,6 +232,8 @@ ListCursor::MoveCursorLast() noexcept
void
ListCursor::MoveCursorNextPage() noexcept
{
highlight_cursor = false;

if (GetHeight() < 2)
return;
if (selected + GetHeight() < length)
Expand All @@ -237,6 +245,8 @@ ListCursor::MoveCursorNextPage() noexcept
void
ListCursor::MoveCursorPreviousPage() noexcept
{
highlight_cursor = false;

if (GetHeight() < 2)
return;
if (selected > GetHeight() - 1)
Expand All @@ -248,6 +258,8 @@ ListCursor::MoveCursorPreviousPage() noexcept
void
ListCursor::ScrollUp(unsigned n) noexcept
{
highlight_cursor = false;

if (start > 0) {
if (n > start)
start = 0;
Expand All @@ -261,6 +273,8 @@ ListCursor::ScrollUp(unsigned n) noexcept
void
ListCursor::ScrollDown(unsigned n) noexcept
{
highlight_cursor = false;

if (start + GetHeight() < length) {
if (start + GetHeight() + n > length - 1)
start = length - GetHeight();
Expand All @@ -274,6 +288,8 @@ ListCursor::ScrollDown(unsigned n) noexcept
void
ListCursor::ScrollNextPage() noexcept
{
highlight_cursor = false;

start += GetHeight();
if (start + GetHeight() > length)
start = length > GetHeight()
Expand All @@ -284,6 +300,8 @@ ListCursor::ScrollNextPage() noexcept
void
ListCursor::ScrollPreviousPage() noexcept
{
highlight_cursor = false;

start = start > GetHeight()
? start - GetHeight()
: 0;
Expand All @@ -292,6 +310,8 @@ ListCursor::ScrollPreviousPage() noexcept
void
ListCursor::ScrollNextHalfPage() noexcept
{
highlight_cursor = false;

start += (GetHeight() - 1) / 2;
if (start + GetHeight() > length) {
start = length > GetHeight()
Expand All @@ -303,6 +323,8 @@ ListCursor::ScrollNextHalfPage() noexcept
void
ListCursor::ScrollPreviousHalfPage() noexcept
{
highlight_cursor = false;

start = start > (GetHeight() - 1) / 2
? start - (GetHeight() - 1) / 2
: 0;
Expand Down
18 changes: 18 additions & 0 deletions src/ListCursor.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ class ListCursor {

bool show_cursor = true;

/**
* @see HighlightCursor()
*/
bool highlight_cursor = false;

public:
explicit ListCursor(unsigned _height) noexcept;

Expand Down Expand Up @@ -131,10 +136,23 @@ public:
show_cursor = true;
}

/**
* Make the cursor visible temporarily (until it is moved)?
* This is useful for highlighting a matching line after
* searching in a (cursorless) text page.
*/
void HighlightCursor() noexcept {
highlight_cursor = true;
}

constexpr bool HasCursor() const noexcept {
return show_cursor;
}

constexpr bool IsCursorVisible() const noexcept {
return show_cursor || highlight_cursor;
}

constexpr bool HasRangeSelection() const noexcept {
return range_selection;
}
Expand Down
5 changes: 4 additions & 1 deletion src/ListWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
void
ListWindow::Paint(const ListRenderer &renderer) const noexcept
{
bool cursor_visible = HasCursor() &&
bool cursor_visible = IsCursorVisible() &&
(!options.hardware_cursor || HasRangeSelection());
ListWindowRange range;

Expand Down Expand Up @@ -87,6 +87,7 @@ ListWindow::Find(const ListText &text,

if (m(label)) {
MoveCursor(i);
HighlightCursor();
return true;
}
if (wrap && i == GetCursorIndex())
Expand Down Expand Up @@ -133,6 +134,7 @@ ListWindow::ReverseFind(const ListText &text,

if (m(label)) {
MoveCursor(i);
HighlightCursor();
return true;
}
if (wrap && i == (int)GetCursorIndex())
Expand Down Expand Up @@ -168,6 +170,7 @@ ListWindow::Jump(const ListText &text, const char *str) noexcept

if (m(label)) {
MoveCursor(i);
HighlightCursor();
return true;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/SongPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,6 @@ SongPage::OnCommand(struct mpdclient &c, Command cmd)
}

if (screen_find(screen, lw, cmd, *this)) {
/* center the row */
lw.Center(lw.GetCursorIndex());
SetDirty();
return true;
}
Expand Down
2 changes: 0 additions & 2 deletions src/TextPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ TextPage::OnCommand(struct mpdclient &c, Command cmd)

lw.SetCursorFromOrigin(0);
if (screen_find(screen, lw, cmd, *this)) {
/* center the row */
lw.Center(lw.GetCursorIndex());
SetDirty();
return true;
}
Expand Down

0 comments on commit 2bbc11d

Please sign in to comment.