Added an option to enable/disable 256 color mode in *nix and macOS

platforms with a dialog explaining the tradeoffs.
This commit is contained in:
casey langen 2017-03-12 22:55:56 -07:00
parent 3d7f36a975
commit 3c4e1002d1
11 changed files with 121 additions and 33 deletions

View File

@ -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);

View File

@ -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<IOutput> output = outputs::SelectedOutput();
if (output) {

View File

@ -117,7 +117,9 @@ namespace musik {
std::shared_ptr<cursespp::TextLabel> transportDropdown;
std::shared_ptr<cursespp::TextLabel> pluginsDropdown;
std::shared_ptr<cursespp::TextLabel> hotkeyDropdown;
std::shared_ptr<cursespp::TextLabel> themeDropdown;
std::shared_ptr<cursespp::Checkbox> paletteCheckbox;
std::shared_ptr<cursespp::Checkbox> dotfileCheckbox;
std::shared_ptr<cursespp::Checkbox> syncOnStartupCheckbox;

View File

@ -174,3 +174,26 @@ void ColorThemeOverlay::Show(std::function<void()> callback) {
cursespp::App::Overlays().Push(dialog);
}
void ColorThemeOverlay::Show256ColorsInfo(bool enabled) {
if (enabled) {
showNeedsRestart();
}
else {
std::shared_ptr<DialogOverlay> 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);
}
}

View File

@ -41,6 +41,7 @@ namespace musik {
class ColorThemeOverlay {
public:
static void Show(std::function<void()> callback);
static void Show256ColorsInfo(bool enabled);
private:
ColorThemeOverlay();

View File

@ -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";

View File

@ -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;
}

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);
};
}