Add drag/drop handling

This commit is contained in:
dan_clark@outlook.com
2022-03-23 21:19:23 +00:00
parent 2ac0e3ed26
commit 263704a772
22 changed files with 189 additions and 63 deletions
@@ -1,11 +1,16 @@
namespace TheXamlGuy.TaskbarGroup.Core namespace TheXamlGuy.TaskbarGroup.Core
{ {
public interface IAsyncMessageHandler<TMessage> public interface IAsyncMessageHandler
{
}
public interface IAsyncMessageHandler<TMessage> : IAsyncMessageHandler
{ {
Task Handle(TMessage message, CancellationToken canellationToken = default); 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); Task<TReturn> Handle(TMessage message, CancellationToken cancellationToken = default);
} }
@@ -1,11 +1,15 @@
namespace TheXamlGuy.TaskbarGroup.Core namespace TheXamlGuy.TaskbarGroup.Core
{ {
public interface IMessageHandler<TMessage> public interface IMessageHandler
{
}
public interface IMessageHandler<TMessage> : IMessageHandler
{ {
void Handle(TMessage message); void Handle(TMessage message);
} }
public interface IMessageHandler<TReturn, TMessage> public interface IMessageHandler<TReturn, TMessage> : IMessageHandler
{ {
TReturn Handle(TMessage message); TReturn Handle(TMessage message);
} }
@@ -2,7 +2,7 @@
namespace TheXamlGuy.TaskbarGroup.Core namespace TheXamlGuy.TaskbarGroup.Core
{ {
public interface IEventAggregatorInvoker public interface IMessageInvoker
{ {
void Invoke<TMessage>(object target, TMessage message, MethodInfo methodInfo); void Invoke<TMessage>(object target, TMessage message, MethodInfo methodInfo);
} }
@@ -6,7 +6,7 @@ namespace TheXamlGuy.TaskbarGroup.Core
public static class IObservableExtensions 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 methodInfo = actionDelegate.Method;
var weakReference = new WeakReference(actionDelegate.Target); var weakReference = new WeakReference(actionDelegate.Target);
@@ -4,12 +4,55 @@ namespace TheXamlGuy.TaskbarGroup.Core
{ {
public static class IServiceCollectionExtensions 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) public static IServiceCollection AddRequiredCore(this IServiceCollection serviceCollection)
{ {
return serviceCollection return serviceCollection
.AddSingleton<IDisposer, Disposer>() .AddSingleton<IDisposer, Disposer>()
.AddSingleton<IServiceFactory>(provider => new ServiceFactory(provider.GetService, (type, parameter) => ActivatorUtilities.CreateInstance(provider, type, parameter))) .AddSingleton<IServiceFactory>(provider => new ServiceFactory(provider.GetService, (type, parameter) => ActivatorUtilities.CreateInstance(provider, type, parameter)))
.AddSingleton<IEventAggregatorInvoker, EventAggregatorInvoker>() .AddSingleton<IMessageInvoker, MessageInvoker>()
.AddSingleton<IMessenger, Messenger>() .AddSingleton<IMessenger, Messenger>()
.AddSingleton<IMediator, Mediator>() .AddSingleton<IMediator, Mediator>()
.AddSingleton<IInitializable, WndProcMonitor>() .AddSingleton<IInitializable, WndProcMonitor>()
+1
View File
@@ -40,6 +40,7 @@
{ {
return HandleAsync<TResponse>(new TRequest(), parameters); return HandleAsync<TResponse>(new TRequest(), parameters);
} }
public Task HandleAsync(object request, CancellationToken cancellationToken, params object[] parameters) public Task HandleAsync(object request, CancellationToken cancellationToken, params object[] parameters)
{ {
return GetHandler(typeof(IAsyncMessageHandler<>).MakeGenericType(request.GetType()), parameters) return GetHandler(typeof(IAsyncMessageHandler<>).MakeGenericType(request.GetType()), parameters)
@@ -2,7 +2,7 @@
namespace TheXamlGuy.TaskbarGroup.Core namespace TheXamlGuy.TaskbarGroup.Core
{ {
public class EventAggregatorInvoker : IEventAggregatorInvoker public class MessageInvoker : IMessageInvoker
{ {
public void Invoke<TMessage>(object target, TMessage message, MethodInfo methodInfo) public void Invoke<TMessage>(object target, TMessage message, MethodInfo methodInfo)
{ {
+2 -2
View File
@@ -7,12 +7,12 @@ namespace TheXamlGuy.TaskbarGroup.Core
{ {
public class Messenger : IMessenger public class Messenger : IMessenger
{ {
public IEventAggregatorInvoker invoker; public IMessageInvoker invoker;
private readonly ConcurrentDictionary<Type, object> subjects = new(); private readonly ConcurrentDictionary<Type, object> subjects = new();
private IScheduler dispatcher; private IScheduler dispatcher;
public Messenger(IEventAggregatorInvoker invoker) public Messenger(IMessageInvoker invoker)
{ {
var synchronizationContext = SynchronizationContext.Current; var synchronizationContext = SynchronizationContext.Current;
if (synchronizationContext is null) throw new NullReferenceException(nameof(synchronizationContext)); 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 Microsoft.Xaml.Interactivity;
using System.Collections.Generic; using System;
using TheXamlGuy.TaskbarGroup.Core;
using Windows.ApplicationModel.DataTransfer; using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.UI.Xaml; using Windows.UI.Xaml;
namespace TheXamlGuy.TaskbarGroup.Flyout.Foundation 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; get => (IMediator)GetValue(MediatorProperty);
target.Drop += OnDrop; 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(); var dropMessageType = typeof(Drop<>).MakeGenericType(sender.GetType());
if (items.Count > 0) var dropMessage = Activator.CreateInstance(dropMessageType, args);
{
foreach (var storageItem in items)
{
if (storageItem is StorageFile storageFile)
{
if (storageFile.Path is { Length: > 0 })
{
} Mediator.HandleAsync(dropMessage);
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)
{
}
}
}
} }
} }
@@ -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 return serviceCollection
.AddSingleton<TemplateSelector>() .AddSingleton<TemplateSelector>()
.AddSingleton<IDataTemplateCollection>(new DataTemplateCollection(new Dictionary<Type, Type>())) .AddSingleton<IDataTemplateCollection>(new DataTemplateCollection(new Dictionary<Type, Type>()))
.AddSingleton<DataTemplateFactory>(); .AddSingleton<DataTemplateFactory>()
.AddSingleton<DropTarget>();
} }
} }
} }
@@ -122,9 +122,11 @@
<RestoreProjectStyle>PackageReference</RestoreProjectStyle> <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="DataTemplateFactory.cs" />
<Compile Include="DropTarget.cs" /> <Compile Include="DropTarget.cs" />
<Compile Include="DataTemplateFactory.cs" />
<Compile Include="Drop.cs" />
<Compile Include="IDataTemplateFactory.cs" /> <Compile Include="IDataTemplateFactory.cs" />
<Compile Include="IDropHandler.cs" />
<Compile Include="IServiceCollectionExtensions.cs" /> <Compile Include="IServiceCollectionExtensions.cs" />
<Compile Include="IWindowPrivate.cs" /> <Compile Include="IWindowPrivate.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@@ -142,6 +144,9 @@
<PackageReference Include="Microsoft.UI.Xaml"> <PackageReference Include="Microsoft.UI.Xaml">
<Version>2.8.0-prerelease.220118001</Version> <Version>2.8.0-prerelease.220118001</Version>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
<Version>2.0.1</Version>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\TheXamlGuy.TaskbarGroup.Core\TheXamlGuy.TaskbarGroup.Core.csproj"> <ProjectReference Include="..\TheXamlGuy.TaskbarGroup.Core\TheXamlGuy.TaskbarGroup.Core.csproj">
@@ -24,10 +24,6 @@ namespace TheXamlGuy.TaskbarGroup.Flyout.Controls
TemplateSettings = new TaskbarButtonFlyoutTemplateSettings(); TemplateSettings = new TaskbarButtonFlyoutTemplateSettings();
} }
public event EventHandler<object> Closed;
public event EventHandler<object> Opened;
public TaskbarButtonFlyoutTemplateSettings TemplateSettings public TaskbarButtonFlyoutTemplateSettings TemplateSettings
{ {
get => (TaskbarButtonFlyoutTemplateSettings)GetValue(TemplateSettingsProperty); get => (TaskbarButtonFlyoutTemplateSettings)GetValue(TemplateSettingsProperty);
@@ -78,6 +78,7 @@
<Compile Include="Controls\TaskbarButtonFlyoutTemplateSettings.cs" /> <Compile Include="Controls\TaskbarButtonFlyoutTemplateSettings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Controls\TaskbarButtonFlyout.cs" /> <Compile Include="Controls\TaskbarButtonFlyout.cs" />
<Compile Include="Views\TaskbarButtonGroupDropHandler.cs" />
<Compile Include="Views\TaskbarButtonGroupItemViewModel.cs" /> <Compile Include="Views\TaskbarButtonGroupItemViewModel.cs" />
<Compile Include="Views\TaskbarButtonGroupView.xaml.cs"> <Compile Include="Views\TaskbarButtonGroupView.xaml.cs">
<DependentUpon>TaskbarButtonGroupView.xaml</DependentUpon> <DependentUpon>TaskbarButtonGroupView.xaml</DependentUpon>
@@ -119,6 +120,9 @@
<PackageReference Include="Microsoft.UI.Xaml"> <PackageReference Include="Microsoft.UI.Xaml">
<Version>2.8.0-prerelease.220118001</Version> <Version>2.8.0-prerelease.220118001</Version>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
<Version>2.0.1</Version>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Page Include="Controls\TaskbarButtonFlyout.xaml"> <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, IServiceFactory serviceFactory,
IDisposer disposer) : base(messenger, serviceFactory, disposer) IDisposer disposer) : base(messenger, serviceFactory, disposer)
{ {
} }
public string Name { get; set; } public string Name { get; set; }
@@ -2,8 +2,13 @@
x:Class="TheXamlGuy.TaskbarGroup.Flyout.TaskbarButtonGroupView" x:Class="TheXamlGuy.TaskbarGroup.Flyout.TaskbarButtonGroupView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:foundation="using:TheXamlGuy.TaskbarGroup.Flyout.Foundation"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
AllowDrop="True"> 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}" /> <TextBox HorizontalAlignment="Stretch" Text="{x:Bind ViewModel.Name, Mode=TwoWay}" />
<GridView <GridView
x:Name="GridView" x:Name="GridView"
@@ -30,5 +35,4 @@
</GridView.ItemsPanel> </GridView.ItemsPanel>
</GridView> </GridView>
</StackPanel> </StackPanel>
</UserControl> </UserControl>
@@ -5,15 +5,19 @@ namespace TheXamlGuy.TaskbarGroup.Flyout
{ {
public partial class TaskbarButtonGroupViewModel : ObservableCollectionViewModel<TaskbarButtonGroupItemViewModel> public partial class TaskbarButtonGroupViewModel : ObservableCollectionViewModel<TaskbarButtonGroupItemViewModel>
{ {
[ObservableProperty]
private string name = "hello";
public TaskbarButtonGroupViewModel(IMessenger messenger, public TaskbarButtonGroupViewModel(IMessenger messenger,
IServiceFactory serviceFactory, IServiceFactory serviceFactory,
IMediator mediator,
IDisposer disposer) : base(messenger, serviceFactory, disposer) IDisposer disposer) : base(messenger, serviceFactory, disposer)
{ {
Register<FileDropped>(OnFileDropped); Register<FileDropped>(OnFileDropped);
Mediator = mediator;
} }
[ObservableProperty] public IMediator Mediator { get; }
private string name = "hello";
private void OnFileDropped(FileDropped args) private void OnFileDropped(FileDropped args)
{ {
@@ -2,7 +2,7 @@
x:Class="TheXamlGuy.TaskbarGroup.Flyout.TaskbarButtonView" x:Class="TheXamlGuy.TaskbarGroup.Flyout.TaskbarButtonView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border> <Border Background="Transparent">
<ContentControl <ContentControl
Margin="12" Margin="12"
HorizontalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"
@@ -9,7 +9,8 @@ namespace TheXamlGuy.TaskbarGroup.Flyout
IServiceFactory serviceFactory, IServiceFactory serviceFactory,
IDisposer disposer, IDisposer disposer,
TemplateSelector templateSelector, TemplateSelector templateSelector,
TaskbarButtonGroupViewModel taskbarButtonGroupViewModel) : base(messenger, serviceFactory, disposer) TaskbarButtonGroupViewModel taskbarButtonGroupViewModel)
: base(messenger, serviceFactory, disposer)
{ {
TemplateSelector = templateSelector; TemplateSelector = templateSelector;
TaskbarButtonGroupViewModel = taskbarButtonGroupViewModel; TaskbarButtonGroupViewModel = taskbarButtonGroupViewModel;
+2 -1
View File
@@ -36,11 +36,12 @@ namespace TheXamlGuy.TaskbarGroup
.AddRequiredCore() .AddRequiredCore()
.AddRequiredFoundation() .AddRequiredFoundation()
.AddRequiredFlyoutFoundation() .AddRequiredFlyoutFoundation()
.AddTransient<IMessageHandler<TaskbarButtonFlyoutActivation>, TaskbarButtonFlyoutActivationHandler>() .AddHandler<TaskbarButtonFlyoutActivationHandler>()
.AddSingleton<TaskbarButtonFlyoutWindow>() .AddSingleton<TaskbarButtonFlyoutWindow>()
.AddTransient<TaskbarButtonView>() .AddTransient<TaskbarButtonView>()
.AddTransient<TaskbarButtonViewModel>() .AddTransient<TaskbarButtonViewModel>()
.AddTransient<TaskbarButtonGroupView>() .AddTransient<TaskbarButtonGroupView>()
.AddAsyncHandler<TaskbarButtonGroupDropHandler>()
.AddTransient<TaskbarButtonGroupViewModel>(); .AddTransient<TaskbarButtonGroupViewModel>();
} }
} }