Skip to content

Commit

Permalink
Fix: Refactor network connection & other dashboards (#2846)
Browse files Browse the repository at this point in the history
* Fix: Refactor network connection & other dashboards

* Fix: Network connection check

* Chore: Add logging

* Fix: Run command animation

* Fix: Start & cancel network check tasks

* Chore: Enhance logging
  • Loading branch information
BornToBeRoot authored Nov 10, 2024
1 parent 02acfe0 commit a69b32d
Show file tree
Hide file tree
Showing 20 changed files with 364 additions and 254 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This file was generated by cswinrt.exe version 2.0.8.240703.1
// This file was generated by cswinrt.exe version 2.1.3.240909.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand Down
12 changes: 6 additions & 6 deletions Source/NETworkManager.Settings/GlobalStaticConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.IO;
using System.Linq;
using DnsClient;
using DnsClient;
using Lextm.SharpSnmpLib.Messaging;
using NETworkManager.Models;
using NETworkManager.Models.Export;
Expand All @@ -10,6 +7,9 @@
using NETworkManager.Models.PuTTY;
using NETworkManager.Models.RemoteDesktop;
using NETworkManager.Utilities;
using System;
using System.IO;
using System.Linq;

// ReSharper disable InconsistentNaming

Expand All @@ -22,8 +22,8 @@ public static class GlobalStaticConfiguration
// Type to search (average type speed --> 187 chars/min)
public static TimeSpan SearchDispatcherTimerTimeSpan => new(0, 0, 0, 0, 750);

// Status window delay in ms
public static int StatusWindowDelayBeforeOpen => 5000;
// Network config
public static int NetworkChangeDetectionDelay => 5000;

// Profile config
public static bool Profile_ExpandProfileView => true;
Expand Down
14 changes: 7 additions & 7 deletions Source/NETworkManager.Utilities/DNSClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,31 @@ public void Configure(DNSClientSettings settings)
{
_settings = settings;

Log.Debug("Configuring DNS client...");
Log.Debug("Configure - Configuring DNS client...");

if (_settings.UseCustomDNSServers)
{
Log.Debug("Using custom DNS servers...");
Log.Debug("Configure - Using custom DNS servers...");

// Setup custom DNS servers
List<NameServer> servers = [];

foreach (var (server, port) in _settings.DNSServers)
{
Log.Debug($"Adding custom DNS server: {server}:{port}");
Log.Debug($"Configure - Adding custom DNS server: {server}:{port}");
servers.Add(new IPEndPoint(IPAddress.Parse(server), port));
}

Log.Debug("Creating LookupClient with custom DNS servers...");
Log.Debug("Configure - Creating LookupClient with custom DNS servers...");
_client = new LookupClient(new LookupClientOptions(servers.ToArray()));
}
else
{
Log.Debug("Creating LookupClient with Windows default DNS servers...");
Log.Debug("Configure - Creating LookupClient with Windows default DNS servers...");
_client = new LookupClient();
}

Log.Debug("DNS client configured.");
Log.Debug("Configure - DNS client configured.");
_isConfigured = true;
}

Expand All @@ -74,7 +74,7 @@ public void Configure(DNSClientSettings settings)
/// </summary>
public void UpdateWindowsDNSSever()
{
Log.Debug("Recreating LookupClient with with Windows default DNS servers...");
Log.Debug("UpdateWindowsDNSSever - Recreating LookupClient with with Windows default DNS servers...");
_client = new LookupClient();
}

Expand Down
3 changes: 2 additions & 1 deletion Source/NETworkManager/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ private void Application_Exit(object sender, ExitEventArgs e)
if (_singleInstanceClose || CommandLineManager.Current.Help)
return;

Log.Info("Stop background job (if it exists)...");
Log.Info("Stop background job, if it exists...");

_dispatcherTimer?.Stop();

Save();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This file was generated by cswinrt.exe version 2.0.8.240703.1
// This file was generated by cswinrt.exe version 2.1.3.240909.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand Down
2 changes: 1 addition & 1 deletion Source/NETworkManager/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
<mah:FlyoutsControl>
<mah:Flyout IsKeyboardFocusWithinChanged="FlyoutRunCommand_IsKeyboardFocusWithinChanged"
IsOpen="{Binding FlyoutRunCommandIsOpen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AreAnimationsEnabled="{Binding FlyoutRunCommandAreAnimationsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AreAnimationsEnabled="False"
Visibility="{Binding FlyoutRunCommandIsOpen, Converter={StaticResource BooleanToVisibilityConverter}}"
Position="Top"
TitleVisibility="Collapsed"
Expand Down
72 changes: 26 additions & 46 deletions Source/NETworkManager/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
using System;
using log4net;
using MahApps.Metro.Controls.Dialogs;
using NETworkManager.Controls;
using NETworkManager.Documentation;
using NETworkManager.Localization;
using NETworkManager.Localization.Resources;
using NETworkManager.Models;
using NETworkManager.Models.AWS;
using NETworkManager.Models.EventSystem;
using NETworkManager.Models.Network;
using NETworkManager.Models.PowerShell;
using NETworkManager.Models.PuTTY;
using NETworkManager.Profiles;
using NETworkManager.Settings;
using NETworkManager.Update;
using NETworkManager.Utilities;
using NETworkManager.ViewModels;
using NETworkManager.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
Expand All @@ -19,24 +37,6 @@
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Threading;
using log4net;
using MahApps.Metro.Controls.Dialogs;
using NETworkManager.Controls;
using NETworkManager.Documentation;
using NETworkManager.Localization;
using NETworkManager.Localization.Resources;
using NETworkManager.Models;
using NETworkManager.Models.AWS;
using NETworkManager.Models.EventSystem;
using NETworkManager.Models.Network;
using NETworkManager.Models.PowerShell;
using NETworkManager.Models.PuTTY;
using NETworkManager.Profiles;
using NETworkManager.Settings;
using NETworkManager.Update;
using NETworkManager.Utilities;
using NETworkManager.ViewModels;
using NETworkManager.Views;
using Application = System.Windows.Application;
using ContextMenu = System.Windows.Controls.ContextMenu;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
Expand Down Expand Up @@ -329,21 +329,6 @@ public bool FlyoutRunCommandIsOpen
}
}

