7 Commits

Author SHA1 Message Date
1e0d3928c6 Update submodules 2021-12-20 16:24:21 -05:00
312c4c5c06 Handle device removal/addition 2021-12-20 16:21:01 -05:00
287c008117 Make sure icon gets removed 2021-12-20 14:01:48 -05:00
42301d5242 More startup/upgrade cleanup 2021-12-20 13:57:21 -05:00
3d10cc9635 Clean up startup 2021-12-20 13:52:34 -05:00
714bd0384a Add IPC listener and tweak update messages 2021-12-20 13:43:14 -05:00
fbb797968d Add update button to about 2021-12-20 13:14:00 -05:00
10 changed files with 153 additions and 37 deletions

View File

@@ -3,6 +3,7 @@ using Common.IO;
using Common.Wpf.Extensions; using Common.Wpf.Extensions;
using Squirrel; using Squirrel;
using System; using System;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Threading; using System.Windows.Threading;
@@ -17,6 +18,7 @@ namespace WorkIndicator
public static string UpdateUrl = "https://github.com/ckaczor/WorkIndicator"; public static string UpdateUrl = "https://github.com/ckaczor/WorkIndicator";
private Dispatcher _dispatcher; private Dispatcher _dispatcher;
private InterprocessMessageListener _commandLineListener;
[STAThread] [STAThread]
public static void Main(string[] args) public static void Main(string[] args)
@@ -34,49 +36,56 @@ namespace WorkIndicator
_dispatcher = Dispatcher.CurrentDispatcher; _dispatcher = Dispatcher.CurrentDispatcher;
// Create an isolation handle to see if we are already running
_isolationHandle = ApplicationIsolation.GetIsolationHandle();
// If there is another copy then pass it the command line and exit
if (_isolationHandle == null)
{
try
{
InterprocessMessageSender.SendMessage(Environment.CommandLine);
}
catch { }
Shutdown();
return;
}
// Initialize the command line listener
_commandLineListener = new InterprocessMessageListener(Assembly.GetEntryAssembly().GetName().Name);
_commandLineListener.MessageReceived += HandleCommandLine;
// Initialize the tray icon // Initialize the tray icon
TrayIcon.Initialize(); TrayIcon.Initialize();
Task.Factory.StartNew(UpdateApp).ContinueWith(task => StartUpdate(task.Result.Result)); // Set automatic start into the registry
Current.SetStartWithWindows(Settings.Default.StartWithWindows);
// Initialize the light controller
LightController.Initialize();
Task.Factory.StartNew(CheckUpdate);
} }
private void StartUpdate(bool updateRequired) private void HandleCommandLine(object sender, InterprocessMessageListener.InterprocessMessageEventArgs e)
{ {
if (updateRequired)
return;
Task.Factory.StartNew(() =>
{
// Create an isolation handle to see if we are already running
_isolationHandle = ApplicationIsolation.GetIsolationHandle();
// If there is another copy then pass it the command line and exit
if (_isolationHandle == null)
{
InterprocessMessageSender.SendMessage(Environment.CommandLine);
Shutdown();
return;
}
// Set automatic start into the registry
Current.SetStartWithWindows(Settings.Default.StartWithWindows);
// Initialize the light controller
LightController.Initialize();
});
} }
private async Task<bool> UpdateApp() private async Task<bool> CheckUpdate()
{ {
return await UpdateCheck.CheckUpdate(HandleUpdateStatus); return await UpdateCheck.CheckUpdate(HandleUpdateStatus);
} }
private void HandleUpdateStatus(UpdateCheck.UpdateStatus status, string message) private void HandleUpdateStatus(UpdateCheck.UpdateStatus status, string message)
{ {
if (status == UpdateCheck.UpdateStatus.None) if (status != UpdateCheck.UpdateStatus.Installing)
message = WorkIndicator.Properties.Resources.Loading; return;
_dispatcher.Invoke(() => TrayIcon.SetText(message)); _dispatcher.Invoke(() => TrayIcon.ShowUpdateMessage(message));
TrayIcon.Dispose();
} }
protected override void OnExit(ExitEventArgs e) protected override void OnExit(ExitEventArgs e)
@@ -88,7 +97,7 @@ namespace WorkIndicator
TrayIcon.Dispose(); TrayIcon.Dispose();
// Get rid of the isolation handle // Get rid of the isolation handle
_isolationHandle.Dispose(); _isolationHandle?.Dispose();
base.OnExit(e); base.OnExit(e);
} }

2
Common

Submodule Common updated: 81856b2700...4da8f426c0

View File

