ListWindow: make all attributes private
authorMax Kellermann <max@musicpd.org>
Wed, 10 Apr 2019 07:33:47 +0000 (09:33 +0200)
committerMax Kellermann <max@musicpd.org>
Wed, 10 Apr 2019 07:33:47 +0000 (09:33 +0200)
16 files changed:
src/FileListPage.cxx
src/HelpPage.cxx
src/LibraryPage.cxx
src/ListPage.hxx
src/ListWindow.hxx
src/LyricsPage.cxx
src/OutputsPage.cxx
src/QueuePage.cxx
src/SearchPage.cxx
src/SongPage.cxx
src/TagListPage.cxx
src/TagListPage.hxx
src/TextPage.cxx
src/TextPage.hxx
src/screen_find.cxx
src/screen_keydef.cxx

index 5658781..b5cbd37 100644 (file)
@@ -340,18 +340,18 @@ bool
 FileListPage::OnMouse(struct mpdclient &c, Point p,
                      mmask_t bstate)
 {
-       unsigned prev_selected = lw.selected;
+       unsigned prev_selected = lw.GetCursorIndex();
 
        if (ListPage::OnMouse(c, p, bstate))
                return true;
 
-       lw.SetCursor(lw.start + p.y);
+       lw.SetCursorFromOrigin(p.y);
 
        if( bstate & BUTTON1_CLICKED ) {
-               if (prev_selected == lw.selected)
+               if (prev_selected == lw.GetCursorIndex())
                        HandleEnter(c);
        } else if (bstate & BUTTON3_CLICKED) {
-               if (prev_selected == lw.selected)
+               if (prev_selected == lw.GetCursorIndex())
                        HandleSelect(c);
        }
 
index 75e97cb..4009da4 100644 (file)
@@ -223,7 +223,7 @@ class HelpPage final : public ListPage, ListRenderer, ListText {
 public:
        HelpPage(ScreenManager &_screen, WINDOW *w, Size size)
                :ListPage(w, size), screen(_screen) {
-               lw.hide_cursor = true;
+               lw.DisableCursor();
                lw.SetLength(ARRAY_SIZE(help_text));
        }
 
@@ -313,10 +313,10 @@ HelpPage::OnCommand(struct mpdclient &c, Command cmd)
        if (ListPage::OnCommand(c, cmd))
                return true;
 
-       lw.SetCursor(lw.start);
+       lw.SetCursorFromOrigin(0);
        if (screen_find(screen, lw, cmd, *this)) {
                /* center the row */
-               lw.Center(lw.selected);
+               lw.Center(lw.GetCursorIndex());
                SetDirty();
                return true;
        }
index 8efa7b1..cd47ed0 100644 (file)
@@ -207,7 +207,7 @@ SongListPage::OnCommand(struct mpdclient &c, Command cmd)
 {
        switch(cmd) {
        case Command::PLAY:
-               if (lw.selected == 0 && parent != nullptr)
+               if (lw.GetCursorIndex() == 0 && parent != nullptr)
                        /* handle ".." */
                        return parent->OnCommand(c, Command::GO_PARENT_DIRECTORY);
 
index 2eab255..5e02387 100644 (file)
@@ -41,9 +41,9 @@ public:
        }
 
        bool OnCommand(struct mpdclient &, Command cmd) override {
-               if (lw.hide_cursor
-                   ? lw.HandleScrollCommand(cmd)
-                   : lw.HandleCommand(cmd)) {
+               if (lw.HasCursor()
+                   ? lw.HandleCommand(cmd)
+                   : lw.HandleScrollCommand(cmd)) {
                        SetDirty();
                        return true;
                }
index 54d4986..063f081 100644 (file)
@@ -84,7 +84,6 @@ struct ListWindowRange {
 };
 
 class ListWindow {
-public:
        WINDOW *w;
        Size size;
 
@@ -108,9 +107,73 @@ public:
 
        bool hide_cursor = false;
 
+public:
        ListWindow(WINDOW *_w, Size _size) noexcept
                :w(_w), size(_size) {}
 
+       const Size &GetSize() const noexcept {
+               return size;
+       }
+
+       void Refresh() const noexcept {
+               wrefresh(w);
+       }
+
+       unsigned GetOrigin() const noexcept {
+               return start;
+       }
+
+       void SetOrigin(unsigned new_orign) noexcept {
+               start = new_orign;
+       }
+
+       void DisableCursor() {
+               hide_cursor = true;
+       }
+
+       void EnableCursor() {
+               hide_cursor = false;
+       }
+
+       bool HasCursor() const noexcept {
+               return !hide_cursor;
+       }
+
+       bool HasRangeSelection() const noexcept {
+               return range_selection;
+       }
+
+       /**
+        * Is the cursor currently pointing to a single valid item?
+        */
+       bool IsSingleCursor() const noexcept {
+               return !HasRangeSelection() && selected < length;
+       }
+
+       unsigned GetCursorIndex() const noexcept {
+               return selected;
+       }
+
+       void SelectionMovedUp() noexcept {
+               selected--;
+               range_base--;
+
+               EnsureSelectionVisible();
+       }
+
+       void SelectionMovedDown() noexcept {
+               selected++;
+               range_base++;
+
+               EnsureSelectionVisible();
+       }
+
+       void EnsureSelectionVisible() noexcept {
+               if (range_selection)
+                       ScrollTo(range_base);
+               ScrollTo(selected);
+       }
+
        /** reset a list window (selected=0, start=0) */
        void Reset() noexcept;
 
@@ -153,6 +216,10 @@ public:
         */
        void SetCursor(unsigned i) noexcept;
 
+       void SetCursorFromOrigin(unsigned i) noexcept {
+               SetCursor(GetOrigin() + i);
+       }
+
        /**
         * Moves the cursor.  Modifies the range if range selection is
         * enabled.
index b3ae87e..0d14d78 100644 (file)
@@ -208,12 +208,12 @@ void
 LyricsPage::Set(const char *s)
 {
        if (reloading) {
-               unsigned saved_start = lw.start;
+               unsigned saved_start = lw.GetOrigin();
 
                TextPage::Set(s);
 
                /* restore the cursor and ensure that it's still valid */
-               lw.start = saved_start;
+               lw.SetOrigin(saved_start);
                lw.FetchCursor();
        } else {
                TextPage::Set(s);
index 01ab20f..3b5581b 100644 (file)
@@ -184,7 +184,7 @@ OutputsPage::OnCommand(struct mpdclient &c, Command cmd)
 
        switch (cmd) {
        case Command::PLAY:
-               Toggle(c, lw.selected);
+               Toggle(c, lw.GetCursorIndex());
                return true;
 
        case Command::SCREEN_UPDATE:
index f9cf1b3..6c698f1 100644 (file)
@@ -97,7 +97,7 @@ private:
 
        void Repaint() const {
                Paint();
-               wrefresh(lw.w);
+               lw.Refresh();
        }
 
        void CenterPlayingItem(const struct mpd_status *status,
@@ -144,9 +144,8 @@ public:
 const struct mpd_song *
 QueuePage::GetSelectedSong() const
 {
-       return !lw.range_selection &&
-               lw.selected < playlist->size()
-               ? &(*playlist)[lw.selected]
+       return lw.IsSingleCursor()
+               ? &(*playlist)[lw.GetCursorIndex()]
                : nullptr;
 }
 
@@ -237,7 +236,7 @@ QueuePage::OnSongChange(const struct mpd_status *status)
        current_song_id = get_current_song_id(status);
 
        /* center the cursor */
-       if (options.auto_center && !lw.range_selection)
+       if (options.auto_center && !lw.HasRangeSelection())
                CenterPlayingItem(status, false);
 
        return true;
@@ -339,7 +338,7 @@ QueuePage::OnHideCursorTimer(const boost::system::error_code &error) noexcept
        /* hide the cursor when mpd is playing and the user is inactive */
 
        if (playing) {
-               lw.hide_cursor = true;
+               lw.DisableCursor();
                Repaint();
        } else
                ScheduleHideCursor();
@@ -351,7 +350,7 @@ QueuePage::OnOpen(struct mpdclient &c) noexcept
        playlist = &c.playlist;
 
        if (options.hide_cursor > std::chrono::steady_clock::duration::zero()) {
-               lw.hide_cursor = false;
+               lw.EnableCursor();
                ScheduleHideCursor();
        }
 
@@ -390,7 +389,7 @@ QueuePage::PaintListItem(WINDOW *w, unsigned i, unsigned y, unsigned width,
 
        class hscroll *row_hscroll = nullptr;
 #ifndef NCMPC_MINI
-       row_hscroll = selected && options.scroll && lw.selected == i
+       row_hscroll = selected && options.scroll && lw.GetCursorIndex() == i
                ? &hscroll : nullptr;
 #endif
 
@@ -451,8 +450,8 @@ QueuePage::OnMouse(struct mpdclient &c, Point p, mmask_t bstate)
                return true;
        }
 
-       const unsigned old_selected = lw.selected;
-       lw.SetCursor(lw.start + p.y);
+       const unsigned old_selected = lw.GetCursorIndex();
+       lw.SetCursorFromOrigin(p.y);
 
        if (bstate & BUTTON1_CLICKED) {
                /* play */
@@ -466,8 +465,8 @@ QueuePage::OnMouse(struct mpdclient &c, Point p, mmask_t bstate)
                }
        } else if (bstate & BUTTON3_CLICKED) {
                /* delete */
-               if (lw.selected == old_selected)
-                       c.RunDelete(lw.selected);
+               if (lw.GetCursorIndex() == old_selected)
+                       c.RunDelete(lw.GetCursorIndex());
 
                lw.SetLength(playlist->size());
        }
@@ -488,7 +487,7 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
        const Command prev_cmd = cached_cmd;
        cached_cmd = cmd;
 
-       lw.hide_cursor = false;
+       lw.EnableCursor();
 
        if (options.hide_cursor > std::chrono::steady_clock::duration::zero()) {
                ScheduleHideCursor();
@@ -542,8 +541,8 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
 
 #ifdef ENABLE_LYRICS_SCREEN
        case Command::SCREEN_LYRICS:
-               if (lw.selected < playlist->size()) {
-                       struct mpd_song &selected = (*playlist)[lw.selected];
+               if (lw.GetCursorIndex() < playlist->size()) {
+                       struct mpd_song &selected = (*playlist)[lw.GetCursorIndex()];
                        bool follow = false;
 
                        if (&selected == c.GetPlayingSong())
@@ -557,7 +556,7 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
 #endif
        case Command::SCREEN_SWAP:
                if (!playlist->empty())
-                       screen.Swap(c, &(*playlist)[lw.selected]);
+                       screen.Swap(c, &(*playlist)[lw.GetCursorIndex()]);
                else
                        screen.Swap(c, nullptr);
                return true;
@@ -625,13 +624,7 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
                if (!c.RunMove(range.end_index - 1, range.start_index - 1))
                        return true;
 
-               lw.selected--;
-               lw.range_base--;
-
-               if (lw.range_selection)
-                       lw.ScrollTo(lw.range_base);
-               lw.ScrollTo(lw.selected);
-
+               lw.SelectionMovedUp();
                SaveSelection();
                return true;
 
@@ -643,13 +636,7 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
                if (!c.RunMove(range.start_index, range.end_index))
                        return true;
 
-               lw.selected++;
-               lw.range_base++;
-
-               if (lw.range_selection)
-                       lw.ScrollTo(lw.range_base);
-               lw.ScrollTo(lw.selected);
-
+               lw.SelectionMovedDown();
                SaveSelection();
                return true;
 
index 61116a3..5d4c010 100644 (file)
@@ -114,8 +114,8 @@ public:
                              !options.search_format.empty()
                              ? options.search_format.c_str()
                              : options.list_format.c_str()) {
+               lw.DisableCursor();
                lw.SetLength(ARRAY_SIZE(help_text));
-               lw.hide_cursor = true;
        }
 
 private:
@@ -344,7 +344,7 @@ SearchPage::Reload(struct mpdclient &c)
        if (pattern.empty())
                return;
 
-       lw.hide_cursor = false;
+       lw.EnableCursor();
        delete filelist;
        filelist = do_search(&c, pattern.c_str());
        if (filelist == nullptr)
index c02f18e..6d4141b 100644 (file)
@@ -115,7 +115,7 @@ public:
        SongPage(ScreenManager &_screen, WINDOW *w, Size size) noexcept
                :ListPage(w, size),
                 screen(_screen) {
-               lw.hide_cursor = true;
+               lw.DisableCursor();
        }
 
        ~SongPage() noexcept override {
@@ -223,7 +223,7 @@ SongPage::AppendLine(const char *label, const char *value_utf8,
 
        /* +2 for ': ' */
        label_col += 2;
-       const int value_col = lw.size.width - label_col;
+       const int value_col = lw.GetSize().width - label_col;
        /* calculate the number of required linebreaks */
        const Utf8ToLocale value_locale(value_utf8);
        const char *value = value_locale.c_str();
@@ -542,7 +542,7 @@ SongPage::OnCommand(struct mpdclient &c, Command cmd)
 
        if (screen_find(screen, lw, cmd, *this)) {
                /* center the row */
-               lw.Center(lw.selected);
+               lw.Center(lw.GetCursorIndex());
                SetDirty();
                return true;
        }
index e197bae..5e19a06 100644 (file)
@@ -35,7 +35,7 @@
 TagFilter
 TagListPage::MakeCursorFilter() const noexcept
 {
-       unsigned i = lw.selected;
+       unsigned i = lw.GetCursorIndex();
        if (parent != nullptr) {
                if (i == 0)
                        return {};
@@ -200,7 +200,7 @@ TagListPage::OnCommand(struct mpdclient &c, Command cmd)
 {
        switch(cmd) {
        case Command::PLAY:
-               if (lw.selected == 0 && parent != nullptr)
+               if (lw.GetCursorIndex() == 0 && parent != nullptr)
                        /* handle ".." */
                        return parent->OnCommand(c, Command::GO_PARENT_DIRECTORY);
 
index 74360d0..258d581 100644 (file)
@@ -76,7 +76,7 @@ public:
 
        gcc_pure
        const char *GetSelectedValue() const {
-               unsigned i = lw.selected;
+               unsigned i = lw.GetCursorIndex();
 
                if (parent != nullptr) {
                        if (i == 0)
index d6383a0..ef3aac4 100644 (file)
@@ -89,10 +89,10 @@ TextPage::OnCommand(struct mpdclient &c, Command cmd)
        if (ListPage::OnCommand(c, cmd))
                return true;
 
-       lw.SetCursor(lw.start);
+       lw.SetCursorFromOrigin(0);
        if (screen_find(screen, lw, cmd, *this)) {
                /* center the row */
-               lw.Center(lw.selected);
+               lw.Center(lw.GetCursorIndex());
                SetDirty();
                return true;
        }
index 5e2de38..b346e4a 100644 (file)
@@ -42,7 +42,7 @@ public:
        TextPage(ScreenManager &_screen,
                 WINDOW *w, Size size) noexcept
                :ListPage(w, size), screen(_screen) {
-               lw.hide_cursor = true;
+               lw.DisableCursor();
        }
 
 protected:
@@ -70,7 +70,7 @@ protected:
         */
        void Repaint() noexcept {
                Paint();
-               wrefresh(lw.w);
+               lw.Refresh();
        }
 
 public:
index 4f8b739..e91a01d 100644 (file)
@@ -127,7 +127,7 @@ screen_jump(ScreenManager &screen, ListWindow &lw,
 
                /* repaint the list_window */
                lw.Paint(renderer);
-               wrefresh(lw.w);
+               lw.Refresh();
        }
 
        screen.findbuf = search_str;
index eb13a4f..e9cdf1a 100644 (file)
@@ -95,8 +95,8 @@ private:
        }
 
        /**
-        * Convert an item id (as in lw.selected) into a "key id", which is an array
-        * subscript to cmds[subcmd].keys.
+        * Convert an item id (as in lw.GetCursorIndex()) into a "key
+        * id", which is an array subscript to cmds[subcmd].keys.
         */
        static constexpr unsigned subcmd_item_to_key_id(unsigned i) {
                return i - 1;
@@ -272,20 +272,20 @@ CommandKeysPage::OnCommand(struct mpdclient &c, Command cmd)
 
        switch(cmd) {
        case Command::PLAY:
-               if (lw.selected == subcmd_item_up()) {
+               if (lw.GetCursorIndex() == subcmd_item_up()) {
                        if (parent != nullptr)
                                return parent->OnCommand(c, Command::GO_PARENT_DIRECTORY);
-               } else if (lw.selected == subcmd_item_add()) {
+               } else if (lw.GetCursorIndex() == subcmd_item_add()) {
                        AddKey();
                } else {
                        /* just to be sure ;-) */
-                       assert(subcmd_item_is_key(lw.selected));
-                       OverwriteKey(subcmd_item_to_key_id(lw.selected));
+                       assert(subcmd_item_is_key(lw.GetCursorIndex()));
+                       OverwriteKey(subcmd_item_to_key_id(lw.GetCursorIndex()));
                }
                return true;
        case Command::DELETE:
-               if (subcmd_item_is_key(lw.selected))
-                       DeleteKey(subcmd_item_to_key_id(lw.selected));
+               if (subcmd_item_is_key(lw.GetCursorIndex()))
+                       DeleteKey(subcmd_item_to_key_id(lw.GetCursorIndex()));
 
                return true;
        case Command::ADD:
@@ -329,8 +329,8 @@ public:
        }
 
        int GetSelectedCommand() const {
-               return lw.selected < command_n_commands
-                       ? (int)lw.selected
+               return lw.GetCursorIndex() < command_n_commands
+                       ? (int)lw.GetCursorIndex()
                        : -1;
        }
 
@@ -493,10 +493,10 @@ CommandListPage::OnCommand(struct mpdclient &c, Command cmd)
 
        switch(cmd) {
        case Command::PLAY:
-               if (lw.selected == command_item_apply()) {
+               if (lw.GetCursorIndex() == command_item_apply()) {
                        Apply();
                        return true;
-               } else if (lw.selected == command_item_save()) {
+               } else if (lw.GetCursorIndex() == command_item_save()) {
                        Apply();
                        Save();
                        return true;