private bool _flyoutRunCommandAreAnimationsEnabled;

public bool FlyoutRunCommandAreAnimationsEnabled
{
get => _flyoutRunCommandAreAnimationsEnabled;
set
{
if (value == _flyoutRunCommandAreAnimationsEnabled)
return;

_flyoutRunCommandAreAnimationsEnabled = value;
OnPropertyChanged();
}
}

private bool _isRestartRequired;

public bool IsRestartRequired
Expand Down Expand Up @@ -701,7 +686,7 @@ private void LoadApplicationList()
// Select the application
// Set application via command line, or select the default one, fallback to the first visible one
var applicationList = Applications.Cast<ApplicationInfo>().ToArray();

if (CommandLineManager.Current.Application != ApplicationName.None)
SelectedApplication = applicationList.FirstOrDefault(x => x.Name == CommandLineManager.Current.Application);
else
Expand Down Expand Up @@ -1244,7 +1229,6 @@ private void OpenRunAction()
{
ConfigurationManager.OnDialogOpen();

FlyoutRunCommandAreAnimationsEnabled = true;
FlyoutRunCommandIsOpen = true;
}

Expand All @@ -1259,7 +1243,7 @@ private void RunCommandDoAction()

private void RunCommandCloseAction()
{
RunCommandFlyoutClose().ConfigureAwait(false);
RunCommandFlyoutClose();
}

#endregion
Expand Down Expand Up @@ -1321,28 +1305,24 @@ private void RunCommandDo()
}

// Close the flyout
RunCommandFlyoutClose(true).ConfigureAwait(false);
RunCommandFlyoutClose(true);
}

/// <summary>
/// Close the run command flyout and clear the search.
/// </summary>
private async Task RunCommandFlyoutClose(bool clearSearch = false)
private void RunCommandFlyoutClose(bool clearSearch = false)
{
if (!FlyoutRunCommandIsOpen)
return;

FlyoutRunCommandAreAnimationsEnabled = false;

FlyoutRunCommandIsOpen = false;

ConfigurationManager.OnDialogClose();

// Clear the search
if (clearSearch)
{
await Task.Delay(500); // Wait for the animation to finish
RunCommandSearch = string.Empty;
}
}

