Files
allwpilib/glass/src/lib/native/cpp/MainMenuBar.cpp
Peter Johnson 0587b7043a [glass] Use JSON files for storage instead of imgui ini
Storage is now nested.

Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.

ImGui's ini (for window position) is mapped to JSON.

You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.

Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.

Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.

Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.

Workspace | Save As Global: "save as" to the global system location

Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-27 00:12:13 -08:00

107 lines
2.4 KiB
C++

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "glass/MainMenuBar.h"
#include <cstdio>
#include <imgui.h>
#include <wpigui.h>
#include "glass/Context.h"
#include "glass/ContextInternal.h"
using namespace glass;
void MainMenuBar::AddMainMenu(std::function<void()> menu) {
if (menu) {
m_menus.emplace_back(std::move(menu));
}
}
void MainMenuBar::AddOptionMenu(std::function<void()> menu) {
if (menu) {
m_optionMenus.emplace_back(std::move(menu));
}
}
void MainMenuBar::Display() {
ImGui::BeginMainMenuBar();
WorkspaceMenu();
if (!m_optionMenus.empty()) {
if (ImGui::BeginMenu("Options")) {
for (auto&& menu : m_optionMenus) {
if (menu) {
menu();
}
}
ImGui::EndMenu();
}
}
wpi::gui::EmitViewMenu();
for (auto&& menu : m_menus) {
if (menu) {
menu();
}
}
#if 0
char str[64];
std::snprintf(str, sizeof(str), "%.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);
ImGui::SameLine(ImGui::GetWindowWidth() - ImGui::CalcTextSize(str).x -
10);
ImGui::Text("%s", str);
#endif
ImGui::EndMainMenuBar();
}
void MainMenuBar::WorkspaceMenu() {
if (ImGui::BeginMenu("Workspace")) {
if (ImGui::MenuItem("Open...")) {
m_openFolder =
std::make_unique<pfd::select_folder>("Choose folder to open");
}
if (ImGui::MenuItem("Save As...")) {
m_saveFolder = std::make_unique<pfd::select_folder>("Choose save folder");
}
if (ImGui::MenuItem("Save As Global", nullptr, false,
!gContext->isPlatformSaveDir)) {
SetStorageDir(wpi::gui::GetPlatformSaveFileDir());
SaveStorage();
}
ImGui::Separator();
if (ImGui::MenuItem("Reset")) {
WorkspaceReset();
}
ImGui::Separator();
if (ImGui::MenuItem("Exit")) {
wpi::gui::Exit();
}
ImGui::EndMenu();
}
if (m_openFolder && m_openFolder->ready(0)) {
auto result = m_openFolder->result();
if (!result.empty()) {
LoadStorage(result);
}
m_openFolder.reset();
}
if (m_saveFolder && m_saveFolder->ready(0)) {
auto result = m_saveFolder->result();
if (!result.empty()) {
SetStorageDir(result);
SaveStorage(result);
}
m_saveFolder.reset();
}
}