-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathnote
333 lines (254 loc) · 11.6 KB
/
note
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Aila.MainPage"
Title="AI超元域">
<!-- <AbsoluteLayout> -->
<!-- <Grid x:Name="_grid" -->
<!-- AbsoluteLayout.LayoutBounds="0, 0, 1, 1" -->
<!-- AbsoluteLayout.LayoutFlags="All"> -->
<!-- <Grid.ColumnDefinitions> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- <ColumnDefinition Width="*" /> -->
<!-- </Grid.ColumnDefinitions> -->
<!-- <Grid.RowDefinitions> -->
<!-- <RowDefinition Height="*" /> -->
<!-- <RowDefinition Height="Auto" /> -->
<!-- </Grid.RowDefinitions> -->
<!-- -->
<!-- ~1~ Editor 占据前10列 @1@ -->
<!-- <Editor -->
<!-- x:Name="_editor" -->
<!-- Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="10" -->
<!-- Placeholder="👉👉👉Enter your question here..." -->
<!-- VerticalOptions="Start" -->
<!-- FontSize="18" -->
<!-- AutoSize="TextChanges"/> -->
<!-- -->
<!-- ~1~ Submit Button 占据第11列 @1@ -->
<!-- <Button -->
<!-- x:Name="SubmitBtn" -->
<!-- Grid.Row="1" Grid.Column="10" Text="➡ Submit" -->
<!-- HeightRequest="50" -->
<!-- VerticalOptions="End" -->
<!-- Clicked="OnSubmitClicked" /> -->
<!-- -->
<!-- ~1~ Switch Button 占据第12列 @1@ -->
<!-- <Button -->
<!-- x:Name="SwitchBtn" -->
<!-- Grid.Row="1" Grid.Column="11" Text="↩ Switch" -->
<!-- HeightRequest="50" -->
<!-- VerticalOptions="End" -->
<!-- Clicked="OnSwitchClicked" /> -->
<!-- -->
<!-- </Grid> -->
<!-- </AbsoluteLayout> -->
</ContentPage>
private Dictionary<WebView, (string Url, int Row, int Column, int ColumnSpan)> _webViewsInfo = new();
private Button SubmitBtn;
private Button SwitchBtn;
private void UpdateToolbarItemsForCurrentGroup()
{
// 首先清除现有的 ToolbarItems
ToolbarItems.Clear();
// 动态计算基于 viewsCount 的当前组的 WebView 范围
int groupSize = _viewsCount; // 使用当前的viewsCount确定每组的大小
int start = _currentGroupIndex * groupSize;
int end = Math.Min(start + groupSize, _configuration.CurrentAi.Count);
//
// // 创建并添加新的ToolbarItem
// var toolbarItem = new ToolbarItem
// {
// Text = "\ud83c\udfe0 Home", // 显示名称
// };
//
// toolbarItem.Clicked += (sender, e) =>
// {
// // 这里添加你希望在点击时执行的代码
// RestoreWebViewsLayout(); // 例如,调用 RestoreWebViewsLayout 方法
// };
//
// ToolbarItems.Add(toolbarItem);
// 为当前组的每个 WebView 添加 ToolbarItem
for (int i = start; i < end; i++)
{
var currentAi = _configuration.CurrentAi[i];
var aiConfig = _configuration.AiConfig.FirstOrDefault(ai => ai.Id == currentAi.Id);
if (aiConfig != null)
{
// 创建并添加新的ToolbarItem
ToolbarItems.Add(new ToolbarItem
{
Text = "\ud83d\udfe2" + aiConfig.Name, // 显示AI配置的名称
Command = new Command(() => ExecuteLoadWebViewCommand(aiConfig.Url))
});
}
}
}
// 新增方法:恢复 WebView 控件到其存储的布局位置
private void RestoreWebViewsLayout()
{
_editor.IsVisible = true;
SubmitBtn.IsVisible = true;
SwitchBtn.IsVisible = true;
foreach (var item in _webViewsInfo)
{
WebView webView = item.Key;
webView.IsVisible = true;
var layoutInfo = item.Value;
Grid.SetRow(webView, layoutInfo.Row);
Grid.SetRowSpan(webView, 1);
Grid.SetColumn(webView, layoutInfo.Column);
Grid.SetColumnSpan(webView, layoutInfo.ColumnSpan);
}
}
private void ShowPopupWithCollectionView()
{
// 创建 CollectionView 并设置数据源
var collectionView = new CollectionView
{
ItemsSource = _configuration.Prompts,
SelectionMode = SelectionMode.Single,
ItemTemplate = new DataTemplate(() =>
{
var frame = new Frame
{
BackgroundColor = Colors.White,
Padding = 10,
CornerRadius = 5,
// BorderColor = Colors.LightGray,
Content = new Label
{
TextColor = Colors.Black,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Start
}
};
frame.Content.SetBinding(Label.TextProperty, nameof(Prompts.Title));
return frame;
}),
};
// 创建 Frame 用于包装 collectionView
var frameForCollectionView = new Frame
{
Content = collectionView,
Padding = 0 // 根据需要调整
};
collectionView.SelectionChanged += (sender, e) =>
{
if (e.CurrentSelection.Count > 0)
{
var selectedItem = e.CurrentSelection[0] as Prompts;
if (selectedItem != null)
{
// 根据 ID 查询对应的 Prompt
var prompt = _configuration.Prompts.FirstOrDefault(p => p.Id == selectedItem.Id)?.Prompt;
if (!string.IsNullOrEmpty(prompt))
{
// 检查文本是否以 "/" 开头
if (_editor.Text.StartsWith("/"))
{
// 移除第一个字符 "/"
_editor.Text = _editor.Text.Substring(1);
// 防止光标移动到文本开头
_editor.CursorPosition = _editor.Text.Length;
}
_editor.Text = prompt;
if (_grid.Children.Contains(frameForCollectionView))
{
_grid.Children.Remove(frameForCollectionView);
}
}
collectionView.SelectedItem = null; // 清除选择
}
}
};
// 将 frame 添加到 Grid 的第一行和第一列
Grid.SetRow(frameForCollectionView, 0);
Grid.SetColumn(frameForCollectionView, 0);
Grid.SetColumnSpan(frameForCollectionView, 3);
_grid.Children.Add(frameForCollectionView);
}
// 定义命令执行的方法
private void ExecuteLoadWebViewCommand(string url)
{
_editor.IsVisible = false;
SubmitBtn.IsVisible = false;
SwitchBtn.IsVisible = false;
_webViewsInfo.Clear();
if (this.Content is View contentView)
{
FindVisibleWebViewsAndInfo(contentView);
}
foreach (var item in _webViewsInfo)
{
if (item.Value.Url.ToString() == url)
{
WebView webView = item.Key;
// 更新 WebView 布局
Grid.SetRow(webView, 0);
Grid.SetRowSpan(webView, 2);
Grid.SetColumn(webView, 0);
Grid.SetColumnSpan(webView, 12);
}
else
{
WebView webView = item.Key;
webView.IsVisible = false;
}
}
}
private void FindVisibleWebViewsAndInfo(View view)
{
if (view is WebView webView && webView.IsVisible)
{
string url = string.Empty;
// 确定 WebView.Source 的类型并相应地获取 URL
if (webView.Source is UrlWebViewSource urlSource)
{
url = urlSource.Url; // 获取实际的 URL 字符串
}
var row = Grid.GetRow(webView);
var column = Grid.GetColumn(webView);
var columnSpan = Grid.GetColumnSpan(webView);
// 将 WebView 及其信息添加到字典中
_webViewsInfo.Add(webView, (Url: url, Row: row, Column: column, ColumnSpan: columnSpan));
}
// 如果当前视图是布局容器,遍历其子视图
if (view is Layout layout)
{
foreach (var child in layout.Children)
{
if (child is View childView)
{
FindVisibleWebViewsAndInfo(childView);
}
}
}
}
AddItemToGrid(_editor, 1, 0, 10);
AddButtonToGrid(SubmitBtn,"\u27a2 Submit", OnSubmitClicked, 1, 10, 1, "Click to Send your question to AI");
AddButtonToGrid(SwitchBtn,"\u21b9 Switch", OnSwitchClicked, 1, 11, 1, "Click to Switch AI");
private void AddButtonToGrid(Button button,string text, EventHandler onClicked, int row, int column, int columnSpan, string toolTip)
{
button = new Button
{
Text = text,
HeightRequest = 50,
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.End
};
ToolTipProperties.SetText(button, toolTip);
button.Clicked += onClicked;
AddItemToGrid(button, row, column, columnSpan);
}