From 3c4e1002d150be470785736e694233a85e1665af Mon Sep 17 00:00:00 2001 From: casey langen Date: Sun, 12 Mar 2017 22:55:56 -0700 Subject: [PATCH] Added an option to enable/disable 256 color mode in *nix and macOS platforms with a dialog explaining the tradeoffs. --- src/musikbox/Main.cpp | 30 ++++++++--- src/musikbox/app/layout/SettingsLayout.cpp | 51 +++++++++++++++---- src/musikbox/app/layout/SettingsLayout.h | 2 + .../app/overlay/ColorThemeOverlay.cpp | 23 +++++++++ src/musikbox/app/overlay/ColorThemeOverlay.h | 1 + src/musikbox/app/util/PreferenceKeys.cpp | 1 + src/musikbox/app/util/PreferenceKeys.h | 1 + src/musikbox/cursespp/App.cpp | 6 +-- src/musikbox/cursespp/App.h | 5 +- src/musikbox/cursespp/Colors.cpp | 26 ++++++---- src/musikbox/cursespp/Colors.h | 8 ++- 11 files changed, 121 insertions(+), 33 deletions(-) diff --git a/src/musikbox/Main.cpp b/src/musikbox/Main.cpp index de93d6a68..203d905b6 100644 --- a/src/musikbox/Main.cpp +++ b/src/musikbox/Main.cpp @@ -78,6 +78,12 @@ using namespace musik::core::db::local; using namespace musik::box; using namespace cursespp; +#ifdef WIN32 +#define DEFAULT_COLOR_MODE Colors::RGB +#else +#define DEFAULT_COLOR_MODE Colors::Palette +#endif + #define MIN_WIDTH 48 #define MIN_HEIGHT 12 @@ -137,13 +143,25 @@ int main(int argc, char* argv[]) auto prefs = Preferences::ForComponent( musik::core::prefs::components::Settings); - app.SetCustomColorsDisabled(prefs->GetBool( - musik::box::prefs::keys::DisableCustomColors.c_str(), false)); + /* set color mode (basic, palette, rgb) */ + Colors::Mode colorMode = DEFAULT_COLOR_MODE; - std::string theme = prefs->GetString(musik::box::prefs::keys::ColorTheme); - if (theme.size()) { - theme = GetApplicationDirectory() + "/themes/" + theme + ".json"; - app.SetColorTheme(theme); + if (prefs->GetBool(musik::box::prefs::keys::DisableCustomColors.c_str(), false)) { + colorMode = Colors::Basic; + } +#ifndef WIN32 + else if (prefs->GetBool(musik::box::prefs::keys::UsePaletteColors.c_str(), true)) { + colorMode = Colors::Palette; + } +#endif + + app.SetColorMode(colorMode); + + /* set color theme */ + std::string colorTheme = prefs->GetString(musik::box::prefs::keys::ColorTheme); + if (colorTheme.size()) { + colorTheme = GetApplicationDirectory() + "/themes/" + colorTheme + ".json"; + app.SetColorTheme(colorTheme); } app.SetMinimumSize(MIN_WIDTH, MIN_HEIGHT); diff --git a/src/musikbox/app/layout/SettingsLayout.cpp b/src/musikbox/app/layout/SettingsLayout.cpp index a4f298a89..351ad93f9 100755 --- a/src/musikbox/app/layout/SettingsLayout.cpp +++ b/src/musikbox/app/layout/SettingsLayout.cpp @@ -66,6 +66,10 @@ using namespace musik::glue::audio; using namespace cursespp; using namespace std::placeholders; +#ifndef WIN32 +#define ENABLE_256_COLOR_OPTION +#endif + #define LABEL_HEIGHT 1 #define INPUT_HEIGHT 3 #define HOTKEY_INPUT_WIDTH 20 @@ -116,6 +120,13 @@ void SettingsLayout::OnCheckboxChanged(cursespp::Checkbox* cb, bool checked) { this->browseAdapter->SetDotfilesVisible(showDotfiles); this->browseList->OnAdapterChanged(); } +#ifdef ENABLE_256_COLOR_OPTION + else if (cb == paletteCheckbox.get()) { + this->libraryPrefs->SetBool(box::prefs::keys::UsePaletteColors, checked); + this->libraryPrefs->Save(); + ColorThemeOverlay::Show256ColorsInfo(checked); + } +#endif } void SettingsLayout::OnOutputDropdownActivated(cursespp::TextLabel* label) { @@ -188,6 +199,9 @@ void SettingsLayout::OnLayout() { this->transportDropdown->MoveAndResize(1, y++, cx - 1, LABEL_HEIGHT); this->pluginsDropdown->MoveAndResize(1, y++, cx - 1, LABEL_HEIGHT); this->themeDropdown->MoveAndResize(1, y++, cx - 1, LABEL_HEIGHT); +#ifdef ENABLE_256_COLOR_OPTION + this->paletteCheckbox->MoveAndResize(1, y++, cx - 1, LABEL_HEIGHT); +#endif this->hotkeyDropdown->MoveAndResize(1, y++, cx - 1, LABEL_HEIGHT); this->dotfileCheckbox->MoveAndResize(1, y++, cx - 1, LABEL_HEIGHT); this->syncOnStartupCheckbox->MoveAndResize(1, y++, cx - 1, LABEL_HEIGHT); @@ -263,6 +277,9 @@ void SettingsLayout::InitializeWindows() { this->themeDropdown.reset(new TextLabel()); this->themeDropdown->SetText(arrow + " color theme: default"); this->themeDropdown->Activated.connect(this, &SettingsLayout::OnThemeDropdownActivate); +#ifdef ENABLE_256_COLOR_OPTION + CREATE_CHECKBOX(this->paletteCheckbox, "use 256 color palette"); +#endif this->hotkeyDropdown.reset(new TextLabel()); this->hotkeyDropdown->SetText(arrow + " hotkey tester"); @@ -272,16 +289,20 @@ void SettingsLayout::InitializeWindows() { CREATE_CHECKBOX(this->syncOnStartupCheckbox, "sync metadata on startup"); CREATE_CHECKBOX(this->removeCheckbox, "remove missing files from library"); - this->browseList->SetFocusOrder(0); - this->addedPathsList->SetFocusOrder(1); - this->outputDropdown->SetFocusOrder(2); - this->transportDropdown->SetFocusOrder(3); - this->pluginsDropdown->SetFocusOrder(4); - this->themeDropdown->SetFocusOrder(5); - this->hotkeyDropdown->SetFocusOrder(6); - this->dotfileCheckbox->SetFocusOrder(7); - this->syncOnStartupCheckbox->SetFocusOrder(8); - this->removeCheckbox->SetFocusOrder(9); + int order = 0; + this->browseList->SetFocusOrder(order++); + this->addedPathsList->SetFocusOrder(order++); + this->outputDropdown->SetFocusOrder(order++); + this->transportDropdown->SetFocusOrder(order++); + this->pluginsDropdown->SetFocusOrder(order++); + this->themeDropdown->SetFocusOrder(order++); +#ifdef ENABLE_256_COLOR_OPTION + this->paletteCheckbox->SetFocusOrder(order++); +#endif + this->hotkeyDropdown->SetFocusOrder(order++); + this->dotfileCheckbox->SetFocusOrder(order++); + this->syncOnStartupCheckbox->SetFocusOrder(order++); + this->removeCheckbox->SetFocusOrder(order++); this->AddWindow(this->browseLabel); this->AddWindow(this->addedPathsLabel); @@ -291,6 +312,9 @@ void SettingsLayout::InitializeWindows() { this->AddWindow(this->transportDropdown); this->AddWindow(this->pluginsDropdown); this->AddWindow(this->themeDropdown); +#ifdef ENABLE_256_COLOR_OPTION + this->AddWindow(this->paletteCheckbox); +#endif this->AddWindow(this->hotkeyDropdown); this->AddWindow(this->dotfileCheckbox); this->AddWindow(this->syncOnStartupCheckbox); @@ -365,6 +389,13 @@ void SettingsLayout::LoadPreferences() { this->themeDropdown->SetText(arrow + " color theme: " + colorTheme); +#ifdef ENABLE_256_COLOR_OPTION + this->paletteCheckbox->CheckChanged.disconnect(this); + this->paletteCheckbox->SetChecked( + this->libraryPrefs->GetBool(box::prefs::keys::UsePaletteColors, true)); + this->paletteCheckbox->CheckChanged.connect(this, &SettingsLayout::OnCheckboxChanged); +#endif + /* output plugin */ std::shared_ptr output = outputs::SelectedOutput(); if (output) { diff --git a/src/musikbox/app/layout/SettingsLayout.h b/src/musikbox/app/layout/SettingsLayout.h index 54c6e9cef..904c5c73a 100755 --- a/src/musikbox/app/layout/SettingsLayout.h +++ b/src/musikbox/app/layout/SettingsLayout.h @@ -117,7 +117,9 @@ namespace musik { std::shared_ptr transportDropdown; std::shared_ptr pluginsDropdown; std::shared_ptr hotkeyDropdown; + std::shared_ptr themeDropdown; + std::shared_ptr paletteCheckbox; std::shared_ptr dotfileCheckbox; std::shared_ptr syncOnStartupCheckbox; diff --git a/src/musikbox/app/overlay/ColorThemeOverlay.cpp b/src/musikbox/app/overlay/ColorThemeOverlay.cpp index 5ef7f4fc5..e1fe0bceb 100644 --- a/src/musikbox/app/overlay/ColorThemeOverlay.cpp +++ b/src/musikbox/app/overlay/ColorThemeOverlay.cpp @@ -174,3 +174,26 @@ void ColorThemeOverlay::Show(std::function callback) { cursespp::App::Overlays().Push(dialog); } + +void ColorThemeOverlay::Show256ColorsInfo(bool enabled) { + if (enabled) { + showNeedsRestart(); + } + else { + std::shared_ptr dialog(new DialogOverlay()); + + (*dialog) + .SetTitle("musikbox") + .SetMessage( + "disabling 256 color mode will enable RGB color mode, which will replace colors in the stock " + "palette. disabling this option results in higher fidelity themes, but it may cause display " + "issues in other applications until the terminal is reset.\n\n" + "you will need to restart musikbox for this change to take effect.") + .AddButton( + "KEY_ENTER", + "ENTER", + "ok"); + + App::Overlays().Push(dialog); + } +} diff --git a/src/musikbox/app/overlay/ColorThemeOverlay.h b/src/musikbox/app/overlay/ColorThemeOverlay.h index 2ec09820e..3e4ce82c8 100644 --- a/src/musikbox/app/overlay/ColorThemeOverlay.h +++ b/src/musikbox/app/overlay/ColorThemeOverlay.h @@ -41,6 +41,7 @@ namespace musik { class ColorThemeOverlay { public: static void Show(std::function callback); + static void Show256ColorsInfo(bool enabled); private: ColorThemeOverlay(); diff --git a/src/musikbox/app/util/PreferenceKeys.cpp b/src/musikbox/app/util/PreferenceKeys.cpp index e6ce9653c..902529bc2 100644 --- a/src/musikbox/app/util/PreferenceKeys.cpp +++ b/src/musikbox/app/util/PreferenceKeys.cpp @@ -38,6 +38,7 @@ namespace musik { namespace box { namespace prefs { const std::string keys::DisableCustomColors = "DisableCustomColors"; + const std::string keys::UsePaletteColors = "UsePaletteColors"; const std::string keys::FirstRunSettingsDisplayed = "FirstRunSettingsDisplayed"; const std::string keys::ColorTheme = "ColorTheme"; diff --git a/src/musikbox/app/util/PreferenceKeys.h b/src/musikbox/app/util/PreferenceKeys.h index 65ed9abb3..dbe711d17 100644 --- a/src/musikbox/app/util/PreferenceKeys.h +++ b/src/musikbox/app/util/PreferenceKeys.h @@ -40,6 +40,7 @@ namespace musik { namespace box { namespace prefs { namespace keys { extern const std::string DisableCustomColors; + extern const std::string UsePaletteColors; extern const std::string FirstRunSettingsDisplayed; extern const std::string ColorTheme; } diff --git a/src/musikbox/cursespp/App.cpp b/src/musikbox/cursespp/App.cpp index ba36cb482..3b1f61a9c 100755 --- a/src/musikbox/cursespp/App.cpp +++ b/src/musikbox/cursespp/App.cpp @@ -115,8 +115,8 @@ void App::SetResizeHandler(ResizeHandler handler) { this->resizeHandler = handler; } -void App::SetCustomColorsDisabled(bool disabled) { - this->disableCustomColors = disabled; +void App::SetColorMode(Colors::Mode mode) { + this->colorMode = mode; } void App::SetColorTheme(const std::string& colorTheme) { @@ -163,7 +163,7 @@ void App::OnResized() { } void App::Run(ILayoutPtr layout) { - Colors::Init(this->disableCustomColors); + Colors::Init(this->colorMode); if (this->colorTheme.size()) { Colors::SetTheme(this->colorTheme); diff --git a/src/musikbox/cursespp/App.h b/src/musikbox/cursespp/App.h index afca2f77b..d40c945e4 100755 --- a/src/musikbox/cursespp/App.h +++ b/src/musikbox/cursespp/App.h @@ -39,6 +39,7 @@ #include "IInput.h" #include "IKeyHandler.h" #include "OverlayStack.h" +#include "Colors.h" namespace cursespp { class App { @@ -51,7 +52,7 @@ namespace cursespp { void SetKeyHandler(MainKeyHandler handler); void SetResizeHandler(ResizeHandler handler); - void SetCustomColorsDisabled(bool disable); + void SetColorMode(Colors::Mode mode); void SetColorTheme(const std::string& fn); void SetMinimumSize(int width, int height); bool IsOverlayVisible() { return this->state.overlay != nullptr; } @@ -100,7 +101,7 @@ namespace cursespp { WindowState state; MainKeyHandler keyHandler; ResizeHandler resizeHandler; - bool disableCustomColors; + Colors::Mode colorMode; std::string colorTheme; int minWidth, minHeight; diff --git a/src/musikbox/cursespp/Colors.cpp b/src/musikbox/cursespp/Colors.cpp index e8c60eff2..2a73e4bc2 100755 --- a/src/musikbox/cursespp/Colors.cpp +++ b/src/musikbox/cursespp/Colors.cpp @@ -126,8 +126,6 @@ palette, use ones that most closely match our desired colors */ struct Theme { struct Color { - enum Mode { Standard, Palette, Custom }; - Color() { Set(0, 0, 0, 0, 0); } @@ -157,11 +155,11 @@ struct Theme { } } - int Id(Mode mode, int defaultValue) { - if (mode == Standard) { + int Id(Colors::Mode mode, int defaultValue) { + if (mode == Colors::Basic) { return defaultValue; } - else if (mode == Palette) { + else if (mode == Colors::Palette) { return this->palette; } @@ -311,7 +309,7 @@ struct Theme { /* initializes all of the color pairs from the specified colors, then applies them to the current session! */ - void Apply(Color::Mode mode) { + void Apply(Colors::Mode mode) { int backgroundId = background.Id(mode, -1); int foregroundId = foreground.Id(mode, -1); @@ -451,15 +449,21 @@ Colors::Colors() { } static Theme theme; -static Theme::Color::Mode colorMode = Theme::Color::Standard; +static Colors::Mode colorMode = Colors::Basic; -void Colors::Init(bool disableCustomColors) { +void Colors::Init(Colors::Mode mode) { start_color(); use_default_colors(); - if (!disableCustomColors && COLORS > 8) { - colorMode = canChangeColors() - ? Theme::Color::Custom : Theme::Color::Palette; + colorMode = Colors::Basic; + + if (mode != Colors::Basic && COLORS > 8) { + if (mode == Colors::RGB && canChangeColors()) { + colorMode = Colors::RGB; + } + else { + colorMode = Colors::Palette; + } } theme.Apply(colorMode); diff --git a/src/musikbox/cursespp/Colors.h b/src/musikbox/cursespp/Colors.h index d4a440bac..3147e7620 100755 --- a/src/musikbox/cursespp/Colors.h +++ b/src/musikbox/cursespp/Colors.h @@ -73,7 +73,13 @@ namespace cursespp { Colors(); public: - static void Init(bool disableCustomColors = false); + enum Mode { + RGB, + Palette, + Basic + }; + + static void Init(Mode mode = Mode::Basic); static void SetTheme(const std::string& fn); }; }