-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamplePlugin.cs
More file actions
283 lines (257 loc) · 11.2 KB
/
Copy pathSamplePlugin.cs
File metadata and controls
283 lines (257 loc) · 11.2 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using Ink_Canvas.Controls;
using Ink_Canvas.Plugins;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace SamplePlugin
{
/// <summary>
/// 示例插件 - 演示如何开发 Ink Canvas 插件,包括工具栏组件的动态注册。
/// </summary>
[PluginEntrance]
public class SamplePlugin : PluginBase
{
// 元数据(Id/Name/Version/Description/Author)从 manifest.json 自动读取,无需在代码中重复定义
private IInkCanvasService _inkCanvasService;
private IAppRestartService _appRestartService;
private Views.SettingsView _settingsView;
private FrameworkElement _sampleButtonPopupContent;
public override void Initialize(IPluginHost host)
{
base.Initialize(host);
Log(string.Format("{0} 已初始化", Name));
_inkCanvasService = GetService<IInkCanvasService>();
if (_inkCanvasService != null)
{
Log("已获取 InkCanvas 服务");
}
_appRestartService = GetService<IAppRestartService>();
if (_appRestartService != null)
{
Log("已获取重启服务,当前运行状态: " + (_appRestartService.IsRunningAsAdmin ? "管理员" : "非管理员"));
}
_settingsView = new Views.SettingsView(_inkCanvasService, _appRestartService, host);
// 注册示例工具栏组件
RegisterSampleButton(host);
RegisterSampleCustomControl(host);
}
private FrameworkElement CreateMenuContent(string menuStyle)
{
var innerText = new TextBlock
{
Text = "这是示例按钮的菜单内容",
Margin = new Thickness(16),
FontSize = 14
};
switch (menuStyle)
{
case "PopupTabShellContent":
var tabShell = new PopupTabShellContent
{
InnerContent = innerText
};
tabShell.TabBar.Tabs.Add(new PopupTabItem { Header = "Tab 1" });
tabShell.TabBar.Tabs.Add(new PopupTabItem { Header = "Tab 2" });
tabShell.TabBar.SelectedIndex = 0;
return tabShell;
default:
return new PopupShellContent
{
Title = "示例菜单",
InnerContent = innerText
};
}
}
/// <summary>
/// 注册示例按钮 - 使用 ToolbarImageButton 的普通组件示例
/// </summary>
private void RegisterSampleButton(IPluginHost host)
{
var itemInfo = new PluginToolbarItemInfo
{
Id = "sample.button",
DisplayName = "示例按钮",
Description = "示例普通组件,点击打开菜单弹窗",
IconGeometry = "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z",
ViewFactory = () =>
{
var btn = new ToolbarImageButton
{
Label = "示例按钮",
Tag = "ToolbarRegistryInjected"
};
// 设置图标
btn.Icon.Geometry = Geometry.Parse("M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z");
return btn;
},
PopupContentFactory = () =>
{
_sampleButtonPopupContent = CreateMenuContent("PopupShellContent");
return _sampleButtonPopupContent;
},
ApplyOrientation = (view, orientation) =>
{
if (view is ToolbarImageButton btn)
{
btn.ApplyOrientation(orientation == Orientation.Vertical);
}
},
ApplySettings = (view, settings) =>
{
if (settings == null) return;
if (!settings.TryGetValue("menuStyle", out var val)) return;
var menuStyle = val?.ToString() ?? "PopupShellContent";
// 通过逻辑树找到父级 Popup 并替换内容
var popup = LogicalTreeHelper.GetParent(_sampleButtonPopupContent) as Popup;
if (popup != null)
{
var newContent = CreateMenuContent(menuStyle);
popup.Child = newContent;
_sampleButtonPopupContent = newContent;
// 重新绑定关闭按钮
if (newContent is PopupShellContent shell)
shell.CloseButtonControl.Click += (s, e) => popup.IsOpen = false;
else if (newContent is PopupTabShellContent tabShell)
tabShell.CloseButtonControl.Click += (s, e) => popup.IsOpen = false;
}
},
CustomSettings = new List<PluginToolbarSettingInfo>
{
new PluginToolbarSettingInfo
{
Key = "menuStyle",
DisplayName = "菜单样式",
Description = "选择弹窗的菜单样式",
Type = PluginToolbarSettingType.ComboBox,
Options = new List<string> { "PopupShellContent", "PopupTabShellContent" },
DefaultValue = "PopupShellContent"
}
}
};
host.RegisterToolbarItem(itemInfo);
}
/// <summary>
/// 注册示例自定义控件 - 在工具栏中嵌入自定义控件
/// </summary>
private void RegisterSampleCustomControl(IPluginHost host)
{
var itemInfo = new PluginToolbarItemInfo
{
Id = "sample.customControl",
DisplayName = "示例自定义控件",
Description = "示例自定义控件插件,支持滑块、开关、复选框、下拉框等控制类型",
IconGeometry = "M3 5h18v2H3V5zm0 6h18v2H3v-2zm0 6h12v2H3v-2z",
ViewFactory = () =>
{
var border = new Border
{
CornerRadius = new CornerRadius(4),
Padding = new Thickness(6, 2, 6, 2)
};
border.SetResourceReference(Border.BackgroundProperty, "FloatBarBackground");
var panel = new StackPanel
{
Orientation = Orientation.Horizontal,
VerticalAlignment = VerticalAlignment.Center
};
var label = new TextBlock
{
Text = "示例控件",
FontSize = 12,
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 0, 6, 0)
};
label.SetResourceReference(TextBlock.ForegroundProperty, "FloatBarForeground");
panel.Children.Add(label);
// 默认显示滑块
var slider = new Slider
{
Width = 80,
Minimum = 0,
Maximum = 100,
Value = 50,
VerticalAlignment = VerticalAlignment.Center
};
panel.Children.Add(slider);
border.Child = panel;
return border;
},
ApplyOrientation = (view, orientation) =>
{
// 自定义控件支持方向切换
},
ApplySettings = (view, settings) =>
{
// 根据控制类型设置渲染不同的控件
if (view is Border border && border.Child is StackPanel panel && panel.Children.Count > 1)
{
string controlType = null;
if (settings != null && settings.TryGetValue("controlType", out var val))
controlType = val?.ToString();
if (string.IsNullOrEmpty(controlType)) return;
// 移除除标签外的所有子控件
while (panel.Children.Count > 1)
panel.Children.RemoveAt(1);
FrameworkElement control = controlType switch
{
"ToggleSwitch" => new iNKORE.UI.WPF.Modern.Controls.ToggleSwitch
{
MinWidth = 0,
OnContent = "",
OffContent = "",
VerticalAlignment = VerticalAlignment.Center
},
"CheckBox" => new CheckBox
{
VerticalAlignment = VerticalAlignment.Center,
IsChecked = true
},
"ComboBox" => new ComboBox
{
VerticalAlignment = VerticalAlignment.Center,
Width = 80
},
_ => new Slider
{
Width = 80,
Minimum = 0,
Maximum = 100,
Value = 50,
VerticalAlignment = VerticalAlignment.Center
}
};
panel.Children.Add(control);
}
},
CustomSettings = new List<PluginToolbarSettingInfo>
{
new PluginToolbarSettingInfo
{
Key = "controlType",
DisplayName = "控制类型",
Description = "选择自定义控件的类型",
Type = PluginToolbarSettingType.ComboBox,
Options = new List<string> { "Slider", "ToggleSwitch", "CheckBox", "ComboBox" },
DefaultValue = "Slider"
}
}
};
host.RegisterToolbarItem(itemInfo);
}
public override void Shutdown()
{
Log(string.Format("{0} 已关闭", Name));
}
public override object GetMainView()
{
return null;
}
public override object GetSettingsView()
{
return _settingsView;
}
}
}