@@ -15,11 +15,15 @@ namespace WorkIndicator
public static class LightController public static class LightController
{ {
private static StoplightIndicator _stoplightIndicator; private static StoplightIndicator _stoplightIndicator;
private static UsbService _usbService;
private static bool _initialized; private static bool _initialized;
private static Status _status = Status.Auto; private static Status _status = Status.Auto;
public static void Initialize() public static void Initialize()
{ {
_usbService = new UsbService();
_usbService.DevicesChanged += DevicesChanged;
_stoplightIndicator = new StoplightIndicator(); _stoplightIndicator = new StoplightIndicator();
_stoplightIndicator.SetLight(StoplightIndicator.Light.Yellow, StoplightIndicator.LightState.On); _stoplightIndicator.SetLight(StoplightIndicator.Light.Yellow, StoplightIndicator.LightState.On);
@@ -29,6 +33,14 @@ namespace WorkIndicator
_initialized = true; _initialized = true;
} }
private static void DevicesChanged()
{
_stoplightIndicator?.Dispose();
_stoplightIndicator = new StoplightIndicator();
UpdateLights();
}
private static void AudioWatcher_MicrophoneInUseChanged(bool microphoneInUse) private static void AudioWatcher_MicrophoneInUseChanged(bool microphoneInUse)
{ {
UpdateLights(); UpdateLights();
@@ -44,6 +56,8 @@ namespace WorkIndicator
_stoplightIndicator.SetLights(StoplightIndicator.LightState.Off, StoplightIndicator.LightState.Off, StoplightIndicator.LightState.Off); _stoplightIndicator.SetLights(StoplightIndicator.LightState.Off, StoplightIndicator.LightState.Off, StoplightIndicator.LightState.Off);
_stoplightIndicator.Dispose(); _stoplightIndicator.Dispose();
_usbService.Dispose();
_initialized = false; _initialized = false;
} }

View File

@@ -4,21 +4,43 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:windows="clr-namespace:Common.Wpf.Windows;assembly=Common.Wpf" xmlns:windows="clr-namespace:Common.Wpf.Windows;assembly=Common.Wpf"
xmlns:properties="clr-namespace:WorkIndicator.Properties"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="300" d:DesignHeight="300"
d:DesignWidth="300"> d:DesignWidth="300">
<Grid> <Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="[Application Name]" <TextBlock Text="[Application Name]"
Name="ApplicationNameLabel" Name="ApplicationNameLabel"
VerticalAlignment="Top" VerticalAlignment="Top"
FontWeight="Bold" /> FontWeight="Bold"
Grid.Row="0" />
<TextBlock Text="[Application Version]" <TextBlock Text="[Application Version]"
Margin="0,22,0,0" Margin="0,6,0,0"
Name="VersionLabel" Name="VersionLabel"
VerticalAlignment="Top" /> VerticalAlignment="Top"
Grid.Row="1" />
<TextBlock Text="[Company]" <TextBlock Text="[Company]"
Margin="0,44,0,0" Margin="0,6,0,0"
Name="CompanyLabel" Name="CompanyLabel"
VerticalAlignment="Top" /> VerticalAlignment="Top"
Grid.Row="2" />
<StackPanel Grid.Row="3"
Grid.Column="0"
Margin="0,20,0,0"
Orientation="Horizontal">
<Button Content="{x:Static properties:Resources.CheckUpdate}"
HorizontalAlignment="Left"
Padding="6,2"
Click="HandleCheckForUpdateButtonClick"
VerticalContentAlignment="Center" />
<Label Name="UpdateMessage" Content="" VerticalContentAlignment="Center" Padding="6,0" />
</StackPanel>
</Grid> </Grid>
</windows:CategoryPanel> </windows:CategoryPanel>

View File

@@ -1,5 +1,6 @@
using Common.Update; using Common.Update;
using System.Reflection; using System.Reflection;
using System.Windows;
namespace WorkIndicator.Options namespace WorkIndicator.Options
{ {
@@ -32,5 +33,10 @@ namespace WorkIndicator.Options
} }
public override string CategoryName => Properties.Resources.OptionCategory_About; public override string CategoryName => Properties.Resources.OptionCategory_About;
private async void HandleCheckForUpdateButtonClick(object sender, RoutedEventArgs e)
{
await UpdateCheck.CheckUpdate((status, message) => UpdateMessage.Content = message);
}
} }
} }

View File

@@ -133,6 +133,15 @@ namespace WorkIndicator.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to _Check for Update.
/// </summary>
public static string CheckUpdate {
get {
return ResourceManager.GetString("CheckUpdate", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Enabled. /// Looks up a localized string similar to Enabled.
/// </summary> /// </summary>

View File

@@ -241,4 +241,7 @@
<data name="Loading" xml:space="preserve"> <data name="Loading" xml:space="preserve">
<value>Loading...</value> <value>Loading...</value>
</data> </data>
<data name="CheckUpdate" xml:space="preserve">
<value>_Check for Update</value>
</data>
</root> </root>

View File

@@ -68,9 +68,9 @@ namespace WorkIndicator
_initialized = true; _initialized = true;
} }
public static void SetText(string text) public static void ShowUpdateMessage(string text)
{ {
_trayIcon.Text = text; _trayIcon.ShowBalloonTip(200, Resources.ApplicationName, text, ToolTipIcon.None);
} }
static void HandleContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e) static void HandleContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)
@@ -136,7 +136,7 @@ namespace WorkIndicator
if (dialogResult.HasValue && dialogResult.Value) if (dialogResult.HasValue && dialogResult.Value)
{ {
Properties.Settings.Default.Save(); Settings.Default.Save();
} }
} }
} }

52
UsbService.cs Normal file
View File

@@ -0,0 +1,52 @@
using System;
using System.Windows.Forms;
namespace WorkIndicator
{
internal class UsbService : NativeWindow, IDisposable
{
public event Action DevicesChanged;
private const int WM_DEVICECHANGE = 0x0219;
private const int DBT_DEVICEARRIVAL = 0x8000;
private const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
private const int DBT_DEVNODES_CHANGED = 0x0007;
private bool _isDisposed;
internal UsbService()
{
base.CreateHandle(new CreateParams());
}
protected override void WndProc(ref Message msg)
{
base.WndProc(ref msg);
if (msg.Msg == WM_DEVICECHANGE)
{
switch (msg.WParam.ToInt32())
{
case DBT_DEVNODES_CHANGED:
case DBT_DEVICEARRIVAL:
case DBT_DEVICEREMOVECOMPLETE:
DevicesChanged?.Invoke();
break;
}
}
}
public void Dispose()
{
if (!_isDisposed)
{
base.DestroyHandle();
_isDisposed = true;
GC.SuppressFinalize(this);
}
}
}
}

View File

@@ -144,6 +144,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="UpdateCheck.cs" /> <Compile Include="UpdateCheck.cs" />
<Compile Include="UsbService.cs" />
<Page Include="App.xaml"> <Page Include="App.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>