6#include "absl/status/status.h"
7#include "absl/strings/str_format.h"
13namespace layout_designer {
20 for (
char chr : str) {
22 case '"': result +=
"\\\"";
break;
23 case '\\': result +=
"\\\\";
break;
24 case '\n': result +=
"\\n";
break;
25 case '\r': result +=
"\\r";
break;
26 case '\t': result +=
"\\t";
break;
27 default: result += chr;
break;
35 case ImGuiDir_None:
return "none";
36 case ImGuiDir_Left:
return "left";
37 case ImGuiDir_Right:
return "right";
38 case ImGuiDir_Up:
return "up";
39 case ImGuiDir_Down:
return "down";
40 case ImGuiDir_COUNT:
return "none";
41 default:
return "none";
46 if (str ==
"left")
return ImGuiDir_Left;
47 if (str ==
"right")
return ImGuiDir_Right;
48 if (str ==
"up")
return ImGuiDir_Up;
49 if (str ==
"down")
return ImGuiDir_Down;
58 default:
return "leaf";
72 std::ostringstream json;
75 json <<
" \"layout\": {\n";
76 json <<
" \"name\": \"" << EscapeJson(layout.
name) <<
"\",\n";
77 json <<
" \"description\": \"" << EscapeJson(layout.
description) <<
"\",\n";
78 json <<
" \"version\": \"" << EscapeJson(layout.
version) <<
"\",\n";
79 json <<
" \"author\": \"" << EscapeJson(layout.
author) <<
"\",\n";
82 json <<
" \"canvas_size\": [" << layout.
canvas_size.x <<
", "
88 json <<
" \"root_node\": null\n";
98 std::ostringstream json;
101 json <<
" \"type\": \"" << NodeTypeToString(node.
type) <<
"\",\n";
104 json <<
" \"split_dir\": \"" << DirToString(node.
split_dir) <<
"\",\n";
105 json <<
" \"split_ratio\": " << node.
split_ratio <<
",\n";
107 json <<
" \"left_child\": ";
115 json <<
" \"right_child\": ";
123 }
else if (node.
IsLeaf()) {
124 json <<
" \"panels\": [\n";
125 for (
size_t idx = 0; idx < node.
panels.size(); ++idx) {
127 if (idx < node.
panels.size() - 1) {
141 return absl::StrFormat(
142 "{\"id\":\"%s\",\"name\":\"%s\",\"icon\":\"%s\","
143 "\"priority\":%d,\"visible\":%s,\"closable\":%s,\"pinnable\":%s}",
146 EscapeJson(panel.
icon),
154 const std::string& json_str) {
157 return absl::UnimplementedError(
158 "JSON deserialization not yet implemented. "
159 "This requires integrating nlohmann/json library.");
163 const std::string& filepath) {
164 std::ofstream file(filepath);
165 if (!file.is_open()) {
166 return absl::InternalError(
167 absl::StrFormat(
"Failed to open file for writing: %s", filepath));
170 std::string json =
ToJson(layout);
175 return absl::InternalError(
176 absl::StrFormat(
"Failed to write to file: %s", filepath));
179 LOG_INFO(
"LayoutSerializer",
"Saved layout to: %s", filepath.c_str());
180 return absl::OkStatus();
184 const std::string& filepath) {
185 std::ifstream file(filepath);
186 if (!file.is_open()) {
187 return absl::InternalError(
188 absl::StrFormat(
"Failed to open file for reading: %s", filepath));
191 std::stringstream buffer;
192 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)
std::string EscapeJson(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.