chore: structured src/gui, run clang-format

This commit is contained in:
2025-12-18 13:23:50 +03:00
parent 613c2c80f5
commit 7641846600
55 changed files with 1647 additions and 1399 deletions

View File

@@ -0,0 +1,96 @@
#include "about_window.hpp"
#include "core/version.hpp"
#include "gui/helpers/imgui_helpers.hpp"
#include "imgui.h"
about_window::about_window()
{
}
void about_window::render(const clrsync::core::palette &pal)
{
if (!m_visible)
return;
ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver);
if (ImGui::Begin("About clrsync", &m_visible, ImGuiWindowFlags_NoResize))
{
const float window_width = ImGui::GetContentRegionAvail().x;
ImGui::PushFont(ImGui::GetFont());
const char *title = "clrsync";
const float title_size = ImGui::CalcTextSize(title).x;
ImGui::SetCursorPosX((window_width - title_size) * 0.5f);
ImVec4 title_color = palette_utils::get_color(pal, "info", "accent");
ImGui::TextColored(title_color, "%s", title);
ImGui::PopFont();
std::string version = "Version " + clrsync::core::version_string();
const float version_size = ImGui::CalcTextSize(version.c_str()).x;
ImGui::SetCursorPosX((window_width - version_size) * 0.5f);
ImVec4 subtitle_color = palette_utils::get_color(pal, "editor_inactive", "foreground");
ImGui::TextColored(subtitle_color, "%s", version.c_str());
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
ImGui::TextWrapped("A color scheme management tool.");
ImGui::Spacing();
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
ImGui::Text("Links:");
const float button_width = 200.0f;
const float spacing = ImGui::GetStyle().ItemSpacing.x;
const float total_width = 2.0f * button_width + spacing;
ImGui::SetCursorPosX((window_width - total_width) * 0.5f);
if (ImGui::Button("GitHub Repository", ImVec2(button_width, 0)))
{
#ifdef _WIN32
system("start https://github.com/obsqrbtz/clrsync");
#elif __APPLE__
system("open https://github.com/obsqrbtz/clrsync");
#else
system("xdg-open https://github.com/obsqrbtz/clrsync");
#endif
}
ImGui::SameLine();
if (ImGui::Button("Documentation", ImVec2(button_width, 0)))
{
#ifdef _WIN32
system("start https://binarygoose.dev/projects/clrsync/overview/");
#elif __APPLE__
system("open https://binarygoose.dev/projects/clrsync/overview/");
#else
system("xdg-open https://binarygoose.dev/projects/clrsync/overview/");
#endif
}
ImGui::Spacing();
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
ImVec4 license_color = palette_utils::get_color(pal, "editor_inactive", "foreground");
ImGui::TextColored(license_color, "MIT License");
ImGui::TextWrapped(
"Copyright (c) 2025 Daniel Dada\n\n"
"Permission is hereby granted, free of charge, to any person obtaining a copy "
"of this software and associated documentation files (the \"Software\"), to deal "
"in the Software without restriction, including without limitation the rights "
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell "
"copies of the Software, and to permit persons to whom the Software is "
"furnished to do so, subject to the following conditions:\n\n"
"The above copyright notice and this permission notice shall be included in all "
"copies or substantial portions of the Software.");
}
ImGui::End();
}

View File

@@ -0,0 +1,33 @@
#ifndef CLRSYNC_GUI_ABOUT_WINDOW_HPP
#define CLRSYNC_GUI_ABOUT_WINDOW_HPP
#include "core/palette/palette.hpp"
class about_window
{
public:
about_window();
void render(const clrsync::core::palette &pal);
void render()
{
render(m_default_palette);
}
void show()
{
m_visible = true;
}
void hide()
{
m_visible = false;
}
bool is_visible() const
{
return m_visible;
}
private:
bool m_visible{false};
clrsync::core::palette m_default_palette;
};
#endif // CLRSYNC_GUI_ABOUT_WINDOW_HPP

View File

