From 7eb37c531837c30f42316162209be06d62fa892a Mon Sep 17 00:00:00 2001
From: Gary Talent <gary@drinkingtea.net>
Date: Sat, 18 Jan 2025 21:29:36 -0600
Subject: [PATCH] [nostalgia/core/studio/paletteeditor] Fix adding page if
 there is no existing page

---
 .../src/studio/paletteeditor/CMakeLists.txt   |  1 +
 .../paletteeditor/commands/addpagecommand.cpp | 28 +++++++++++++++++
 .../paletteeditor/commands/addpagecommand.hpp | 31 +++++++++++++++++++
 .../paletteeditor/commands/commands.hpp       |  1 +
 .../commands/duplicatepagecommand.cpp         |  2 +-
 .../paletteeditor/paletteeditor-imgui.cpp     |  7 ++++-
 6 files changed, 68 insertions(+), 2 deletions(-)
 create mode 100644 src/nostalgia/modules/core/src/studio/paletteeditor/commands/addpagecommand.cpp
 create mode 100644 src/nostalgia/modules/core/src/studio/paletteeditor/commands/addpagecommand.hpp

diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/CMakeLists.txt b/src/nostalgia/modules/core/src/studio/paletteeditor/CMakeLists.txt
index 245c5e9a..cdaf9d3e 100644
--- a/src/nostalgia/modules/core/src/studio/paletteeditor/CMakeLists.txt
+++ b/src/nostalgia/modules/core/src/studio/paletteeditor/CMakeLists.txt
@@ -1,6 +1,7 @@
 target_sources(
 	NostalgiaCore-Studio PRIVATE
 		commands/addcolorcommand.cpp
+		commands/addpagecommand.cpp
 		commands/applycolorallpagescommand.cpp
 		commands/duplicatepagecommand.cpp
 		commands/movecolorcommand.cpp
diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/commands/addpagecommand.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor/commands/addpagecommand.cpp
new file mode 100644
index 00000000..828bbc9b
--- /dev/null
+++ b/src/nostalgia/modules/core/src/studio/paletteeditor/commands/addpagecommand.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
+ */
+
+#include "commands.hpp"
+
+#include "addpagecommand.hpp"
+
+namespace nostalgia::core {
+
+AddPageCommand::AddPageCommand(Palette &pal) noexcept:
+		m_pal(pal) {}
+
+int AddPageCommand::commandId() const noexcept {
+	return static_cast<int>(PaletteEditorCommandId::AddPage);
+}
+
+ox::Error AddPageCommand::redo() noexcept {
+	m_pal.pages.emplace_back(ox::sfmt("Page {}", m_pal.pages.size() + 1), ox::Vector<PaletteColor>{});
+	return {};
+}
+
+ox::Error AddPageCommand::undo() noexcept {
+	m_pal.pages.pop_back();
+	return {};
+}
+
+}
diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/commands/addpagecommand.hpp b/src/nostalgia/modules/core/src/studio/paletteeditor/commands/addpagecommand.hpp
new file mode 100644
index 00000000..58f31d76
--- /dev/null
+++ b/src/nostalgia/modules/core/src/studio/paletteeditor/commands/addpagecommand.hpp
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
+ */
+
+#pragma once
+
+#include <studio/studio.hpp>
+
+#include <nostalgia/core/palette.hpp>
+
+namespace nostalgia::core {
+
+class AddPageCommand: public studio::UndoCommand {
+	private:
+		Palette &m_pal;
+
+	public:
+		explicit AddPageCommand(Palette &pal) noexcept;
+
+		~AddPageCommand() noexcept override = default;
+
+		[[nodiscard]]
+		int commandId() const noexcept final;
+
+		ox::Error redo() noexcept final;
+
+		ox::Error undo() noexcept final;
+
+};
+
+}
diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/commands/commands.hpp b/src/nostalgia/modules/core/src/studio/paletteeditor/commands/commands.hpp
index 778e4507..e6be3b4b 100644
--- a/src/nostalgia/modules/core/src/studio/paletteeditor/commands/commands.hpp
+++ b/src/nostalgia/modules/core/src/studio/paletteeditor/commands/commands.hpp
@@ -9,6 +9,7 @@ namespace nostalgia::core {
 enum class PaletteEditorCommandId {
 	ApplyColorAllPages,
 	RenamePage,
+	AddPage,
 	DuplicatePage,
 	RemovePage,
 	AddColor,
diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/commands/duplicatepagecommand.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor/commands/duplicatepagecommand.cpp
index 6f52860b..e4888ef7 100644
--- a/src/nostalgia/modules/core/src/studio/paletteeditor/commands/duplicatepagecommand.cpp
+++ b/src/nostalgia/modules/core/src/studio/paletteeditor/commands/duplicatepagecommand.cpp
@@ -23,7 +23,7 @@ int DuplicatePageCommand::commandId() const noexcept {
 }
 
 ox::Error DuplicatePageCommand::redo() noexcept {
-	m_pal.pages.emplace(m_dstIdx, "", std::move(m_page));
+	m_pal.pages.emplace(m_dstIdx, ox::sfmt("Page {}", m_pal.pages.size() + 1), std::move(m_page));
 	return {};
 }
 
diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp
index ade37511..1ae4f7bf 100644
--- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp
+++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp
@@ -7,6 +7,7 @@
 #include <keel/media.hpp>
 
 #include "commands/addcolorcommand.hpp"
+#include "commands/addpagecommand.hpp"
 #include "commands/applycolorallpagescommand.hpp"
 #include "commands/duplicatepagecommand.hpp"
 #include "commands/movecolorcommand.hpp"
@@ -196,7 +197,11 @@ void PaletteEditorImGui::drawPagesEditor() noexcept {
 	constexpr auto toolbarHeight = 40;
 	auto const btnSz = ImVec2{paneSz.x / 4 - 5.5f, 24};
 	if (ImGui::Button("Add", btnSz)) {
-		std::ignore = pushCommand<DuplicatePageCommand>(m_pal, 0u, m_pal.pages.size());
+		if (m_pal.pages.empty()) {
+			std::ignore = pushCommand<AddPageCommand>(m_pal);
+		} else {
+			std::ignore = pushCommand<DuplicatePageCommand>(m_pal, 0u, m_pal.pages.size());
+		}
 		m_page = m_pal.pages.size() - 1;
 	}
 	ImGui::SameLine();