108 lines
3.4 KiB
C++
108 lines
3.4 KiB
C++
#include "Composition/EditorWindowWorkspaceStore.h"
|
|
|
|
#include <algorithm>
|
|
#include <utility>
|
|
|
|
namespace XCEngine::UI::Editor::App {
|
|
|
|
EditorWindowWorkspaceStore::EditorWindowWorkspaceStore(UIEditorPanelRegistry panelRegistry)
|
|
: m_panelRegistry(std::move(panelRegistry)) {}
|
|
|
|
bool EditorWindowWorkspaceStore::ValidateWindowSet(
|
|
const UIEditorWindowWorkspaceSet& windowSet,
|
|
std::string& outError) const {
|
|
const UIEditorWindowWorkspaceValidationResult validation =
|
|
ValidateUIEditorWindowWorkspaceSet(m_panelRegistry, windowSet);
|
|
if (!validation.IsValid()) {
|
|
outError = validation.message;
|
|
return false;
|
|
}
|
|
|
|
outError.clear();
|
|
return true;
|
|
}
|
|
|
|
bool EditorWindowWorkspaceStore::TrySetWindowSet(
|
|
UIEditorWindowWorkspaceSet windowSet,
|
|
std::string& outError) {
|
|
if (!ValidateWindowSet(windowSet, outError)) {
|
|
return false;
|
|
}
|
|
|
|
m_windowSet = std::move(windowSet);
|
|
outError.clear();
|
|
return true;
|
|
}
|
|
|
|
bool EditorWindowWorkspaceStore::UpsertWindowState(
|
|
const UIEditorWindowWorkspaceState& windowState,
|
|
bool primary,
|
|
std::string& outError) {
|
|
UIEditorWindowWorkspaceSet nextWindowSet = m_windowSet;
|
|
if (UIEditorWindowWorkspaceState* existingState =
|
|
FindMutableUIEditorWindowWorkspaceState(nextWindowSet, windowState.windowId);
|
|
existingState != nullptr) {
|
|
*existingState = windowState;
|
|
} else {
|
|
nextWindowSet.windows.push_back(windowState);
|
|
}
|
|
|
|
if (primary || nextWindowSet.primaryWindowId.empty()) {
|
|
nextWindowSet.primaryWindowId = windowState.windowId;
|
|
}
|
|
if (nextWindowSet.activeWindowId.empty() ||
|
|
FindUIEditorWindowWorkspaceState(nextWindowSet, nextWindowSet.activeWindowId) == nullptr) {
|
|
nextWindowSet.activeWindowId = windowState.windowId;
|
|
}
|
|
|
|
if (!ValidateWindowSet(nextWindowSet, outError)) {
|
|
return false;
|
|
}
|
|
|
|
m_windowSet = std::move(nextWindowSet);
|
|
outError.clear();
|
|
return true;
|
|
}
|
|
|
|
void EditorWindowWorkspaceStore::RemoveWindowState(std::string_view windowId, bool primary) {
|
|
if (primary) {
|
|
m_windowSet = {};
|
|
return;
|
|
}
|
|
|
|
UIEditorWindowWorkspaceSet nextWindowSet = m_windowSet;
|
|
nextWindowSet.windows.erase(
|
|
std::remove_if(
|
|
nextWindowSet.windows.begin(),
|
|
nextWindowSet.windows.end(),
|
|
[windowId](const UIEditorWindowWorkspaceState& state) {
|
|
return state.windowId == windowId;
|
|
}),
|
|
nextWindowSet.windows.end());
|
|
|
|
if (nextWindowSet.windows.empty()) {
|
|
m_windowSet = {};
|
|
return;
|
|
}
|
|
|
|
if (nextWindowSet.primaryWindowId == windowId ||
|
|
FindUIEditorWindowWorkspaceState(nextWindowSet, nextWindowSet.primaryWindowId) == nullptr) {
|
|
nextWindowSet.primaryWindowId = nextWindowSet.windows.front().windowId;
|
|
}
|
|
if (nextWindowSet.activeWindowId == windowId ||
|
|
FindUIEditorWindowWorkspaceState(nextWindowSet, nextWindowSet.activeWindowId) == nullptr) {
|
|
nextWindowSet.activeWindowId = nextWindowSet.primaryWindowId;
|
|
}
|
|
|
|
std::string ignoredError = {};
|
|
if (ValidateWindowSet(nextWindowSet, ignoredError)) {
|
|
m_windowSet = std::move(nextWindowSet);
|
|
}
|
|
}
|
|
|
|
bool EditorWindowWorkspaceStore::IsPrimaryWindowId(std::string_view windowId) const {
|
|
return !windowId.empty() && m_windowSet.primaryWindowId == windowId;
|
|
}
|
|
|
|
} // namespace XCEngine::UI::Editor::App
|