Repository navigation
Expand file tree
/
Copy pathKeybindingContextManager.cs
More file actions
81 lines (75 loc) · 1.83 KB
/
Copy pathKeybindingContextManager.cs
File metadata and controls
81 lines (75 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace OneCode.Core.Keybindings;
/// <summary>
/// 上下文管理器,管理活跃上下文集合。
/// 上下文优先级:注册的活跃上下文 > 焦点视图上下文 > Global。
/// </summary>
public sealed class KeybindingContextManager
{
private readonly HashSet<string> _activeContexts = new();
private string? _focusContext;
private readonly object _lock = new();
/// <summary>
/// 当前活跃的上下文集合(包含 Global)。
/// </summary>
public IReadOnlySet<string> ActiveContexts
{
get
{
lock (_lock)
{
var result = new HashSet<string>(_activeContexts);
if (_focusContext is not null)
{
result.Add(_focusContext);
}
// Global 始终活跃
result.Add(KeybindingDefaults.ContextGlobal);
return result;
}
}
}
/// <summary>
/// 当前焦点视图上下文。
/// </summary>
public string? FocusContext
{
get
{
lock (_lock)
{
return _focusContext;
}
}
set
{
lock (_lock)
{
_focusContext = value;
}
}
}
/// <summary>
/// 推入一个活跃上下文。
/// </summary>
public void PushContext(string context)
{
if (!KeybindingDefaults.IsValidContext(context))
{
return;
}
lock (_lock)
{
_activeContexts.Add(context);
}
}
/// <summary>
/// 弹出一个活跃上下文。
/// </summary>
public void PopContext(string context)
{
lock (_lock)
{
_activeContexts.Remove(context);
}
}
}