48 changed files with 1018 additions and 464 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,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,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,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; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue