From 22a3edb4a331f284d768d5070552b417e0b17d27 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 10 Sep 2024 00:03:42 +0200 Subject: [PATCH] screen_find: convert to class --- meson.build | 1 - src/ChatPage.cxx | 6 ++- src/FileListPage.cxx | 5 +-- src/HelpPage.cxx | 3 +- src/KeyDefPage.cxx | 5 +-- src/LibraryPage.cxx | 2 +- src/LyricsPage.cxx | 12 ++++-- src/QueuePage.cxx | 5 +-- src/SongPage.cxx | 3 +- src/TagListPage.cxx | 6 +-- src/TagListPage.hxx | 10 +++-- src/{screen_find.cxx => page/FindSupport.cxx} | 39 +++++++++--------- src/page/FindSupport.hxx | 40 +++++++++++++++++++ src/page/TextPage.cxx | 10 ++--- src/page/TextPage.hxx | 10 ++--- src/page/meson.build | 1 + src/screen.hxx | 11 ++--- src/screen_find.hxx | 33 --------------- 18 files changed, 103 insertions(+), 99 deletions(-) rename src/{screen_find.cxx => page/FindSupport.cxx} (70%) create mode 100644 src/page/FindSupport.hxx delete mode 100644 src/screen_find.hxx diff --git a/meson.build b/meson.build index c4de7cef..e230d904 100644 --- a/meson.build +++ b/meson.build @@ -410,7 +410,6 @@ ncmpc = executable('ncmpc', 'src/screen_utils.cxx', 'src/screen_status.cxx', 'src/screen_list.cxx', - 'src/screen_find.cxx', 'src/screen_client.cxx', 'src/QueuePage.cxx', 'src/FileListPage.cxx', diff --git a/src/ChatPage.cxx b/src/ChatPage.cxx index 5a85b45f..f8bdba33 100644 --- a/src/ChatPage.cxx +++ b/src/ChatPage.cxx @@ -3,6 +3,7 @@ #include "ChatPage.hxx" #include "PageMeta.hxx" +#include "screen.hxx" #include "screen_utils.hxx" #include "screen_status.hxx" #include "mpdclient.hxx" @@ -25,6 +26,8 @@ using std::string_view_literals::operator""sv; static constexpr char chat_channel[] = "chat"; class ChatPage final : public TextPage { + ScreenManager &screen; + unsigned last_connection_id = 0; bool was_supported = false; @@ -32,7 +35,8 @@ class ChatPage final : public TextPage { public: ChatPage(ScreenManager &_screen, const Window _window, Size size) - :TextPage(_screen, _window, size) {} + :TextPage(_screen, _window, size, _screen.find_support), + screen(_screen) {} private: bool CheckChatSupport(struct mpdclient &c); diff --git a/src/FileListPage.cxx b/src/FileListPage.cxx index c8966994..16959f54 100644 --- a/src/FileListPage.cxx +++ b/src/FileListPage.cxx @@ -7,7 +7,6 @@ #include "EditPlaylistPage.hxx" #include "Command.hxx" #include "screen_status.hxx" -#include "screen_find.hxx" #include "screen.hxx" #include "i18n.h" #include "Options.hxx" @@ -387,11 +386,11 @@ FileListPage::OnCommand(struct mpdclient &c, Command cmd) case Command::LIST_RFIND: case Command::LIST_FIND_NEXT: case Command::LIST_RFIND_NEXT: - screen_find(screen, lw, cmd, *this); + screen.find_support.Find(lw, *this, cmd); SchedulePaint(); return true; case Command::LIST_JUMP: - screen_jump(screen, lw, *this, *this); + screen.find_support.Jump(lw, *this, *this); SchedulePaint(); return true; diff --git a/src/HelpPage.cxx b/src/HelpPage.cxx index 19c45abe..436e22dc 100644 --- a/src/HelpPage.cxx +++ b/src/HelpPage.cxx @@ -3,7 +3,6 @@ #include "HelpPage.hxx" #include "PageMeta.hxx" -#include "screen_find.hxx" #include "Bindings.hxx" #include "GlobalBindings.hxx" #include "config.h" @@ -310,7 +309,7 @@ HelpPage::OnCommand(struct mpdclient &c, Command cmd) search */ lw.SetCursorFromOrigin(0); - if (screen_find(screen, lw, cmd, *this)) { + if (screen.find_support.Find(lw, *this, cmd)) { SchedulePaint(); return true; } diff --git a/src/KeyDefPage.cxx b/src/KeyDefPage.cxx index 9864425f..dff52d62 100644 --- a/src/KeyDefPage.cxx +++ b/src/KeyDefPage.cxx @@ -5,7 +5,6 @@ #include "PageMeta.hxx" #include "screen.hxx" #include "screen_status.hxx" -#include "screen_find.hxx" #include "KeyName.hxx" #include "i18n.h" #include "ConfigFile.hxx" @@ -277,7 +276,7 @@ CommandKeysPage::OnCommand(struct mpdclient &c, Command cmd) case Command::LIST_RFIND: case Command::LIST_FIND_NEXT: case Command::LIST_RFIND_NEXT: - screen_find(screen, lw, cmd, *this); + screen.find_support.Find(lw, *this, cmd); SchedulePaint(); return true; @@ -485,7 +484,7 @@ CommandListPage::OnCommand(struct mpdclient &c, Command cmd) case Command::LIST_RFIND: case Command::LIST_FIND_NEXT: case Command::LIST_RFIND_NEXT: - screen_find(screen, lw, cmd, *this); + screen.find_support.Find(lw, *this, cmd); SchedulePaint(); return true; diff --git a/src/LibraryPage.cxx b/src/LibraryPage.cxx index 209787f9..ddda03d6 100644 --- a/src/LibraryPage.cxx +++ b/src/LibraryPage.cxx @@ -106,7 +106,7 @@ class LibraryTagListPage final : public TagListPage { const enum mpd_tag_type _tag, const char *_all_text, const Window _window, Size size) noexcept - :TagListPage(_container, _screen, _parent, _tag, _all_text, _window, size), + :TagListPage(_container, _screen.find_support, _parent, _tag, _all_text, _window, size), library_page(_library_page) {} protected: diff --git a/src/LyricsPage.cxx b/src/LyricsPage.cxx index 65f1a42b..39b2acf4 100644 --- a/src/LyricsPage.cxx +++ b/src/LyricsPage.cxx @@ -52,9 +52,11 @@ class LyricsPage final : public TextPage, PluginResponseHandler { CoarseTimerEvent plugin_timeout; public: - LyricsPage(ScreenManager &_screen, const Window _window, Size size) noexcept - :TextPage(_screen, _window, size), - plugin_timeout(_screen.GetEventLoop(), + LyricsPage(EventLoop &event_loop, + PageContainer &_parent, const Window _window, Size size, + FindSupport &_find_support) noexcept + :TextPage(_parent, _window, size, _find_support), + plugin_timeout(event_loop, BIND_THIS_METHOD(OnTimeout)) {} ~LyricsPage() noexcept override { @@ -272,7 +274,9 @@ LyricsPage::Reload() noexcept static std::unique_ptr lyrics_screen_init(ScreenManager &_screen, const Window window, Size size) noexcept { - return std::make_unique(_screen, window, size); + return std::make_unique(_screen.GetEventLoop(), + _screen, window, size, + _screen.find_support); } void diff --git a/src/QueuePage.cxx b/src/QueuePage.cxx index 3894b251..917f9071 100644 --- a/src/QueuePage.cxx +++ b/src/QueuePage.cxx @@ -4,7 +4,6 @@ #include "QueuePage.hxx" #include "PageMeta.hxx" #include "screen_status.hxx" -#include "screen_find.hxx" #include "save_playlist.hxx" #include "config.h" #include "i18n.h" @@ -544,12 +543,12 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd) case Command::LIST_RFIND: case Command::LIST_FIND_NEXT: case Command::LIST_RFIND_NEXT: - screen_find(screen, lw, cmd, *this); + screen.find_support.Find(lw, *this, cmd); SaveSelection(); SchedulePaint(); return true; case Command::LIST_JUMP: - screen_jump(screen, lw, *this, *this); + screen.find_support.Jump(lw, *this, *this); SaveSelection(); SchedulePaint(); return true; diff --git a/src/SongPage.cxx b/src/SongPage.cxx index 300f5c41..dc9cab6c 100644 --- a/src/SongPage.cxx +++ b/src/SongPage.cxx @@ -3,7 +3,6 @@ #include "SongPage.hxx" #include "PageMeta.hxx" -#include "screen_find.hxx" #include "Command.hxx" #include "i18n.h" #include "screen.hxx" @@ -480,7 +479,7 @@ SongPage::OnCommand(struct mpdclient &c, Command cmd) if (ListPage::OnCommand(c, cmd)) return true; - if (screen_find(screen, lw, cmd, *this)) { + if (screen.find_support.Find(lw, *this, cmd)) { SchedulePaint(); return true; } diff --git a/src/TagListPage.cxx b/src/TagListPage.cxx index 90630793..fd9a80c2 100644 --- a/src/TagListPage.cxx +++ b/src/TagListPage.cxx @@ -3,12 +3,12 @@ #include "TagListPage.hxx" #include "screen_status.hxx" -#include "screen_find.hxx" #include "FileListPage.hxx" #include "Command.hxx" #include "i18n.h" #include "charset.hxx" #include "mpdclient.hxx" +#include "page/FindSupport.hxx" #include "util/StringUTF8.hxx" #include @@ -240,12 +240,12 @@ TagListPage::OnCommand(struct mpdclient &c, Command cmd) case Command::LIST_RFIND: case Command::LIST_FIND_NEXT: case Command::LIST_RFIND_NEXT: - screen_find(screen, lw, cmd, *this); + find_support.Find(lw, *this, cmd); SchedulePaint(); return true; case Command::LIST_JUMP: - screen_jump(screen, lw, *this, *this); + find_support.Jump(lw, *this, *this); SchedulePaint(); return true; diff --git a/src/TagListPage.hxx b/src/TagListPage.hxx index 8e6fd161..3438029f 100644 --- a/src/TagListPage.hxx +++ b/src/TagListPage.hxx @@ -11,10 +11,10 @@ #include #include -class ScreenManager; +class FindSupport; class TagListPage : public ListPage, ListRenderer, ListText { - ScreenManager &screen; + FindSupport &find_support; Page *const parent; const enum mpd_tag_type tag; @@ -26,11 +26,13 @@ class TagListPage : public ListPage, ListRenderer, ListText { std::vector values; public: - TagListPage(PageContainer &_container, ScreenManager &_screen, Page *_parent, + TagListPage(PageContainer &_container, + FindSupport &_find_support, Page *_parent, const enum mpd_tag_type _tag, const char *_all_text, Window _window, Size size) noexcept - :ListPage(_container, _window, size), screen(_screen), parent(_parent), + :ListPage(_container, _window, size), + find_support(_find_support), parent(_parent), tag(_tag), all_text(_all_text) {} auto GetTag() const noexcept { diff --git a/src/screen_find.cxx b/src/page/FindSupport.cxx similarity index 70% rename from src/screen_find.cxx rename to src/page/FindSupport.cxx index 9b396a5a..4521f73b 100644 --- a/src/screen_find.cxx +++ b/src/page/FindSupport.cxx @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#include "screen_find.hxx" +#include "FindSupport.hxx" #include "screen_utils.hxx" #include "screen_status.hxx" #include "screen.hxx" @@ -19,10 +19,8 @@ #define RFIND_PROMPT _("Find backward") #define JUMP_PROMPT _("Jump") -/* query user for a string and find it in a list window */ bool -screen_find(ScreenManager &screen, ListWindow &lw, Command findcmd, - const ListText &text) noexcept +FindSupport::Find(ListWindow &lw, const ListText &text, Command findcmd) noexcept { bool found; const char *prompt = FIND_PROMPT; @@ -36,29 +34,30 @@ screen_find(ScreenManager &screen, ListWindow &lw, Command findcmd, switch (findcmd) { case Command::LIST_FIND: case Command::LIST_RFIND: - screen.findbuf.clear(); + last.clear(); /* fall through */ case Command::LIST_FIND_NEXT: case Command::LIST_RFIND_NEXT: - if (screen.findbuf.empty()) { + if (last.empty()) { char *value = ui_options.find_show_last_pattern ? (char *) -1 : nullptr; - screen.findbuf=screen_readln(screen, prompt, - value, - &screen.find_history, - nullptr); + last = screen_readln(screen, + prompt, + value, + &history, + nullptr); } - if (screen.findbuf.empty()) + if (last.empty()) return true; found = reversed - ? lw.ReverseFind(text, screen.findbuf) - : lw.Find(text, screen.findbuf); + ? lw.ReverseFind(text, last) + : lw.Find(text, last); if (!found) { screen_status_printf(_("Unable to find \'%s\'"), - screen.findbuf.c_str()); + last.c_str()); Bell(); } return true; @@ -68,19 +67,17 @@ screen_find(ScreenManager &screen, ListWindow &lw, Command findcmd, return false; } -/* query user for a string and jump to the entry - * which begins with this string while the users types */ void -screen_jump(ScreenManager &screen, ListWindow &lw, - const ListText &text, - const ListRenderer &renderer) noexcept +FindSupport::Jump(ListWindow &lw, + const ListText &text, + const ListRenderer &renderer) noexcept { constexpr size_t WRLN_MAX_LINE_SIZE = 1024; int key = 65; char buffer[WRLN_MAX_LINE_SIZE]; - /* In screen.findbuf is the whole string which is displayed in the status_window + /* In last is the whole string which is displayed in the status_window * and search_str is the string the user entered (without the prompt) */ char *search_str = buffer + snprintf(buffer, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT); char *iter = search_str; @@ -110,7 +107,7 @@ screen_jump(ScreenManager &screen, ListWindow &lw, lw.Refresh(); } - screen.findbuf = search_str; + last = search_str; /* ncmpc should get the command */ keyboard_unread(key); diff --git a/src/page/FindSupport.hxx b/src/page/FindSupport.hxx new file mode 100644 index 00000000..05a6f39c --- /dev/null +++ b/src/page/FindSupport.hxx @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright The Music Player Daemon Project + +#pragma once + +#include "History.hxx" + +#include + +enum class Command : unsigned; +class ScreenManager; +class ListWindow; +class ListRenderer; +class ListText; + +class FindSupport { + ScreenManager &screen; + + std::string last; + History history; + +public: + explicit FindSupport(ScreenManager &_screen) noexcept + :screen(_screen) {} + + /** + * query user for a string and find it in a list window + * + * @param lw the list window to search + * @param findcmd the search command/mode + * @param callback_fn a function returning the text of a given line + * @param callback_data a pointer passed to callback_fn + * @return true if the command has been handled, false if not + */ + bool Find(ListWindow &lw, const ListText &text, Command cmd) noexcept; + + /* query user for a string and jump to the entry + * which begins with this string while the users types */ + void Jump(ListWindow &lw, const ListText &text, const ListRenderer &renderer) noexcept; +}; diff --git a/src/page/TextPage.cxx b/src/page/TextPage.cxx index d65c262b..a9e45922 100644 --- a/src/page/TextPage.cxx +++ b/src/page/TextPage.cxx @@ -2,7 +2,7 @@ // Copyright The Music Player Daemon Project #include "TextPage.hxx" -#include "screen_find.hxx" +#include "FindSupport.hxx" #include "charset.hxx" #include "ui/TextListRenderer.hxx" #include "screen.hxx" @@ -12,9 +12,9 @@ #include #include -TextPage::TextPage(ScreenManager &_screen, - Window window, Size size) noexcept - :ListPage(_screen, window, size), screen(_screen) +TextPage::TextPage(PageContainer &_parent, Window window, Size size, + FindSupport &_find_support) noexcept + :ListPage(_parent, window, size), find_support(_find_support) { lw.HideCursor(); } @@ -90,7 +90,7 @@ TextPage::OnCommand(struct mpdclient &c, Command cmd) search */ lw.SetCursorFromOrigin(0); - if (screen_find(screen, lw, cmd, *this)) { + if (find_support.Find(lw, *this, cmd)) { SchedulePaint(); return true; } diff --git a/src/page/TextPage.hxx b/src/page/TextPage.hxx index 5c34a23f..865aef57 100644 --- a/src/page/TextPage.hxx +++ b/src/page/TextPage.hxx @@ -10,20 +10,20 @@ #include struct mpdclient; -class ScreenManager; +class FindSupport; class TextPage : public ListPage, ListText { -protected: - ScreenManager &screen; + FindSupport &find_support; +protected: /** * Strings are UTF-8. */ std::vector lines; public: - TextPage(ScreenManager &_screen, - Window window, Size size) noexcept; + TextPage(PageContainer &_parent, Window window, Size size, + FindSupport &_find_support) noexcept; protected: bool IsEmpty() const noexcept { diff --git a/src/page/meson.build b/src/page/meson.build index ce9e4670..8337d778 100644 --- a/src/page/meson.build +++ b/src/page/meson.build @@ -1,6 +1,7 @@ page_sources = [ 'Page.cxx', 'ProxyPage.cxx', + 'FindSupport.cxx', ] if need_screen_text diff --git a/src/screen.hxx b/src/screen.hxx index c4d266ac..5ce0e109 100644 --- a/src/screen.hxx +++ b/src/screen.hxx @@ -1,15 +1,14 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef SCREEN_H -#define SCREEN_H +#pragma once #include "config.h" #include "TitleBar.hxx" #include "ProgressBar.hxx" #include "StatusBar.hxx" -#include "History.hxx" #include "page/Container.hxx" +#include "page/FindSupport.hxx" #include "ui/Point.hxx" #include "ui/Window.hxx" #include "event/IdleEvent.hxx" @@ -18,7 +17,6 @@ #include #include -#include #include enum class Command : unsigned; @@ -93,8 +91,7 @@ private: bool main_dirty = true; public: - std::string findbuf; - History find_history; + FindSupport find_support{*this}; explicit ScreenManager(EventLoop &_event_loop) noexcept; ~ScreenManager() noexcept; @@ -158,5 +155,3 @@ public: // virtual methods from PageContainer void SchedulePaint(Page &page) noexcept override; }; - -#endif diff --git a/src/screen_find.hxx b/src/screen_find.hxx deleted file mode 100644 index a58fd758..00000000 --- a/src/screen_find.hxx +++ /dev/null @@ -1,33 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -// Copyright The Music Player Daemon Project - -#ifndef NCMPC_SCREEN_FIND_H -#define NCMPC_SCREEN_FIND_H - -enum class Command : unsigned; -class ScreenManager; -class ListWindow; -class ListRenderer; -class ListText; - -/** - * query user for a string and find it in a list window - * - * @param lw the list window to search - * @param findcmd the search command/mode - * @param callback_fn a function returning the text of a given line - * @param callback_data a pointer passed to callback_fn - * @return true if the command has been handled, false if not - */ -bool -screen_find(ScreenManager &screen, ListWindow &lw, - Command findcmd, - const ListText &text) noexcept; - -/* query user for a string and jump to the entry - * which begins with this string while the users types */ -void -screen_jump(ScreenManager &screen, ListWindow &lw, - const ListText &text, const ListRenderer &renderer) noexcept; - -#endif