-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
MenuBar.cs
130 lines (104 loc) · 2.74 KB
/
MenuBar.cs
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
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Microsoft.Maui.Controls
{
public partial class MenuBar : Element, IMenuBar
{
/// <summary>Bindable property for <see cref="IsEnabled"/>.</summary>
public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool),
typeof(MenuBar), true);
public bool IsEnabled
{
get { return (bool)GetValue(IsEnabledProperty); }
set { SetValue(IsEnabledProperty, value); }
}
readonly ObservableCollection<IMenuBarItem> _menus = new ObservableCollection<IMenuBarItem>();
public IMenuBarItem this[int index]
{
get { return _menus[index]; }
set
{
RemoveAt(index);
Insert(index, value);
}
}
public int Count => _menus.Count;
public bool IsReadOnly => false;
public void Add(IMenuBarItem item)
{
var index = _menus.Count;
_menus.Add(item);
NotifyHandler(nameof(IMenuBarHandler.Add), index, item);
}
internal void SyncMenuBarItemsFromPages(IList<MenuBarItem> menuBarItems)
{
for (int i = 0; i < menuBarItems.Count; i++)
{
var menuBarItem = menuBarItems[i];
if (this.Count > i && this[i] == menuBarItem)
continue;
if (this.Contains(menuBarItem))
Remove(menuBarItem);
Insert(i, menuBarItem);
}
while (this.Count > menuBarItems.Count)
RemoveAt(this.Count - 1);
}
public void Clear()
{
for (int i = _menus.Count - 1; i >= 0; i--)
RemoveAt(i);
}
public bool Contains(IMenuBarItem item)
{
return _menus.Contains(item);
}
public void CopyTo(IMenuBarItem[] array, int arrayIndex)
{
_menus.CopyTo(array, arrayIndex);
}
public IEnumerator<IMenuBarItem> GetEnumerator()
{
return _menus.GetEnumerator();
}
public int IndexOf(IMenuBarItem item)
{
return _menus.IndexOf(item);
}
public void Insert(int index, IMenuBarItem item)
{
_menus.Insert(index, item);
NotifyHandler(nameof(IMenuBarHandler.Insert), index, item);
}
public bool Remove(IMenuBarItem item)
{
var index = _menus.IndexOf(item);
var result = _menus.Remove(item);
NotifyHandler(nameof(IMenuBarHandler.Remove), index, item);
if (item is Element e)
e.Parent = null;
return result;
}
public void RemoveAt(int index)
{
var item = _menus[index];
if (index < 0)
return;
_menus.RemoveAt(index);
NotifyHandler(nameof(IMenuBarHandler.Remove), index, item);
if (item is Element e)
e.Parent = null;
}
IEnumerator IEnumerable.GetEnumerator()
{
return _menus.GetEnumerator();
}
void NotifyHandler(string action, int index, IMenuBarItem view)
{
Handler?.Invoke(action, new Maui.Handlers.MenuBarHandlerUpdate(index, view));
}
}
}