Add drag/drop handling
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public interface IAsyncMessageHandler<TMessage>
|
||||
public interface IAsyncMessageHandler
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface IAsyncMessageHandler<TMessage> : IAsyncMessageHandler
|
||||
{
|
||||
Task Handle(TMessage message, CancellationToken canellationToken = default);
|
||||
}
|
||||
|
||||
public interface IAsyncMessageHandler<TReturn, TMessage>
|
||||
public interface IAsyncMessageHandler<TReturn, TMessage> : IAsyncMessageHandler
|
||||
{
|
||||
Task<TReturn> Handle(TMessage message, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public interface IMessageHandler<TMessage>
|
||||
public interface IMessageHandler
|
||||
{
|
||||
|
||||
}
|
||||
public interface IMessageHandler<TMessage> : IMessageHandler
|
||||
{
|
||||
void Handle(TMessage message);
|
||||
}
|
||||
|
||||
public interface IMessageHandler<TReturn, TMessage>
|
||||
public interface IMessageHandler<TReturn, TMessage> : IMessageHandler
|
||||
{
|
||||
TReturn Handle(TMessage message);
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public interface IEventAggregatorInvoker
|
||||
public interface IMessageInvoker
|
||||
{
|
||||
void Invoke<TMessage>(object target, TMessage message, MethodInfo methodInfo);
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
|
||||
public static class IObservableExtensions
|
||||
{
|
||||
public static IDisposable WeakSubscribe<TMessage>(this IObservable<TMessage> observable, IEventAggregatorInvoker invoker, Action<TMessage> actionDelegate)
|
||||
public static IDisposable WeakSubscribe<TMessage>(this IObservable<TMessage> observable, IMessageInvoker invoker, Action<TMessage> actionDelegate)
|
||||
{
|
||||
var methodInfo = actionDelegate.Method;
|
||||
var weakReference = new WeakReference(actionDelegate.Target);
|
||||
|
||||
@@ -4,12 +4,55 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public static class IServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddAsyncHandler<TAsyncMessageHandle>(this IServiceCollection serviceCollection)
|
||||
where TAsyncMessageHandle : IAsyncMessageHandler
|
||||
{
|
||||
if (typeof(TAsyncMessageHandle).GetInterfaces().FirstOrDefault(x =>
|
||||
x.GetGenericTypeDefinition() == typeof(IAsyncMessageHandler<>)
|
||||
|| x.GetGenericTypeDefinition() == typeof(IAsyncMessageHandler<>)) is { } messageHandler)
|
||||
{
|
||||
var messageArguments = messageHandler.GetGenericArguments();
|
||||
if (messageArguments is { Length: 1 })
|
||||
{
|
||||
serviceCollection.AddTransient(typeof(IAsyncMessageHandler<>).MakeGenericType(messageArguments[0]), typeof(TAsyncMessageHandle));
|
||||
}
|
||||
else
|
||||
{
|
||||
serviceCollection.AddTransient(typeof(IAsyncMessageHandler<,>).MakeGenericType(messageArguments[0], messageArguments[1]), typeof(TAsyncMessageHandle));
|
||||
}
|
||||
}
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHandler<TMessageHandle>(this IServiceCollection serviceCollection)
|
||||
where TMessageHandle : IMessageHandler
|
||||
{
|
||||
if (typeof(TMessageHandle).GetInterfaces().FirstOrDefault(x =>
|
||||
x.GetGenericTypeDefinition() == typeof(IMessageHandler<>)
|
||||
|| x.GetGenericTypeDefinition() == typeof(IMessageHandler<,>)) is { } messageHandler)
|
||||
{
|
||||
var messageArguments = messageHandler.GetGenericArguments();
|
||||
|
||||
if (messageArguments is { Length: 1 })
|
||||
{
|
||||
serviceCollection.AddTransient(typeof(IMessageHandler<>).MakeGenericType(messageArguments[0]), typeof(TMessageHandle));
|
||||
}
|
||||
else
|
||||
{
|
||||
serviceCollection.AddTransient(typeof(IMessageHandler<,>).MakeGenericType(messageArguments[0], messageArguments[1]), typeof(TMessageHandle));
|
||||
}
|
||||
}
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddRequiredCore(this IServiceCollection serviceCollection)
|
||||
{
|
||||
return serviceCollection
|
||||
.AddSingleton<IDisposer, Disposer>()
|
||||
.AddSingleton<IServiceFactory>(provider => new ServiceFactory(provider.GetService, (type, parameter) => ActivatorUtilities.CreateInstance(provider, type, parameter)))
|
||||
.AddSingleton<IEventAggregatorInvoker, EventAggregatorInvoker>()
|
||||
.AddSingleton<IMessageInvoker, MessageInvoker>()
|
||||
.AddSingleton<IMessenger, Messenger>()
|
||||
.AddSingleton<IMediator, Mediator>()
|
||||
.AddSingleton<IInitializable, WndProcMonitor>()
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
{
|
||||
return HandleAsync<TResponse>(new TRequest(), parameters);
|
||||
}
|
||||
|
||||
public Task HandleAsync(object request, CancellationToken cancellationToken, params object[] parameters)
|
||||
{
|
||||
return GetHandler(typeof(IAsyncMessageHandler<>).MakeGenericType(request.GetType()), parameters)
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public class EventAggregatorInvoker : IEventAggregatorInvoker
|
||||
public class MessageInvoker : IMessageInvoker
|
||||
{
|
||||
public void Invoke<TMessage>(object target, TMessage message, MethodInfo methodInfo)
|
||||
{
|
||||
@@ -7,12 +7,12 @@ namespace TheXamlGuy.TaskbarGroup.Core
|
||||
{
|
||||
public class Messenger : IMessenger
|
||||
{
|
||||
public IEventAggregatorInvoker invoker;
|
||||
public IMessageInvoker invoker;
|
||||
private readonly ConcurrentDictionary<Type, object> subjects = new();
|
||||
|
||||
private IScheduler dispatcher;
|
||||
|
||||
public Messenger(IEventAggregatorInvoker invoker)
|
||||
public Messenger(IMessageInvoker invoker)
|
||||
{
|
||||
var synchronizationContext = SynchronizationContext.Current;
|
||||
if (synchronizationContext is null) throw new NullReferenceException(nameof(synchronizationContext));
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using Windows.UI.Xaml;
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation
|
||||
{
|
||||
public record class Drop<TTarget>(DragEventArgs DropEventArgs) where TTarget : UIElement
|
||||
{
|
||||
public TTarget Target { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,55 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Xaml.Interactivity;
|
||||
using System;
|
||||
using TheXamlGuy.TaskbarGroup.Core;
|
||||
using Windows.ApplicationModel.DataTransfer;
|
||||
using Windows.Storage;
|
||||
using Windows.UI.Xaml;
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation
|
||||
{
|
||||
public class DropTarget
|
||||
public class DropTarget : Behavior<FrameworkElement>
|
||||
{
|
||||
public void Initialize(UIElement target)
|
||||
public static readonly DependencyProperty MediatorProperty =
|
||||
DependencyProperty.Register(nameof(Mediator),
|
||||
typeof(IMediator), typeof(DropTarget),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
public IMediator Mediator
|
||||
{
|
||||
target.DragOver += OnDragOver;
|
||||
target.Drop += OnDrop;
|
||||
get => (IMediator)GetValue(MediatorProperty);
|
||||
set => SetValue(MediatorProperty, value);
|
||||
}
|
||||
|
||||
private async void OnDrop(object sender, DragEventArgs args)
|
||||
protected override void OnAttached()
|
||||
{
|
||||
if (args.DataView.Contains(StandardDataFormats.StorageItems))
|
||||
AssociatedObject.DragOver -= OnDragOver;
|
||||
AssociatedObject.Drop -= OnDrop;
|
||||
|
||||
AssociatedObject.DragOver += OnDragOver;
|
||||
AssociatedObject.Drop += OnDrop;
|
||||
|
||||
base.OnAttached();
|
||||
}
|
||||
|
||||
private void OnDrop(object sender, DragEventArgs args)
|
||||
{
|
||||
if (Mediator is not null)
|
||||
{
|
||||
var items = await args.DataView.GetStorageItemsAsync();
|
||||
if (items.Count > 0)
|
||||
{
|
||||
foreach (var storageItem in items)
|
||||
{
|
||||
if (storageItem is StorageFile storageFile)
|
||||
{
|
||||
if (storageFile.Path is { Length: > 0 })
|
||||
{
|
||||
var dropMessageType = typeof(Drop<>).MakeGenericType(sender.GetType());
|
||||
var dropMessage = Activator.CreateInstance(dropMessageType, args);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var properties = await storageFile.Properties.RetrievePropertiesAsync(new List<string>
|
||||
{
|
||||
"System.AppUserModel.ID"
|
||||
});
|
||||
|
||||
var appUserModelId = properties["System.AppUserModel.ID"];
|
||||
if (appUserModelId is not null)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (storageItem is StorageFolder storageFolder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Mediator.HandleAsync(dropMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
using TheXamlGuy.TaskbarGroup.Core;
|
||||
using Windows.UI.Xaml;
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation
|
||||
{
|
||||
public interface IDropHandler<TTarget> : IAsyncMessageHandler<Drop<TTarget>> where TTarget : UIElement
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,8 @@ namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation
|
||||
return serviceCollection
|
||||
.AddSingleton<TemplateSelector>()
|
||||
.AddSingleton<IDataTemplateCollection>(new DataTemplateCollection(new Dictionary<Type, Type>()))
|
||||
.AddSingleton<DataTemplateFactory>();
|
||||
.AddSingleton<DataTemplateFactory>()
|
||||
.AddSingleton<DropTarget>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-1
@@ -122,9 +122,11 @@
|
||||
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DataTemplateFactory.cs" />
|
||||
<Compile Include="DropTarget.cs" />
|
||||
<Compile Include="DataTemplateFactory.cs" />
|
||||
<Compile Include="Drop.cs" />
|
||||
<Compile Include="IDataTemplateFactory.cs" />
|
||||
<Compile Include="IDropHandler.cs" />
|
||||
<Compile Include="IServiceCollectionExtensions.cs" />
|
||||
<Compile Include="IWindowPrivate.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
@@ -142,6 +144,9 @@
|
||||
<PackageReference Include="Microsoft.UI.Xaml">
|
||||
<Version>2.8.0-prerelease.220118001</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
|
||||
<Version>2.0.1</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TheXamlGuy.TaskbarGroup.Core\TheXamlGuy.TaskbarGroup.Core.csproj">
|
||||
|
||||
@@ -24,10 +24,6 @@ namespace TheXamlGuy.TaskbarGroup.Flyout.Controls
|
||||
TemplateSettings = new TaskbarButtonFlyoutTemplateSettings();
|
||||
}
|
||||
|
||||
public event EventHandler<object> Closed;
|
||||
|
||||
public event EventHandler<object> Opened;
|
||||
|
||||
public TaskbarButtonFlyoutTemplateSettings TemplateSettings
|
||||
{
|
||||
get => (TaskbarButtonFlyoutTemplateSettings)GetValue(TemplateSettingsProperty);
|
||||
|
||||
@@ -78,6 +78,7 @@
|
||||
<Compile Include="Controls\TaskbarButtonFlyoutTemplateSettings.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Controls\TaskbarButtonFlyout.cs" />
|
||||
<Compile Include="Views\TaskbarButtonGroupDropHandler.cs" />
|
||||
<Compile Include="Views\TaskbarButtonGroupItemViewModel.cs" />
|
||||
<Compile Include="Views\TaskbarButtonGroupView.xaml.cs">
|
||||
<DependentUpon>TaskbarButtonGroupView.xaml</DependentUpon>
|
||||
@@ -119,6 +120,9 @@
|
||||
<PackageReference Include="Microsoft.UI.Xaml">
|
||||
<Version>2.8.0-prerelease.220118001</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
|
||||
<Version>2.0.1</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Page Include="Controls\TaskbarButtonFlyout.xaml">
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using TheXamlGuy.TaskbarGroup.Flyout.Foundation;
|
||||
using Windows.ApplicationModel.DataTransfer;
|
||||
using Windows.Storage;
|
||||
|
||||
namespace TheXamlGuy.TaskbarGroup.Flyout
|
||||
{
|
||||
public class TaskbarButtonGroupDropHandler : IDropHandler<TaskbarButtonGroupView>
|
||||
{
|
||||
public async Task Handle(Drop<TaskbarButtonGroupView> message, CancellationToken canellationToken = default)
|
||||
{
|
||||
if (message.DropEventArgs.DataView.Contains(StandardDataFormats.StorageItems))
|
||||
{
|
||||
var items = await message.DropEventArgs.DataView.GetStorageItemsAsync();
|
||||
foreach (var storageItem in items)
|
||||
{
|
||||
if (storageItem is StorageFile storageFile)
|
||||
{
|
||||
if (storageFile.Path is { Length: > 0 })
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var properties = await storageFile.Properties.RetrievePropertiesAsync(new List<string>
|
||||
{
|
||||
"System.AppUserModel.ID"
|
||||
});
|
||||
|
||||
var appUserModelId = properties["System.AppUserModel.ID"];
|
||||
if (appUserModelId is not null)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (storageItem is StorageFolder storageFolder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ namespace TheXamlGuy.TaskbarGroup.Flyout
|
||||
IServiceFactory serviceFactory,
|
||||
IDisposer disposer) : base(messenger, serviceFactory, disposer)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
@@ -2,8 +2,13 @@
|
||||
x:Class="TheXamlGuy.TaskbarGroup.Flyout.TaskbarButtonGroupView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:foundation="using:TheXamlGuy.TaskbarGroup.Flyout.Foundation"
|
||||
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
|
||||
AllowDrop="True">
|
||||
<StackPanel>
|
||||
<interactivity:Interaction.Behaviors>
|
||||
<foundation:DropTarget Mediator="{x:Bind ViewModel.Mediator}" />
|
||||
</interactivity:Interaction.Behaviors>
|
||||
<StackPanel AllowDrop="True" Background="Red">
|
||||
<TextBox HorizontalAlignment="Stretch" Text="{x:Bind ViewModel.Name, Mode=TwoWay}" />
|
||||
<GridView
|
||||
x:Name="GridView"
|
||||
@@ -30,5 +35,4 @@
|
||||
</GridView.ItemsPanel>
|
||||
</GridView>
|
||||
</StackPanel>
|
||||
|
||||
</UserControl>
|
||||
@@ -5,15 +5,19 @@ namespace TheXamlGuy.TaskbarGroup.Flyout
|
||||
{
|
||||
public partial class TaskbarButtonGroupViewModel : ObservableCollectionViewModel<TaskbarButtonGroupItemViewModel>
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string name = "hello";
|
||||
|
||||
public TaskbarButtonGroupViewModel(IMessenger messenger,
|
||||
IServiceFactory serviceFactory,
|
||||
IMediator mediator,
|
||||
IDisposer disposer) : base(messenger, serviceFactory, disposer)
|
||||
{
|
||||
Register<FileDropped>(OnFileDropped);
|
||||
Mediator = mediator;
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
private string name = "hello";
|
||||
public IMediator Mediator { get; }
|
||||
|
||||
private void OnFileDropped(FileDropped args)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
x:Class="TheXamlGuy.TaskbarGroup.Flyout.TaskbarButtonView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<Border>
|
||||
<Border Background="Transparent">
|
||||
<ContentControl
|
||||
Margin="12"
|
||||
HorizontalContentAlignment="Stretch"
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace TheXamlGuy.TaskbarGroup.Flyout
|
||||
IServiceFactory serviceFactory,
|
||||
IDisposer disposer,
|
||||
TemplateSelector templateSelector,
|
||||
TaskbarButtonGroupViewModel taskbarButtonGroupViewModel) : base(messenger, serviceFactory, disposer)
|
||||
TaskbarButtonGroupViewModel taskbarButtonGroupViewModel)
|
||||
: base(messenger, serviceFactory, disposer)
|
||||
{
|
||||
TemplateSelector = templateSelector;
|
||||
TaskbarButtonGroupViewModel = taskbarButtonGroupViewModel;
|
||||
|
||||
@@ -36,11 +36,12 @@ namespace TheXamlGuy.TaskbarGroup
|
||||
.AddRequiredCore()
|
||||
.AddRequiredFoundation()
|
||||
.AddRequiredFlyoutFoundation()
|
||||
.AddTransient<IMessageHandler<TaskbarButtonFlyoutActivation>, TaskbarButtonFlyoutActivationHandler>()
|
||||
.AddHandler<TaskbarButtonFlyoutActivationHandler>()
|
||||
.AddSingleton<TaskbarButtonFlyoutWindow>()
|
||||
.AddTransient<TaskbarButtonView>()
|
||||
.AddTransient<TaskbarButtonViewModel>()
|
||||
.AddTransient<TaskbarButtonGroupView>()
|
||||
.AddAsyncHandler<TaskbarButtonGroupDropHandler>()
|
||||
.AddTransient<TaskbarButtonGroupViewModel>();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user