#endregion
Expand All @@ -1360,7 +1340,7 @@ private void FlyoutRunCommand_IsKeyboardFocusWithinChanged(object sender, Depend
if (e.NewValue is not false)
return;

RunCommandFlyoutClose().ConfigureAwait(false);
RunCommandFlyoutClose();
}

#endregion
Expand Down Expand Up @@ -1946,7 +1926,7 @@ private async void OnNetworkHasChanged()
_isNetworkChanging = true;

// Wait, because the event may be triggered several times.
await Task.Delay(GlobalStaticConfiguration.StatusWindowDelayBeforeOpen);
await Task.Delay(GlobalStaticConfiguration.NetworkChangeDetectionDelay);

Log.Info("Network availability or address has changed!");

Expand Down
26 changes: 18 additions & 8 deletions Source/NETworkManager/StatusWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
using log4net;
using NETworkManager.Settings;
using NETworkManager.Utilities;
using NETworkManager.Views;
Expand Down Expand Up @@ -42,7 +43,7 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null)
#endregion

#region Variables

// Set priority to make the ui smoother
private readonly DispatcherTimer _dispatcherTimerClose = new(DispatcherPriority.Normal);

Expand Down Expand Up @@ -102,7 +103,7 @@ public int Time

private void ReloadAction()
{
Reload();
Check();
}

public ICommand ShowMainWindowCommand => new RelayCommand(_ => ShowMainWindowAction());
Expand All @@ -126,9 +127,9 @@ private void CloseAction()

#region Methods

private void Reload()
private void Check()
{
_networkConnectionView.Reload();
_networkConnectionView.Check();
}

/// <summary>
Expand All @@ -137,19 +138,28 @@ private void Reload()
/// <param name="enableCloseTimer">Automatically close the window after a certain time.</param>
public void ShowWindow(bool enableCloseTimer = false)
{
// Show on primary screen in left/bottom corner
// Set window position on primary screen
// ToDo: User setting...
Left = Screen.PrimaryScreen.WorkingArea.Right - Width - 10;
Top = Screen.PrimaryScreen.WorkingArea.Bottom - Height - 10;
if (Screen.PrimaryScreen != null)
{
Left = Screen.PrimaryScreen.WorkingArea.Right - Width - 10;
Top = Screen.PrimaryScreen.WorkingArea.Bottom - Height - 10;
}

// Show the window
Show();


// Check the network connection
Check();

// Close the window after a certain time
if (enableCloseTimer)
{
SetupCloseTimer();
return;
}

// Focus the window
Activate();
}

Expand Down
3 changes: 3 additions & 0 deletions Source/NETworkManager/ViewModels/DashboardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ public DashboardViewModel()

private void LoadSettings()
{

}

public void OnViewVisible()
{

}

public void OnViewHide()
{

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.ComponentModel;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Threading.Tasks;
using System.Windows.Input;
using NETworkManager.Models.IPApi;
using NETworkManager.Settings;
Expand All @@ -10,23 +8,6 @@ namespace NETworkManager.ViewModels;

public class IPApiDNSResolverWidgetViewModel : ViewModelBase
{
#region Events

private void SettingsManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(SettingsInfo.Dashboard_CheckIPApiDNSResolver):
// Check if enabled via settings
if (SettingsManager.Current.Dashboard_CheckIPApiDNSResolver)
Check();

break;
}
}

#endregion

#region Variables

private bool _isRunning;
Expand Down Expand Up @@ -65,18 +46,12 @@ private set

public IPApiDNSResolverWidgetViewModel()
{
// Detect if network address or status changed...
NetworkChange.NetworkAvailabilityChanged += (_, _) => Check();
NetworkChange.NetworkAddressChanged += (_, _) => Check();

LoadSettings();

// Detect if settings have changed...
SettingsManager.Current.PropertyChanged += SettingsManager_PropertyChanged;
}

private void LoadSettings()
{

}

#endregion
Expand Down
Loading

0 comments on commit a69b32d

Please sign in to comment.