Skip to content

Commit

Permalink
added up to 18 custom user colours in the colour picker palette (#2422)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuspen committed Jan 12, 2024
1 parent b6bf8be commit 015ec2c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/ct/ct_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ void CtConfig::_populate_keyfile_from_data()
if (not currColour_fg.empty()) _uKeyFile->set_string(_currentGroup, "fg", currColour_fg);
if (not currColour_bg.empty()) _uKeyFile->set_string(_currentGroup, "bg", currColour_bg);
if (not currColour_nn.empty()) _uKeyFile->set_string(_currentGroup, "nn", currColour_nn);
if (not coloursUserPalette.empty()) _uKeyFile->set_string(_currentGroup, "colours_user", coloursUserPalette.serialise_to_colon_sep());

// [tree]
_currentGroup = "tree";
Expand Down Expand Up @@ -492,6 +493,13 @@ void CtConfig::_populate_data_from_keyfile()
_populate_string_from_keyfile("fg", &currColour_fg);
_populate_string_from_keyfile("bg", &currColour_bg);
_populate_string_from_keyfile("nn", &currColour_nn);
std::string colours_user;
_populate_string_from_keyfile("colours_user", &colours_user);
for (const auto& colour : str::split(colours_user, ":")) {
if (not colour.empty()) {
coloursUserPalette.push_back(Gdk::RGBA{colour});
}
}

// [tree]
_currentGroup = "tree";
Expand Down
1 change: 1 addition & 0 deletions src/ct/ct_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class CtConfig
Glib::ustring currColour_fg;
Glib::ustring currColour_bg;
Glib::ustring currColour_nn;
CtColoursUserPalette coloursUserPalette;

// [tree]
CtRestoreExpColl restoreExpColl{CtRestoreExpColl::FROM_STR};
Expand Down
30 changes: 26 additions & 4 deletions src/ct/ct_dialogs_gen_purp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,27 @@ CtDialogs::CtPickDlgState CtDialogs::colour_pick_dialog(CtMainWin* pCtMainWin,
"#ffffff", "#f6f5f4", "#deddda", "#c0bfbc", "#9a9996", /* Light */
"#77767b", "#5e5c64", "#3d3846", "#241f31", "#000000"};/* Dark */
std::vector<Gdk::RGBA> default_colours;
for (int i=0; i<45; ++i) { default_colours.push_back(Gdk::RGBA{default_colors[i]}); }
dialog.add_palette(Gtk::Orientation::ORIENTATION_VERTICAL, 5, default_colours);
dialog.set_rgba(Gdk::RGBA(ret_colour));
std::vector<Gdk::RGBA> palette_colours;
auto& coloursUserPalette = pCtMainWin->get_ct_config()->coloursUserPalette;
size_t column_idx{0u};
for (int i = 0; i < 45; ++i) {
const Gdk::RGBA curr_colour{default_colors[i]};
default_colours.push_back(curr_colour);
palette_colours.push_back(curr_colour);
if (coloursUserPalette.size() > 0u) {
if (4 == i % 5) {
if (coloursUserPalette.size() > column_idx) palette_colours.push_back(*coloursUserPalette.at(column_idx));
else palette_colours.push_back(Gdk::RGBA{});
if (coloursUserPalette.size() > 9u) {
if (coloursUserPalette.size() > (9+column_idx)) palette_colours.push_back(*coloursUserPalette.at(9+column_idx));
else palette_colours.push_back(Gdk::RGBA{});
}
++column_idx;
}
}
}
dialog.add_palette(Gtk::Orientation::ORIENTATION_VERTICAL, 5 + (coloursUserPalette.size() + 8)/9, palette_colours);
dialog.set_rgba(Gdk::RGBA{ret_colour});

auto on_key_press_dialog = [&](GdkEventKey* pEventKey)->bool{
if (GDK_KEY_Return == pEventKey->keyval or GDK_KEY_KP_Enter == pEventKey->keyval) {
Expand All @@ -259,7 +277,11 @@ CtDialogs::CtPickDlgState CtDialogs::colour_pick_dialog(CtMainWin* pCtMainWin,
if (Gtk::RESPONSE_OK != response) {
return CtPickDlgState::CANCEL;
}
ret_colour = dialog.get_rgba().to_string();
const Gdk::RGBA sel_colour = dialog.get_rgba();
ret_colour = sel_colour.to_string();
if (not vec::exists(default_colours, sel_colour)) {
coloursUserPalette.move_or_push_front(sel_colour);
}
return CtPickDlgState::SELECTED;
}

Expand Down
25 changes: 24 additions & 1 deletion src/ct/ct_types.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ct_types.h
*
* Copyright 2009-2023
* Copyright 2009-2024
* Giuseppe Penone <[email protected]>
* Evgenii Gurianov <https://github.com/txe>
*
Expand Down Expand Up @@ -206,6 +206,29 @@ struct CtRecentDocsFilepaths : public CtMaxSizedList<fs::path>
CtRecentDocsFilepaths() : CtMaxSizedList<fs::path>{10} {}
};

struct CtColoursUserPalette : public CtMaxSizedList<Gdk::RGBA>
{
CtColoursUserPalette() : CtMaxSizedList<Gdk::RGBA>{18} {}
std::string serialise_to_colon_sep() const {
std::string outString;
bool firstIteration{true};
for (const Gdk::RGBA& element : *this) {
if (not firstIteration) outString += ":";
else firstIteration = false;
outString += element.to_string();
}
return outString;
}
Gdk::RGBA* at(const int i) {
int idx{0};
for (Gdk::RGBA& element : *this) {
if (i == idx) return &element;
++idx;
}
return nullptr;
}
};

class CtStringSplittable
{
private:
Expand Down

0 comments on commit 015ec2c

Please sign in to comment.