forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropdownMenu.cs
More file actions
664 lines (628 loc) · 28 KB
/
Copy pathDropdownMenu.cs
File metadata and controls
664 lines (628 loc) · 28 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
namespace UnityEngine.UIElements
{
/// <summary>
/// Provides information about the event that caused the dropdown menu to display.
/// </summary>
public class DropdownMenuEventInfo
{
/// <summary>
/// The modifier keys that were pressed if those keys triggered the dropdown menu to
/// display of the dropdown menu. For example, Alt, Ctrl, Shift, Windows, and Command.
/// </summary>
public EventModifiers modifiers { get; }
/// <summary>
/// The mouse position expressed in the global coordinate system if the event that triggered the dropdown
/// menu to display was a mouse event.
/// </summary>
/// <remarks>
/// If the event that triggered the dropdown menu to display was not a mouse event, this property is zero.
/// </remarks>
public Vector2 mousePosition { get; }
/// <summary>
/// The position of the mouse if the event that triggered the dropdown menu to display was a mouse event.
/// </summary>
/// <remarks>
/// The position is expressed in the coordinate system of the element that received the mouse event.
/// If the event that triggered the dropdown menu to display was not a mouse event, the value of this property is zero.
/// </remarks>
public Vector2 localMousePosition { get; }
char character { get; }
KeyCode keyCode { get; }
/// <summary>
/// Initialized menu event info with event provided.
/// </summary>
/// <param name="e">Source event.</param>
public DropdownMenuEventInfo(EventBase e)
{
if (e is IMouseEvent mouseEvent)
{
mousePosition = mouseEvent.mousePosition;
localMousePosition = mouseEvent.localMousePosition;
modifiers = mouseEvent.modifiers;
character = '\0';
keyCode = KeyCode.None;
}
else if (e is IPointerEvent pointerEvent)
{
mousePosition = pointerEvent.position;
localMousePosition = pointerEvent.localPosition;
modifiers = pointerEvent.modifiers;
character = '\0';
keyCode = KeyCode.None;
}
else if (e is IKeyboardEvent keyboardEvent)
{
character = keyboardEvent.character;
keyCode = keyboardEvent.keyCode;
modifiers = keyboardEvent.modifiers;
mousePosition = Vector2.zero;
localMousePosition = Vector2.zero;
}
}
}
/// <summary>
/// Represents an item in a dropdown menu.
/// </summary>
public abstract class DropdownMenuItem {}
/// <summary>
/// Provides a separator menu item.
/// </summary>
public class DropdownMenuSeparator : DropdownMenuItem
{
/// <summary>
/// The submenu path to the separator. Path components are delimited by forward slashes ('/').
/// </summary>
public string subMenuPath { get; }
/// <summary>
/// Initializes a separator at a path.
/// </summary>
/// <param name="subMenuPath">The path for the submenu. Path components are delimited by forward slashes ('/').</param>
public DropdownMenuSeparator(string subMenuPath)
{
this.subMenuPath = subMenuPath;
}
}
/// <summary>
/// Represents a menu action item.
/// </summary>
public class DropdownMenuAction : DropdownMenuItem
{
/// <summary>
/// Status of the menu item.
/// </summary>
/// <remarks>
/// Use the values of this enumeration as flags
/// </remarks>
[Flags]
public enum Status
{
/// <summary>
/// Do not display the item.
/// </summary>
/// <remarks>
/// This is the default value and represents the absence of flags.
/// </remarks>
None = 0,
/// <summary>
/// Display the item normally.
/// </summary>
Normal = 1, // Enabled, unchecked, shown
/// <summary>
/// Disable the item and make it so it is not selectable by the user.
/// </summary>
Disabled = 2,
/// <summary>
/// Display the item with a checkmark.
/// </summary>
Checked = 4,
/// <summary>
/// Do not display the item.
/// </summary>
/// <remarks>
/// This flag can be used with other flags.
/// </remarks>
Hidden = 8
}
/// <summary>
/// The name of the item.
/// </summary>
/// <remarks>
/// The name of the time can be prefixed by its submenu path. Path components are delimited by forward slashes ('/').
/// </remarks>
public string name { get; }
/// <summary>
/// The status of the item.
/// </summary>
public Status status { get; private set; }
/// <summary>
/// Provides information about the event that triggered the dropdown menu.
/// </summary>
public DropdownMenuEventInfo eventInfo { get; private set; }
/// <summary>
/// The userData object stored by the constructor.
/// </summary>
public object userData { get; private set; }
internal VisualElement content { get; }
readonly Action<DropdownMenuAction> actionCallback;
readonly Func<DropdownMenuAction, Status> actionStatusCallback;
/// <summary>
/// Creates a status callback that always returns <see cref="Status.Enabled"/>.
/// </summary>
/// <param name="a">Unused parameter.</param>
/// <returns>Always returns <see cref="Status.Enabled"/> </returns>
public static Status AlwaysEnabled(DropdownMenuAction a)
{
return Status.Normal;
}
/// <summary>
/// Creates a status callback that always returns <see cref="Status.Disabled"/> status.
/// </summary>
/// <param name="a">Unused parameter.</param>
/// <returns>Always returns <see cref="Status.Disabled"/>.</returns>
public static Status AlwaysDisabled(DropdownMenuAction a)
{
return Status.Disabled;
}
/// <summary>
/// Initializes a menu action with specified parameters.
/// </summary>
/// <param name="actionName">The path and name of the menu item. Use the path, delimited by forward slashes ('/'), to place the menu item within a submenu.</param>
/// <param name="actionCallback">The action to execute when the menu item is selected.</param>
/// <param name="actionStatusCallback">The function called to determine if the menu item is enabled.</param>
/// <param name="userData">The object to store in the <b>userData</b> property.</param>
public DropdownMenuAction(string actionName, Action<DropdownMenuAction> actionCallback, Func<DropdownMenuAction, Status> actionStatusCallback, object userData = null)
{
name = actionName;
this.actionCallback = actionCallback;
this.actionStatusCallback = actionStatusCallback;
this.userData = userData;
}
/// <summary>
/// Updates the status flag of this item by calling the item status callback.
/// </summary>
/// <param name="eventInfo">Information about the event that caused the dropdown menu to display, such as the mouse position or the key pressed.</param>
public void UpdateActionStatus(DropdownMenuEventInfo eventInfo)
{
this.eventInfo = eventInfo;
status = actionStatusCallback?.Invoke(this) ?? Status.Hidden;
}
/// <summary>
/// Executes the callback associated with this item.
/// </summary>
public void Execute()
{
actionCallback?.Invoke(this);
}
}
/// <summary>
/// Represents a dropdown menu.
/// </summary>
public class DropdownMenu
{
List<DropdownMenuItem> m_MenuItems = new List<DropdownMenuItem>();
DropdownMenuEventInfo m_DropdownMenuEventInfo;
internal int Count => m_MenuItems.Count;
/// <summary>
/// Gets the list of menu items.
/// </summary>
/// <returns>The list of items in the menu.</returns>
public List<DropdownMenuItem> MenuItems()
{
return m_MenuItems;
}
/// <summary>
/// Adds an item that executes an action in the dropdown menu.
/// </summary>
/// <remarks>
/// The item is added to the end of the current item list.
/// </remarks>
/// <param name="actionName">The name of the item. This name is displayed in the dropdown menu.</param>
/// <param name="action">The callback to execute when the user selects this item in the menu.</param>
/// <param name="actionStatusCallback">The callback to execute to determine the status of this item.</param>
/// <param name="userData">The object to store in the <b>userData</b> property of the <see cref="DropdownMenuAction"/> item.</param>
/// <example>
/// <code>
/// <![CDATA[
///using UnityEditor;
///using UnityEngine;
///using UnityEngine.UIElements;
///
///public class ContextMenuWindow : EditorWindow
///{
/// [MenuItem("My/Context Menu Window")]
/// static void ShowMe() => GetWindow<ContextMenuWindow>();
///
/// void CreateGUI()
/// {
/// var contextMenuContainer = new VisualElement();
/// contextMenuContainer.style.flexGrow = 1;
/// contextMenuContainer.AddManipulator(new ContextualMenuManipulator(e =>
/// {
/// e.menu.AppendAction("Submenu/My Action 1", a => Debug.Log("My Action 1 Works"), action =>
/// {
/// action.tooltip = "Status dependent tooltip";
/// return DropdownMenuAction.Status.Normal;
/// }, null);
/// }));
///
/// rootVisualElement.Add(contextMenuContainer);
/// }
///}
/// ]]>
/// </code>
/// </example>
public void AppendAction(string actionName, Action<DropdownMenuAction> action, Func<DropdownMenuAction, DropdownMenuAction.Status> actionStatusCallback, object userData = null)
{
DropdownMenuAction menuAction = new DropdownMenuAction(actionName, action, actionStatusCallback, userData);
m_MenuItems.Add(menuAction);
}
/// <summary>
/// Adds an item that executes an action in the dropdown menu.
/// </summary>
/// <remarks>
/// The item is added to the end of the current item list.
/// </remarks>
/// <param name="actionName">The name of the item. This name is displayed in the dropdown menu.</param>
/// <param name="action">The callback to execute when the user selects this item in the menu.</param>
/// <param name="status">The status of the item.</param>
/// <example>
/// <code>
/// <![CDATA[
///using UnityEditor;
///using UnityEngine;
///using UnityEngine.UIElements;
///
///public class ContextMenuWindow : EditorWindow
///{
/// [MenuItem("My/Context Menu Window")]
/// static void ShowMe() => GetWindow<ContextMenuWindow>();
///
/// void CreateGUI()
/// {
/// var contextMenuContainer = new VisualElement();
/// contextMenuContainer.style.flexGrow = 1;
/// contextMenuContainer.AddManipulator(new ContextualMenuManipulator(e =>
/// {
/// e.menu.AppendAction("My Action 1", a => Debug.Log("My Action 1 Works"), DropdownMenuAction.Status.Normal);
/// }));
///
/// rootVisualElement.Add(contextMenuContainer);
/// }
///}
/// ]]>
/// </code>
/// </example>
public void AppendAction(string actionName, Action<DropdownMenuAction> action, DropdownMenuAction.Status status = DropdownMenuAction.Status.Normal)
{
if (status == DropdownMenuAction.Status.Normal)
{
AppendAction(actionName, action, DropdownMenuAction.AlwaysEnabled);
}
else if (status == DropdownMenuAction.Status.Disabled)
{
AppendAction(actionName, action, DropdownMenuAction.AlwaysDisabled);
}
else
{
AppendAction(actionName, action, e => status);
}
}
/// <summary>
/// Adds an item that executes an action in the dropdown menu.
/// </summary>
/// <remarks>
/// The item is added to the end of the specified index in the list.
/// </remarks>
/// <param name="atIndex">The index to insert the item at.</param>
/// <param name="actionName">The name of the item. This name is displayed in the dropdown menu.</param>
/// <param name="action">Callback to execute when the user selects this item in the menu.</param>
/// <param name="actionStatusCallback">The callback to execute to determine the status of the item.</param>
/// <param name="userData">The object to store in the <b>userData</b> property of the <see cref="DropdownMenuAction"/> item. This object is accessible through the action callback.</param>
/// <example>
/// <code>
/// <![CDATA[
///using UnityEditor;
///using UnityEngine;
///using UnityEngine.UIElements;
///
///public class ContextMenuWindow : EditorWindow
///{
/// [MenuItem("My/Context Menu Window")]
/// static void ShowMe() => GetWindow<ContextMenuWindow>();
///
/// void CreateGUI()
/// {
/// var contextMenuContainer = new VisualElement();
/// contextMenuContainer.style.flexGrow = 1;
/// contextMenuContainer.AddManipulator(new ContextualMenuManipulator(e =>
/// {
/// e.menu.AppendAction("My Action 1", a => Debug.Log("My Action 1 Works"), DropdownMenuAction.Status.Normal); // 0
/// e.menu.AppendAction("My Action 3", a => Debug.Log("My Action 3 Works"), DropdownMenuAction.Status.Normal); // 1
/// e.menu.AppendAction("Submenu/My Action 4", a => Debug.Log("My Action 4 Works"), DropdownMenuAction.Status.Normal); // 2
/// e.menu.AppendAction("Submenu/My Action 6", a => Debug.Log("My Action 6 Works"), DropdownMenuAction.Status.Normal); // 3
///
/// // Indices from 1 to 3 are shifted up index by 1. In result 'My Action 2' now has an index of 2.
/// e.menu.InsertAction(1, "My Action 2", a => Debug.Log("My Action 2 Works"), DropdownMenuAction.AlwaysEnabled);
///
/// // If we want to insert an between submenu items, we have to use shifted indices
/// e.menu.InsertAction(4, "Submenu/My Action 5", a => Debug.Log("My Action 5 Works"), DropdownMenuAction.AlwaysDisabled);
/// }));
///
/// rootVisualElement.Add(contextMenuContainer);
/// }
///}
/// ]]>
/// </code>
/// </example>
public void InsertAction(int atIndex, string actionName, Action<DropdownMenuAction> action, Func<DropdownMenuAction, DropdownMenuAction.Status> actionStatusCallback, object userData = null)
{
DropdownMenuAction menuAction = new DropdownMenuAction(actionName, action, actionStatusCallback, userData);
m_MenuItems.Insert(atIndex, menuAction);
}
/// <summary>
/// Adds an item that executes an action in the dropdown menu.
/// </summary>
/// <remarks>
/// The item is added to the end of the specified index in the list.
/// </remarks>
/// <param name="atIndex">The index to insert the item at.</param>
/// <param name="actionName">The name of the item. This name is displayed in the dropdown menu.</param>
/// <param name="action">The callback to execute when the user selects this item in the menu.</param>
/// <param name="status">The status of the item.</param>
/// <example>
/// <code>
/// <![CDATA[
///using UnityEditor;
///using UnityEngine;
///using UnityEngine.UIElements;
///
///public class ContextMenuWindow : EditorWindow
///{
/// [MenuItem("My/Context Menu Window")]
/// static void ShowMe() => GetWindow<ContextMenuWindow>();
///
/// void CreateGUI()
/// {
/// var contextMenuContainer = new VisualElement();
/// contextMenuContainer.style.flexGrow = 1;
/// contextMenuContainer.AddManipulator(new ContextualMenuManipulator(e =>
/// {
/// e.menu.AppendAction("My Action 1", a => Debug.Log("My Action 1 Works"), DropdownMenuAction.Status.Normal); // 0
/// e.menu.AppendAction("My Action 3", a => Debug.Log("My Action 3 Works"), DropdownMenuAction.Status.Normal); // 1
/// e.menu.AppendAction("Submenu/My Action 4", a => Debug.Log("My Action 4 Works"), DropdownMenuAction.Status.Normal); // 2
/// e.menu.AppendAction("Submenu/My Action 6", a => Debug.Log("My Action 6 Works"), DropdownMenuAction.Status.Normal); // 3
///
/// // Indices from 1 to 3 are shifted up index by 1. In result 'My Action 2' now has an index of 2.
/// e.menu.InsertAction(1, "My Action 2", a => Debug.Log("My Action 2 Works"), DropdownMenuAction.Status.Normal);
///
/// // If we want to insert an between submenu items, we have to use shifted indices
/// e.menu.InsertAction(4, "Submenu/My Action 5", a => Debug.Log("My Action 5 Works"), DropdownMenuAction.Status.Disabled);
/// }));
///
/// rootVisualElement.Add(contextMenuContainer);
/// }
///}
/// ]]>
/// </code>
/// </example>
public void InsertAction(int atIndex, string actionName, Action<DropdownMenuAction> action, DropdownMenuAction.Status status = DropdownMenuAction.Status.Normal)
{
if (status == DropdownMenuAction.Status.Normal)
{
InsertAction(atIndex, actionName, action, DropdownMenuAction.AlwaysEnabled);
}
else if (status == DropdownMenuAction.Status.Disabled)
{
InsertAction(atIndex, actionName, action, DropdownMenuAction.AlwaysDisabled);
}
else
{
InsertAction(atIndex, actionName, action, e => status);
}
}
/// <summary>
/// Adds a separator line in the menu.
/// </summary>
/// <remarks>
/// The separator is added at the end of the current item list.
/// </remarks>
/// <param name="subMenuPath">The submenu path to add the separator to. Path components are delimited by forward slashes ('/').</param>
/// <example>
/// <code>
/// <![CDATA[
///using UnityEditor;
///using UnityEngine;
///using UnityEngine.UIElements;
///
///public class ContextMenuWindow : EditorWindow
///{
/// [MenuItem("My/Context Menu Window")]
/// static void ShowMe() => GetWindow<ContextMenuWindow>();
///
/// void CreateGUI()
/// {
/// var contextMenuContainer = new VisualElement();
/// contextMenuContainer.style.flexGrow = 1;
/// contextMenuContainer.AddManipulator(new ContextualMenuManipulator(e =>
/// {
/// e.menu.AppendAction("My Action 1", a => Debug.Log("My Action 1 Works"), DropdownMenuAction.Status.Normal);
/// e.menu.AppendSeparator();
/// e.menu.AppendAction("My Action 2", a => Debug.Log("My Action 2 Works"), DropdownMenuAction.Status.Normal);
///
/// e.menu.AppendAction("Submenu/My Action 3", a => Debug.Log("My Action 3 Works"), DropdownMenuAction.Status.Normal);
/// e.menu.AppendSeparator("Submenu/");
/// e.menu.AppendAction("Submenu/My Action 4", a => Debug.Log("My Action 4 Works"), DropdownMenuAction.Status.Normal);
/// }));
///
/// rootVisualElement.Add(contextMenuContainer);
/// }
///}
/// ]]>
/// </code>
/// </example>
public void AppendSeparator(string subMenuPath = null)
{
if (m_MenuItems.Count > 0 && !(m_MenuItems[m_MenuItems.Count - 1] is DropdownMenuSeparator))
{
DropdownMenuSeparator separator = new DropdownMenuSeparator(subMenuPath ?? String.Empty);
m_MenuItems.Add(separator);
}
}
/// <summary>
/// Adds a separator line in the menu.
/// </summary>
/// <remarks>
/// The separator is added at the end of the specified index in the list.
/// </remarks>
/// <param name="subMenuPath">The submenu path to add the separator to. Path components are delimited by forward slashes ('/').</param>
/// <param name="atIndex">The index to insert the separator at.</param>
/// <example>
/// <code>
/// <![CDATA[
///using UnityEditor;
///using UnityEngine;
///using UnityEngine.UIElements;
///
///public class ContextMenuWindow : EditorWindow
///{
/// [MenuItem("My/Context Menu Window")]
/// static void ShowMe() => GetWindow<ContextMenuWindow>();
///
/// void CreateGUI()
/// {
/// var contextMenuContainer = new VisualElement();
/// contextMenuContainer.style.flexGrow = 1;
/// contextMenuContainer.AddManipulator(new ContextualMenuManipulator(e =>
/// {
/// e.menu.AppendAction("My Action 1", a => Debug.Log("My Action 1 Works"), DropdownMenuAction.Status.Normal); // 0
/// e.menu.AppendAction("My Action 2", a => Debug.Log("My Action 2 Works"), DropdownMenuAction.Status.Normal); // 1
/// e.menu.AppendAction("Submenu/My Action 3", a => Debug.Log("My Action 3 Works"), DropdownMenuAction.Status.Normal); // 2
/// e.menu.AppendAction("Submenu/My Action 4", a => Debug.Log("My Action 4 Works"), DropdownMenuAction.Status.Normal); // 3
///
/// e.menu.InsertSeparator("/", 1); // Indices from 1 to 3 are shifted up index by 1. In result 'My Action 2' now has an index of 2.
/// e.menu.InsertSeparator("Submenu/", 4); // If we want to insert a separator between submenu items, we have to use shifted indices
/// }));
///
/// rootVisualElement.Add(contextMenuContainer);
/// }
///}
/// ]]>
/// </code>
/// </example>
public void InsertSeparator(string subMenuPath, int atIndex)
{
if (atIndex > 0 && atIndex <= m_MenuItems.Count && !(m_MenuItems[atIndex - 1] is DropdownMenuSeparator))
{
DropdownMenuSeparator separator = new DropdownMenuSeparator(subMenuPath ?? String.Empty);
m_MenuItems.Insert(atIndex, separator);
}
}
/// <summary>
/// Removes the menu item at index.
/// </summary>
/// <param name="index">The index of the item to remove.</param>
/// <example>
/// <code>
/// <![CDATA[
///using UnityEditor;
///using UnityEngine;
///using UnityEngine.UIElements;
///
///public class ContextMenuWindow : EditorWindow
///{
/// [MenuItem("My/Context Menu Window")]
/// static void ShowMe() => GetWindow<ContextMenuWindow>();
///
/// void CreateGUI()
/// {
/// var contextMenuContainer = new VisualElement();
/// contextMenuContainer.style.flexGrow = 1;
/// contextMenuContainer.AddManipulator(new ContextualMenuManipulator(e =>
/// {
/// e.menu.AppendAction("My Action 1", a => Debug.Log("My Action 1 Works"), DropdownMenuAction.Status.Normal);
/// e.menu.AppendAction("My Action 2", a => Debug.Log("My Action 2 Works"), DropdownMenuAction.Status.Normal);
/// e.menu.AppendAction("My Action 3", a => Debug.Log("My Action 3 Works"), DropdownMenuAction.Status.Normal);
///
/// e.menu.RemoveItemAt(0); // Remove My Action 1
/// e.menu.RemoveItemAt(1); // Remove My Action 3 (item indices have shifted after first removal)
/// }));
///
/// rootVisualElement.Add(contextMenuContainer);
/// }
///}
/// ]]>
/// </code>
/// </example>
public void RemoveItemAt(int index)
{
m_MenuItems.RemoveAt(index);
}
/// <summary>
/// Clears all items from the menu.
/// </summary>
/// <example>
/// <code>
/// <![CDATA[
///using UnityEditor;
///using UnityEngine;
///using UnityEngine.UIElements;
///
///public class ContextMenuWindow : EditorWindow
///{
/// [MenuItem("My/Context Menu Window")]
/// static void ShowMe() => GetWindow<ContextMenuWindow>();
///
/// void CreateGUI()
/// {
/// var contextMenuContainer = new VisualElement();
/// contextMenuContainer.style.flexGrow = 1;
/// contextMenuContainer.AddManipulator(new ContextualMenuManipulator(e =>
/// {
/// e.menu.AppendAction("My Action 1", a => Debug.Log("My Action 1 Works"), DropdownMenuAction.Status.Normal);
/// e.menu.AppendAction("My Action 2", a => Debug.Log("My Action 2 Works"), DropdownMenuAction.Status.Normal);
/// e.menu.ClearItems();
/// e.menu.AppendAction("My Action 3", a => Debug.Log("My Action 3 Works"), DropdownMenuAction.Status.Normal);
/// }));
///
/// rootVisualElement.Add(contextMenuContainer);
/// }
///}
/// ]]>
/// </code>
/// </example>
public void ClearItems()
{
m_MenuItems.Clear();
}
/// <summary>
/// Gets the status of all items by calling their status callback and removes the excess separators.
/// </summary>
/// <remarks>
/// Called before the menu is displayed.
/// </remarks>
/// <param name="e">The source event.</param>
public void PrepareForDisplay(EventBase e)
{
m_DropdownMenuEventInfo = e != null ? new DropdownMenuEventInfo(e) : null;
if (m_MenuItems.Count == 0)
return;
foreach (DropdownMenuItem item in m_MenuItems)
{
DropdownMenuAction action = item as DropdownMenuAction;
if (action != null)
{
action.UpdateActionStatus(m_DropdownMenuEventInfo);
}
}
if (m_MenuItems[m_MenuItems.Count - 1] is DropdownMenuSeparator)
{
m_MenuItems.RemoveAt(m_MenuItems.Count - 1);
}
}
}
}