-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab.cpp
More file actions
401 lines (353 loc) · 13.6 KB
/
Copy pathtab.cpp
File metadata and controls
401 lines (353 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "tab.h"
#include "sidebar.h"
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include "httpengine.h"
#include <imgui_stdlib.h>
#include "imgui_helpers.h"
#include "TextEditor.h"
#include <stack>
namespace UI
{
namespace
{
void ConfigureTextEditor(TextEditor& editor, bool readOnly, const TextEditor::Language* language = nullptr)
{
editor.SetTabSize(2);
editor.SetInsertSpacesOnTabs(true);
editor.SetAutoIndentEnabled(true);
editor.SetReadOnlyEnabled(readOnly);
editor.SetShowLineNumbersEnabled(true);
editor.SetShowWhitespacesEnabled(false);
editor.SetShowMatchingBrackets(true);
editor.SetCompletePairedGlyphs(true);
editor.SetShowPanScrollIndicatorEnabled(true);
if (language != nullptr)
{
editor.SetLanguage(language);
}
}
}
// Helper function to initialize global tabs vector
std::vector<std::unique_ptr<Tab>> InitializeTabs()
{
std::vector<std::unique_ptr<Tab>> tabs;
tabs.push_back(std::make_unique<HttpTab>(1, "Tab 1"));
return tabs;
}
std::vector<std::unique_ptr<Tab>> g_Tabs = InitializeTabs();
int g_ActiveTabId = 1;
int g_NextTabId = 4;
int g_SelectedTopTab = 0;
std::vector<SavedRequest> g_Collection;
void SaveCollectionToFile(const std::string& path) {
std::ofstream out(path);
if (!out) return;
out << g_Collection.size() << "\n";
for (const auto& req : g_Collection) {
out << req.serialized.size() << "\n" << req.serialized;
}
}
void LoadCollectionFromFile(const std::string& path) {
std::ifstream in(path);
if (!in) return;
g_Collection.clear();
std::string line;
if (std::getline(in, line)) {
try {
int count = std::stoi(line);
for (int i=0; i<count; ++i) {
if (std::getline(in, line)) {
int size = std::stoi(line);
std::string serialized;
serialized.resize(size);
in.read(&serialized[0], size);
// Extract name from first line of serialized string
std::string name = "Unnamed";
size_t pos = serialized.find('\n');
if (pos != std::string::npos) {
name = serialized.substr(0, pos);
}
g_Collection.push_back({name, serialized});
}
}
} catch (...) {}
}
}
std::string HttpTab::SerializeToString(const std::string& name) {
std::string export_str = name + "\n" +
std::to_string(static_cast<int>(this->method)) + "\n" +
this->url + "\n" +
std::to_string(this->requestHeaders.size()) + "\n";
for(auto& h : this->requestHeaders) {
export_str += h.first + "\n" + h.second + "\n";
}
std::string body = requestEditor ? requestEditor->GetText() : "";
export_str += std::to_string(body.length()) + "\n" + body;
return export_str;
}
void HttpTab::DeserializeFromString(const std::string& serialized) {
std::istringstream iss(serialized);
std::string line;
std::getline(iss, line); // ignore name
if (std::getline(iss, line)) {
try { this->method = static_cast<HTTP::Method>(std::stoi(line)); } catch(...) {}
}
if (std::getline(iss, line)) {
this->url = line;
}
if (std::getline(iss, line)) {
try {
int num_headers = std::stoi(line);
this->requestHeaders.clear();
for(int i=0; i<num_headers; ++i) {
std::string k, v;
std::getline(iss, k);
std::getline(iss, v);
this->requestHeaders.push_back({k, v});
}
} catch(...) {}
}
if (std::getline(iss, line)) {
try {
int body_len = std::stoi(line);
std::string body;
if (body_len > 0) {
body.resize(body_len);
iss.read(&body[0], body_len);
}
if (requestEditor) requestEditor->SetText(body);
} catch(...) {}
}
}
void HttpTab::Draw()
{
ImGui::PushID(this->id);
static bool responseReadOnly = true;
if (!requestEditor)
{
requestEditor = std::make_unique<TextEditor>();
ConfigureTextEditor(*requestEditor, false, TextEditor::Language::Json());
}
if (!responseEditor)
{
responseEditor = std::make_unique<TextEditor>();
ConfigureTextEditor(*responseEditor, true, TextEditor::Language::Json());
}
if (!requestEditorInitialized)
{
requestEditor->SetText(req.body);
requestEditorInitialized = true;
}
if (req.gotresponse && req.getresponse().body.has_value())
{
const auto &body = req.getresponse().body.value();
if (body != lastResponseText)
{
responseEditor->SetText(body);
lastResponseText = body;
}
}
// Clean layout with zero vertical item spacing between panels and splitter
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 0.0f));
// Clamp requestHeight to keep response panel slightly visible (min 40px)
float max_height = ImGui::GetContentRegionAvail().y - 40.0f;
if (this->requestHeight < 0.0f) {
this->requestHeight = ImGui::GetContentRegionAvail().y * 0.80f;
}
if (this->requestHeight > max_height) this->requestHeight = max_height;
if (this->requestHeight < 40.0f) this->requestHeight = 40.0f;
// 1. Top Panel: Request Editor
ImGui::BeginChild("##request_panel", ImVec2(0.0f, this->requestHeight), false, ImGuiWindowFlags_NoScrollbar);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(8.0f, 8.0f));
ImGui::Text("HTTP Request Settings");
ImGui::SameLine();
if (ImGui::Button("Copy to Clipboard")) {
ImGui::SetClipboardText(this->SerializeToString("Clipboard Request").c_str());
}
ImGui::SameLine();
if (ImGui::Button("Paste from Clipboard")) {
const char* clip = ImGui::GetClipboardText();
if (clip) {
this->DeserializeFromString(clip);
}
}
ImGui::SameLine();
if (ImGui::Button("Save to Collection")) {
std::string name = "Saved Request " + std::to_string(g_Collection.size() + 1);
g_Collection.push_back({name, this->SerializeToString(name)});
}
ImGui::Spacing();
ImGui::InputText("#URL", &this->url, 0, nullptr, nullptr);
const char *methods[] = {"GET", "POST", "PUT", "DELETE"};
int current_method = static_cast<int>(this->method);
if (ImGui::Combo("Method", ¤t_method, methods, IM_ARRAYSIZE(methods)))
{
this->method = static_cast<HTTP::Method>(current_method);
}
ImGui::Spacing();
ImGui::Text("Headers");
ImGui::SameLine();
if (ImGui::Button("+##add_header")) {
requestHeaders.push_back({"", ""});
}
for (size_t i = 0; i < requestHeaders.size(); ++i) {
ImGui::PushID(static_cast<int>(i));
ImGui::SetNextItemWidth(150.0f);
ImGui::InputText("##key", &requestHeaders[i].first);
ImGui::SameLine();
ImGui::SetNextItemWidth(250.0f);
ImGui::InputText("##value", &requestHeaders[i].second);
ImGui::SameLine();
if (ImGui::Button("X")) {
requestHeaders.erase(requestHeaders.begin() + i);
--i;
}
ImGui::PopID();
}
ImGui::Spacing();
ImGui::Text("Request Body");
ImGui::Separator();
ImFont* editorFont = UI::Vulkan::g_MonoFont ? UI::Vulkan::g_MonoFont : ImGui::GetFont();
ImGui::PushFont(editorFont);
requestEditor->Render("##request_body_editor", ImVec2(-1.0f, 160.0f), true);
ImGui::PopFont();
req.body = requestEditor->GetText();
ImGui::Spacing();
if (ImGui::Button("Send Request"))
{
req.url = this->url;
req.method = this->method;
req.gotresponse = false;
req.body = requestEditor->GetText();
req.headers.clear();
for (const auto& h : requestHeaders) {
if (!h.first.empty()) {
req.headers[h.first] = h.second;
}
}
eng.dispatchrequest(&req);
std::cout << "button pressed" << std::endl;
}
ImGui::PopStyleVar();
ImGui::EndChild();
// 2. Splitter Bar
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.20f, 0.22f, 0.27f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.35f, 0.40f, 0.50f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.28f, 0.32f, 0.40f, 1.0f));
ImGui::Button("##hsplitter", ImVec2(-1.0f, 8.0f));
// Removed custom cursor changes so it relies purely on the OS default cursor.
if (ImGui::IsItemActive())
{
this->requestHeight += ImGui::GetIO().MouseDelta.y;
}
ImGui::PopStyleColor(3);
ImGui::Dummy(ImVec2(0.0f, 8.0f));
// 3. Bottom Panel: Response Area
ImGui::BeginChild("##response_panel", ImVec2(0.0f, 0.0f), false);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(8.0f, 8.0f));
ImGui::Text("Response");
ImGui::Separator();
ImGui::Spacing();
if (req.gotresponse)
{
auto &resp = req.getresponse();
ImGui::BeginGroup();
if (ImGui::Button("Copy"))
{
responseEditor->Copy();
}
ImGui::SameLine();
if (ImGui::Button("Clear"))
{
responseEditor->ClearText();
if (resp.body.has_value()) resp.body.reset();
lastResponseText.clear();
}
ImGui::SameLine();
ImGui::Checkbox("Read-only", &responseReadOnly);
ImGui::EndGroup();
ImGui::Spacing();
if (resp.body.has_value())
{
responseEditor->SetReadOnlyEnabled(responseReadOnly);
ImFont* editorFont = UI::Vulkan::g_MonoFont ? UI::Vulkan::g_MonoFont : ImGui::GetFont();
ImGui::PushFont(editorFont);
responseEditor->Render("##response_editor", ImGui::GetContentRegionAvail(), true);
ImGui::PopFont();
}
else
{
ImGui::TextColored(ImVec4(0.5f, 0.5f, 0.5f, 1.0f), "Response body is empty.");
}
}
else
{
ImGui::TextColored(ImVec4(0.5f, 0.5f, 0.5f, 1.0f), "No response yet. Send a request to see output.");
}
ImGui::PopStyleVar();
ImGui::EndChild();
ImGui::PopStyleVar();
ImGui::PopID();
}
void WsTab::Draw()
{
ImGui::Text("WebSocket Connection Settings");
ImGui::Spacing();
ImGui::InputText("Address", &this->address);
ImGui::Spacing();
if (ImGui::Button("Connect"))
{
// Dummy action
}
}
void DrawTabContent()
{
ImGuiViewport *viewport = ImGui::GetMainViewport();
// Position the window directly to the right of the sidebar and let it occupy the space
float offset = g_SidebarCollapsed ? 0.0f : g_SidebarWidth + 4.0f;
ImGui::SetNextWindowPos(ImVec2(viewport->Pos.x + offset, viewport->Pos.y));
ImGui::SetNextWindowSize(ImVec2(viewport->Size.x - offset, viewport->Size.y));
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoScrollbar;
// Use clean padding
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(16.0f, 16.0f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
if (ImGui::Begin("Tab Content Window", nullptr, flags))
{
if (g_SidebarCollapsed)
{
if (ImGui::Button(">>", ImVec2(32, 24)))
{
g_SidebarCollapsed = false;
g_SidebarRestoreRequested = true;
}
ImGui::SameLine();
}
Tab *active_tab = nullptr;
for (auto &tab : g_Tabs)
{
if (tab->id == g_ActiveTabId)
{
active_tab = tab.get();
break;
}
}
if (active_tab)
{
ImGui::Text("Active Tab: %s", active_tab->name.c_str());
ImGui::Separator();
ImGui::Spacing();
active_tab->Draw();
}
else
{
ImGui::Text("No active tab.");
ImGui::Text("Click the '+' button in the sidebar to create an HTTP or WebSocket tab.");
}
}
ImGui::End();
ImGui::PopStyleVar(2);
}
} // namespace UI