added single instancing

This commit is contained in:
dan_clark@outlook.com
2022-03-26 21:14:22 +00:00
parent 506161e4b9
commit 6f20f5d1ba
6 changed files with 60 additions and 45 deletions
@@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows;
using TheXamlGuy.TaskbarGroup.Core; using TheXamlGuy.TaskbarGroup.Core;
namespace TheXamlGuy.TaskbarGroup namespace TheXamlGuy.TaskbarGroup
@@ -11,20 +10,13 @@ namespace TheXamlGuy.TaskbarGroup
{ {
private readonly TaskbarButtonFlyoutWindow flyoutWindow; private readonly TaskbarButtonFlyoutWindow flyoutWindow;
private readonly IEnumerable<IInitializable> initializables; private readonly IEnumerable<IInitializable> initializables;
private readonly IMediator mediator;
private bool isInitialized; private bool isInitialized;
public ApplicationHost(IEnumerable<IInitializable> initializables, public ApplicationHost(IEnumerable<IInitializable> initializables,
IMessenger messenger,
IMediator mediator,
TaskbarButtonFlyoutWindow flyoutWindow) TaskbarButtonFlyoutWindow flyoutWindow)
{ {
this.initializables = initializables; this.initializables = initializables;
this.mediator = mediator;
this.flyoutWindow = flyoutWindow; this.flyoutWindow = flyoutWindow;
messenger.Subscribe<TaskbarButtonInvoked>(OnTaskbarButtonInvoked);
messenger.Subscribe<TaskbarButtonDragEnter>(OnTaskbarButtonDragEnter);
} }
public async Task StartAsync(CancellationToken cancellationToken) public async Task StartAsync(CancellationToken cancellationToken)
@@ -40,6 +32,7 @@ namespace TheXamlGuy.TaskbarGroup
{ {
await Task.CompletedTask; await Task.CompletedTask;
} }
private async Task InitializeAsync() private async Task InitializeAsync()
{ {
if (!isInitialized) if (!isInitialized)
@@ -53,21 +46,6 @@ namespace TheXamlGuy.TaskbarGroup
} }
} }
private void OnTaskbarButtonDragEnter(TaskbarButtonDragEnter args)
{
Application.Current.Dispatcher.Invoke(() => Open(args.Button));
}
private void OnTaskbarButtonInvoked(TaskbarButtonInvoked args)
{
Application.Current.Dispatcher.Invoke(() => Open(args.Button));
}
private void Open(TaskbarButton button)
{
mediator.Handle(new TaskbarButtonFlyoutActivation(button));
}
private async Task StartupAsync() private async Task StartupAsync()
{ {
if (!isInitialized) if (!isInitialized)
+28
View File
@@ -0,0 +1,28 @@
using System;
using System.Threading;
namespace TheXamlGuy.TaskbarGroup
{
public class Program
{
[STAThread()]
public static void Main()
{
using (new Mutex(true, "TheXamlGuy.TaskbarGroup", out var createdNew))
{
if (createdNew)
{
using (new Flyout.App())
{
var app = new App();
app.Run();
}
}
else
{
Environment.Exit(0);
}
}
}
}
}
@@ -1,17 +0,0 @@
using System;
namespace TheXamlGuy.TaskbarGroup
{
public class Program
{
[STAThread()]
public static void Main()
{
using (new Flyout.App())
{
var app = new App();
app.Run();
}
}
}
}
@@ -5,7 +5,9 @@
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<StartupObject>TheXamlGuy.TaskbarGroup.Program</StartupObject> <StartupObject>TheXamlGuy.TaskbarGroup.Program</StartupObject>
<AssetTargetFallback>uap10.0.19041</AssetTargetFallback> <AssetTargetFallback>uap10.0.19041</AssetTargetFallback>
<TargetPlatformVersion>10.0.19041</TargetPlatformVersion>
<Platforms>x64;x86</Platforms> <Platforms>x64;x86</Platforms>
<RuntimeIdentifiers>win10-x64;win10-x86</RuntimeIdentifiers>
<LangVersion>10.0</LangVersion> <LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
@@ -1,16 +1,19 @@
using System; using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading; using System.Windows.Threading;
using TheXamlGuy.TaskbarGroup.Core;
using TheXamlGuy.TaskbarGroup.Flyout.Controls; using TheXamlGuy.TaskbarGroup.Flyout.Controls;
namespace TheXamlGuy.TaskbarGroup namespace TheXamlGuy.TaskbarGroup
{ {
public class TaskbarButtonFlyoutWindow : TransparentXamlWindow<TaskbarButtonFlyout> public class TaskbarButtonFlyoutWindow : TransparentXamlWindow<TaskbarButtonFlyout>
{ {
public TaskbarButtonFlyoutWindow() private readonly IMediator mediator;
public TaskbarButtonFlyoutWindow(IMessenger messenger,
IMediator mediator)
{ {
Deactivated += OnDeactivated; this.mediator = mediator;
Topmost = true; Topmost = true;
Width = 258; Width = 258;
Height = 258; Height = 258;
@@ -19,15 +22,36 @@ namespace TheXamlGuy.TaskbarGroup
{ {
Hide(); Hide();
}), DispatcherPriority.ContextIdle, null); }), DispatcherPriority.ContextIdle, null);
messenger.Subscribe<TaskbarButtonInvoked>(OnTaskbarButtonInvoked);
messenger.Subscribe<TaskbarButtonDragEnter>(OnTaskbarButtonDragEnter);
} }
private void OnDeactivated(object? sender, EventArgs args) protected override void OnDeactivated(EventArgs args)
{ {
if (XamlContent is TaskbarButtonFlyout flyout) if (XamlContent is TaskbarButtonFlyout flyout)
{ {
flyout.Close(); flyout.Close();
Hide(); Hide();
} }
}
base.OnDeactivated(args);
}
private void Open(TaskbarButton button)
{
mediator.Handle(new TaskbarButtonFlyoutActivation(button));
}
private void OnTaskbarButtonDragEnter(TaskbarButtonDragEnter args)
{
Dispatcher.Invoke(() => Open(args.Button));
}
private void OnTaskbarButtonInvoked(TaskbarButtonInvoked args)
{
Dispatcher.Invoke(() => Open(args.Button));
}
} }
} }