@@ -0,0 +1,201 @@
#include "color_scheme_editor.hpp"
#include "gui/controllers/theme_applier.hpp"
#include "gui/helpers/imgui_helpers.hpp"
#include "imgui.h"
#include "settings_window.hpp"
#include "template_editor.hpp"
#include <iostream>
#include <ranges>
color_scheme_editor::color_scheme_editor()
{
const auto &current = m_controller.current_palette();
if (!current.colors().empty())
{
theme_applier::apply_to_imgui(current);
m_preview.apply_palette(current);
}
else
{
std::cout << "WARNING: No palette loaded, skipping theme application\n";
}
}
void color_scheme_editor::notify_palette_changed()
{
if (m_template_editor)
{
m_template_editor->apply_current_palette(m_controller.current_palette());
}
if (m_settings_window)
{
m_settings_window->set_palette(m_controller.current_palette());
}
}
void color_scheme_editor::apply_themes()
{
const auto &current = m_controller.current_palette();
theme_applier::apply_to_imgui(current);
m_preview.apply_palette(current);
notify_palette_changed();
}
void color_scheme_editor::render_controls_and_colors()
{
ImGui::Begin("Color Schemes");
render_controls();
ImGui::Separator();
ImGui::BeginChild("ColorTableContent", ImVec2(0, 0), false);
m_color_table.render(m_controller.current_palette(), m_controller,
[this]() { apply_themes(); });
ImGui::EndChild();
ImGui::End();
}
void color_scheme_editor::render_preview()
{
ImGui::Begin("Color Preview");
m_preview.render(m_controller.current_palette());
ImGui::End();
}
void color_scheme_editor::render_controls()
{
const auto &current = m_controller.current_palette();
const auto &palettes = m_controller.palettes();
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(6, 8));
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(8, 5));
ImGui::AlignTextToFramePadding();
ImGui::Text("Palette:");
ImGui::SameLine();
ImGui::SetNextItemWidth(200.0f);
if (ImGui::BeginCombo("##scheme", current.name().c_str()))
{
for (const auto &name : palettes | std::views::keys)
{
const bool selected = current.name() == name;
if (ImGui::Selectable(name.c_str(), selected))
{
m_controller.select_palette(name);
apply_themes();
}
if (selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Select a color palette to edit");
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 8);
static char new_palette_name_buf[128] = "";
if (ImGui::Button(" + New "))
{
new_palette_name_buf[0] = 0;
ImGui::OpenPopup("New Palette");
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Create a new palette");
if (ImGui::BeginPopupModal("New Palette", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::Text("Enter a name for the new palette:");
ImGui::Spacing();
ImGui::SetNextItemWidth(250);
ImGui::InputTextWithHint("##new_palette_input", "Palette name...", new_palette_name_buf,
IM_ARRAYSIZE(new_palette_name_buf));
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
bool can_create = strlen(new_palette_name_buf) > 0;
if (!can_create)
ImGui::BeginDisabled();
if (ImGui::Button("Create", ImVec2(120, 0)))
{
m_controller.create_palette(new_palette_name_buf);
m_controller.select_palette(new_palette_name_buf);
apply_themes();
new_palette_name_buf[0] = 0;
ImGui::CloseCurrentPopup();
}
if (!can_create)
ImGui::EndDisabled();
ImGui::SameLine();
if (ImGui::Button("Cancel", ImVec2(120, 0)))
{
new_palette_name_buf[0] = 0;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
ImGui::SameLine();
if (ImGui::Button(" Save "))
{
m_controller.save_current_palette();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Save current palette to file");
ImGui::SameLine();
auto error = palette_utils::get_color(current, "error");
auto error_hover = ImVec4(error.x * 1.1f, error.y * 1.1f, error.z * 1.1f, error.w);
auto error_active = ImVec4(error.x * 0.8f, error.y * 0.8f, error.z * 0.8f, error.w);
auto on_error = palette_utils::get_color(current, "on_error");
ImGui::PushStyleColor(ImGuiCol_Button, error);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, error_hover);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, error_active);
ImGui::PushStyleColor(ImGuiCol_Text, on_error);
if (ImGui::Button(" Delete "))
{
m_show_delete_confirmation = true;
}
ImGui::PopStyleColor(4);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Delete current palette");
if (m_show_delete_confirmation)
{
ImGui::OpenPopup("Delete Palette?");
m_show_delete_confirmation = false;
}
palette_utils::render_delete_confirmation_popup("Delete Palette?", current.name(), "palette",
current, [this]() {
m_controller.delete_current_palette();
apply_themes();
});
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 16);
if (ImGui::Button(" Apply Theme "))
{
m_controller.apply_current_theme();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Apply current palette to all enabled templates");
ImGui::PopStyleVar(2);
}

View File

@@ -0,0 +1,44 @@
#ifndef CLRSYNC_GUI_COLOR_SCHEME_EDITOR_HPP
#define CLRSYNC_GUI_COLOR_SCHEME_EDITOR_HPP
#include "gui/controllers/palette_controller.hpp"
#include "gui/views/color_table_renderer.hpp"
#include "gui/views/preview_renderer.hpp"
class template_editor;
class settings_window;
class color_scheme_editor
{
public:
color_scheme_editor();
void render_controls_and_colors();
void render_preview();
void set_template_editor(template_editor *editor)
{
m_template_editor = editor;
}
void set_settings_window(settings_window *window)
{
m_settings_window = window;
}
const palette_controller &controller() const
{
return m_controller;
}
private:
void render_controls();
void apply_themes();
void notify_palette_changed();
palette_controller m_controller;
color_table_renderer m_color_table;
preview_renderer m_preview;
template_editor *m_template_editor{nullptr};
settings_window *m_settings_window{nullptr};
bool m_show_delete_confirmation{false};
};
#endif // CLRSYNC_GUI_COLOR_SCHEME_EDITOR_HPP

View File

@@ -0,0 +1,202 @@
#include "gui/views/color_table_renderer.hpp"
#include "gui/helpers/imgui_helpers.hpp"
#include "imgui.h"
#include <algorithm>
#include <cctype>
#include <vector>
bool color_table_renderer::matches_filter(const std::string &name) const
{
if (m_filter_text[0] == '\0')
return true;
std::string filter_lower = m_filter_text;
std::string name_lower = name;
std::transform(filter_lower.begin(), filter_lower.end(), filter_lower.begin(),
[](unsigned char c) { return std::tolower(c); });
std::transform(name_lower.begin(), name_lower.end(), name_lower.begin(),
[](unsigned char c) { return std::tolower(c); });
return name_lower.find(filter_lower) != std::string::npos;
}
void color_table_renderer::render_color_row(const std::string &name,
const clrsync::core::palette &current,
palette_controller &controller,
const OnColorChangedCallback &on_changed)
{
if (!matches_filter(name))
return;
const clrsync::core::color &col = current.get_color(name);
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
const float key_col_width = ImGui::GetContentRegionAvail().x;
ImVec4 text_color = palette_utils::get_color(current, "info", "accent");
ImGui::PushStyleColor(ImGuiCol_Text, text_color);
const bool copied = ImGui::Selectable(name.c_str(), false, 0, ImVec2(key_col_width, 0.0f));
ImGui::PopStyleColor();
if (ImGui::IsItemHovered())
{
ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
ImGui::SetTooltip("Click to copy: {%s.hex}", name.c_str());
}
if (copied)
{
std::string template_var = "{" + name + ".hex}";
ImGui::SetClipboardText(template_var.c_str());
}
ImGui::TableSetColumnIndex(1);
{
std::string hex_str = col.to_hex_string();
char buf[9];
strncpy(buf, hex_str.c_str(), sizeof(buf));
buf[8] = 0;
ImGui::SetNextItemWidth(-FLT_MIN);
if (ImGui::InputText(("##hex_" + name).c_str(), buf, sizeof(buf),
ImGuiInputTextFlags_CharsUppercase))
{
try
{
clrsync::core::color new_color;
new_color.from_hex_string(buf);
controller.set_color(name, new_color);
if (on_changed)
on_changed();
}
catch (...)
{
}
}
}
ImGui::TableSetColumnIndex(2);
ImGui::PushID(name.c_str());
float c[4] = {((col.hex() >> 24) & 0xFF) / 255.0f, ((col.hex() >> 16) & 0xFF) / 255.0f,
((col.hex() >> 8) & 0xFF) / 255.0f, (col.hex() & 0xFF) / 255.0f};
ImGui::SetNextItemWidth(-FLT_MIN);
if (ImGui::ColorEdit4(("##color_" + name).c_str(), c,
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel |
ImGuiColorEditFlags_AlphaBar | ImGuiColorEditFlags_AlphaPreviewHalf))
{
uint32_t r = (uint32_t)(c[0] * 255.0f);
uint32_t g = (uint32_t)(c[1] * 255.0f);
uint32_t b = (uint32_t)(c[2] * 255.0f);
uint32_t a = (uint32_t)(c[3] * 255.0f);
uint32_t hex = (r << 24) | (g << 16) | (b << 8) | a;
controller.set_color(name, clrsync::core::color(hex));
if (on_changed)
on_changed();
}
ImGui::PopID();
}
void color_table_renderer::render(const clrsync::core::palette &current,
palette_controller &controller,
const OnColorChangedCallback &on_changed)
{
if (current.colors().empty())
{
ImVec4 warning_color = palette_utils::get_color(current, "warning", "accent");
ImGui::TextColored(warning_color, "No palette loaded");
return;
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(8, 6));
ImGui::Text("Filter:");
ImGui::SameLine();
ImGui::SetNextItemWidth(200);
bool filter_changed = ImGui::InputTextWithHint("##color_filter", "Search colors...",
m_filter_text, sizeof(m_filter_text));
if (m_filter_text[0] != '\0')
{
ImGui::SameLine();
if (ImGui::SmallButton("X"))
{
m_filter_text[0] = '\0';
filter_changed = true;
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Clear filter");
}
ImGui::PopStyleVar();
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
auto draw_table = [&](const char *title, const char *id,
const std::vector<const char *> &keys) {
bool has_matches = false;
for (auto *k : keys)
{
if (matches_filter(k))
{
has_matches = true;
break;
}
}
if (!has_matches)
return;
ImGui::PushStyleColor(ImGuiCol_Text, palette_utils::get_color(current, "accent"));
bool header_open = ImGui::TreeNodeEx(title, ImGuiTreeNodeFlags_DefaultOpen |
ImGuiTreeNodeFlags_SpanAvailWidth);
ImGui::PopStyleColor();
if (header_open)
{
if (ImGui::BeginTable(id, 3,
ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg |
ImGuiTableFlags_SizingStretchProp))
{
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, 160.0f);
ImGui::TableSetupColumn("HEX", ImGuiTableColumnFlags_WidthFixed, 95.0f);
ImGui::TableSetupColumn("Color", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableHeadersRow();
for (auto *k : keys)
render_color_row(k, current, controller, on_changed);
ImGui::EndTable();
}
ImGui::TreePop();
}
ImGui::Spacing();
};
draw_table("General UI", "##general_ui",
{"background", "on_background", "surface", "on_surface", "surface_variant",
"on_surface_variant", "foreground", "cursor", "accent"});
draw_table("Borders", "##borders", {"border_focused", "border"});
draw_table(
"Semantic Colors", "##semantic",
{"success", "info", "warning", "error", "on_success", "on_info", "on_warning", "on_error"});
draw_table("Editor", "##editor",
{"editor_background", "editor_command", "editor_comment", "editor_disabled",
"editor_emphasis", "editor_error", "editor_inactive", "editor_line_number",
"editor_link", "editor_main", "editor_selected", "editor_selection_inactive",
"editor_string", "editor_success", "editor_warning"});
draw_table("Terminal (Base16)", "##terminal",
{"base00", "base01", "base02", "base03", "base04", "base05", "base06", "base07",
"base08", "base09", "base0A", "base0B", "base0C", "base0D", "base0E", "base0F"});
}

View File

@@ -0,0 +1,27 @@
#ifndef CLRSYNC_GUI_COLOR_TABLE_RENDERER_HPP
#define CLRSYNC_GUI_COLOR_TABLE_RENDERER_HPP
#include "core/palette/palette.hpp"
#include "gui/controllers/palette_controller.hpp"
#include <functional>
#include <string>
class color_table_renderer
{
public:
using OnColorChangedCallback = std::function<void()>;
void render(const clrsync::core::palette &palette, palette_controller &controller,
const OnColorChangedCallback &on_changed);
private:
void render_color_row(const std::string &name, const clrsync::core::palette &palette,
palette_controller &controller, const OnColorChangedCallback &on_changed);
bool matches_filter(const std::string &name) const;
char m_filter_text[128] = {0};
bool m_show_only_modified{false};
};
#endif // CLRSYNC_GUI_COLOR_TABLE_RENDERER_HPP

View File

@@ -0,0 +1,246 @@
#include "gui/views/preview_renderer.hpp"
#include "gui/controllers/theme_applier.hpp"
#include "gui/helpers/imgui_helpers.hpp"
#include "imgui.h"
#include <algorithm>
#include <array>
preview_renderer::preview_renderer()
{
m_editor.SetLanguageDefinition(TextEditor::LanguageDefinition::CPlusPlus());
m_editor.SetText(R"(#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include <cstdlib>
namespace fs = std::filesystem;
std::string expand_user(const std::string &path)
{
if (path.empty()) return "";
std::string result;
if (path[0] == '~')
{
#ifdef _WIN32
const char* home = std::getenv("USERPROFILE");
#else
const char* home = std::getenv("HOME");
#endif
result = home ? std::string(home) : "~";
result += path.substr(1);
}
else
{
result = path;
}
return result;
}
std::vector<std::string> list_files(const std::string &dir_path)
{
std::vector<std::string> files;
try
{
for (const auto &entry : fs::directory_iterator(dir_path))
{
if (entry.is_regular_file())
files.push_back(entry.path().string());
}
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
return files;
}
int main()
{
std::string path = expand_user("~/Documents");
std::cout << "Listing files in: " << path << std::endl;
auto files = list_files(path);
for (const auto &f : files)
std::cout << " " << f << std::endl;
return 0;
})");
m_editor.SetShowWhitespaces(false);
}
static ImVec4 hex_to_imvec4(uint32_t hex)
{
return {((hex >> 24) & 0xFF) / 255.0f, ((hex >> 16) & 0xFF) / 255.0f,
((hex >> 8) & 0xFF) / 255.0f, (hex & 0xFF) / 255.0f};
}
void preview_renderer::apply_palette(const clrsync::core::palette &palette)
{
theme_applier::apply_to_editor(m_editor, palette);
}
void preview_renderer::render_code_preview()
{
const float avail_height = ImGui::GetContentRegionAvail().y;
const float code_preview_height = std::max(250.0f, avail_height * 0.50f);
ImGui::Text("Code Editor Preview:");
ImGui::SameLine();
ImGui::TextDisabled("(editor_* colors)");
m_editor.Render("##CodeEditor", ImVec2(0, code_preview_height), true);
}
void preview_renderer::render_terminal_preview(const clrsync::core::palette &current)
{
auto get_color = [&](const std::string &key) -> ImVec4 {
const auto &col = current.get_color(key);
return hex_to_imvec4(col.hex());
};
const ImVec4 bg = get_color("base00");
const ImVec4 fg = get_color("base07");
const ImVec4 cursor_col = get_color("cursor");
const ImVec4 border_col = get_color("border");
const ImVec4 black = get_color("base00");
const ImVec4 red = get_color("base01");
const ImVec4 green = get_color("base02");
const ImVec4 yellow = get_color("base03");
const ImVec4 blue = get_color("base04");
const ImVec4 magenta = get_color("base05");
const ImVec4 cyan = get_color("base06");
const ImVec4 white = get_color("base07");
const ImVec4 bright_black = get_color("base08");
const ImVec4 bright_red = get_color("base09");
const ImVec4 bright_green = get_color("base0A");
const ImVec4 bright_yellow = get_color("base0B");
const ImVec4 bright_blue = get_color("base0C");
const ImVec4 bright_magenta = get_color("base0D");
const ImVec4 bright_cyan = get_color("base0E");
const ImVec4 bright_white = get_color("base0F");
ImGui::Spacing();
ImGui::Text("Terminal Preview:");
ImGui::SameLine();
ImGui::TextDisabled("(base00-base0F colors)");
ImGui::PushStyleColor(ImGuiCol_ChildBg, bg);
ImGui::PushStyleColor(ImGuiCol_Border, border_col);
const float terminal_height = std::max(200.0f, ImGui::GetContentRegionAvail().y - 10.0f);
ImGui::BeginChild("TerminalPreview", ImVec2(0, terminal_height), true);
ImGui::TextColored(green, "user@host");
ImGui::SameLine(0, 0);
ImGui::TextColored(fg, ":");
ImGui::SameLine(0, 0);
ImGui::TextColored(blue, "~/projects");
ImGui::SameLine(0, 0);
ImGui::TextColored(fg, "$ ");
ImGui::SameLine(0, 0);
ImGui::TextColored(fg, "ls -la");
ImGui::TextColored(fg, "total 48");
ImGui::TextColored(blue, "drwxr-xr-x");
ImGui::SameLine();
ImGui::TextColored(fg, " 5 user group 4096 Dec 2 10:30 ");
ImGui::SameLine(0, 0);
ImGui::TextColored(blue, ".");
ImGui::TextColored(blue, "drwxr-xr-x");
ImGui::SameLine();
ImGui::TextColored(fg, " 3 user group 4096 Dec 1 09:15 ");
ImGui::SameLine(0, 0);
ImGui::TextColored(blue, "..");
ImGui::TextColored(fg, "-rw-r--r--");
ImGui::SameLine();
ImGui::TextColored(fg, " 1 user group 1234 Dec 2 10:30 ");
ImGui::SameLine(0, 0);
ImGui::TextColored(fg, "README.md");
ImGui::TextColored(fg, "-rwxr-xr-x");
ImGui::SameLine();
ImGui::TextColored(fg, " 1 user group 8192 Dec 2 10:28 ");
ImGui::SameLine(0, 0);
ImGui::TextColored(green, "build.sh");
ImGui::TextColored(cyan, "lrwxrwxrwx");
ImGui::SameLine();
ImGui::TextColored(fg, " 1 user group 24 Dec 1 15:00 ");
ImGui::SameLine(0, 0);
ImGui::TextColored(cyan, "config -> ~/.config/app");
ImGui::Spacing();
ImGui::TextColored(green, "user@host");
ImGui::SameLine(0, 0);
ImGui::TextColored(fg, ":");
ImGui::SameLine(0, 0);
ImGui::TextColored(blue, "~/projects");
ImGui::SameLine(0, 0);
ImGui::TextColored(fg, "$ ");
ImGui::SameLine(0, 0);
ImGui::TextColored(fg, "git status");
ImGui::TextColored(fg, "On branch ");
ImGui::SameLine(0, 0);
ImGui::TextColored(green, "main");
ImGui::TextColored(fg, "Changes to be committed:");
ImGui::TextColored(green, " modified: src/main.cpp");
ImGui::TextColored(green, " new file: src/utils.hpp");
ImGui::TextColored(fg, "Changes not staged:");
ImGui::TextColored(red, " modified: README.md");
ImGui::Spacing();
ImGui::TextColored(fg, "ANSI Colors (0-7 / 8-15):");
const float box_size = 20.0f;
const float spacing = 4.0f;
ImVec2 start_pos = ImGui::GetCursorScreenPos();
ImDrawList *draw_list = ImGui::GetWindowDrawList();
std::array<ImVec4, 8> normal_colors = {black, red, green, yellow, blue, magenta, cyan, white};
for (size_t i = 0; i < 8; i++)
{
ImVec2 p0 = ImVec2(start_pos.x + i * (box_size + spacing), start_pos.y);
ImVec2 p1 = ImVec2(p0.x + box_size, p0.y + box_size);
draw_list->AddRectFilled(p0, p1, ImGui::ColorConvertFloat4ToU32(normal_colors[i]));
draw_list->AddRect(p0, p1, ImGui::ColorConvertFloat4ToU32(border_col));
}
std::array<ImVec4, 8> bright_colors = {bright_black, bright_red, bright_green,
bright_yellow, bright_blue, bright_magenta,
bright_cyan, bright_white};
for (size_t i = 0; i < 8; i++)
{
ImVec2 p0 =
ImVec2(start_pos.x + i * (box_size + spacing), start_pos.y + box_size + spacing);
ImVec2 p1 = ImVec2(p0.x + box_size, p0.y + box_size);
draw_list->AddRectFilled(p0, p1, ImGui::ColorConvertFloat4ToU32(bright_colors[i]));
draw_list->AddRect(p0, p1, ImGui::ColorConvertFloat4ToU32(border_col));
}
ImGui::Dummy(ImVec2(8 * (box_size + spacing), 2 * box_size + spacing + 4));
ImGui::PopStyleColor(2);
ImGui::EndChild();
}
void preview_renderer::render(const clrsync::core::palette &current)
{
if (current.colors().empty())
{
ImVec4 error_color = palette_utils::get_color(current, "error", "accent");
ImGui::TextColored(error_color, "Current palette is empty");
return;
}
render_code_preview();
render_terminal_preview(current);
}

View File

@@ -0,0 +1,22 @@
#ifndef CLRSYNC_GUI_PREVIEW_RENDERER_HPP
#define CLRSYNC_GUI_PREVIEW_RENDERER_HPP
#include "color_text_edit/TextEditor.h"
#include "core/palette/palette.hpp"
class preview_renderer
{
public:
preview_renderer();
void render(const clrsync::core::palette &palette);
void apply_palette(const clrsync::core::palette &palette);
private:
void render_code_preview();
void render_terminal_preview(const clrsync::core::palette &palette);
TextEditor m_editor;
};
#endif // CLRSYNC_GUI_PREVIEW_RENDERER_HPP

View File

@@ -0,0 +1,378 @@
#include "gui/views/settings_window.hpp"
#include "core/config/config.hpp"
#include "core/error.hpp"
#include "gui/helpers/imgui_helpers.hpp"
#include "gui/platform/file_browser.hpp"
#include "gui/platform/font_loader.hpp"
#include "imgui.h"
#include <cstring>
settings_window::settings_window()
: m_font_size(14), m_selected_font_idx(0), m_settings_changed(false), m_current_tab(0)
{
m_default_theme[0] = '\0';
m_palettes_path[0] = '\0';
m_font[0] = '\0';
font_loader loader;
m_available_fonts = loader.get_system_fonts();
load_settings();
}
void settings_window::render()
{
if (!m_visible)
return;
ImGui::SetNextWindowSize(ImVec2(700, 500), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_FirstUseEver,
ImVec2(0.5f, 0.5f));
ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoCollapse;
if (ImGui::Begin("Settings", &m_visible, window_flags))
{
if (ImGui::BeginTabBar("SettingsTabs", ImGuiTabBarFlags_None))
{
if (ImGui::BeginTabItem("General"))
{
render_general_tab();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Appearance"))
{
render_appearance_tab();
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
render_status_messages();
ImGui::Separator();
render_action_buttons();
}
ImGui::End();
}
void settings_window::load_settings()
{
auto &cfg = clrsync::core::config::instance();
std::string default_theme = cfg.default_theme();
strncpy(m_default_theme, default_theme.c_str(), sizeof(m_default_theme) - 1);
m_default_theme[sizeof(m_default_theme) - 1] = '\0';
std::string palettes_path = cfg.palettes_path();
strncpy(m_palettes_path, palettes_path.c_str(), sizeof(m_palettes_path) - 1);
m_palettes_path[sizeof(m_palettes_path) - 1] = '\0';
std::string font = cfg.font();
strncpy(m_font, font.c_str(), sizeof(m_font) - 1);
m_font[sizeof(m_font) - 1] = '\0';
m_selected_font_idx = 0;
for (int i = 0; i < static_cast<int>(m_available_fonts.size()); i++)
{
if (m_available_fonts[i] == font)
{
m_selected_font_idx = i;
break;
}
}
m_font_size = cfg.font_size();
m_error_message.clear();
m_settings_changed = false;
}
void settings_window::apply_settings()
{
auto &cfg = clrsync::core::config::instance();
if (strlen(m_default_theme) == 0)
{
m_error_message = "Default theme cannot be empty";
return;
}
if (strlen(m_palettes_path) == 0)
{
m_error_message = "Palettes path cannot be empty";
return;
}
if (strlen(m_font) == 0)
{
m_error_message = "Font cannot be empty";
return;
}
if (m_font_size < 8 || m_font_size > 48)
{
m_error_message = "Font size must be between 8 and 48";
return;
}
auto result1 = cfg.set_default_theme(m_default_theme);
if (!result1)
{
m_error_message = "Failed to set default theme: " + result1.error().description();
return;
}
auto result2 = cfg.set_palettes_path(m_palettes_path);
if (!result2)
{
m_error_message = "Failed to set palettes path: " + result2.error().description();
return;
}
auto result3 = cfg.set_font(m_font);
if (!result3)
{
m_error_message = "Failed to set font: " + result3.error().description();
return;
}
auto result4 = cfg.set_font_size(m_font_size);
if (!result4)
{
m_error_message = "Failed to set font size: " + result4.error().description();
return;
}
font_loader fn_loader;
auto font = fn_loader.load_font(m_font, m_font_size);
if (font)
ImGui::GetIO().FontDefault = font;
m_error_message.clear();
m_settings_changed = false;
}
void settings_window::render_general_tab()
{
ImGui::Spacing();
auto accent_color = palette_utils::get_color(m_current_palette, "accent");
ImGui::TextColored(accent_color, "Theme Settings");
ImGui::Separator();
ImGui::Spacing();
ImGui::Text("Default Theme:");
ImGui::SameLine();
show_help_marker("The default color scheme to load on startup");
ImGui::SetNextItemWidth(-100.0f);
if (ImGui::InputText("##default_theme", m_default_theme, sizeof(m_default_theme)))
m_settings_changed = true;
ImGui::Spacing();
ImGui::TextColored(accent_color, "Path Settings");
ImGui::Separator();
ImGui::Spacing();
ImGui::Text("Palettes Directory:");
ImGui::SameLine();
show_help_marker("Directory where color palettes are stored\nSupports ~ for home directory");
ImGui::SetNextItemWidth(-120.0f);
if (ImGui::InputText("##palettes_path", m_palettes_path, sizeof(m_palettes_path)))
m_settings_changed = true;
ImGui::SameLine();
if (ImGui::Button("Browse"))
{
std::string selected_path =
file_dialogs::select_folder_dialog("Select Palettes Directory", m_palettes_path);
if (!selected_path.empty())
{
strncpy(m_palettes_path, selected_path.c_str(), sizeof(m_palettes_path) - 1);
m_palettes_path[sizeof(m_palettes_path) - 1] = '\0';
m_settings_changed = true;
}
}
}
void settings_window::render_appearance_tab()
{
ImGui::Spacing();
auto accent_color = palette_utils::get_color(m_current_palette, "accent");
ImGui::TextColored(accent_color, "Font Settings");
ImGui::Separator();
ImGui::Spacing();
ImGui::Text("Font Family:");
ImGui::SameLine();
show_help_marker("Select font family for the application interface");
ImGui::SetNextItemWidth(-1.0f);
if (ImGui::BeginCombo("##font", m_font))
{
for (int i = 0; i < static_cast<int>(m_available_fonts.size()); i++)
{
bool is_selected = (i == m_selected_font_idx);
if (ImGui::Selectable(m_available_fonts[i].c_str(), is_selected))
{
m_selected_font_idx = i;
strncpy(m_font, m_available_fonts[i].c_str(), sizeof(m_font) - 1);
m_font[sizeof(m_font) - 1] = '\0';
m_settings_changed = true;
}
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
ImGui::Spacing();
ImGui::Text("Font Size:");
ImGui::SameLine();
show_help_marker("Font size for the application interface (8-48)");
ImGui::SetNextItemWidth(120.0f);
int old_size = m_font_size;
if (ImGui::SliderInt("##font_size", &m_font_size, 8, 48, "%d px"))
{
if (old_size != m_font_size)
m_settings_changed = true;
}
ImGui::SameLine();
if (ImGui::Button("Reset"))
{
m_font_size = 14;
m_settings_changed = true;
}
}
void settings_window::render_status_messages()
{
if (!m_error_message.empty())
{
ImGui::Spacing();
auto error_bg_color = palette_utils::get_color(m_current_palette, "error");
auto error_text_color = palette_utils::get_color(m_current_palette, "on_error");
ImGui::PushStyleColor(ImGuiCol_ChildBg, error_bg_color);
ImGui::PushStyleColor(ImGuiCol_Border, error_bg_color);
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 4.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 1.0f);
if (ImGui::BeginChild("##error_box", ImVec2(0, 0),
ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_Borders))
{
ImGui::PushStyleColor(ImGuiCol_Text, error_text_color);
ImGui::TextWrapped("Error: %s", m_error_message.c_str());
ImGui::PopStyleColor();
ImGui::Spacing();
ImGui::PushStyleColor(ImGuiCol_Button,
ImVec4(error_bg_color.x * 0.8f, error_bg_color.y * 0.8f,
error_bg_color.z * 0.8f, error_bg_color.w));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered,
ImVec4(error_bg_color.x * 0.6f, error_bg_color.y * 0.6f,
error_bg_color.z * 0.6f, error_bg_color.w));
ImGui::PushStyleColor(ImGuiCol_Text, error_text_color);
if (ImGui::Button("Dismiss##error"))
m_error_message.clear();
ImGui::PopStyleColor(3);
}
ImGui::EndChild();
ImGui::PopStyleVar(2);
ImGui::PopStyleColor(2);
}
}
void settings_window::render_action_buttons()
{
ImGui::Spacing();
float button_width = 100.0f;
float spacing = ImGui::GetStyle().ItemSpacing.x;
float window_width = ImGui::GetContentRegionAvail().x;
float total_buttons_width = 4 * button_width + 3 * spacing;
float start_pos = (window_width - total_buttons_width) * 0.5f;
if (start_pos > 0)
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + start_pos);
if (ImGui::Button("OK", ImVec2(button_width, 0)))
{
apply_settings();
if (m_error_message.empty())
{
m_visible = false;
m_settings_changed = false;
}
}
ImGui::SameLine();
bool apply_disabled = !m_settings_changed;
if (apply_disabled)
{
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.5f);
}
if (ImGui::Button("Apply", ImVec2(button_width, 0)) && !apply_disabled)
{
apply_settings();
if (m_error_message.empty())
{
m_settings_changed = false;
}
}
if (apply_disabled)
{
ImGui::PopStyleVar();
}
ImGui::SameLine();
if (ImGui::Button("Reset", ImVec2(button_width, 0)))
{
reset_to_defaults();
}
ImGui::SameLine();
if (ImGui::Button("Cancel", ImVec2(button_width, 0)))
{
load_settings();
m_visible = false;
m_error_message.clear();
m_settings_changed = false;
}
}
void settings_window::show_help_marker(const char *desc)
{
ImGui::TextDisabled("(?)");
if (ImGui::BeginItemTooltip())
{
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
void settings_window::reset_to_defaults()
{
strncpy(m_default_theme, "dark", sizeof(m_default_theme));
strncpy(m_palettes_path, "~/.config/clrsync/palettes", sizeof(m_palettes_path));
strncpy(m_font, "JetBrains Mono Nerd Font", sizeof(m_font));
m_font_size = 14;
m_error_message.clear();
m_settings_changed = true;
}

View File

@@ -0,0 +1,60 @@
#ifndef CLRSYNC_GUI_SETTINGS_WINDOW_HPP
#define CLRSYNC_GUI_SETTINGS_WINDOW_HPP
#include "core/palette/palette.hpp"
#include <string>
#include <vector>
class settings_window
{
public:
settings_window();
void render();
void show()
{
m_visible = true;
}
void hide()
{
m_visible = false;
}
bool is_visible() const
{
return m_visible;
}
private:
void load_settings();
void save_settings();
void apply_settings();
void render_general_tab();
void render_appearance_tab();
void render_status_messages();
void render_action_buttons();
void show_help_marker(const char *desc);
void reset_to_defaults();
public:
void set_palette(const clrsync::core::palette &palette)
{
m_current_palette = palette;
}
bool m_visible{false};
char m_default_theme[128];
char m_palettes_path[512];
char m_font[128];
int m_font_size;
std::vector<std::string> m_available_fonts;
int m_selected_font_idx;
std::string m_error_message;
bool m_settings_changed;
int m_current_tab;
clrsync::core::palette m_current_palette;
};
#endif // CLRSYNC_GUI_SETTINGS_WINDOW_HPP

View File

@@ -0,0 +1,954 @@
#include "template_editor.hpp"
#include "core/config/config.hpp"
#include "core/palette/color_keys.hpp"
#include "core/theme/theme_template.hpp"
#include "core/utils.hpp"
#include "gui/helpers/imgui_helpers.hpp"
#include "gui/platform/file_browser.hpp"
#include "imgui.h"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <ranges>
namespace
{
const std::vector<std::string> COLOR_FORMATS = {
"hex", "hex_stripped", "hexa", "hexa_stripped", "r", "g", "b", "a", "rgb", "rgba", "h", "s",
"l", "hsl", "hsla"};
}
template_editor::template_editor() : m_template_name("new_template")
{
m_autocomplete_bg_color = ImVec4(0.12f, 0.12f, 0.15f, 0.98f);
m_autocomplete_border_color = ImVec4(0.4f, 0.4f, 0.45f, 1.0f);
m_autocomplete_selected_color = ImVec4(0.25f, 0.45f, 0.75f, 0.9f);
m_autocomplete_text_color = ImVec4(0.85f, 0.85f, 0.9f, 1.0f);
m_autocomplete_selected_text_color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
m_autocomplete_dim_text_color = ImVec4(0.6f, 0.6f, 0.7f, 1.0f);
TextEditor::LanguageDefinition lang;
lang.mName = "Template";
lang.mCommentStart = "/*";
lang.mCommentEnd = "*/";
lang.mSingleLineComment = "#";
lang.mTokenRegexStrings.push_back(std::make_pair<std::string, TextEditor::PaletteIndex>(
"\\{[a-zA-Z_][a-zA-Z0-9_]*(\\.[a-zA-Z_][a-zA-Z0-9_]*)?\\}",
TextEditor::PaletteIndex::KnownIdentifier));
lang.mTokenRegexStrings.push_back(std::make_pair<std::string, TextEditor::PaletteIndex>(
"\"([^\"]*)\"", TextEditor::PaletteIndex::String));
lang.mTokenRegexStrings.push_back(std::make_pair<std::string, TextEditor::PaletteIndex>(
"'([^']*)'", TextEditor::PaletteIndex::String));
m_editor.SetLanguageDefinition(lang);
m_editor.SetText("# Enter your template here\n# Use {color_key} for color variables\n# "
"Examples: {color.hex}, {color.rgb}, {color.r}\n\n");
m_editor.SetShowWhitespaces(false);
}
void template_editor::apply_current_palette(const clrsync::core::palette &pal)
{
m_current_palette = pal;
auto colors = pal.colors();
if (colors.empty())
return;
auto get_color_u32 = [&](const std::string &key, const std::string &fallback = "") -> uint32_t {
return palette_utils::get_color_u32(pal, key, fallback);
};
auto palette = m_editor.GetPalette();
palette[int(TextEditor::PaletteIndex::Default)] = get_color_u32("editor_main", "foreground");
palette[int(TextEditor::PaletteIndex::Keyword)] = get_color_u32("editor_command", "accent");
palette[int(TextEditor::PaletteIndex::Number)] = get_color_u32("editor_warning", "warning");
palette[int(TextEditor::PaletteIndex::String)] = get_color_u32("editor_string", "success");
palette[int(TextEditor::PaletteIndex::CharLiteral)] = get_color_u32("editor_string", "success");
palette[int(TextEditor::PaletteIndex::Punctuation)] =
get_color_u32("editor_main", "foreground");
palette[int(TextEditor::PaletteIndex::Preprocessor)] =
get_color_u32("editor_emphasis", "accent");
palette[int(TextEditor::PaletteIndex::Identifier)] = get_color_u32("editor_main", "foreground");
palette[int(TextEditor::PaletteIndex::KnownIdentifier)] = get_color_u32("editor_link", "info");
palette[int(TextEditor::PaletteIndex::PreprocIdentifier)] =
get_color_u32("editor_link", "info");
palette[int(TextEditor::PaletteIndex::Comment)] =
get_color_u32("editor_comment", "editor_inactive");
palette[int(TextEditor::PaletteIndex::MultiLineComment)] =
get_color_u32("editor_comment", "editor_inactive");
palette[int(TextEditor::PaletteIndex::Background)] =
get_color_u32("editor_background", "background");
palette[int(TextEditor::PaletteIndex::Cursor)] = get_color_u32("cursor", "accent");
palette[int(TextEditor::PaletteIndex::Selection)] =
get_color_u32("editor_selected", "surface_variant");
palette[int(TextEditor::PaletteIndex::ErrorMarker)] = get_color_u32("editor_error", "error");
palette[int(TextEditor::PaletteIndex::Breakpoint)] = get_color_u32("editor_error", "error");
palette[int(TextEditor::PaletteIndex::LineNumber)] =
get_color_u32("editor_line_number", "editor_inactive");
palette[int(TextEditor::PaletteIndex::CurrentLineFill)] = get_color_u32("surface_variant");
palette[int(TextEditor::PaletteIndex::CurrentLineFillInactive)] = get_color_u32("surface");
palette[int(TextEditor::PaletteIndex::CurrentLineEdge)] =
get_color_u32("border_focused", "border");
m_editor.SetPalette(palette);
m_autocomplete_bg_color = palette_utils::get_color(pal, "surface", "background");
m_autocomplete_bg_color.w = 0.98f;
m_autocomplete_border_color = palette_utils::get_color(pal, "border", "surface_variant");
m_autocomplete_selected_color = palette_utils::get_color(pal, "accent", "surface_variant");
m_autocomplete_text_color = palette_utils::get_color(pal, "on_surface", "foreground");
m_autocomplete_selected_text_color = palette_utils::get_color(pal, "on_surface", "foreground");
m_autocomplete_dim_text_color =
palette_utils::get_color(pal, "on_surface_variant", "editor_inactive");
}
void template_editor::update_autocomplete_suggestions()
{
m_autocomplete_suggestions.clear();
auto cursor = m_editor.GetCursorPosition();
std::string line = m_editor.GetCurrentLineText();
int col = cursor.mColumn;
// Check if inside '{'
int brace_pos = -1;
for (int i = col - 1; i >= 0; --i)
{
if (i < (int)line.length())
{
if (line[i] == '{')
{
brace_pos = i;
break;
}
else if (line[i] == '}' || line[i] == ' ' || line[i] == '\t')
{
break;
}
}
}
if (brace_pos < 0)
{
m_show_autocomplete = false;
m_autocomplete_dismissed = false;
return;
}
if (m_autocomplete_dismissed)
{
bool should_reset_dismissal = false;
if (cursor.mLine != m_dismiss_position.mLine || brace_pos != m_dismiss_brace_pos ||
abs(cursor.mColumn - m_dismiss_position.mColumn) > 3)
{
should_reset_dismissal = true;
}
if (should_reset_dismissal)
{
m_autocomplete_dismissed = false;
}
else
{
m_show_autocomplete = false;
return;
}
}
m_autocomplete_prefix = line.substr(brace_pos + 1, col - brace_pos - 1);
m_autocomplete_start_pos = TextEditor::Coordinates(cursor.mLine, brace_pos + 1);
size_t dot_pos = m_autocomplete_prefix.find('.');
if (dot_pos != std::string::npos)
{
std::string color_key = m_autocomplete_prefix.substr(0, dot_pos);
std::string format_prefix = m_autocomplete_prefix.substr(dot_pos + 1);
bool valid_key = false;
for (size_t i = 0; i < clrsync::core::NUM_COLOR_KEYS; ++i)
{
if (clrsync::core::COLOR_KEYS[i] == color_key)
{
valid_key = true;
break;
}
}
if (valid_key)
{
for (const auto &fmt : COLOR_FORMATS)
{
if (format_prefix.empty() || fmt.find(format_prefix) == 0 ||
fmt.find(format_prefix) != std::string::npos)
{
m_autocomplete_suggestions.push_back(color_key + "." + fmt);
}
}
}
}
else
{
for (size_t i = 0; i < clrsync::core::NUM_COLOR_KEYS; ++i)
{
std::string key = clrsync::core::COLOR_KEYS[i];
if (m_autocomplete_prefix.empty() || key.find(m_autocomplete_prefix) == 0 ||
key.find(m_autocomplete_prefix) != std::string::npos)
{
m_autocomplete_suggestions.push_back(key);
}
}
}
std::sort(m_autocomplete_suggestions.begin(), m_autocomplete_suggestions.end(),
[this](const std::string &a, const std::string &b) {
bool a_prefix = a.find(m_autocomplete_prefix) == 0;
bool b_prefix = b.find(m_autocomplete_prefix) == 0;
if (a_prefix != b_prefix)
return a_prefix;
return a < b;
});
m_show_autocomplete = !m_autocomplete_suggestions.empty();
if (m_show_autocomplete && m_autocomplete_selected >= (int)m_autocomplete_suggestions.size())
{
m_autocomplete_selected = 0;
}
}
void template_editor::render_autocomplete(const ImVec2 &editor_pos)
{
if (!m_show_autocomplete || m_autocomplete_suggestions.empty())
return;
float line_height = ImGui::GetTextLineHeightWithSpacing();
float char_width = ImGui::GetFontSize() * 0.5f;
auto cursor = m_editor.GetCursorPosition();
const float line_number_width = 50.0f;
ImVec2 popup_pos;
popup_pos.x =
editor_pos.x + line_number_width + (m_autocomplete_start_pos.mColumn * char_width);
popup_pos.y = editor_pos.y + ((cursor.mLine + 1) * line_height);
ImGui::SetNextWindowPos(popup_pos, ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(300, 0));
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoFocusOnAppearing |
ImGuiWindowFlags_AlwaysAutoResize;
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(6, 6));
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 6.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 2));
ImGui::PushStyleColor(ImGuiCol_WindowBg, m_autocomplete_bg_color);
ImGui::PushStyleColor(ImGuiCol_Border, m_autocomplete_border_color);
if (ImGui::Begin("##autocomplete", nullptr, flags))
{
ImGui::PushStyleColor(ImGuiCol_Text, m_autocomplete_dim_text_color);
if (m_autocomplete_prefix.find('.') != std::string::npos)
ImGui::Text("Formats");
else
ImGui::Text("Color Keys");
ImGui::PopStyleColor();
ImGui::Separator();
int max_items = std::min((int)m_autocomplete_suggestions.size(), 8);
for (int i = 0; i < max_items; ++i)
{
const auto &suggestion = m_autocomplete_suggestions[i];
bool is_selected = (i == m_autocomplete_selected);
if (is_selected)
{
ImVec4 selected_hover = m_autocomplete_selected_color;
selected_hover.w = std::min(selected_hover.w + 0.1f, 1.0f);
ImGui::PushStyleColor(ImGuiCol_Header, m_autocomplete_selected_color);
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, selected_hover);
ImGui::PushStyleColor(ImGuiCol_Text, m_autocomplete_selected_text_color);
}
else
{
ImVec4 normal_bg = m_autocomplete_bg_color;
normal_bg.w = 0.5f;
ImVec4 hover_bg = m_autocomplete_selected_color;
hover_bg.w = 0.3f;
ImGui::PushStyleColor(ImGuiCol_Header, normal_bg);
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, hover_bg);
ImGui::PushStyleColor(ImGuiCol_Text, m_autocomplete_text_color);
}
std::string display_text = " " + suggestion;
if (ImGui::Selectable(display_text.c_str(), is_selected, ImGuiSelectableFlags_None,
ImVec2(0, 0)))
{
auto start = m_autocomplete_start_pos;
auto end = m_editor.GetCursorPosition();
m_editor.SetSelection(start, end);
m_editor.Delete();
m_editor.InsertText(suggestion + "}");
m_show_autocomplete = false;
m_autocomplete_dismissed = false;
}
ImGui::PopStyleColor(3);
if (is_selected && ImGui::IsWindowAppearing())
{
ImGui::SetScrollHereY();
}
}
if (m_autocomplete_suggestions.size() > 8)
{
ImGui::Separator();
ImGui::PushStyleColor(ImGuiCol_Text, m_autocomplete_dim_text_color);
ImGui::Text(" +%d more", (int)m_autocomplete_suggestions.size() - 8);
ImGui::PopStyleColor();
}
ImGui::Separator();
ImGui::PushStyleColor(ImGuiCol_Text, m_autocomplete_dim_text_color);
ImGui::Text(" Tab/Enter: accept | Esc: dismiss");
ImGui::PopStyleColor();
}
ImGui::End();
ImGui::PopStyleColor(2);
ImGui::PopStyleVar(3);
}
void template_editor::render()
{
ImGui::Begin("Templates");
render_controls();
ImGui::Separator();
const float panel_width = ImGui::GetContentRegionAvail().x;
constexpr float left_panel_width = 200.0f;
const float right_panel_width = panel_width - left_panel_width - 10;
ImGui::BeginChild("TemplateList", ImVec2(left_panel_width, 0), true);
render_template_list();
ImGui::EndChild();
ImGui::SameLine();
ImGui::BeginChild("EditorPanel", ImVec2(right_panel_width, 0), false);
render_editor();
ImGui::EndChild();
if (m_show_delete_confirmation)
{
ImGui::OpenPopup("Delete Template?");
m_show_delete_confirmation = false;
}
palette_utils::render_delete_confirmation_popup(
"Delete Template?", m_template_name, "template", m_current_palette, [this]() {
bool success = m_template_controller.remove_template(m_template_name);
if (success)
{
new_template();
refresh_templates();
}
else
{
m_validation_error = "Failed to delete template";
}
});
ImGui::End();
}
void template_editor::render_controls()
{
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 8));
if (ImGui::Button(" + New "))
{
new_template();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Create a new template");
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) &&
ImGui::IsKeyDown(ImGuiKey_LeftCtrl) && ImGui::IsKeyPressed(ImGuiKey_S))
{
save_template();
}
ImGui::SameLine();
if (ImGui::Button(" Save "))
{
save_template();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Save template (Ctrl+S)");
if (m_is_editing_existing)
{
ImGui::SameLine();
auto error = palette_utils::get_color(m_current_palette, "error");
auto error_hover = ImVec4(error.x * 1.1f, error.y * 1.1f, error.z * 1.1f, error.w);
auto error_active = ImVec4(error.x * 0.8f, error.y * 0.8f, error.z * 0.8f, error.w);
auto on_error = palette_utils::get_color(m_current_palette, "on_error");
ImGui::PushStyleColor(ImGuiCol_Button, error);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, error_hover);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, error_active);
ImGui::PushStyleColor(ImGuiCol_Text, on_error);
if (ImGui::Button(" Delete "))
{
delete_template();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Delete this template");
ImGui::PopStyleColor(4);
}
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 10);
bool enabled_changed = false;
if (m_enabled)
{
ImVec4 success_color = palette_utils::get_color(m_current_palette, "success", "accent");
ImVec4 success_on_color =
palette_utils::get_color(m_current_palette, "on_success", "on_surface");
ImVec4 success_hover =
ImVec4(success_color.x * 1.2f, success_color.y * 1.2f, success_color.z * 1.2f, 0.6f);
ImGui::PushStyleColor(ImGuiCol_FrameBg,
ImVec4(success_color.x, success_color.y, success_color.z, 0.5f));
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, success_hover);
ImGui::PushStyleColor(ImGuiCol_CheckMark, success_on_color);
}
else
{
ImVec4 error_color = palette_utils::get_color(m_current_palette, "error", "accent");
ImVec4 error_on_color =
palette_utils::get_color(m_current_palette, "on_error", "on_surface");
ImVec4 error_hover =
ImVec4(error_color.x * 1.2f, error_color.y * 1.2f, error_color.z * 1.2f, 0.6f);
ImGui::PushStyleColor(ImGuiCol_FrameBg,
ImVec4(error_color.x, error_color.y, error_color.z, 0.5f));
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, error_hover);
ImGui::PushStyleColor(ImGuiCol_CheckMark, error_on_color);
}
enabled_changed = ImGui::Checkbox("Enabled", &m_enabled);
ImGui::PopStyleColor(3);
if (enabled_changed && m_is_editing_existing)
{
m_template_controller.set_template_enabled(m_template_name, m_enabled);
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Enable/disable this template for theme application");
ImGui::PopStyleVar();
ImGui::Spacing();
ImGui::AlignTextToFramePadding();
ImGui::Text("Name:");
ImGui::SameLine(80);
ImGui::SetNextItemWidth(180.0f);
char name_buf[256] = {0};
snprintf(name_buf, sizeof(name_buf), "%s", m_template_name.c_str());
if (ImGui::InputText("##template_name", name_buf, sizeof(name_buf)))
{
m_template_name = name_buf;
if (!m_template_name.empty())
{
m_validation_error = "";
}
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Unique name for this template");
ImGui::AlignTextToFramePadding();
ImGui::Text("Input:");
ImGui::SameLine(80);
ImGui::SetNextItemWidth(-120.0f);
char input_path_buf[512] = {0};
snprintf(input_path_buf, sizeof(input_path_buf), "%s", m_input_path.c_str());
if (ImGui::InputTextWithHint("##input_path", "Path to template file...", input_path_buf,
sizeof(input_path_buf)))
{
m_input_path = input_path_buf;
if (!m_input_path.empty())
{
m_validation_error = "";
}
if (m_is_editing_existing)
{
m_template_controller.set_template_input_path(m_template_name, m_input_path);
}
}
ImGui::SameLine();
if (ImGui::Button("Browse##input"))
{
std::string selected_path =
file_dialogs::open_file_dialog("Select Template File", m_input_path);
if (!selected_path.empty())
{
m_input_path = selected_path;
if (m_is_editing_existing)
{
m_template_controller.set_template_input_path(m_template_name, m_input_path);
}
m_validation_error = "";
}
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Path where the template source file is stored");
ImGui::AlignTextToFramePadding();
ImGui::Text("Output:");
ImGui::SameLine(80);
ImGui::SetNextItemWidth(-120.0f);
char path_buf[512] = {0};
snprintf(path_buf, sizeof(path_buf), "%s", m_output_path.c_str());
if (ImGui::InputTextWithHint("##output_path", "Path for generated config...", path_buf,
sizeof(path_buf)))
{
m_output_path = path_buf;
if (!m_output_path.empty())
{
m_validation_error = "";
}
if (m_is_editing_existing)
{
m_template_controller.set_template_output_path(m_template_name, m_output_path);
}
}
ImGui::SameLine();
if (ImGui::Button("Browse##output"))
{
std::string selected_path =
file_dialogs::save_file_dialog("Select Output File", m_output_path);
if (!selected_path.empty())
{
m_output_path = selected_path;
if (m_is_editing_existing)
{
m_template_controller.set_template_output_path(m_template_name, m_output_path);
}
m_validation_error = "";
}
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Path where the processed config will be written");
ImGui::AlignTextToFramePadding();
ImGui::Text("Reload:");
ImGui::SameLine(80);
ImGui::SetNextItemWidth(-FLT_MIN);
char reload_buf[512] = {0};
snprintf(reload_buf, sizeof(reload_buf), "%s", m_reload_command.c_str());
if (ImGui::InputTextWithHint("##reload_cmd", "Command to reload app (optional)...", reload_buf,
sizeof(reload_buf)))
{
m_reload_command = reload_buf;
if (m_is_editing_existing)
{
m_template_controller.set_template_reload_command(m_template_name, m_reload_command);
}
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Shell command to run after applying theme (e.g., 'pkill -USR1 kitty')");
if (!m_validation_error.empty())
{
ImGui::Spacing();
ImVec4 error_color = palette_utils::get_color(m_current_palette, "error", "accent");
ImGui::PushStyleColor(ImGuiCol_Text, error_color);
ImGui::TextWrapped("%s", m_validation_error.c_str());
ImGui::PopStyleColor();
}
}
void template_editor::render_editor()
{
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(8, 4));
if (!m_is_editing_existing)
{
ImVec4 success_color = palette_utils::get_color(m_current_palette, "success", "accent");
ImGui::PushStyleColor(ImGuiCol_Text, success_color);
ImGui::Text(" New Template");
ImGui::PopStyleColor();
}
else
{
ImGui::Text(" %s", m_template_name.c_str());
auto trim_right = [](const std::string &s) -> std::string {
size_t end = s.find_last_not_of("\r\n");
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
};
std::string current_content = trim_right(m_editor.GetText());
std::string saved_content = trim_right(m_saved_content);
m_has_unsaved_changes = (current_content != saved_content);
if (m_has_unsaved_changes)
{
ImGui::SameLine();
ImVec4 warning_color = palette_utils::get_color(m_current_palette, "warning", "accent");
ImGui::PushStyleColor(ImGuiCol_Text, warning_color);
ImGui::Text("(unsaved)");
ImGui::PopStyleColor();
}
}
ImGui::PopStyleVar();
ImGui::Separator();
bool consume_keys = false;
if (m_show_autocomplete && ImGui::IsKeyPressed(ImGuiKey_Escape, false))
{
m_show_autocomplete = false;
m_autocomplete_dismissed = true;
m_dismiss_position = m_editor.GetCursorPosition();
std::string line = m_editor.GetCurrentLineText();
m_dismiss_brace_pos = -1;
for (int i = m_dismiss_position.mColumn - 1; i >= 0; --i)
{
if (i < (int)line.length() && line[i] == '{')
{
m_dismiss_brace_pos = i;
break;
}
}
consume_keys = true;
}
else if (m_show_autocomplete && !m_autocomplete_suggestions.empty())
{
int max_visible = std::min((int)m_autocomplete_suggestions.size(), 8);
if (ImGui::IsKeyPressed(ImGuiKey_DownArrow, false))
{
m_autocomplete_selected = (m_autocomplete_selected + 1) % max_visible;
consume_keys = true;
}
else if (ImGui::IsKeyPressed(ImGuiKey_UpArrow, false))
{
m_autocomplete_selected = (m_autocomplete_selected - 1 + max_visible) % max_visible;
consume_keys = true;
}
else if (ImGui::IsKeyPressed(ImGuiKey_Tab, false) ||
ImGui::IsKeyPressed(ImGuiKey_Enter, false))
{
auto start = m_autocomplete_start_pos;
auto end = m_editor.GetCursorPosition();
m_editor.SetSelection(start, end);
m_editor.Delete();
m_editor.InsertText(m_autocomplete_suggestions[m_autocomplete_selected] + "}");
m_show_autocomplete = false;
m_autocomplete_dismissed = false;
consume_keys = true;
}
}
if (consume_keys)
{
m_editor.SetHandleKeyboardInputs(false);
}
ImVec2 editor_pos = ImGui::GetCursorScreenPos();
m_editor.Render("##TemplateEditor", ImVec2(0, 0), true);
if (consume_keys)
{
m_editor.SetHandleKeyboardInputs(true);
}
update_autocomplete_suggestions();
render_autocomplete(editor_pos);
}
void template_editor::render_template_list()
{
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(8, 6));
ImGui::Text("Templates");
ImGui::SameLine(ImGui::GetContentRegionAvail().x - 20);
ImGui::TextDisabled("(%d)", (int)m_template_controller.templates().size());
ImGui::Separator();
if (!m_is_editing_existing)
{
ImVec4 success_color = palette_utils::get_color(m_current_palette, "success", "accent");
ImVec4 success_bg = ImVec4(success_color.x, success_color.y, success_color.z, 0.5f);
ImGui::PushStyleColor(ImGuiCol_Text, success_color);
ImGui::PushStyleColor(ImGuiCol_Header, success_bg);
ImGui::Selectable("+ New Template", true);
ImGui::PopStyleColor(2);
ImGui::Separator();
}
const auto &templates = m_template_controller.templates();
for (const auto &[key, tmpl] : templates)
{
const bool selected = (m_template_name == key && m_is_editing_existing);
if (!tmpl.enabled())
{
ImVec4 disabled_color = palette_utils::get_color(
m_current_palette, "on_surface_variant", "editor_inactive");
ImGui::PushStyleColor(ImGuiCol_Text, disabled_color);
}
if (ImGui::Selectable(key.c_str(), selected))
{
load_template(key);
}
if (!tmpl.enabled())
{
ImGui::PopStyleColor();
}
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::Text("Template: %s", key.c_str());
ImGui::Separator();
ImGui::Text("Status: %s", tmpl.enabled() ? "Enabled" : "Disabled");
if (!tmpl.output_path().empty())
ImGui::Text("Output: %s", tmpl.output_path().c_str());
ImGui::EndTooltip();
}
}
ImGui::PopStyleVar();
}
bool template_editor::is_valid_path(const std::string &path)
{
const std::string invalid_chars = "<>|\"";
for (const char c : invalid_chars)
{
if (path.find(c) != std::string::npos)
{
return false;
}
}
if (path.find_first_not_of(" \t\n\r./\\") == std::string::npos)
{
return false;
}
try
{
std::filesystem::path fs_path(path);
const auto parent = fs_path.parent_path();
if (parent.empty())
{
return false;
}
if (!parent.empty() && !std::filesystem::exists(parent))
{
if (parent.string().find_first_of("<>|\"") != std::string::npos)
{
return false;
}
}
const auto filename = fs_path.filename().string();
if (filename.empty() || filename == "." || filename == "..")
{
return false;
}
return true;
}
catch (...)
{
return false;
}
}
void template_editor::save_template()
{
std::string trimmed_name = m_template_name;
trimmed_name.erase(0, trimmed_name.find_first_not_of(" \t\n\r"));
trimmed_name.erase(trimmed_name.find_last_not_of(" \t\n\r") + 1);
std::string trimmed_input_path = m_input_path;
trimmed_input_path.erase(0, trimmed_input_path.find_first_not_of(" \t\n\r"));
trimmed_input_path.erase(trimmed_input_path.find_last_not_of(" \t\n\r") + 1);
std::string trimmed_path = m_output_path;
trimmed_path.erase(0, trimmed_path.find_first_not_of(" \t\n\r"));
trimmed_path.erase(trimmed_path.find_last_not_of(" \t\n\r") + 1);
if (trimmed_name.empty())
{
m_validation_error = "Error: Template name cannot be empty!";
return;
}
if (trimmed_input_path.empty())
{
m_validation_error = "Error: Input path cannot be empty!";
return;
}
if (trimmed_path.empty())
{
m_validation_error = "Error: Output path cannot be empty!";
return;
}
if (!is_valid_path(trimmed_path))
{
m_validation_error =
"Error: Output path is invalid! Must be a valid file path with directory.";
return;
}
m_validation_error = "";
auto &cfg = clrsync::core::config::instance();
std::filesystem::path template_file = clrsync::core::normalize_path(trimmed_input_path);
auto parent_dir = template_file.parent_path();
if (!parent_dir.empty() && !std::filesystem::exists(parent_dir))
{
try
{
std::filesystem::create_directories(parent_dir);
}
catch (const std::exception &e)
{
m_validation_error = "Error: Could not create directory for input path";
return;
}
}
std::string template_content = m_editor.GetText();
std::ofstream out(template_file);
if (!out.is_open())
{
m_validation_error = "Failed to write template file";
return;
}
out << template_content;
out.close();
clrsync::core::theme_template tmpl(trimmed_name, template_file.string(), trimmed_path);
tmpl.set_reload_command(m_reload_command);
tmpl.set_enabled(m_enabled);
auto result = cfg.update_template(trimmed_name, tmpl);
if (!result)
{
m_validation_error = "Error saving template: " + result.error().description();
return;
}
m_template_name = trimmed_name;
m_input_path = trimmed_input_path;
m_output_path = trimmed_path;
m_is_editing_existing = true;
m_saved_content = m_editor.GetText();
m_has_unsaved_changes = false;
refresh_templates();
}
void template_editor::load_template(const std::string &name)
{
const auto &templates = m_template_controller.templates();
auto it = templates.find(name);
if (it != templates.end())
{
const auto &tmpl = it->second;
m_template_name = name;
m_input_path = tmpl.template_path();
m_output_path = tmpl.output_path();
m_reload_command = tmpl.reload_command();
m_enabled = tmpl.enabled();
m_is_editing_existing = true;
m_validation_error = "";
std::ifstream in(tmpl.template_path());
if (in.is_open())
{
std::string content;
std::string line;
while (std::getline(in, line))
{
content += line + "\n";
}
in.close();
m_editor.SetText(content);
m_saved_content = content;
m_has_unsaved_changes = false;
}
else
{
m_validation_error = "Error loading template: Failed to open file";
}
}
}
void template_editor::new_template()
{
m_template_name = "new_template";
std::string default_content =
"# Enter your template here\n# Use {color_key} for color variables\n# "
"Examples: {color.hex}, {color.rgb}, {color.r}\n\n";
m_editor.SetText(default_content);
m_saved_content = default_content;
m_input_path = "";
m_output_path = "";
m_reload_command = "";
m_enabled = true;
m_is_editing_existing = false;
m_validation_error = "";
m_has_unsaved_changes = false;
}
void template_editor::delete_template()
{
if (!m_is_editing_existing || m_template_name.empty())
return;
m_show_delete_confirmation = true;
}
void template_editor::refresh_templates()
{
m_template_controller.refresh();
}

View File

@@ -0,0 +1,67 @@
#ifndef CLRSYNC_GUI_TEMPLATE_EDITOR_HPP
#define CLRSYNC_GUI_TEMPLATE_EDITOR_HPP
#include "color_text_edit/TextEditor.h"
#include "gui/controllers/template_controller.hpp"
#include "imgui.h"
#include <core/palette/palette.hpp>
#include <string>
#include <vector>
class template_editor
{
public:
template_editor();
void render();
void apply_current_palette(const clrsync::core::palette &pal);
private:
void render_controls();
void render_editor();
void render_template_list();
void render_autocomplete(const ImVec2 &editor_pos);
void update_autocomplete_suggestions();
void save_template();
void load_template(const std::string &name);
void new_template();
void delete_template();
void refresh_templates();
bool is_valid_path(const std::string &path);
template_controller m_template_controller;
TextEditor m_editor;
std::string m_template_name;
std::string m_input_path;
std::string m_output_path;
std::string m_reload_command;
std::string m_validation_error;
std::string m_saved_content;
bool m_has_unsaved_changes = false;
bool m_enabled{true};
bool m_is_editing_existing{false};
bool m_show_delete_confirmation{false};
bool m_show_autocomplete{false};
bool m_autocomplete_dismissed{false};
TextEditor::Coordinates m_dismiss_position;
int m_dismiss_brace_pos{-1};
std::vector<std::string> m_autocomplete_suggestions;
int m_autocomplete_selected{0};
std::string m_autocomplete_prefix;
TextEditor::Coordinates m_autocomplete_start_pos;
ImVec4 m_autocomplete_bg_color;
ImVec4 m_autocomplete_border_color;
ImVec4 m_autocomplete_selected_color;
ImVec4 m_autocomplete_text_color;
ImVec4 m_autocomplete_selected_text_color;
ImVec4 m_autocomplete_dim_text_color;
clrsync::core::palette m_current_palette;
};
#endif // CLRSYNC_GUI_TEMPLATE_EDITOR_HPP