[nostalgia/studio] Add active editor tab to config

This commit is contained in:
Gary Talent 2022-04-04 01:11:01 -05:00
parent f0cbcbbddf
commit 632ade60b9
2 changed files with 44 additions and 30 deletions

View File

@ -18,11 +18,13 @@ struct StudioConfig {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.studio.StudioConfig";
static constexpr auto TypeVersion = 1;
ox::String projectPath;
ox::String activeTabItemName;
ox::Vector<ox::String> openFiles;
bool showProjectExplorer = true;
};
oxModelBegin(StudioConfig)
oxModelFieldRename(active_tab_item_name, activeTabItemName)
oxModelFieldRename(project_path, projectPath)
oxModelFieldRename(open_files, openFiles)
oxModelFieldRename(show_project_explorer, showProjectExplorer)
@ -39,7 +41,7 @@ StudioUI::StudioUI(core::Context *ctx) noexcept {
if (!err) {
oxIgnoreError(openProject(config.projectPath));
for (const auto &f : config.openFiles) {
auto openFileErr = openFile(f);
auto openFileErr = openFileActiveTab(f, config.activeTabItemName == f);
if (openFileErr) {
oxErrorf("Could not open editor: {}\n", toStr(openFileErr));
}
@ -64,7 +66,7 @@ void StudioUI::handleKeyEvent(core::Key key, bool down) noexcept {
toggleProjectExplorer();
break;
case core::Key::Alpha_C:
m_acitveEditor->copy();
m_activeEditor->copy();
break;
case core::Key::Alpha_O:
m_taskRunner.add(new FileDialogManager(this, &StudioUI::openProject));
@ -76,10 +78,10 @@ void StudioUI::handleKeyEvent(core::Key key, bool down) noexcept {
save();
break;
case core::Key::Alpha_V:
m_acitveEditor->paste();
m_activeEditor->paste();
break;
case core::Key::Alpha_X:
m_acitveEditor->cut();
m_activeEditor->cut();
break;
case core::Key::Alpha_Y:
redo();
@ -116,8 +118,8 @@ void StudioUI::drawMenu() noexcept {
if (ImGui::MenuItem("Open Project...", "Ctrl+O")) {
m_taskRunner.add(new FileDialogManager(this, &StudioUI::openProject));
}
if (ImGui::MenuItem("Save", "Ctrl+S", false, m_acitveEditor && m_acitveEditor->unsavedChanges())) {
m_acitveEditor->save();
if (ImGui::MenuItem("Save", "Ctrl+S", false, m_activeEditor && m_activeEditor->unsavedChanges())) {
m_activeEditor->save();
}
if (ImGui::MenuItem("Quit", "Ctrl+Q")) {
core::shutdown(m_ctx);
@ -125,22 +127,22 @@ void StudioUI::drawMenu() noexcept {
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Edit")) {
auto undoStack = m_acitveEditor ? m_acitveEditor->undoStack() : nullptr;
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (ImGui::MenuItem("Undo", "Ctrl+Z", false, undoStack && undoStack->canUndo())) {
m_acitveEditor->undoStack()->undo();
m_activeEditor->undoStack()->undo();
}
if (ImGui::MenuItem("Redo", "Ctrl+Y", false, undoStack && undoStack->canRedo())) {
m_acitveEditor->undoStack()->redo();
m_activeEditor->undoStack()->redo();
}
ImGui::Separator();
if (ImGui::MenuItem("Copy", "Ctrl+C")) {
m_acitveEditor->copy();
m_activeEditor->copy();
}
if (ImGui::MenuItem("Cut", "Ctrl+X")) {
m_acitveEditor->cut();
m_activeEditor->cut();
}
if (ImGui::MenuItem("Paste", "Ctrl+V")) {
m_acitveEditor->paste();
m_activeEditor->paste();
}
ImGui::EndMenu();
}
@ -184,13 +186,17 @@ void StudioUI::drawTabs() noexcept {
auto const &e = *it;
auto open = true;
const auto unsavedChanges = e->unsavedChanges() ? ImGuiTabItemFlags_UnsavedDocument : 0;
const auto selected = m_activeEditorUpdatePending && e.get() == m_acitveEditor ?
ImGuiTabItemFlags_SetSelected : 0;
const auto selected = m_activeEditorUpdatePending == e.get() ? ImGuiTabItemFlags_SetSelected : 0;
const auto flags = unsavedChanges | selected;
if (ImGui::BeginTabItem(e->itemDisplayName().c_str(), &open, flags)) {
if (selected) {
m_acitveEditor = e.get();
m_activeEditorUpdatePending = false;
if (m_activeEditor != e.get()) {
m_activeEditor = e.get();
studio::editConfig<StudioConfig>(m_ctx, [&](StudioConfig *config) {
config->activeTabItemName = m_activeEditor->itemName();
});
}
if (m_activeEditorUpdatePending == e.get()) {
m_activeEditorUpdatePending = nullptr;
}
e->draw(m_ctx);
ImGui::EndTabItem();
@ -250,22 +256,22 @@ void StudioUI::toggleProjectExplorer() noexcept {
}
void StudioUI::redo() noexcept {
auto undoStack = m_acitveEditor ? m_acitveEditor->undoStack() : nullptr;
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (undoStack && undoStack->canRedo()) {
m_acitveEditor->undoStack()->redo();
m_activeEditor->undoStack()->redo();
}
}
void StudioUI::undo() noexcept {
auto undoStack = m_acitveEditor ? m_acitveEditor->undoStack() : nullptr;
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (undoStack && undoStack->canUndo()) {
m_acitveEditor->undoStack()->undo();
m_activeEditor->undoStack()->undo();
}
}
void StudioUI::save() noexcept {
if (m_acitveEditor && m_acitveEditor->unsavedChanges()) {
m_acitveEditor->save();
if (m_activeEditor && m_activeEditor->unsavedChanges()) {
m_activeEditor->save();
}
}
@ -288,11 +294,15 @@ ox::Error StudioUI::openProject(const ox::String &path) noexcept {
}
ox::Error StudioUI::openFile(const ox::String &path) noexcept {
return openFileActiveTab(path, true);
}
ox::Error StudioUI::openFileActiveTab(const ox::String &path, bool makeActiveTab) noexcept {
if (m_openFiles.contains(path)) {
for (auto &e : m_editors) {
if (e->itemName() == path) {
m_acitveEditor = e.get();
m_activeEditorUpdatePending = true;
if (makeActiveTab && e->itemName() == path) {
m_activeEditor = e.get();
m_activeEditorUpdatePending = e.get();
break;
}
}
@ -311,8 +321,10 @@ ox::Error StudioUI::openFile(const ox::String &path) noexcept {
editor->closed.connect(this, &StudioUI::closeFile);
m_editors.emplace_back(editor);
m_openFiles.emplace_back(path);
m_acitveEditor = m_editors.back().value.get();
m_activeEditorUpdatePending = true;
if (makeActiveTab) {
m_activeEditor = m_editors.back().value.get();
m_activeEditorUpdatePending = editor;
}
// save to config
studio::editConfig<StudioConfig>(m_ctx, [&](StudioConfig *config) {
if (!config->openFiles.contains((path))) {

View File

@ -29,8 +29,8 @@ class StudioUI: public ox::SignalHandler {
ox::HashMap<ox::String, studio::EditorMaker::Func> m_editorMakers;
ProjectExplorer *m_projectExplorer = nullptr;
ox::Vector<ox::String> m_openFiles;
studio::Editor *m_acitveEditor = nullptr;
bool m_activeEditorUpdatePending = false;
studio::Editor *m_activeEditor = nullptr;
studio::Editor *m_activeEditorUpdatePending = nullptr;
bool m_saveEnabled = false;
bool m_aboutEnabled = false;
bool m_showProjectExplorer = true;
@ -76,6 +76,8 @@ class StudioUI: public ox::SignalHandler {
ox::Error openFile(const ox::String &path) noexcept;
ox::Error openFileActiveTab(const ox::String &path, bool makeActiveTab) noexcept;
ox::Error closeFile(const ox::String &path) noexcept;
};