Skip to content

Commit

Permalink
Merge pull request #60 from jimmyeao/dev-refactor
Browse files Browse the repository at this point in the history
added minimise to x in UI
  • Loading branch information
jimmyeao authored May 20, 2024
2 parents 3053bd4 + 1108aec commit 3f2cc2d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
58 changes: 39 additions & 19 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ public MainWindow()
SetWindowTitle();
// Add event handler for when the main window is loaded
this.Loaded += MainPage_Loaded;

this.Closing += MainWindow_Closing;
this.StateChanged += MainWindow_StateChanged;
// Set the icon for the notification tray
string iconPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Square150x150Logo.scale-200.ico");
MyNotifyIcon.Icon = new System.Drawing.Icon(iconPath);
Expand Down Expand Up @@ -358,30 +359,30 @@ await WebSocketManager.Instance.PairWithTeamsAsync(newToken =>
WebSocketManager.Instance.TeamsUpdateReceived += TeamsClient_TeamsUpdateReceived;
Dispatcher.Invoke(() => UpdateStatusMenuItems());
}

private void MainWindow_StateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
// Hide the window and show the NotifyIcon when minimized
this.Hide();
MyNotifyIcon.Visibility = Visibility.Visible;
}
else
{
// Ensure the NotifyIcon is hidden when the window is not minimized
MyNotifyIcon.Visibility = Visibility.Collapsed;
}
}

#endregion Public Methods

#region Protected Methods

protected override void OnClosing(CancelEventArgs e)
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
if(_mqttService != null)
{
_mqttService.StatusUpdated -= UpdateMqttStatus;
_mqttService.CommandToTeams -= HandleCommandToTeams;

// Disconnect asynchronously without waiting
_ = _mqttService.DisconnectAsync();

// Dispose of the MQTT service
_mqttService.Dispose();
Log.Debug("MQTT Client Disposed");
}


// Ensure to call the base class method to properly close the application
base.OnClosing(e);
// Minimize to system tray instead of closing
e.Cancel = true;
this.WindowState = WindowState.Minimized;
}


Expand All @@ -402,6 +403,8 @@ protected override void OnStateChanged(EventArgs e)
base.OnStateChanged(e);
}



#endregion Protected Methods

#region Private Methods
Expand Down Expand Up @@ -513,6 +516,22 @@ private void CreateNotifyIconContextMenu()
private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
{
// Handle the click event for the exit menu item (Close the application)
if (_mqttService != null)
{
_mqttService.StatusUpdated -= UpdateMqttStatus;
_mqttService.CommandToTeams -= HandleCommandToTeams;

// Disconnect asynchronously without waiting
_ = _mqttService.DisconnectAsync();

// Dispose of the MQTT service
_mqttService.Dispose();
Log.Debug("MQTT Client Disposed");
}


// Ensure to call the base class method to properly close the application

Application.Current.Shutdown();
}

Expand Down Expand Up @@ -786,6 +805,7 @@ private async void TeamsClient_TeamsUpdateReceived(object sender, TeamsUpdateEve
Log.Debug("TeamsClient_TeamsUpdateReceived: Teams Update Received {update}", _latestMeetingUpdate);
// Update sensor configurations
await _mqttService.PublishConfigurations(_latestMeetingUpdate, _settings);
UpdateStatusMenuItems();
}
}

Expand Down
4 changes: 2 additions & 2 deletions TEAMS2HA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<AssemblyVersion>1.1.0.733</AssemblyVersion>
<FileVersion>1.1.0.733</FileVersion>
<AssemblyVersion>1.1.0.738</AssemblyVersion>
<FileVersion>1.1.0.738</FileVersion>
<ApplicationIcon>Assets\Square150x150Logo.scale-200.ico</ApplicationIcon>
<Title>Teams2HA</Title>
<PackageIcon>Square150x150Logo.scale-200.png</PackageIcon>
Expand Down

0 comments on commit 3f2cc2d

Please sign in to comment.