6#include "absl/status/status.h"
7#include "absl/strings/str_format.h"
13namespace layout_designer {
18std::string EscapeJson(
const std::string& str) {
20 for (
char chr : str) {
68 return ImGuiDir_Right;
102 std::ostringstream json;
105 json <<
" \"layout\": {\n";
106 json <<
" \"name\": \"" << EscapeJson(layout.
name) <<
"\",\n";
107 json <<
" \"description\": \"" << EscapeJson(layout.
description)
109 json <<
" \"version\": \"" << EscapeJson(layout.
version) <<
"\",\n";
110 json <<
" \"author\": \"" << EscapeJson(layout.
author) <<
"\",\n";
113 json <<
" \"canvas_size\": [" << layout.
canvas_size.x <<
", "
119 json <<
" \"root_node\": null\n";
129 std::ostringstream json;
132 json <<
" \"type\": \"" << NodeTypeToString(node.
type) <<
"\",\n";
135 json <<
" \"split_dir\": \"" << DirToString(node.
split_dir) <<
"\",\n";
136 json <<
" \"split_ratio\": " << node.
split_ratio <<
",\n";
138 json <<
" \"left_child\": ";
146 json <<
" \"right_child\": ";
154 }
else if (node.
IsLeaf()) {
155 json <<
" \"panels\": [\n";
156 for (
size_t idx = 0; idx < node.
panels.size(); ++idx) {
158 if (idx < node.
panels.size() - 1) {
172 return absl::StrFormat(
173 "{\"id\":\"%s\",\"name\":\"%s\",\"icon\":\"%s\","
174 "\"priority\":%d,\"visible\":%s,\"closable\":%s,\"pinnable\":%s}",
182 const std::string& json_str) {
185 return absl::UnimplementedError(
186 "JSON deserialization not yet implemented. "
187 "This requires integrating nlohmann/json library.");
191 const std::string& filepath) {
192 std::ofstream file(filepath);
193 if (!file.is_open()) {
194 return absl::InternalError(
195 absl::StrFormat(
"Failed to open file for writing: %s", filepath));
198 std::string json =
ToJson(layout);
203 return absl::InternalError(
204 absl::StrFormat(
"Failed to write to file: %s", filepath));
207 LOG_INFO(
"LayoutSerializer",
"Saved layout to: %s", filepath.c_str());
208 return absl::OkStatus();
212 const std::string& filepath) {
213 std::ifstream file(filepath);
214 if (!file.is_open()) {
215 return absl::InternalError(
216 absl::StrFormat(
"Failed to open file for reading: %s", filepath));
219 std::stringstream buffer;
220 buffer << file.rdbuf();
static absl::StatusOr< LayoutDefinition > LoadFromFile(const std::string &filepath)
Load layout from JSON file.
static absl::Status SaveToFile(const LayoutDefinition &layout, const std::string &filepath)
Save layout to JSON file.
static absl::StatusOr< LayoutDefinition > FromJson(const std::string &json_str)
Deserialize a layout from JSON string.
static std::string ToJson(const LayoutDefinition &layout)
Serialize a layout to JSON string.
static std::string SerializeDockNode(const DockNode &node)
static std::string SerializePanel(const LayoutPanel &panel)
#define LOG_INFO(category, format,...)
ImGuiDir StringToDir(const std::string &str)
DockNodeType StringToNodeType(const std::string &str)
std::string DirToString(ImGuiDir dir)
std::string NodeTypeToString(DockNodeType type)
DockNodeType
Type of dock node in the layout tree.
Represents a dock node in the layout tree.
std::vector< LayoutPanel > panels
std::unique_ptr< DockNode > child_left
std::unique_ptr< DockNode > child_right
Complete layout definition with metadata.
int64_t created_timestamp
std::unique_ptr< DockNode > root
int64_t modified_timestamp
Represents a single panel in a layout.