载入中...
搜索中...
未找到
EditorSelection.cpp
浏览该文件的文档.
2
3#include <algorithm>
4
5namespace eve::editor {
6
7Result<SelectionSnapshot> EditorSelectionService::set(std::string channel, std::vector<SelectionItem> items,
8 std::optional<SelectionItem> primary) {
9 if (channel.empty())
10 return eve::editing::failed<SelectionSnapshot>(
11 EditorStatus::Rejected, RuleId("editor.selection.channel-required"), "Selection channel is required");
12 if (primary && std::find(items.begin(), items.end(), *primary) == items.end())
13 return eve::editing::failed<SelectionSnapshot>(EditorStatus::Rejected,
14 RuleId("editor.selection.primary-missing"),
15 "Primary selection must be included in items");
17 value.channel = std::move(channel);
18 value.items = std::move(items);
19 value.primary = std::move(primary);
20 value.sequence = ++sequence_;
21 selections_.insert_or_assign(value.channel, value);
22 for (const auto& [owner, listener] : listeners_) {
23 (void)owner;
24 listener(value);
25 }
26 return eve::editing::applied<SelectionSnapshot>(std::move(value));
27}
28
29Result<SelectionSnapshot> EditorSelectionService::clear(const std::string& channel) { return set(channel, {}); }
30
31SelectionSnapshot EditorSelectionService::snapshot(const std::string& channel) const {
32 const auto found = selections_.find(channel);
33 if (found != selections_.end()) return found->second;
35 empty.channel = channel;
36 return empty;
37}
38
40 if (owner.empty() || !listener)
41 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.selection.invalid-listener"),
42 "Selection listener owner and callback are required");
43 listeners_.insert_or_assign(std::move(owner), std::move(listener));
44 return eve::editing::applied<void>();
45}
46
47bool EditorSelectionService::unsubscribe(const std::string& owner) { return listeners_.erase(owner) != 0; }
48
50 if (channel.empty() || surface.empty())
51 return eve::editing::failed<EditorFocusSnapshot>(EditorStatus::Rejected, RuleId("editor.focus.invalid"),
52 "Focus channel and surface are required");
54 value.channel = std::move(channel);
55 value.surface = std::move(surface);
56 value.item = std::move(item);
57 value.sequence = ++sequence_;
58 focus_.insert_or_assign(value.channel, value);
59 return eve::editing::applied<EditorFocusSnapshot>(std::move(value));
60}
61
62EditorFocusSnapshot EditorFocusService::snapshot(const std::string& channel) const {
63 const auto found = focus_.find(channel);
64 if (found != focus_.end()) return found->second;
66 empty.channel = channel;
67 return empty;
68}
69
70} // namespace eve::editor
double value
bool found
Move-only operation result carrying either a value or Status.
Definition Result.h:155
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:314
bool empty() const noexcept
True when no compatibility spelling has been assigned.
Definition Identity.h:374
EditorFocusSnapshot snapshot(const std::string &channel) const
Read the current focus of one channel.
Result< EditorFocusSnapshot > focus(std::string channel, StableId surface, StableId item={})
Set the focused surface/item for one channel.
Result< SelectionSnapshot > clear(const std::string &channel)
Clear a channel while retaining a monotonic sequence.
std::function< void(const SelectionSnapshot &)> Listener
SelectionSnapshot snapshot(const std::string &channel) const
Read one channel, returning an empty sequence-zero snapshot when absent.
bool unsubscribe(const std::string &owner)
Remove one listener owner.
Result< void > subscribe(std::string owner, Listener listener)
Subscribe an owner callback; owner replacement and unload are deterministic.
Result< SelectionSnapshot > set(std::string channel, std::vector< SelectionItem > items, std::optional< SelectionItem > primary=std::nullopt)
Atomically replace a channel selection and optional primary item.
eve::editing::RuleId RuleId
Definition EditorIds.h:32
FocusSnapshot public API.
SelectionSnapshot public API.