committed by
GitHub
74 changed files with 1736 additions and 508 deletions
@ -0,0 +1,73 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Reactive; |
||||
|
using System.Threading.Tasks; |
||||
|
using Avalonia.Controls; |
||||
|
using ReactiveUI; |
||||
|
|
||||
|
namespace ControlCatalog.ViewModels |
||||
|
{ |
||||
|
public class ContextMenuPageViewModel |
||||
|
{ |
||||
|
public ContextMenuPageViewModel() |
||||
|
{ |
||||
|
OpenCommand = ReactiveCommand.CreateFromTask(Open); |
||||
|
SaveCommand = ReactiveCommand.Create(Save); |
||||
|
OpenRecentCommand = ReactiveCommand.Create<string>(OpenRecent); |
||||
|
|
||||
|
MenuItems = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel { Header = "_Open...", Command = OpenCommand }, |
||||
|
new MenuItemViewModel { Header = "Save", Command = SaveCommand }, |
||||
|
new MenuItemViewModel { Header = "-" }, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "Recent", |
||||
|
Items = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "File1.txt", |
||||
|
Command = OpenRecentCommand, |
||||
|
CommandParameter = @"c:\foo\File1.txt" |
||||
|
}, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "File2.txt", |
||||
|
Command = OpenRecentCommand, |
||||
|
CommandParameter = @"c:\foo\File2.txt" |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
public IReadOnlyList<MenuItemViewModel> MenuItems { get; set; } |
||||
|
public ReactiveCommand<Unit, Unit> OpenCommand { get; } |
||||
|
public ReactiveCommand<Unit, Unit> SaveCommand { get; } |
||||
|
public ReactiveCommand<string, Unit> OpenRecentCommand { get; } |
||||
|
|
||||
|
public async Task Open() |
||||
|
{ |
||||
|
var dialog = new OpenFileDialog(); |
||||
|
var result = await dialog.ShowAsync(App.Current.MainWindow); |
||||
|
|
||||
|
if (result != null) |
||||
|
{ |
||||
|
foreach (var path in result) |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine($"Opened: {path}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Save() |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine("Save"); |
||||
|
} |
||||
|
|
||||
|
public void OpenRecent(string path) |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine($"Open recent: {path}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Windows.Input; |
||||
|
|
||||
|
namespace ControlCatalog.ViewModels |
||||
|
{ |
||||
|
public class MenuItemViewModel |
||||
|
{ |
||||
|
public string Header { get; set; } |
||||
|
public ICommand Command { get; set; } |
||||
|
public object CommandParameter { get; set; } |
||||
|
public IList<MenuItemViewModel> Items { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,89 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Reactive; |
||||
|
using System.Threading.Tasks; |
||||
|
using Avalonia.Controls; |
||||
|
using ReactiveUI; |
||||
|
|
||||
|
namespace ControlCatalog.ViewModels |
||||
|
{ |
||||
|
public class MenuPageViewModel |
||||
|
{ |
||||
|
public MenuPageViewModel() |
||||
|
{ |
||||
|
OpenCommand = ReactiveCommand.CreateFromTask(Open); |
||||
|
SaveCommand = ReactiveCommand.Create(Save); |
||||
|
OpenRecentCommand = ReactiveCommand.Create<string>(OpenRecent); |
||||
|
|
||||
|
MenuItems = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "_File", |
||||
|
Items = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel { Header = "_Open...", Command = OpenCommand }, |
||||
|
new MenuItemViewModel { Header = "Save", Command = SaveCommand }, |
||||
|
new MenuItemViewModel { Header = "-" }, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "Recent", |
||||
|
Items = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "File1.txt", |
||||
|
Command = OpenRecentCommand, |
||||
|
CommandParameter = @"c:\foo\File1.txt" |
||||
|
}, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "File2.txt", |
||||
|
Command = OpenRecentCommand, |
||||
|
CommandParameter = @"c:\foo\File2.txt" |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
new MenuItemViewModel |
||||
|
{ |
||||
|
Header = "_Edit", |
||||
|
Items = new[] |
||||
|
{ |
||||
|
new MenuItemViewModel { Header = "_Copy" }, |
||||
|
new MenuItemViewModel { Header = "_Paste" }, |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
public IReadOnlyList<MenuItemViewModel> MenuItems { get; set; } |
||||
|
public ReactiveCommand<Unit, Unit> OpenCommand { get; } |
||||
|
public ReactiveCommand<Unit, Unit> SaveCommand { get; } |
||||
|
public ReactiveCommand<string, Unit> OpenRecentCommand { get; } |
||||
|
|
||||
|
public async Task Open() |
||||
|
{ |
||||
|
var dialog = new OpenFileDialog(); |
||||
|
var result = await dialog.ShowAsync(App.Current.MainWindow); |
||||
|
|
||||
|
if (result != null) |
||||
|
{ |
||||
|
foreach (var path in result) |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine($"Opened: {path}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Save() |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine("Save"); |
||||
|
} |
||||
|
|
||||
|
public void OpenRecent(string path) |
||||
|
{ |
||||
|
System.Diagnostics.Debug.WriteLine($"Open recent: {path}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,219 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Reflection; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace Avalonia.Utilities |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Manages subscriptions to events using weak listeners.
|
||||
|
/// </summary>
|
||||
|
public static class WeakEventHandlerManager |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Subscribes to an event on an object using a weak subscription.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TTarget">The type of the target.</typeparam>
|
||||
|
/// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
|
||||
|
/// <param name="target">The event source.</param>
|
||||
|
/// <param name="eventName">The name of the event.</param>
|
||||
|
/// <param name="subscriber">The subscriber.</param>
|
||||
|
public static void Subscribe<TTarget, TEventArgs, TSubscriber>(TTarget target, string eventName, EventHandler<TEventArgs> subscriber) |
||||
|
where TEventArgs : EventArgs where TSubscriber : class |
||||
|
{ |
||||
|
var dic = SubscriptionTypeStorage<TEventArgs, TSubscriber>.Subscribers.GetOrCreateValue(target); |
||||
|
Subscription<TEventArgs, TSubscriber> sub; |
||||
|
|
||||
|
if (!dic.TryGetValue(eventName, out sub)) |
||||
|
{ |
||||
|
dic[eventName] = sub = new Subscription<TEventArgs, TSubscriber>(dic, typeof(TTarget), target, eventName); |
||||
|
} |
||||
|
|
||||
|
sub.Add(subscriber); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Unsubscribes from an event.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
|
||||
|
/// <param name="target">The event source.</param>
|
||||
|
/// <param name="eventName">The name of the event.</param>
|
||||
|
/// <param name="subscriber">The subscriber.</param>
|
||||
|
public static void Unsubscribe<TEventArgs, TSubscriber>(object target, string eventName, EventHandler<TEventArgs> subscriber) |
||||
|
where TEventArgs : EventArgs where TSubscriber : class |
||||
|
{ |
||||
|
SubscriptionDic<TEventArgs, TSubscriber> dic; |
||||
|
|
||||
|
if (SubscriptionTypeStorage<TEventArgs, TSubscriber>.Subscribers.TryGetValue(target, out dic)) |
||||
|
{ |
||||
|
Subscription<TEventArgs, TSubscriber> sub; |
||||
|
|
||||
|
if (dic.TryGetValue(eventName, out sub)) |
||||
|
{ |
||||
|
sub.Remove(subscriber); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static class SubscriptionTypeStorage<TArgs, TSubscriber> |
||||
|
where TArgs : EventArgs where TSubscriber : class |
||||
|
{ |
||||
|
public static readonly ConditionalWeakTable<object, SubscriptionDic<TArgs, TSubscriber>> Subscribers |
||||
|
= new ConditionalWeakTable<object, SubscriptionDic<TArgs, TSubscriber>>(); |
||||
|
} |
||||
|
|
||||
|
private class SubscriptionDic<T, TSubscriber> : Dictionary<string, Subscription<T, TSubscriber>> |
||||
|
where T : EventArgs where TSubscriber : class |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
private static readonly Dictionary<Type, Dictionary<string, EventInfo>> Accessors |
||||
|
= new Dictionary<Type, Dictionary<string, EventInfo>>(); |
||||
|
|
||||
|
private class Subscription<T, TSubscriber> where T : EventArgs where TSubscriber : class |
||||
|
{ |
||||
|
private readonly EventInfo _info; |
||||
|
private readonly SubscriptionDic<T, TSubscriber> _sdic; |
||||
|
private readonly object _target; |
||||
|
private readonly string _eventName; |
||||
|
private readonly Delegate _delegate; |
||||
|
|
||||
|
private Descriptor[] _data = new Descriptor[2]; |
||||
|
private int _count = 0; |
||||
|
|
||||
|
delegate void CallerDelegate(TSubscriber s, object sender, T args); |
||||
|
|
||||
|
struct Descriptor |
||||
|
{ |
||||
|
public WeakReference<TSubscriber> Subscriber; |
||||
|
public CallerDelegate Caller; |
||||
|
} |
||||
|
|
||||
|
private static Dictionary<MethodInfo, CallerDelegate> s_Callers = |
||||
|
new Dictionary<MethodInfo, CallerDelegate>(); |
||||
|
|
||||
|
public Subscription(SubscriptionDic<T, TSubscriber> sdic, Type targetType, object target, string eventName) |
||||
|
{ |
||||
|
_sdic = sdic; |
||||
|
_target = target; |
||||
|
_eventName = eventName; |
||||
|
Dictionary<string, EventInfo> evDic; |
||||
|
if (!Accessors.TryGetValue(targetType, out evDic)) |
||||
|
Accessors[targetType] = evDic = new Dictionary<string, EventInfo>(); |
||||
|
|
||||
|
if (!evDic.TryGetValue(eventName, out _info)) |
||||
|
{ |
||||
|
var ev = targetType.GetRuntimeEvents().FirstOrDefault(x => x.Name == eventName); |
||||
|
|
||||
|
if (ev == null) |
||||
|
{ |
||||
|
throw new ArgumentException( |
||||
|
$"The event {eventName} was not found on {target.GetType()}."); |
||||
|
} |
||||
|
|
||||
|
evDic[eventName] = _info = ev; |
||||
|
} |
||||
|
|
||||
|
var del = new Action<object, T>(OnEvent); |
||||
|
_delegate = del.GetMethodInfo().CreateDelegate(_info.EventHandlerType, del.Target); |
||||
|
_info.AddMethod.Invoke(target, new[] { _delegate }); |
||||
|
} |
||||
|
|
||||
|
void Destroy() |
||||
|
{ |
||||
|
_info.RemoveMethod.Invoke(_target, new[] { _delegate }); |
||||
|
_sdic.Remove(_eventName); |
||||
|
} |
||||
|
|
||||
|
public void Add(EventHandler<T> s) |
||||
|
{ |
||||
|
Compact(true); |
||||
|
if (_count == _data.Length) |
||||
|
{ |
||||
|
//Extend capacity
|
||||
|
var ndata = new Descriptor[_data.Length*2]; |
||||
|
Array.Copy(_data, ndata, _data.Length); |
||||
|
_data = ndata; |
||||
|
} |
||||
|
|
||||
|
var subscriber = (TSubscriber)s.Target; |
||||
|
if (!s_Callers.TryGetValue(s.Method, out var caller)) |
||||
|
s_Callers[s.Method] = caller = |
||||
|
(CallerDelegate)Delegate.CreateDelegate(typeof(CallerDelegate), null, s.Method); |
||||
|
_data[_count] = new Descriptor |
||||
|
{ |
||||
|
Caller = caller, |
||||
|
Subscriber = new WeakReference<TSubscriber>(subscriber) |
||||
|
}; |
||||
|
_count++; |
||||
|
} |
||||
|
|
||||
|
public void Remove(EventHandler<T> s) |
||||
|
{ |
||||
|
var removed = false; |
||||
|
|
||||
|
for (int c = 0; c < _count; ++c) |
||||
|
{ |
||||
|
var reference = _data[c].Subscriber; |
||||
|
TSubscriber instance; |
||||
|
|
||||
|
if (reference != null && reference.TryGetTarget(out instance) && instance == s) |
||||
|
{ |
||||
|
_data[c] = default; |
||||
|
removed = true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (removed) |
||||
|
{ |
||||
|
Compact(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void Compact(bool preventDestroy = false) |
||||
|
{ |
||||
|
int empty = -1; |
||||
|
for (int c = 0; c < _count; c++) |
||||
|
{ |
||||
|
var r = _data[c]; |
||||
|
//Mark current index as first empty
|
||||
|
if (r.Subscriber == null && empty == -1) |
||||
|
empty = c; |
||||
|
//If current element isn't null and we have an empty one
|
||||
|
if (r.Subscriber != null && empty != -1) |
||||
|
{ |
||||
|
_data[c] = default; |
||||
|
_data[empty] = r; |
||||
|
empty++; |
||||
|
} |
||||
|
} |
||||
|
if (empty != -1) |
||||
|
_count = empty; |
||||
|
if (_count == 0 && !preventDestroy) |
||||
|
Destroy(); |
||||
|
} |
||||
|
|
||||
|
void OnEvent(object sender, T eventArgs) |
||||
|
{ |
||||
|
var needCompact = false; |
||||
|
for(var c=0; c<_count; c++) |
||||
|
{ |
||||
|
var r = _data[c].Subscriber; |
||||
|
TSubscriber sub; |
||||
|
if (r.TryGetTarget(out sub)) |
||||
|
{ |
||||
|
_data[c].Caller(sub, sender, eventArgs); |
||||
|
} |
||||
|
else |
||||
|
needCompact = true; |
||||
|
} |
||||
|
if (needCompact) |
||||
|
Compact(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,192 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Avalonia.Controls.Generators; |
||||
|
using Avalonia.Controls.Platform; |
||||
|
using Avalonia.Controls.Primitives; |
||||
|
using Avalonia.Controls.Templates; |
||||
|
using Avalonia.Input; |
||||
|
using Avalonia.Interactivity; |
||||
|
using Avalonia.LogicalTree; |
||||
|
|
||||
|
namespace Avalonia.Controls |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Base class for menu controls.
|
||||
|
/// </summary>
|
||||
|
public abstract class MenuBase : SelectingItemsControl, IFocusScope, IMenu |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="IsOpen"/> property.
|
||||
|
/// </summary>
|
||||
|
public static readonly DirectProperty<Menu, bool> IsOpenProperty = |
||||
|
AvaloniaProperty.RegisterDirect<Menu, bool>( |
||||
|
nameof(IsOpen), |
||||
|
o => o.IsOpen); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="MenuOpened"/> event.
|
||||
|
/// </summary>
|
||||
|
public static readonly RoutedEvent<RoutedEventArgs> MenuOpenedEvent = |
||||
|
RoutedEvent.Register<MenuItem, RoutedEventArgs>(nameof(MenuOpened), RoutingStrategies.Bubble); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the <see cref="MenuClosed"/> event.
|
||||
|
/// </summary>
|
||||
|
public static readonly RoutedEvent<RoutedEventArgs> MenuClosedEvent = |
||||
|
RoutedEvent.Register<MenuItem, RoutedEventArgs>(nameof(MenuClosed), RoutingStrategies.Bubble); |
||||
|
|
||||
|
private bool _isOpen; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="MenuBase"/> class.
|
||||
|
/// </summary>
|
||||
|
public MenuBase() |
||||
|
{ |
||||
|
InteractionHandler = new DefaultMenuInteractionHandler(false); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="MenuBase"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="interactionHandler">The menu interaction handler.</param>
|
||||
|
public MenuBase(IMenuInteractionHandler interactionHandler) |
||||
|
{ |
||||
|
Contract.Requires<ArgumentNullException>(interactionHandler != null); |
||||
|
|
||||
|
InteractionHandler = interactionHandler; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes static members of the <see cref="MenuBase"/> class.
|
||||
|
/// </summary>
|
||||
|
static MenuBase() |
||||
|
{ |
||||
|
MenuItem.SubmenuOpenedEvent.AddClassHandler<MenuBase>(x => x.OnSubmenuOpened); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets a value indicating whether the menu is open.
|
||||
|
/// </summary>
|
||||
|
public bool IsOpen |
||||
|
{ |
||||
|
get { return _isOpen; } |
||||
|
protected set { SetAndRaise(IsOpenProperty, ref _isOpen, value); } |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
IMenuInteractionHandler IMenu.InteractionHandler => InteractionHandler; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
IMenuItem IMenuElement.SelectedItem |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
var index = SelectedIndex; |
||||
|
return (index != -1) ? |
||||
|
(IMenuItem)ItemContainerGenerator.ContainerFromIndex(index) : |
||||
|
null; |
||||
|
} |
||||
|
set |
||||
|
{ |
||||
|
SelectedIndex = ItemContainerGenerator.IndexFromContainer(value); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
IEnumerable<IMenuItem> IMenuElement.SubItems |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return ItemContainerGenerator.Containers |
||||
|
.Select(x => x.ContainerControl) |
||||
|
.OfType<IMenuItem>(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the interaction handler for the menu.
|
||||
|
/// </summary>
|
||||
|
protected IMenuInteractionHandler InteractionHandler { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Occurs when a <see cref="Menu"/> is opened.
|
||||
|
/// </summary>
|
||||
|
public event EventHandler<RoutedEventArgs> MenuOpened |
||||
|
{ |
||||
|
add { AddHandler(MenuOpenedEvent, value); } |
||||
|
remove { RemoveHandler(MenuOpenedEvent, value); } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Occurs when a <see cref="Menu"/> is closed.
|
||||
|
/// </summary>
|
||||
|
public event EventHandler<RoutedEventArgs> MenuClosed |
||||
|
{ |
||||
|
add { AddHandler(MenuClosedEvent, value); } |
||||
|
remove { RemoveHandler(MenuClosedEvent, value); } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Closes the menu.
|
||||
|
/// </summary>
|
||||
|
public abstract void Close(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Opens the menu.
|
||||
|
/// </summary>
|
||||
|
public abstract void Open(); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
bool IMenuElement.MoveSelection(NavigationDirection direction, bool wrap) => MoveSelection(direction, wrap); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
protected override IItemContainerGenerator CreateItemContainerGenerator() |
||||
|
{ |
||||
|
return new ItemContainerGenerator<MenuItem>(this, MenuItem.HeaderProperty, null); |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
protected override void OnKeyDown(KeyEventArgs e) |
||||
|
{ |
||||
|
// Don't handle here: let the interaction handler handle it.
|
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) |
||||
|
{ |
||||
|
base.OnAttachedToVisualTree(e); |
||||
|
InteractionHandler.Attach(this); |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) |
||||
|
{ |
||||
|
base.OnDetachedFromVisualTree(e); |
||||
|
InteractionHandler.Detach(this); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Called when a submenu opens somewhere in the menu.
|
||||
|
/// </summary>
|
||||
|
/// <param name="e">The event args.</param>
|
||||
|
protected virtual void OnSubmenuOpened(RoutedEventArgs e) |
||||
|
{ |
||||
|
if (e.Source is MenuItem menuItem && menuItem.Parent == this) |
||||
|
{ |
||||
|
foreach (var child in this.GetLogicalChildren().OfType<MenuItem>()) |
||||
|
{ |
||||
|
if (child != menuItem && child.IsSubMenuOpen) |
||||
|
{ |
||||
|
child.IsSubMenuOpen = false; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
IsOpen = true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,131 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Avalonia.Styling |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The OR style selector.
|
||||
|
/// </summary>
|
||||
|
internal class OrSelector : Selector |
||||
|
{ |
||||
|
private readonly IReadOnlyList<Selector> _selectors; |
||||
|
private string _selectorString; |
||||
|
private Type _targetType; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="OrSelector"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="selectors">The selectors to OR.</param>
|
||||
|
public OrSelector(IReadOnlyList<Selector> selectors) |
||||
|
{ |
||||
|
Contract.Requires<ArgumentNullException>(selectors != null); |
||||
|
Contract.Requires<ArgumentException>(selectors.Count > 1); |
||||
|
|
||||
|
_selectors = selectors; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override bool InTemplate => false; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override bool IsCombinator => false; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override Type TargetType |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
if (_targetType == null) |
||||
|
{ |
||||
|
_targetType = EvaluateTargetType(); |
||||
|
} |
||||
|
|
||||
|
return _targetType; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override string ToString() |
||||
|
{ |
||||
|
if (_selectorString == null) |
||||
|
{ |
||||
|
_selectorString = string.Join(", ", _selectors); |
||||
|
} |
||||
|
|
||||
|
return _selectorString; |
||||
|
} |
||||
|
|
||||
|
protected override SelectorMatch Evaluate(IStyleable control, bool subscribe) |
||||
|
{ |
||||
|
var activators = new List<IObservable<bool>>(); |
||||
|
var neverThisInstance = false; |
||||
|
|
||||
|
foreach (var selector in _selectors) |
||||
|
{ |
||||
|
var match = selector.Match(control, subscribe); |
||||
|
|
||||
|
switch (match.Result) |
||||
|
{ |
||||
|
case SelectorMatchResult.AlwaysThisType: |
||||
|
case SelectorMatchResult.AlwaysThisInstance: |
||||
|
return match; |
||||
|
case SelectorMatchResult.NeverThisInstance: |
||||
|
neverThisInstance = true; |
||||
|
break; |
||||
|
case SelectorMatchResult.Sometimes: |
||||
|
activators.Add(match.Activator); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (activators.Count > 1) |
||||
|
{ |
||||
|
return new SelectorMatch(StyleActivator.Or(activators)); |
||||
|
} |
||||
|
else if (activators.Count == 1) |
||||
|
{ |
||||
|
return new SelectorMatch(activators[0]); |
||||
|
} |
||||
|
else if (neverThisInstance) |
||||
|
{ |
||||
|
return SelectorMatch.NeverThisInstance; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
return SelectorMatch.NeverThisType; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected override Selector MovePrevious() => null; |
||||
|
|
||||
|
private Type EvaluateTargetType() |
||||
|
{ |
||||
|
var result = default(Type); |
||||
|
|
||||
|
foreach (var selector in _selectors) |
||||
|
{ |
||||
|
if (selector.TargetType == null) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
else if (result == null) |
||||
|
{ |
||||
|
result = selector.TargetType; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
while (!result.IsAssignableFrom(selector.TargetType)) |
||||
|
{ |
||||
|
result = result.BaseType; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,99 @@ |
|||||
|
using System; |
||||
|
using System.ComponentModel; |
||||
|
using System.Globalization; |
||||
|
using System.Linq; |
||||
|
using System.Linq.Expressions; |
||||
|
using System.Reflection; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Markup.Xaml.PortableXaml; |
||||
|
using Portable.Xaml; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.Converters |
||||
|
{ |
||||
|
internal class AvaloniaEventConverter : TypeConverter |
||||
|
{ |
||||
|
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
||||
|
{ |
||||
|
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); |
||||
|
} |
||||
|
|
||||
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
||||
|
{ |
||||
|
var text = value as string; |
||||
|
if (text != null) |
||||
|
{ |
||||
|
var rootObjectProvider = context.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider; |
||||
|
var destinationTypeProvider = context.GetService(typeof(IDestinationTypeProvider)) as IDestinationTypeProvider; |
||||
|
if (rootObjectProvider != null && destinationTypeProvider != null) |
||||
|
{ |
||||
|
var target = rootObjectProvider.RootObject; |
||||
|
var eventType = destinationTypeProvider.GetDestinationType(); |
||||
|
var eventParameters = eventType.GetRuntimeMethods().First(r => r.Name == "Invoke").GetParameters(); |
||||
|
// go in reverse to match System.Xaml behaviour
|
||||
|
var methods = target.GetType().GetRuntimeMethods().Reverse(); |
||||
|
|
||||
|
// find based on exact match parameter types first
|
||||
|
foreach (var method in methods) |
||||
|
{ |
||||
|
if (method.Name != text) |
||||
|
continue; |
||||
|
var parameters = method.GetParameters(); |
||||
|
if (eventParameters.Length != parameters.Length) |
||||
|
continue; |
||||
|
if (parameters.Length == 0) |
||||
|
return method.CreateDelegate(eventType, target); |
||||
|
|
||||
|
for (int i = 0; i < parameters.Length; i++) |
||||
|
{ |
||||
|
var param = parameters[i]; |
||||
|
var eventParam = eventParameters[i]; |
||||
|
if (param.ParameterType != eventParam.ParameterType) |
||||
|
break; |
||||
|
if (i == parameters.Length - 1) |
||||
|
return method.CreateDelegate(eventType, target); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// EnhancedXaml: Find method with compatible base class parameters
|
||||
|
foreach (var method in methods) |
||||
|
{ |
||||
|
if (method.Name != text) |
||||
|
continue; |
||||
|
var parameters = method.GetParameters(); |
||||
|
if (parameters.Length == 0 || eventParameters.Length != parameters.Length) |
||||
|
continue; |
||||
|
|
||||
|
for (int i = 0; i < parameters.Length; i++) |
||||
|
{ |
||||
|
var param = parameters[i]; |
||||
|
var eventParam = eventParameters[i]; |
||||
|
if (!param.ParameterType.GetTypeInfo().IsAssignableFrom(eventParam.ParameterType.GetTypeInfo())) |
||||
|
break; |
||||
|
if (i == parameters.Length - 1) |
||||
|
return method.CreateDelegate(eventType, target); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var contextProvider = (IXamlSchemaContextProvider)context.GetService(typeof(IXamlSchemaContextProvider)); |
||||
|
var avaloniaContext = (AvaloniaXamlSchemaContext)contextProvider.SchemaContext; |
||||
|
|
||||
|
if (avaloniaContext.IsDesignMode) |
||||
|
{ |
||||
|
// We want to ignore missing events in the designer, so if event handler
|
||||
|
// wasn't found create an empty delegate.
|
||||
|
var lambdaExpression = Expression.Lambda( |
||||
|
eventType, |
||||
|
Expression.Empty(), |
||||
|
eventParameters.Select(x => Expression.Parameter(x.ParameterType))); |
||||
|
return lambdaExpression.Compile(); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
throw new XamlObjectWriterException($"Referenced value method {text} in type {target.GetType()} indicated by event {eventType.FullName} was not found"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return base.ConvertFrom(context, culture, value); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,71 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Avalonia.Utilities; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Base.UnitTests |
||||
|
{ |
||||
|
public class WeakEventHandlerManagerTests |
||||
|
{ |
||||
|
class EventSource |
||||
|
{ |
||||
|
public event EventHandler<EventArgs> Event; |
||||
|
|
||||
|
public void Fire() |
||||
|
{ |
||||
|
Event?.Invoke(this, new EventArgs()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Subscriber |
||||
|
{ |
||||
|
private readonly Action _onEvent; |
||||
|
|
||||
|
public Subscriber(Action onEvent) |
||||
|
{ |
||||
|
_onEvent = onEvent; |
||||
|
} |
||||
|
|
||||
|
public void OnEvent(object sender, EventArgs ev) |
||||
|
{ |
||||
|
_onEvent?.Invoke(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void EventShoudBePassedToSubscriber() |
||||
|
{ |
||||
|
bool handled = false; |
||||
|
var subscriber = new Subscriber(() => handled = true); |
||||
|
var source = new EventSource(); |
||||
|
WeakEventHandlerManager.Subscribe<EventSource, EventArgs, Subscriber>(source, "Event", |
||||
|
subscriber.OnEvent); |
||||
|
source.Fire(); |
||||
|
Assert.True(handled); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[Fact] |
||||
|
public void EventHandlerShouldNotBeKeptAlive() |
||||
|
{ |
||||
|
bool handled = false; |
||||
|
var source = new EventSource(); |
||||
|
AddCollectableSubscriber(source, "Event", () => handled = true); |
||||
|
for (int c = 0; c < 10; c++) |
||||
|
{ |
||||
|
GC.Collect(); |
||||
|
GC.Collect(3, GCCollectionMode.Forced, true); |
||||
|
} |
||||
|
source.Fire(); |
||||
|
Assert.False(handled); |
||||
|
} |
||||
|
|
||||
|
private void AddCollectableSubscriber(EventSource source, string name, Action func) |
||||
|
{ |
||||
|
WeakEventHandlerManager.Subscribe<EventSource, EventArgs, Subscriber>(source, name, new Subscriber(func).OnEvent); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using Avalonia.Controls.Shapes; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Controls.UnitTests.Shapes |
||||
|
{ |
||||
|
public class PathTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Path_With_Null_Data_Does_Not_Throw_On_Measure() |
||||
|
{ |
||||
|
var target = new Path(); |
||||
|
|
||||
|
target.Measure(Size.Infinity); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,66 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Input; |
||||
|
using Portable.Xaml; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.UnitTests.Xaml |
||||
|
{ |
||||
|
public class EventTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Event_Is_Attached() |
||||
|
{ |
||||
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='OnClick'/>"; |
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var target = new MyButton(); |
||||
|
|
||||
|
loader.Load(xaml, rootInstance: target); |
||||
|
RaiseClick(target); |
||||
|
|
||||
|
Assert.True(target.Clicked); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Exception_Is_Thrown_If_Event_Not_Found() |
||||
|
{ |
||||
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='NotFound'/>"; |
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var target = new MyButton(); |
||||
|
|
||||
|
Assert.Throws<XamlObjectWriterException>(() => loader.Load(xaml, rootInstance: target)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Exception_Is_Not_Thrown_If_Event_Not_Found_In_Design_Mode() |
||||
|
{ |
||||
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='NotFound'/>"; |
||||
|
var loader = new AvaloniaXamlLoader { IsDesignMode = true }; |
||||
|
var target = new MyButton(); |
||||
|
|
||||
|
loader.Load(xaml, rootInstance: target); |
||||
|
} |
||||
|
|
||||
|
private void RaiseClick(MyButton target) |
||||
|
{ |
||||
|
target.RaiseEvent(new KeyEventArgs |
||||
|
{ |
||||
|
RoutedEvent = Button.KeyDownEvent, |
||||
|
Key = Key.Enter, |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
class MyButton : Button |
||||
|
{ |
||||
|
public bool Clicked { get; private set; } |
||||
|
|
||||
|
public void OnClick(object sender, EventArgs e) |
||||
|
{ |
||||
|
Clicked = true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,106 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Styling.UnitTests |
||||
|
{ |
||||
|
public class SelectorTests_Or |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Or_Selector_Should_Have_Correct_String_Representation() |
||||
|
{ |
||||
|
var target = Selectors.Or( |
||||
|
default(Selector).OfType<Control1>().Class("foo"), |
||||
|
default(Selector).OfType<Control2>().Class("bar")); |
||||
|
|
||||
|
Assert.Equal("Control1.foo, Control2.bar", target.ToString()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Or_Selector_Matches_Control_Of_Correct_Type() |
||||
|
{ |
||||
|
var target = Selectors.Or( |
||||
|
default(Selector).OfType<Control1>(), |
||||
|
default(Selector).OfType<Control2>().Class("bar")); |
||||
|
var control = new Control1(); |
||||
|
|
||||
|
Assert.Equal(SelectorMatchResult.AlwaysThisType, target.Match(control).Result); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Or_Selector_Matches_Control_Of_Correct_Type_With_Class() |
||||
|
{ |
||||
|
var target = Selectors.Or( |
||||
|
default(Selector).OfType<Control1>(), |
||||
|
default(Selector).OfType<Control2>().Class("bar")); |
||||
|
var control = new Control2(); |
||||
|
|
||||
|
Assert.Equal(SelectorMatchResult.Sometimes, target.Match(control).Result); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Or_Selector_Doesnt_Match_Control_Of_Incorrect_Type() |
||||
|
{ |
||||
|
var target = Selectors.Or( |
||||
|
default(Selector).OfType<Control1>(), |
||||
|
default(Selector).OfType<Control2>().Class("bar")); |
||||
|
var control = new Control3(); |
||||
|
|
||||
|
Assert.Equal(SelectorMatchResult.NeverThisType, target.Match(control).Result); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Or_Selector_Doesnt_Match_Control_With_Incorrect_Name() |
||||
|
{ |
||||
|
var target = Selectors.Or( |
||||
|
default(Selector).OfType<Control1>().Name("foo"), |
||||
|
default(Selector).OfType<Control2>().Name("foo")); |
||||
|
var control = new Control1 { Name = "bar" }; |
||||
|
|
||||
|
Assert.Equal(SelectorMatchResult.NeverThisInstance, target.Match(control).Result); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Returns_Correct_TargetType_When_Types_Same() |
||||
|
{ |
||||
|
var target = Selectors.Or( |
||||
|
default(Selector).OfType<Control1>().Class("foo"), |
||||
|
default(Selector).OfType<Control1>().Class("bar")); |
||||
|
|
||||
|
Assert.Equal(typeof(Control1), target.TargetType); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Returns_Common_TargetType() |
||||
|
{ |
||||
|
var target = Selectors.Or( |
||||
|
default(Selector).OfType<Control1>().Class("foo"), |
||||
|
default(Selector).OfType<Control2>().Class("bar")); |
||||
|
|
||||
|
Assert.Equal(typeof(TestControlBase), target.TargetType); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Returns_Null_TargetType_When_A_Selector_Has_No_TargetType() |
||||
|
{ |
||||
|
var target = Selectors.Or( |
||||
|
default(Selector).OfType<Control1>().Class("foo"), |
||||
|
default(Selector).Class("bar")); |
||||
|
|
||||
|
Assert.Equal(null, target.TargetType); |
||||
|
} |
||||
|
|
||||
|
public class Control1 : TestControlBase |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public class Control2 : TestControlBase |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public class Control3 : TestControlBase |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 610 B |
|
After Width: | Height: | Size: 610 B |
Loading…
Reference in new issue