committed by
GitHub
13 changed files with 534 additions and 373 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; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue