-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Context Menu for add native new files
- Loading branch information
Showing
23 changed files
with
511 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/SDK/XFiler.SDK/Services/ContextMenu/IAddNewContextMenuModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Windows.Controls; | ||
|
||
namespace XFiler.SDK; | ||
|
||
public interface IAddNewContextMenuModel | ||
{ | ||
string Extension { get; } | ||
string Name { get; } | ||
byte[]? Data { get; } | ||
string? Template { get; } | ||
Image? Icon { get; } | ||
} |
14 changes: 4 additions & 10 deletions
14
...DK/Services/IRegistryContextMenuLoader.cs → ...s/ContextMenu/INativeContextMenuLoader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,15 @@ | ||
using System.Collections.Generic; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
|
||
namespace XFiler.SDK; | ||
|
||
public interface INativeContextMenuLoader | ||
{ | ||
IReadOnlyList<IAddNewContextMenuModel> AddItems { get; } | ||
ICommand InvokeCommand { get; } | ||
ICommand InvokeAddNewItemsCommand { get; } | ||
|
||
IReadOnlyList<IRegistryContextMenuModel> CreateMenuItems(IEnumerable<string> selectedItems); | ||
} | ||
|
||
public interface IRegistryContextMenuModel | ||
{ | ||
string Name { get; } | ||
void Init(); | ||
|
||
ImageSource? Icon { get; } | ||
|
||
IReadOnlyList<IRegistryContextMenuModel>? Children { get; } | ||
IReadOnlyList<IRegistryContextMenuModel> CreateMenuItems(IEnumerable<string> selectedItems); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/SDK/XFiler.SDK/Services/ContextMenu/IRegistryContextMenuModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Collections.Generic; | ||
using System.Windows.Media; | ||
|
||
namespace XFiler.SDK; | ||
|
||
public interface IRegistryContextMenuModel | ||
{ | ||
string Name { get; } | ||
|
||
ImageSource? Icon { get; } | ||
|
||
IReadOnlyList<IRegistryContextMenuModel>? Children { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
using System; | ||
|
||
namespace XFiler.SDK | ||
namespace XFiler.SDK; | ||
|
||
public interface IDriveDetector | ||
{ | ||
event Action<EventType, string> DriveChanged; | ||
|
||
void Init(); | ||
} | ||
|
||
public enum EventType | ||
{ | ||
public interface IDriveDetector | ||
{ | ||
event Action<EventType, string> DriveChanged; | ||
} | ||
|
||
public enum EventType | ||
{ | ||
Added = 2, | ||
Removed = 3 | ||
} | ||
Added = 2, | ||
Removed = 3 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
namespace XFiler.SDK | ||
namespace XFiler.SDK; | ||
|
||
public interface ILaunchAtStartupService | ||
{ | ||
public interface ILaunchAtStartupService | ||
{ | ||
void Init(); | ||
} | ||
void Init(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
namespace XFiler.SDK | ||
namespace XFiler.SDK; | ||
|
||
public interface IRestartService | ||
{ | ||
public interface IRestartService | ||
{ | ||
void RestartApplication(); | ||
void RestartApplication(); | ||
|
||
const string RestartKey = "/restart"; | ||
} | ||
const string RestartKey = "/restart"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/Themes/XFiler.GoogleChromeStyle/XFiler.GoogleChromeStyle.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System.Windows.Controls; | ||
using Microsoft.Xaml.Behaviors; | ||
|
||
namespace XFiler; | ||
|
||
internal sealed class AddNewContextMenuBehavior : Behavior<MenuItem> | ||
{ | ||
#region Attached Properties | ||
|
||
public static readonly DependencyProperty RootItemProperty = DependencyProperty.RegisterAttached( | ||
"RootItem", typeof(bool), typeof(AddNewContextMenuBehavior), | ||
new PropertyMetadata(false)); | ||
|
||
public static void SetRootItem(DependencyObject element, bool value) | ||
=> element.SetValue(RootItemProperty, value); | ||
|
||
public static bool GetRootItem(DependencyObject element) | ||
=> (bool) element.GetValue(RootItemProperty); | ||
|
||
#endregion | ||
|
||
#region Dependency Properties | ||
|
||
public static readonly DependencyProperty NativeContextMenuLoaderProperty = DependencyProperty.Register( | ||
nameof(NativeContextMenuLoader), typeof(INativeContextMenuLoader), | ||
typeof(AddNewContextMenuBehavior), | ||
new PropertyMetadata(default(INativeContextMenuLoader))); | ||
|
||
public static readonly DependencyProperty FileInfoModelProperty = DependencyProperty.Register( | ||
nameof(FileInfoModel), typeof(IFileSystemModel), | ||
typeof(AddNewContextMenuBehavior), | ||
new PropertyMetadata(default(IFileSystemModel))); | ||
|
||
#endregion | ||
|
||
#region Public Properties | ||
|
||
public INativeContextMenuLoader? NativeContextMenuLoader | ||
{ | ||
get => (INativeContextMenuLoader) GetValue(NativeContextMenuLoaderProperty); | ||
set => SetValue(NativeContextMenuLoaderProperty, value); | ||
} | ||
|
||
public IFileSystemModel? FileInfoModel | ||
{ | ||
get => (IFileSystemModel) GetValue(FileInfoModelProperty); | ||
set => SetValue(FileInfoModelProperty, value); | ||
} | ||
|
||
#endregion | ||
|
||
protected override void OnAttached() | ||
{ | ||
base.OnAttached(); | ||
|
||
AssociatedObject.Loaded += AssociatedObjectOnLoaded; | ||
} | ||
|
||
private void AssociatedObjectOnLoaded(object sender, RoutedEventArgs e) | ||
{ | ||
AssociatedObject.Loaded -= AssociatedObjectOnLoaded; | ||
|
||
if (NativeContextMenuLoader != null && FileInfoModel != null) | ||
LoadNativeContextMenuItems(NativeContextMenuLoader, FileInfoModel); | ||
} | ||
|
||
#region Private Methods | ||
|
||
private void LoadNativeContextMenuItems(INativeContextMenuLoader loader, IFileSystemModel parentInfo) | ||
{ | ||
ClearOldItems(); | ||
|
||
var rootItem = AssociatedObject.Items.OfType<DependencyObject>() | ||
.FirstOrDefault(GetRootItem); | ||
|
||
var index = AssociatedObject.Items.Count; | ||
|
||
if (rootItem != null) | ||
index = AssociatedObject.Items.IndexOf(rootItem) + 1; | ||
|
||
var models = loader.AddItems; | ||
|
||
foreach (var model in models) | ||
AssociatedObject.Items.Insert(index++, CreateRegistryMenuItem(loader, model, parentInfo)); | ||
} | ||
|
||
private void ClearOldItems() | ||
{ | ||
if (AssociatedObject.Items is { } items) | ||
{ | ||
var oldItems = items.OfType<MenuItem>() | ||
.Where(i => i.DataContext is IAddNewContextMenuModel) | ||
.ToList(); | ||
|
||
foreach (var oldItem in oldItems) | ||
AssociatedObject.Items.Remove(oldItem); | ||
} | ||
} | ||
|
||
private static MenuItem CreateRegistryMenuItem(INativeContextMenuLoader loader, IAddNewContextMenuModel model, | ||
IFileSystemModel parentModel) | ||
{ | ||
MenuItem item = new() | ||
{ | ||
Header = model.Name, | ||
DataContext = model, | ||
Command = loader.InvokeAddNewItemsCommand, | ||
CommandParameter = new Tuple<IAddNewContextMenuModel, IFileSystemModel>(model, parentModel), | ||
Icon = model.Icon, | ||
}; | ||
|
||
return item; | ||
} | ||
|
||
#endregion | ||
} |
Oops, something went wrong.