Skip to content

Commit db4bc97

Browse files
committed
Optimize interaction
1 parent 4967456 commit db4bc97

7 files changed

+90
-41
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [1.1.1] - 2023-10-07
4+
5+
- Fix bugs
6+
- Optimize interaction
7+
38
## [1.1.0] - 2023-09-06
49

510
- Support categories

Editor/Scripts/CodeExecutorData.cs

+12
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ public static void RemoveSnippet(string guid)
229229
snippets.Remove(snippet);
230230
}
231231

232+
public static void RemoveSnippetsWithCategory(string category)
233+
{
234+
foreach (SnippetInfo snippet in snippets.ToArray())
235+
{
236+
if (snippet.MatchCategory(category))
237+
{
238+
s_GuidMap.Remove(snippet.guid);
239+
snippets.Remove(snippet);
240+
}
241+
}
242+
}
243+
232244
public static bool HasSnippet(string guid)
233245
{
234246
return (!string.IsNullOrEmpty(guid) && s_GuidMap.ContainsKey(guid));

Editor/Scripts/CodeExecutorManager.cs

+13
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,19 @@ public static void RemoveSnippets(IEnumerable<string> guids)
276276
SaveData(true);
277277
}
278278

279+
/// <summary>
280+
/// 移除代码段
281+
/// </summary>
282+
/// <param name="category">类别</param>
283+
/// <param name="notify">通知</param>
284+
public static void RemoveSnippetsWithCategory(string category, bool notify = true)
285+
{
286+
// 更新数据
287+
CodeExecutorData.RemoveSnippetsWithCategory(category);
288+
// 保存到本地并通知更新
289+
SaveData(notify);
290+
}
291+
279292
#endregion
280293

281294
#region Data: Snippet Info

Editor/Scripts/Window/Content/Detail/CodeExecutorWindowCodeEditor.cs

+27-24
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ public partial class CodeExecutorWindow
1919
private VisualElement m_CodeEditor = null;
2020

2121
/// <summary>
22-
/// 代码编辑器滚动视图
22+
/// 代码滚动视图
2323
/// </summary>
2424
private ScrollView m_CodeScrollView = null;
2525

2626
/// <summary>
27-
/// 代码编辑器输入框
27+
/// 代码文本框
2828
/// </summary>
2929
private TextField m_CodeTextField = null;
3030

3131
/// <summary>
32-
/// 代码编辑器输入框
32+
/// 代码文本框
3333
/// </summary>
3434
private VisualElement m_CodeTextFieldTextInput = null;
3535

@@ -81,8 +81,10 @@ private void InitCodeEditor()
8181
}
8282
};
8383
m_CodeEditor.Add(m_CodeScrollView);
84+
// 代码滚动视图形变回调
85+
m_CodeScrollView.RegisterCallback<GeometryChangedEvent>(OnCodeScrollViewGeometryChangedEvent);
8486

85-
// 代码输入框
87+
// 代码文本框
8688
m_CodeTextField = new TextField()
8789
{
8890
name = "TextField",
@@ -101,7 +103,7 @@ private void InitCodeEditor()
101103
}
102104
};
103105
m_CodeScrollView.Add(m_CodeTextField);
104-
106+
// 样式Hack
105107
{
106108
// 输入框文本设为左上对齐
107109
VisualElement textInput = m_CodeTextFieldTextInput = m_CodeTextField.Q<VisualElement>("unity-text-input");
@@ -117,15 +119,11 @@ private void InitCodeEditor()
117119
textInput.style.borderBottomLeftRadius = 0;
118120
textInput.style.borderBottomRightRadius = 0;
119121
}
120-
121-
// 滚动视图变化回调
122-
m_CodeScrollView.RegisterCallback<GeometryChangedEvent>(OnCodeScrollViewGeometryChangedEventChanged);
123-
124-
// 输入框内容变化回调
122+
// 内容变化回调
125123
m_CodeTextField.RegisterValueChangedCallback(OnCodeTextFieldValueChanged);
126-
// 输入框键盘按下回调
124+
// 键盘按下回调
127125
m_CodeTextField.RegisterCallback<KeyDownEvent>(OnCodeTextFieldKeyDown);
128-
// 输入框鼠标滚轮回调
126+
// 鼠标滚轮回调
129127
m_CodeTextField.RegisterCallback<WheelEvent>(OnCodeTextFieldMouseWheel);
130128

131129
// 复制到剪切板按钮
@@ -162,19 +160,22 @@ private void InitCodeEditor()
162160
}
163161

164162
/// <summary>
165-
/// 滚动视图变化回调
163+
/// 代码滚动视图形变回调
166164
/// </summary>
167165
/// <param name="evt"></param>
168-
private void OnCodeScrollViewGeometryChangedEventChanged(GeometryChangedEvent evt)
166+
private void OnCodeScrollViewGeometryChangedEvent(GeometryChangedEvent evt)
169167
{
170168
EditorApplication.delayCall += UpdateCodeTextFieldHeight;
169+
}
171170

172-
void UpdateCodeTextFieldHeight()
173-
{
174-
if (m_CodeScrollView == null) return;
175-
float height = m_CodeScrollView.contentViewport.localBound.height;
176-
m_CodeTextField.style.minHeight = height;
177-
}
171+
/// <summary>
172+
/// 更新代码文本元素高度
173+
/// </summary>
174+
private void UpdateCodeTextFieldHeight()
175+
{
176+
if (m_CodeScrollView == null) return;
177+
float height = m_CodeScrollView.contentViewport.localBound.height;
178+
m_CodeTextField.style.minHeight = height;
178179
}
179180

180181
/// <summary>
@@ -183,7 +184,6 @@ void UpdateCodeTextFieldHeight()
183184
/// <param name="evt"></param>
184185
private void OnCodeTextFieldValueChanged(ChangeEvent<string> evt)
185186
{
186-
if (m_CodeTextField.isReadOnly) return;
187187
if (m_CurrSnippetInfo == null) return;
188188
// 更新数据
189189
string code = m_CodeTextField.value;
@@ -322,10 +322,13 @@ private int SetCodeEditorFontSize(int size)
322322

323323
private void SetCodeEditorEditable(bool isEditable, bool focus = false)
324324
{
325-
// 输入框只读
325+
// 文本框只读
326326
m_CodeTextField.isReadOnly = !isEditable;
327-
// 输入框背景颜色
328-
m_CodeTextFieldTextInput.style.backgroundColor = (isEditable ? textFieldNormalBgColor : textFieldReadOnlyBgColor);
327+
328+
// 背景颜色
329+
Color bgColor = (isEditable ? textFieldNormalBgColor : textFieldReadOnlyBgColor);
330+
m_CodeScrollView.style.backgroundColor = bgColor;
331+
m_CodeTextFieldTextInput.style.backgroundColor = bgColor;
329332
// 边框颜色
330333
Color borderColor = (isEditable ? textFieldNormalBorderColor : textFieldReadOnlyBorderColor);
331334
m_CodeScrollView.style.borderTopColor = borderColor;

Editor/Scripts/Window/Content/Detail/CodeExecutorWindowHeader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void InitHeadline()
110110
m_EditButton = new ButtonWithIcon()
111111
{
112112
name = "EditButton",
113-
tooltip = "Edit snippet",
113+
tooltip = "Edit current snippet",
114114
focusable = false,
115115
style =
116116
{

Editor/Scripts/Window/Content/Sidebar/CodeExecutorWindowSnippetTreeView.cs

+31-15
Original file line numberDiff line numberDiff line change
@@ -688,20 +688,17 @@ private void DuplicateSelectedSnippets()
688688
private void DeleteSelectedSnippets()
689689
{
690690
List<SnippetInfo> snippets = GetSnippetTreeViewSelectedSnippets(true);
691-
string[] names = snippets.Select(v => $"- {v.name}").ToArray();
691+
692692
bool isOk = EditorUtility.DisplayDialog(
693693
"[Code Executor] Delete snippets",
694-
$"Are you sure to delete the following snippets?\n{string.Join("\n", names)}",
694+
$"Are you sure to delete the following snippets?\n{string.Join("\n", snippets.Select(v => $"- {v.name}"))}",
695695
"Confirm!",
696696
"Cancel"
697697
);
698-
if (!isOk)
699-
{
700-
return;
701-
}
702-
string[] guids = snippets.Select(v => v.guid).ToArray();
703-
CodeExecutorManager.RemoveSnippets(guids);
704-
// 提示
698+
if (!isOk) return;
699+
700+
CodeExecutorManager.RemoveSnippets(snippets.Select(v => v.guid));
701+
705702
ShowNotification("Deleted");
706703
}
707704

@@ -710,17 +707,36 @@ private void DeleteSelectedSnippets()
710707
/// </summary>
711708
private void DeleteSelectedCategories()
712709
{
713-
List<CustomTreeViewItem> items = GetSelectedSnippetTreeViewItems();
714-
for (int i = 0; i < items.Count; i++)
710+
List<string> categories = new List<string>();
711+
foreach (CustomTreeViewItem item in GetSelectedSnippetTreeViewItems())
715712
{
716-
CustomTreeViewItem item = items[i];
717713
if (item.isContainer)
718714
{
719-
string category = item.displayName;
720-
bool notify = (i == items.Count - 1);
721-
CodeExecutorManager.RemoveCategory(category, notify);
715+
categories.Add(item.displayName);
716+
}
717+
}
718+
719+
int dialogResult = EditorUtility.DisplayDialogComplex(
720+
"[Code Executor] Delete categories",
721+
$"Whether to delete snippets under these categories?\n{string.Join("\n", categories.Select(v => $"- {v}"))}",
722+
"Keep snippets!",
723+
"Cancel",
724+
"Delete!"
725+
);
726+
if (dialogResult == 1) return;
727+
728+
for (int i = 0; i < categories.Count; i++)
729+
{
730+
string category = categories[i];
731+
if (dialogResult == 2)
732+
{
733+
CodeExecutorManager.RemoveSnippetsWithCategory(category, false);
722734
}
735+
bool notify = (i == categories.Count - 1);
736+
CodeExecutorManager.RemoveCategory(category, notify);
723737
}
738+
739+
ShowNotification("Deleted");
724740
}
725741

726742
#endregion

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.chenpipi.code-executor",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"unity": "2020.2",
55
"displayName": "Code Executor",
66
"keywords": [

0 commit comments

Comments
 (0)