mirror of
https://github.com/obsqrbtz/clrsync.git
synced 2026-04-09 12:37:41 +03:00
chore: structured src/gui, run clang-format
This commit is contained in:
80
src/gui/controllers/palette_controller.cpp
Normal file
80
src/gui/controllers/palette_controller.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "gui/controllers/palette_controller.hpp"
|
||||
#include "core/config/config.hpp"
|
||||
#include "core/theme/theme_renderer.hpp"
|
||||
|
||||
palette_controller::palette_controller()
|
||||
{
|
||||
m_palette_manager.load_palettes_from_directory(
|
||||
clrsync::core::config::instance().palettes_path());
|
||||
m_palettes = m_palette_manager.palettes();
|
||||
|
||||
if (m_palettes.empty())
|
||||
return;
|
||||
|
||||
auto default_theme = clrsync::core::config::instance().default_theme();
|
||||
auto it = m_palettes.find(default_theme);
|
||||
if (it != m_palettes.end())
|
||||
{
|
||||
m_current_palette = it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_current_palette = m_palettes.begin()->second;
|
||||
}
|
||||
}
|
||||
|
||||
void palette_controller::select_palette(const std::string &name)
|
||||
{
|
||||
auto it = m_palettes.find(name);
|
||||
if (it != m_palettes.end())
|
||||
{
|
||||
m_current_palette = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
void palette_controller::create_palette(const std::string &name)
|
||||
{
|
||||
clrsync::core::palette new_palette(name);
|
||||
|
||||
for (const auto &[key, hex_value] : clrsync::core::DEFAULT_COLORS)
|
||||
{
|
||||
new_palette.set_color(key, clrsync::core::color(hex_value));
|
||||
}
|
||||
|
||||
auto dir = clrsync::core::config::instance().palettes_path();
|
||||
m_palette_manager.save_palette_to_file(new_palette, dir);
|
||||
|
||||
reload_palettes();
|
||||
m_current_palette = new_palette;
|
||||
}
|
||||
|
||||
void palette_controller::save_current_palette()
|
||||
{
|
||||
auto dir = clrsync::core::config::instance().palettes_path();
|
||||
m_palette_manager.save_palette_to_file(m_current_palette, dir);
|
||||
reload_palettes();
|
||||
}
|
||||
|
||||
void palette_controller::delete_current_palette()
|
||||
{
|
||||
m_palette_manager.delete_palette(m_current_palette.file_path(), m_current_palette.name());
|
||||
reload_palettes();
|
||||
}
|
||||
|
||||
void palette_controller::apply_current_theme() const
|
||||
{
|
||||
clrsync::core::theme_renderer<clrsync::core::io::toml_file> theme_renderer;
|
||||
(void)theme_renderer.apply_theme(m_current_palette.name());
|
||||
}
|
||||
|
||||
void palette_controller::set_color(const std::string &key, const clrsync::core::color &color)
|
||||
{
|
||||
m_current_palette.set_color(key, color);
|
||||
}
|
||||
|
||||
void palette_controller::reload_palettes()
|
||||
{
|
||||
m_palette_manager.load_palettes_from_directory(
|
||||
clrsync::core::config::instance().palettes_path());
|
||||
m_palettes = m_palette_manager.palettes();
|
||||
}
|
||||
38
src/gui/controllers/palette_controller.hpp
Normal file
38
src/gui/controllers/palette_controller.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef CLRSYNC_GUI_PALETTE_CONTROLLER_HPP
|
||||
#define CLRSYNC_GUI_PALETTE_CONTROLLER_HPP
|
||||
|
||||
#include "core/io/toml_file.hpp"
|
||||
#include "core/palette/palette_manager.hpp"
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class palette_controller
|
||||
{
|
||||
public:
|
||||
palette_controller();
|
||||
|
||||
const clrsync::core::palette ¤t_palette() const
|
||||
{
|
||||
return m_current_palette;
|
||||
}
|
||||
const std::unordered_map<std::string, clrsync::core::palette> &palettes() const
|
||||
{
|
||||
return m_palettes;
|
||||
}
|
||||
|
||||
void select_palette(const std::string &name);
|
||||
void create_palette(const std::string &name);
|
||||
void save_current_palette();
|
||||
void delete_current_palette();
|
||||
void apply_current_theme() const;
|
||||
void set_color(const std::string &key, const clrsync::core::color &color);
|
||||
|
||||
private:
|
||||
void reload_palettes();
|
||||
|
||||
clrsync::core::palette_manager<clrsync::core::io::toml_file> m_palette_manager;
|
||||
std::unordered_map<std::string, clrsync::core::palette> m_palettes;
|
||||
clrsync::core::palette m_current_palette;
|
||||
};
|
||||
|
||||
#endif // CLRSYNC_GUI_PALETTE_CONTROLLER_HPP
|
||||
64
src/gui/controllers/template_controller.cpp
Normal file
64
src/gui/controllers/template_controller.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "gui/controllers/template_controller.hpp"
|
||||
#include "core/config/config.hpp"
|
||||
|
||||
template_controller::template_controller()
|
||||
{
|
||||
m_templates = m_template_manager.templates();
|
||||
}
|
||||
|
||||
void template_controller::set_template_enabled(const std::string &key, bool enabled)
|
||||
{
|
||||
auto it = m_templates.find(key);
|
||||
if (it != m_templates.end())
|
||||
{
|
||||
it->second.set_enabled(enabled);
|
||||
(void)clrsync::core::config::instance().update_template(key, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
void template_controller::set_template_input_path(const std::string &key, const std::string &path)
|
||||
{
|
||||
auto it = m_templates.find(key);
|
||||
if (it != m_templates.end())
|
||||
{
|
||||
it->second.set_template_path(path);
|
||||
(void)clrsync::core::config::instance().update_template(key, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
void template_controller::set_template_output_path(const std::string &key, const std::string &path)
|
||||
{
|
||||
auto it = m_templates.find(key);
|
||||
if (it != m_templates.end())
|
||||
{
|
||||
it->second.set_output_path(path);
|
||||
(void)clrsync::core::config::instance().update_template(key, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
void template_controller::set_template_reload_command(const std::string &key,
|
||||
const std::string &cmd)
|
||||
{
|
||||
auto it = m_templates.find(key);
|
||||
if (it != m_templates.end())
|
||||
{
|
||||
it->second.set_reload_command(cmd);
|
||||
(void)clrsync::core::config::instance().update_template(key, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
bool template_controller::remove_template(const std::string &key)
|
||||
{
|
||||
auto result = clrsync::core::config::instance().remove_template(key);
|
||||
if (result)
|
||||
{
|
||||
m_templates.erase(key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void template_controller::refresh()
|
||||
{
|
||||
m_templates = m_template_manager.templates();
|
||||
}
|
||||
31
src/gui/controllers/template_controller.hpp
Normal file
31
src/gui/controllers/template_controller.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef CLRSYNC_GUI_TEMPLATE_CONTROLLER_HPP
|
||||
#define CLRSYNC_GUI_TEMPLATE_CONTROLLER_HPP
|
||||
|
||||
#include "core/io/toml_file.hpp"
|
||||
#include "core/theme/template_manager.hpp"
|
||||
#include "core/theme/theme_template.hpp"
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class template_controller
|
||||
{
|
||||
public:
|
||||
template_controller();
|
||||
[[nodiscard]] const std::unordered_map<std::string, clrsync::core::theme_template> &templates()
|
||||
const
|
||||
{
|
||||
return m_templates;
|
||||
}
|
||||
void set_template_enabled(const std::string &key, bool enabled);
|
||||
void set_template_input_path(const std::string &key, const std::string &path);
|
||||
void set_template_output_path(const std::string &key, const std::string &path);
|
||||
void set_template_reload_command(const std::string &key, const std::string &cmd);
|
||||
bool remove_template(const std::string &key);
|
||||
void refresh();
|
||||
|
||||
private:
|
||||
clrsync::core::template_manager<clrsync::core::io::toml_file> m_template_manager;
|
||||
std::unordered_map<std::string, clrsync::core::theme_template> m_templates;
|
||||
};
|
||||
|
||||
#endif // CLRSYNC_GUI_TEMPLATE_CONTROLLER_HPP
|
||||
162
src/gui/controllers/theme_applier.cpp
Normal file
162
src/gui/controllers/theme_applier.cpp
Normal file
@@ -0,0 +1,162 @@
|
||||
#include "gui/controllers/theme_applier.hpp"
|
||||
#include "imgui.h"
|
||||
|
||||
namespace theme_applier
|
||||
{
|
||||
|
||||
static uint32_t get_color_u32(const clrsync::core::palette ¤t, const std::string &key)
|
||||
{
|
||||
const auto &col = current.get_color(key);
|
||||
const uint32_t hex = col.hex();
|
||||
// Convert from RRGGBBAA to AABBGGRR (ImGui format)
|
||||
const uint32_t r = (hex >> 24) & 0xFF;
|
||||
const uint32_t g = (hex >> 16) & 0xFF;
|
||||
const uint32_t b = (hex >> 8) & 0xFF;
|
||||
const uint32_t a = hex & 0xFF;
|
||||
return (a << 24) | (b << 16) | (g << 8) | r;
|
||||
}
|
||||
|
||||
void apply_to_editor(TextEditor &editor, const clrsync::core::palette ¤t)
|
||||
{
|
||||
auto palette = editor.GetPalette();
|
||||
|
||||
palette[int(TextEditor::PaletteIndex::Default)] = get_color_u32(current, "editor_main");
|
||||
palette[int(TextEditor::PaletteIndex::Keyword)] = get_color_u32(current, "editor_command");
|
||||
palette[int(TextEditor::PaletteIndex::Number)] = get_color_u32(current, "editor_warning");
|
||||
palette[int(TextEditor::PaletteIndex::String)] = get_color_u32(current, "editor_string");
|
||||
palette[int(TextEditor::PaletteIndex::CharLiteral)] = get_color_u32(current, "editor_string");
|
||||
palette[int(TextEditor::PaletteIndex::Punctuation)] = get_color_u32(current, "editor_main");
|
||||
palette[int(TextEditor::PaletteIndex::Preprocessor)] =
|
||||
get_color_u32(current, "editor_emphasis");
|
||||
palette[int(TextEditor::PaletteIndex::Identifier)] = get_color_u32(current, "editor_main");
|
||||
palette[int(TextEditor::PaletteIndex::KnownIdentifier)] = get_color_u32(current, "editor_link");
|
||||
palette[int(TextEditor::PaletteIndex::PreprocIdentifier)] =
|
||||
get_color_u32(current, "editor_link");
|
||||
|
||||
palette[int(TextEditor::PaletteIndex::Comment)] = get_color_u32(current, "editor_comment");
|
||||
palette[int(TextEditor::PaletteIndex::MultiLineComment)] =
|
||||
get_color_u32(current, "editor_comment");
|
||||
|
||||
palette[int(TextEditor::PaletteIndex::Background)] =
|
||||
get_color_u32(current, "editor_background");
|
||||
palette[int(TextEditor::PaletteIndex::Cursor)] = get_color_u32(current, "cursor");
|
||||
|
||||
palette[int(TextEditor::PaletteIndex::Selection)] = get_color_u32(current, "editor_selected");
|
||||
palette[int(TextEditor::PaletteIndex::ErrorMarker)] = get_color_u32(current, "editor_error");
|
||||
palette[int(TextEditor::PaletteIndex::Breakpoint)] = get_color_u32(current, "editor_error");
|
||||
|
||||
palette[int(TextEditor::PaletteIndex::LineNumber)] =
|
||||
get_color_u32(current, "editor_line_number");
|
||||
|
||||
palette[int(TextEditor::PaletteIndex::CurrentLineFill)] =
|
||||
get_color_u32(current, "surface_variant");
|
||||
palette[int(TextEditor::PaletteIndex::CurrentLineFillInactive)] =
|
||||
get_color_u32(current, "surface");
|
||||
|
||||
palette[int(TextEditor::PaletteIndex::CurrentLineEdge)] =
|
||||
get_color_u32(current, "border_focused");
|
||||
|
||||
editor.SetPalette(palette);
|
||||
}
|
||||
|
||||
void apply_to_imgui(const clrsync::core::palette ¤t)
|
||||
{
|
||||
auto getColor = [&](const std::string &key) -> ImVec4 {
|
||||
const auto &col = current.get_color(key);
|
||||
const uint32_t hex = col.hex();
|
||||
return {((hex >> 24) & 0xFF) / 255.0f, ((hex >> 16) & 0xFF) / 255.0f,
|
||||
((hex >> 8) & 0xFF) / 255.0f, ((hex) & 0xFF) / 255.0f};
|
||||
};
|
||||
|
||||
ImGuiStyle &style = ImGui::GetStyle();
|
||||
|
||||
const ImVec4 bg = getColor("background");
|
||||
const ImVec4 onBg = getColor("on_background");
|
||||
const ImVec4 surface = getColor("surface");
|
||||
const ImVec4 onSurface = getColor("on_surface");
|
||||
const ImVec4 surfaceVariant = getColor("surface_variant");
|
||||
const ImVec4 onSurfaceVariant = getColor("on_surface_variant");
|
||||
const ImVec4 fg = getColor("foreground");
|
||||
const ImVec4 fgInactive = getColor("editor_inactive");
|
||||
const ImVec4 accent = getColor("accent");
|
||||
const ImVec4 border = getColor("border");
|
||||
|
||||
const ImVec4 error = getColor("error");
|
||||
const ImVec4 onError = getColor("on_error");
|
||||
const ImVec4 success = getColor("success");
|
||||
const ImVec4 onSuccess = getColor("on_success");
|
||||
const ImVec4 warning = getColor("warning");
|
||||
const ImVec4 onWarning = getColor("on_warning");
|
||||
const ImVec4 info = getColor("info");
|
||||
const ImVec4 onInfo = getColor("on_info");
|
||||
|
||||
style.Colors[ImGuiCol_WindowBg] = bg;
|
||||
style.Colors[ImGuiCol_ChildBg] = surface;
|
||||
style.Colors[ImGuiCol_PopupBg] = surface;
|
||||
|
||||
style.Colors[ImGuiCol_Border] = border;
|
||||
style.Colors[ImGuiCol_BorderShadow] = ImVec4(0, 0, 0, 0);
|
||||
|
||||
style.Colors[ImGuiCol_Text] = onSurface;
|
||||
style.Colors[ImGuiCol_TextDisabled] = onSurfaceVariant;
|
||||
style.Colors[ImGuiCol_TextSelectedBg] = accent;
|
||||
|
||||
style.Colors[ImGuiCol_Header] = surfaceVariant;
|
||||
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(accent.x, accent.y, accent.z, 0.8f);
|
||||
style.Colors[ImGuiCol_HeaderActive] = accent;
|
||||
|
||||
style.Colors[ImGuiCol_Button] = surfaceVariant;
|
||||
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(accent.x, accent.y, accent.z, 0.6f);
|
||||
style.Colors[ImGuiCol_ButtonActive] = accent;
|
||||
|
||||
style.Colors[ImGuiCol_FrameBg] = surfaceVariant;
|
||||
style.Colors[ImGuiCol_FrameBgHovered] =
|
||||
ImVec4(surfaceVariant.x * 1.1f, surfaceVariant.y * 1.1f, surfaceVariant.z * 1.1f, 1.0f);
|
||||
style.Colors[ImGuiCol_FrameBgActive] =
|
||||
ImVec4(surfaceVariant.x * 1.2f, surfaceVariant.y * 1.2f, surfaceVariant.z * 1.2f, 1.0f);
|
||||
|
||||
style.Colors[ImGuiCol_TitleBg] = surface;
|
||||
style.Colors[ImGuiCol_TitleBgActive] = surfaceVariant;
|
||||
style.Colors[ImGuiCol_TitleBgCollapsed] = surface;
|
||||
|
||||
style.Colors[ImGuiCol_ScrollbarBg] = surface;
|
||||
style.Colors[ImGuiCol_ScrollbarGrab] = surfaceVariant;
|
||||
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(accent.x, accent.y, accent.z, 0.6f);
|
||||
style.Colors[ImGuiCol_ScrollbarGrabActive] = accent;
|
||||
|
||||
style.Colors[ImGuiCol_SliderGrab] = accent;
|
||||
style.Colors[ImGuiCol_SliderGrabActive] =
|
||||
ImVec4(accent.x * 1.2f, accent.y * 1.2f, accent.z * 1.2f, 1.0f);
|
||||
|
||||
style.Colors[ImGuiCol_CheckMark] = accent;
|
||||
style.Colors[ImGuiCol_ResizeGrip] = surfaceVariant;
|
||||
style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(accent.x, accent.y, accent.z, 0.6f);
|
||||
style.Colors[ImGuiCol_ResizeGripActive] = accent;
|
||||
|
||||
style.Colors[ImGuiCol_Tab] = surface;
|
||||
style.Colors[ImGuiCol_TabHovered] = ImVec4(accent.x, accent.y, accent.z, 0.8f);
|
||||
style.Colors[ImGuiCol_TabActive] = surfaceVariant;
|
||||
style.Colors[ImGuiCol_TabUnfocused] = surface;
|
||||
style.Colors[ImGuiCol_TabUnfocusedActive] = surfaceVariant;
|
||||
style.Colors[ImGuiCol_TabSelectedOverline] = accent;
|
||||
|
||||
style.Colors[ImGuiCol_TableHeaderBg] = surfaceVariant;
|
||||
style.Colors[ImGuiCol_TableBorderStrong] = border;
|
||||
style.Colors[ImGuiCol_TableBorderLight] =
|
||||
ImVec4(border.x * 0.7f, border.y * 0.7f, border.z * 0.7f, border.w);
|
||||
|
||||
style.Colors[ImGuiCol_TableRowBg] = ImVec4(0, 0, 0, 0);
|
||||
style.Colors[ImGuiCol_TableRowBgAlt] =
|
||||
ImVec4(onSurfaceVariant.x, onSurfaceVariant.y, onSurfaceVariant.z, 0.06f);
|
||||
|
||||
style.Colors[ImGuiCol_Separator] = border;
|
||||
style.Colors[ImGuiCol_SeparatorHovered] = accent;
|
||||
style.Colors[ImGuiCol_SeparatorActive] = accent;
|
||||
|
||||
style.Colors[ImGuiCol_MenuBarBg] = surface;
|
||||
|
||||
style.Colors[ImGuiCol_DockingPreview] = ImVec4(accent.x, accent.y, accent.z, 0.7f);
|
||||
style.Colors[ImGuiCol_DockingEmptyBg] = bg;
|
||||
}
|
||||
|
||||
} // namespace theme_applier
|
||||
13
src/gui/controllers/theme_applier.hpp
Normal file
13
src/gui/controllers/theme_applier.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef CLRSYNC_GUI_THEME_APPLIER_HPP
|
||||
#define CLRSYNC_GUI_THEME_APPLIER_HPP
|
||||
|
||||
#include "color_text_edit/TextEditor.h"
|
||||
#include "core/palette/palette.hpp"
|
||||
|
||||
namespace theme_applier
|
||||
{
|
||||
void apply_to_imgui(const clrsync::core::palette &pal);
|
||||
void apply_to_editor(TextEditor &editor, const clrsync::core::palette &pal);
|
||||
} // namespace theme_applier
|
||||
|
||||
#endif // CLRSYNC_GUI_THEME_APPLIER_HPP
|
||||
Reference in New Issue
Block a user