Refactor new editor state ownership model

This commit is contained in:
2026-04-19 04:36:52 +08:00
parent 48bfde28e3
commit f45b34a03a
46 changed files with 1979 additions and 217 deletions

View File

@@ -1,7 +1,5 @@
#include "Project/EditorProjectRuntime.h"
#include "State/EditorSelectionStamp.h"
namespace XCEngine::UI::Editor::App {
namespace {
@@ -81,11 +79,18 @@ EditCommandTarget BuildEditCommandTarget(
bool EditorProjectRuntime::Initialize(const std::filesystem::path& repoRoot) {
m_browserModel.Initialize(repoRoot);
m_selection = {};
m_ownedSelectionService = {};
m_selectionService = &m_ownedSelectionService;
m_pendingSceneOpenPath.reset();
return true;
}
void EditorProjectRuntime::BindSelectionService(
EditorSelectionService* selectionService) {
m_selectionService =
selectionService != nullptr ? selectionService : &m_ownedSelectionService;
}
void EditorProjectRuntime::SetFolderIcon(const ::XCEngine::UI::UITextureHandle& icon) {
m_browserModel.SetFolderIcon(icon);
}
@@ -104,16 +109,15 @@ ProjectBrowserModel& EditorProjectRuntime::GetBrowserModel() {
}
bool EditorProjectRuntime::HasSelection() const {
return m_selection.kind == EditorSelectionKind::ProjectItem &&
!m_selection.itemId.empty();
return SelectionService().HasSelectionKind(EditorSelectionKind::ProjectItem);
}
const EditorSelectionState& EditorProjectRuntime::GetSelection() const {
return m_selection;
return SelectionService().GetSelection();
}
std::uint64_t EditorProjectRuntime::GetSelectionStamp() const {
return m_selection.stamp;
return SelectionService().GetStamp();
}
bool EditorProjectRuntime::SetSelection(std::string_view itemId) {
@@ -122,18 +126,18 @@ bool EditorProjectRuntime::SetSelection(std::string_view itemId) {
return false;
}
const EditorSelectionState& selection = SelectionService().GetSelection();
const bool changed =
m_selection.kind != EditorSelectionKind::ProjectItem ||
m_selection.itemId != asset->itemId ||
m_selection.absolutePath != asset->absolutePath ||
m_selection.directory != asset->directory;
ApplySelection(*asset, !changed);
selection.kind != EditorSelectionKind::ProjectItem ||
selection.itemId != asset->itemId ||
selection.absolutePath != asset->absolutePath ||
selection.directory != asset->directory;
ApplySelection(*asset);
return changed;
}
void EditorProjectRuntime::ClearSelection() {
m_selection = {};
m_selection.stamp = GenerateEditorSelectionStamp();
SelectionService().ClearSelection();
}
bool EditorProjectRuntime::NavigateToFolder(std::string_view itemId) {
@@ -230,8 +234,9 @@ EditorProjectRuntime::ResolveAssetCommandTarget(
}
if (HasSelection()) {
const EditorSelectionState& selection = GetSelection();
if (const ProjectBrowserModel::AssetEntry* selectedAsset =
FindAssetEntry(m_selection.itemId);
FindAssetEntry(selection.itemId);
selectedAsset != nullptr) {
PopulateAssetCommandTargetFromAsset(
target,
@@ -278,8 +283,9 @@ EditorProjectRuntime::ResolveEditCommandTarget(
}
if (HasSelection()) {
const EditorSelectionState& selection = GetSelection();
if (const ProjectBrowserModel::AssetEntry* selectedAsset =
FindAssetEntry(m_selection.itemId);
FindAssetEntry(selection.itemId);
selectedAsset != nullptr) {
return BuildEditCommandTarget(*selectedAsset);
}
@@ -416,21 +422,21 @@ bool EditorProjectRuntime::MoveFolderToRoot(
return true;
}
EditorSelectionService& EditorProjectRuntime::SelectionService() {
return *m_selectionService;
}
const EditorSelectionService& EditorProjectRuntime::SelectionService() const {
return *m_selectionService;
}
void EditorProjectRuntime::ApplySelection(
const ProjectBrowserModel::AssetEntry& asset,
bool preserveStamp) {
const std::uint64_t previousStamp = m_selection.stamp;
m_selection = {};
m_selection.kind = EditorSelectionKind::ProjectItem;
m_selection.itemId = asset.itemId;
m_selection.displayName = asset.displayName.empty()
? asset.nameWithExtension
: asset.displayName;
m_selection.absolutePath = asset.absolutePath;
m_selection.directory = asset.directory;
m_selection.stamp = preserveStamp && previousStamp != 0u
? previousStamp
: GenerateEditorSelectionStamp();
const ProjectBrowserModel::AssetEntry& asset) {
SelectionService().SetProjectSelection(
asset.itemId,
asset.displayName.empty() ? asset.nameWithExtension : asset.displayName,
asset.absolutePath,
asset.directory);
}
void EditorProjectRuntime::RevalidateSelection() {
@@ -438,17 +444,19 @@ void EditorProjectRuntime::RevalidateSelection() {
return;
}
const ProjectBrowserModel::AssetEntry* asset = FindAssetEntry(m_selection.itemId);
const ProjectBrowserModel::AssetEntry* asset =
FindAssetEntry(GetSelection().itemId);
if (asset == nullptr) {
ClearSelection();
return;
}
ApplySelection(*asset, true);
ApplySelection(*asset);
}
bool EditorProjectRuntime::SelectionTargetsItem(std::string_view itemId) const {
return HasSelection() && IsSameOrDescendantItemId(m_selection.itemId, itemId);
return HasSelection() &&
IsSameOrDescendantItemId(GetSelection().itemId, itemId);
}
} // namespace XCEngine::UI::Editor::App