From 8212cd2b48bf7f01f512957c7c0f650bb9116697 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 2 Sep 2024 14:41:23 +0200 Subject: [PATCH] Options: move generic UI options to struct UiOptions --- src/ConfigParser.cxx | 23 ++++++++++++----------- src/Options.cxx | 5 +++-- src/Options.hxx | 23 ++--------------------- src/ProgressBar.cxx | 4 ++-- src/SongRowPaint.cxx | 1 + src/StatusBar.cxx | 3 ++- src/Styles.cxx | 10 +++++----- src/TitleBar.cxx | 3 ++- src/screen.cxx | 5 +++-- src/screen_find.cxx | 4 ++-- src/screen_init.cxx | 6 +++--- src/screen_paint.cxx | 4 ++-- src/ui/Bell.cxx | 4 ++-- src/ui/ListCursor.cxx | 8 ++++---- src/ui/ListWindow.cxx | 14 +++++++------- src/ui/Options.hxx | 30 ++++++++++++++++++++++++++++++ src/ui/paint.hxx | 2 +- 17 files changed, 83 insertions(+), 66 deletions(-) create mode 100644 src/ui/Options.hxx diff --git a/src/ConfigParser.cxx b/src/ConfigParser.cxx index 2db79ea7..74e00088 100644 --- a/src/ConfigParser.cxx +++ b/src/ConfigParser.cxx @@ -15,6 +15,7 @@ #include "screen_list.hxx" #include "PageMeta.hxx" #include "Options.hxx" +#include "ui/Options.hxx" #include "lib/fmt/RuntimeError.hxx" #include "util/CharUtil.hxx" #include "util/PrintException.hxx" @@ -594,12 +595,12 @@ parse_line(char *line) /* enable colors */ else if(StringIsEqualIgnoreCase(CONF_ENABLE_COLORS, name)) #ifdef ENABLE_COLORS - options.enable_colors = str2bool(value); + ui_options.enable_colors = str2bool(value); #else {} #endif else if (StringIsEqualIgnoreCase(CONF_SCROLL_OFFSET, name)) - options.scroll_offset = atoi(value); + ui_options.scroll_offset = atoi(value); /* auto center */ else if (StringIsEqualIgnoreCase(CONF_AUTO_CENTER, name)) options.auto_center = str2bool(value); @@ -612,9 +613,9 @@ parse_line(char *line) #endif /* wide cursor */ else if (StringIsEqualIgnoreCase(CONF_WIDE_CURSOR, name)) - options.wide_cursor = str2bool(value); + ui_options.wide_cursor = str2bool(value); else if (StringIsEqualIgnoreCase(name, CONF_HARDWARE_CURSOR)) - options.hardware_cursor = str2bool(value); + ui_options.hardware_cursor = str2bool(value); /* welcome screen list */ else if (StringIsEqualIgnoreCase(CONF_WELCOME_SCREEN_LIST, name)) options.welcome_screen_list = str2bool(value); @@ -649,17 +650,17 @@ parse_line(char *line) } else if (StringIsEqualIgnoreCase(CONF_XTERM_TITLE_FORMAT, name)) { options.xterm_title_format = GetStringValue(value); } else if (StringIsEqualIgnoreCase(CONF_LIST_WRAP, name)) - options.list_wrap = str2bool(value); + ui_options.list_wrap = str2bool(value); else if (StringIsEqualIgnoreCase(CONF_FIND_WRAP, name)) - options.find_wrap = str2bool(value); + ui_options.find_wrap = str2bool(value); else if (StringIsEqualIgnoreCase(CONF_FIND_SHOW_LAST,name)) - options.find_show_last_pattern = str2bool(value); + ui_options.find_show_last_pattern = str2bool(value); else if (StringIsEqualIgnoreCase(CONF_AUDIBLE_BELL, name)) - options.audible_bell = str2bool(value); + ui_options.audible_bell = str2bool(value); else if (StringIsEqualIgnoreCase(CONF_VISIBLE_BELL, name)) - options.visible_bell = str2bool(value); + ui_options.visible_bell = str2bool(value); else if (StringIsEqualIgnoreCase(CONF_BELL_ON_WRAP, name)) - options.bell_on_wrap = str2bool(value); + ui_options.bell_on_wrap = str2bool(value); else if (StringIsEqualIgnoreCase(CONF_STATUS_MESSAGE_TIME, name)) options.status_message_time = std::chrono::seconds(atoi(value)); else if (StringIsEqualIgnoreCase(CONF_XTERM_TITLE, name)) @@ -710,7 +711,7 @@ parse_line(char *line) #ifdef NCMPC_MINI {} #else - options.jump_prefix_only = str2bool(value); + ui_options.jump_prefix_only = str2bool(value); #endif else if (StringIsEqualIgnoreCase(CONF_LYRICS_AUTOSAVE, name)) #ifdef ENABLE_LYRICS_SCREEN diff --git a/src/Options.cxx b/src/Options.cxx index df87a27e..dac25b31 100644 --- a/src/Options.cxx +++ b/src/Options.cxx @@ -8,6 +8,7 @@ #include "charset.hxx" #include "ConfigFile.hxx" #include "i18n.h" +#include "ui/Options.hxx" #include "util/StringAPI.hxx" #include @@ -199,12 +200,12 @@ handle_option(int c, const char *arg) exit(EXIT_SUCCESS); case 'c': /* --colors */ #ifdef ENABLE_COLORS - options.enable_colors = true; + ui_options.enable_colors = true; #endif break; case 'C': /* --no-colors */ #ifdef ENABLE_COLORS - options.enable_colors = false; + ui_options.enable_colors = false; #endif break; case 'm': /* --mouse */ diff --git a/src/Options.hxx b/src/Options.hxx index 4df5977e..7d1fbf0f 100644 --- a/src/Options.hxx +++ b/src/Options.hxx @@ -1,8 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef OPTIONS_HXX -#define OPTIONS_HXX +#pragma once #include "config.h" #include "defaults.hxx" @@ -47,8 +46,6 @@ struct Options { int search_mode; int seek_time = 1; - unsigned scroll_offset = 0; - #ifdef ENABLE_CHAT_SCREEN std::string chat_prefix; #endif @@ -61,32 +58,18 @@ struct Options { bool text_editor_ask = false; #endif - bool find_wrap = true; - bool find_show_last_pattern; - bool list_wrap; bool auto_center; - bool wide_cursor = true; - bool hardware_cursor; -#ifdef ENABLE_COLORS - bool enable_colors = true; -#endif - bool audible_bell = true; - bool visible_bell; - bool bell_on_wrap = true; #ifndef NCMPC_MINI bool enable_xterm_title; #endif #ifdef HAVE_GETMOUSE bool enable_mouse; #endif -#ifdef NCMPC_MINI - static constexpr bool jump_prefix_only = true; -#else +#ifndef NCMPC_MINI bool scroll = DEFAULT_SCROLL; bool visible_bitrate; bool welcome_screen_list = true; - bool jump_prefix_only = true; bool second_column = true; #endif @@ -96,5 +79,3 @@ struct Options { extern Options options; void options_parse(int argc, const char **argv); - -#endif diff --git a/src/ProgressBar.cxx b/src/ProgressBar.cxx index 33d63c6b..a2b64976 100644 --- a/src/ProgressBar.cxx +++ b/src/ProgressBar.cxx @@ -3,7 +3,7 @@ #include "ProgressBar.hxx" #include "Styles.hxx" -#include "Options.hxx" +#include "ui/Options.hxx" #include "config.h" #include @@ -13,7 +13,7 @@ ProgressBar::ProgressBar(Point p, unsigned _width) noexcept { leaveok(window.w, true); #ifdef ENABLE_COLORS - if (options.enable_colors) + if (ui_options.enable_colors) window.SetBackgroundStyle(Style::PROGRESSBAR); #endif } diff --git a/src/SongRowPaint.cxx b/src/SongRowPaint.cxx index 8812906f..c97299e1 100644 --- a/src/SongRowPaint.cxx +++ b/src/SongRowPaint.cxx @@ -5,6 +5,7 @@ #include "strfsong.hxx" #include "time_format.hxx" #include "hscroll.hxx" +#include "Options.hxx" #include "ui/Window.hxx" #include "ui/paint.hxx" #include "util/LocaleString.hxx" diff --git a/src/StatusBar.cxx b/src/StatusBar.cxx index 9b4dd8e2..9a159850 100644 --- a/src/StatusBar.cxx +++ b/src/StatusBar.cxx @@ -8,6 +8,7 @@ #include "strfsong.hxx" #include "DelayedSeek.hxx" #include "time_format.hxx" +#include "ui/Options.hxx" #include "lib/fmt/ToSpan.hxx" #include "util/LocaleString.hxx" @@ -29,7 +30,7 @@ StatusBar::StatusBar(EventLoop &event_loop, keypad(window.w, true); #ifdef ENABLE_COLORS - if (options.enable_colors) + if (ui_options.enable_colors) window.SetBackgroundStyle(Style::STATUS); #endif } diff --git a/src/Styles.cxx b/src/Styles.cxx index 91195e57..ef269c17 100644 --- a/src/Styles.cxx +++ b/src/Styles.cxx @@ -11,7 +11,7 @@ #include "util/StringStrip.hxx" #ifdef ENABLE_COLORS -#include "Options.hxx" +#include "ui/Options.hxx" #endif #include @@ -329,15 +329,15 @@ ApplyStyles() noexcept /* define any custom colors defined in the configuration file */ ApplyCustomColors(); - if (options.enable_colors) { + if (ui_options.enable_colors) { for (size_t i = 1; i < size_t(Style::END); ++i) /* update the color pairs */ colors_update_pair(Style(i)); } - } else if (options.enable_colors) { + } else if (ui_options.enable_colors) { fprintf(stderr, "%s\n", _("Terminal lacks color capabilities")); - options.enable_colors = false; + ui_options.enable_colors = false; } } #endif @@ -348,7 +348,7 @@ SelectStyle(const Window window, Style style) noexcept const auto &data = GetStyle(style); #ifdef ENABLE_COLORS - if (options.enable_colors) { + if (ui_options.enable_colors) { /* color mode */ wattr_set(window.w, data.attr, short(style), nullptr); } else { diff --git a/src/TitleBar.cxx b/src/TitleBar.cxx index 3f280ac4..d0c05cf0 100644 --- a/src/TitleBar.cxx +++ b/src/TitleBar.cxx @@ -6,6 +6,7 @@ #include "Styles.hxx" #include "Options.hxx" #include "i18n.h" +#include "ui/Options.hxx" #include "util/LocaleString.hxx" #include "config.h" @@ -21,7 +22,7 @@ TitleBar::TitleBar(Point p, unsigned width) noexcept keypad(window.w, true); #ifdef ENABLE_COLORS - if (options.enable_colors) + if (ui_options.enable_colors) window.SetBackgroundStyle(Style::TITLE); #endif } diff --git a/src/screen.cxx b/src/screen.cxx index 5a4c5a6d..cc7ac84c 100644 --- a/src/screen.cxx +++ b/src/screen.cxx @@ -15,6 +15,7 @@ #include "player_command.hxx" #include "SongPage.hxx" #include "LyricsPage.hxx" +#include "ui/Options.hxx" #include "util/StringAPI.hxx" #include @@ -226,8 +227,8 @@ ScreenManager::OnCommand(struct mpdclient &c, DelayedSeek &seek, Command cmd) switch(cmd) { case Command::TOGGLE_FIND_WRAP: - options.find_wrap = !options.find_wrap; - screen_status_message(options.find_wrap ? + ui_options.find_wrap = !ui_options.find_wrap; + screen_status_message(ui_options.find_wrap ? _("Find mode: Wrapped") : _("Find mode: Normal")); break; diff --git a/src/screen_find.cxx b/src/screen_find.cxx index 44759900..f5178a4e 100644 --- a/src/screen_find.cxx +++ b/src/screen_find.cxx @@ -8,9 +8,9 @@ #include "AsyncUserInput.hxx" #include "i18n.h" #include "Command.hxx" -#include "Options.hxx" #include "ui/Bell.hxx" #include "ui/ListWindow.hxx" +#include "ui/Options.hxx" #include "util/LocaleString.hxx" #include @@ -42,7 +42,7 @@ screen_find(ScreenManager &screen, ListWindow &lw, Command findcmd, case Command::LIST_FIND_NEXT: case Command::LIST_RFIND_NEXT: if (screen.findbuf.empty()) { - char *value = options.find_show_last_pattern + char *value = ui_options.find_show_last_pattern ? (char *) -1 : nullptr; screen.findbuf=screen_readln(screen, prompt, value, diff --git a/src/screen_init.cxx b/src/screen_init.cxx index fe0668f6..25ff4a70 100644 --- a/src/screen_init.cxx +++ b/src/screen_init.cxx @@ -5,8 +5,8 @@ #include "Page.hxx" #include "QueuePage.hxx" #include "config.h" -#include "Options.hxx" #include "Styles.hxx" +#include "ui/Options.hxx" /* minimum window size */ static const unsigned SCREEN_MIN_COLS = 14; @@ -26,13 +26,13 @@ ScreenManager::ScreenManager(EventLoop &event_loop) noexcept buf_size = layout.size.width; buf = new char[buf_size]; - if (!options.hardware_cursor) + if (!ui_options.hardware_cursor) leaveok(main_window.w, true); keypad(main_window.w, true); #ifdef ENABLE_COLORS - if (options.enable_colors) { + if (ui_options.enable_colors) { /* set background attributes */ main_window.SetBackgroundStyle(Style::LIST); } diff --git a/src/screen_paint.cxx b/src/screen_paint.cxx index 6c5624aa..01bc2916 100644 --- a/src/screen_paint.cxx +++ b/src/screen_paint.cxx @@ -3,7 +3,7 @@ #include "screen.hxx" #include "Page.hxx" -#include "Options.hxx" +#include "ui/Options.hxx" void ScreenManager::PaintTopWindow() noexcept @@ -34,7 +34,7 @@ ScreenManager::Paint() noexcept /* move the cursor to the origin */ - if (!options.hardware_cursor) + if (!ui_options.hardware_cursor) main_window.MoveCursor({0, 0}); main_window.RefreshNoOut(); diff --git a/src/ui/Bell.cxx b/src/ui/Bell.cxx index 020fb0cb..b557a8c1 100644 --- a/src/ui/Bell.cxx +++ b/src/ui/Bell.cxx @@ -6,8 +6,8 @@ void Bell() noexcept { - if (options.audible_bell) + if (ui_options.audible_bell) beep(); - if (options.visible_bell) + if (ui_options.visible_bell) flash(); } diff --git a/src/ui/ListCursor.cxx b/src/ui/ListCursor.cxx index 26216886..99f85d23 100644 --- a/src/ui/ListCursor.cxx +++ b/src/ui/ListCursor.cxx @@ -6,7 +6,7 @@ ListCursor::ListCursor(unsigned _height) noexcept :height(_height), - scroll_offset(ClampScrollOffset(options.scroll_offset, height)) + scroll_offset(ClampScrollOffset(ui_options.scroll_offset, height)) { } @@ -44,7 +44,7 @@ void ListCursor::SetHeight(unsigned _height) noexcept { height = _height; - scroll_offset = ClampScrollOffset(options.scroll_offset, height); + scroll_offset = ClampScrollOffset(ui_options.scroll_offset, height); CheckOrigin(); } @@ -161,7 +161,7 @@ ListCursor::MoveCursorNext() noexcept { if (selected + 1 < length) MoveCursor(selected + 1); - else if (options.list_wrap) + else if (ui_options.list_wrap) MoveCursor(0); } @@ -170,7 +170,7 @@ ListCursor::MoveCursorPrevious() noexcept { if (selected > 0) MoveCursor(selected - 1); - else if (options.list_wrap) + else if (ui_options.list_wrap) MoveCursor(length - 1); } diff --git a/src/ui/ListWindow.cxx b/src/ui/ListWindow.cxx index 6db5528b..941fa46b 100644 --- a/src/ui/ListWindow.cxx +++ b/src/ui/ListWindow.cxx @@ -15,7 +15,7 @@ void ListWindow::Paint(const ListRenderer &renderer) const noexcept { bool cursor_visible = IsCursorVisible() && - (!options.hardware_cursor || HasRangeSelection()); + (!ui_options.hardware_cursor || HasRangeSelection()); ListWindowRange range; if (cursor_visible) @@ -38,7 +38,7 @@ ListWindow::Paint(const ListRenderer &renderer) const noexcept row_color_end(window); - if (options.hardware_cursor && IsVisible(GetCursorIndex())) { + if (ui_options.hardware_cursor && IsVisible(GetCursorIndex())) { curs_set(1); window.MoveCursor({0, (int)GetCursorIndex() - (int)GetOrigin()}); } @@ -54,7 +54,7 @@ ListWindow::Find(const ListText &text, if (!m.Compile(str, false)) return false; - const bool wrap = options.find_wrap; + const bool wrap = ui_options.find_wrap; do { while (i < GetLength()) { @@ -74,7 +74,7 @@ ListWindow::Find(const ListText &text, if (i == 0) /* empty list */ return 1; i=0; /* first item */ - if (options.bell_on_wrap) { + if (ui_options.bell_on_wrap) { Bell(); } } @@ -96,7 +96,7 @@ ListWindow::ReverseFind(const ListText &text, if (!m.Compile(str, false)) return false; - const bool wrap = options.find_wrap; + const bool wrap = ui_options.find_wrap; do { while (i >= 0) { @@ -114,7 +114,7 @@ ListWindow::ReverseFind(const ListText &text, } if (wrap) { i = GetLength() - 1; /* last item */ - if (options.bell_on_wrap) { + if (ui_options.bell_on_wrap) { Bell(); } } @@ -127,7 +127,7 @@ bool ListWindow::Jump(const ListText &text, std::string_view str) noexcept { MatchExpression m; - if (!m.Compile(str, options.jump_prefix_only)) + if (!m.Compile(str, ui_options.jump_prefix_only)) return false; for (unsigned i = 0; i < GetLength(); i++) { diff --git a/src/ui/Options.hxx b/src/ui/Options.hxx new file mode 100644 index 00000000..61bfa035 --- /dev/null +++ b/src/ui/Options.hxx @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright The Music Player Daemon Project + +#pragma once + +#include "config.h" + +struct UiOptions { + unsigned scroll_offset = 0; + + bool find_wrap = true; + bool find_show_last_pattern = false; + bool list_wrap = false; + bool wide_cursor = true; + bool hardware_cursor; + +#ifdef ENABLE_COLORS + bool enable_colors = true; +#endif + bool audible_bell = true; + bool visible_bell; + bool bell_on_wrap = true; +#ifdef NCMPC_MINI + static constexpr bool jump_prefix_only = true; +#else + bool jump_prefix_only = true; +#endif +}; + +inline UiOptions ui_options; diff --git a/src/ui/paint.hxx b/src/ui/paint.hxx index 04ce56ac..7884697b 100644 --- a/src/ui/paint.hxx +++ b/src/ui/paint.hxx @@ -40,7 +40,7 @@ row_color_end(const Window window) noexcept static inline void row_clear_to_eol(const Window window, unsigned width, bool selected) noexcept { - if (selected && options.wide_cursor) + if (selected && ui_options.wide_cursor) window.HLine(width, ' '); else window.ClearToEol();