diff --git a/MacroDeck/GUI/MainWindow.Designer.cs b/MacroDeck/GUI/MainWindow.Designer.cs index 835ee8ae..1e220312 100644 --- a/MacroDeck/GUI/MainWindow.Designer.cs +++ b/MacroDeck/GUI/MainWindow.Designer.cs @@ -30,9 +30,9 @@ protected override void Dispose(bool disposing) } try { - if (this.notificationsList != null && this.Controls.Contains(this.notificationsList)) + if (this._notificationsList != null && this.Controls.Contains(this._notificationsList)) { - this.Controls.Remove(this.notificationsList); + this.Controls.Remove(this._notificationsList); } LanguageManager.LanguageChanged -= LanguageChanged; UpdateService.Instance().UpdateAvailable -= UpdateAvailable; diff --git a/MacroDeck/GUI/MainWindow.cs b/MacroDeck/GUI/MainWindow.cs index d00fd880..a3293b72 100644 --- a/MacroDeck/GUI/MainWindow.cs +++ b/MacroDeck/GUI/MainWindow.cs @@ -22,13 +22,14 @@ namespace SuchByte.MacroDeck.GUI; public partial class MainWindow : Form { - private DeckView _deckView { get; set; } + private NotificationsList? _notificationsList; + private DeckView? _deckView; public DeckView DeckView { get { - if (_deckView == null || _deckView.IsDisposed) + if (_deckView is null || _deckView.IsDisposed || !_deckView.IsHandleCreated) { _deckView = new DeckView(); } @@ -36,7 +37,6 @@ public DeckView DeckView } } - private NotificationsList notificationsList; public MainWindow() { @@ -59,7 +59,7 @@ private void UpdateTranslation() lblIpAddressHostname.Text = LanguageManager.Strings.IpAddressHostNamePort; } - private void LanguageChanged(object sender, EventArgs e) + private void LanguageChanged(object? sender, EventArgs e) { UpdateTranslation(); DeckView?.UpdateTranslation(); @@ -91,27 +91,27 @@ public void SetView(Control view) switch (view) { - case DeckView deck: + case DeckView _: SelectContentButton(btnDeck); break; - case DeviceManagerView deviceManager: + case DeviceManagerView: SelectContentButton(btnDeviceManager); break; - case ExtensionsView extensions: + case ExtensionsView: SelectContentButton(btnExtensions); break; - case SettingsView settings: + case SettingsView: SelectContentButton(btnSettings); break; - case VariablesView variables: + case VariablesView: SelectContentButton(btnVariables); break; } } - private void MainWindow_Load(object sender, EventArgs e) + private void MainWindow_Load(object? sender, EventArgs e) { - lblVersion.Text = $"Macro Deck {MacroDeck.Version}"; + lblVersion.Text = $@"Macro Deck {MacroDeck.Version}"; PluginManager.OnPluginsChange += OnPluginsChanged; IconManager.OnIconPacksChanged += OnPluginsChanged; @@ -159,28 +159,37 @@ private void LoadHosts() hosts.SelectedIndexChanged -= Hosts_SelectedIndexChanged; foreach (var networkInterface in NetworkInterface.GetAllNetworkInterfaces()) { - hosts.Items.Add(networkInterface.GetIPProperties().UnicastAddresses.Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().Address.ToString()); + var ipAddress = networkInterface + .GetIPProperties() + .UnicastAddresses + .FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork) + ?.Address + .ToString(); + if (!string.IsNullOrWhiteSpace(ipAddress)) + { + hosts.Items.Add(ipAddress); + } } hosts.Text = MacroDeck.Configuration.HostAddress; hosts.SelectedIndexChanged += Hosts_SelectedIndexChanged; } - private void NotificationsChanged(object sender, EventArgs e) + private void NotificationsChanged(object? sender, EventArgs e) { btnNotifications.NotificationCount = NotificationManager.Notifications.Count; } - private void ExtensionStoreHelper_OnInstallationFinished(object sender, EventArgs e) + private void ExtensionStoreHelper_OnInstallationFinished(object? sender, EventArgs e) { RefreshPluginsLabels(); } - private void OnPackageManagerUpdateCheckFinished(object sender, EventArgs e) + private void OnPackageManagerUpdateCheckFinished(object? sender, EventArgs e) { RefreshPluginsLabels(); } - private void OnPluginsChanged(object sender, EventArgs e) + private void OnPluginsChanged(object? sender, EventArgs e) { RefreshPluginsLabels(); @@ -195,11 +204,11 @@ private void RefreshPluginsLabels() }); } - private void OnServerStateChanged(object sender, EventArgs e) + private void OnServerStateChanged(object? sender, EventArgs e) { Invoke(() => { - if (MacroDeckServer.WebSocketServer.ListenerSocket == null) + if (MacroDeckServer.WebSocketServer?.ListenerSocket == null) { lblServerStatus.Text = LanguageManager.Strings.ServerOffline; } @@ -212,35 +221,35 @@ private void OnServerStateChanged(object sender, EventArgs e) }); } - private void OnClientsConnectedChanged(object sender, EventArgs e) + private void OnClientsConnectedChanged(object? sender, EventArgs e) { Invoke(new Action(() => lblNumClientsConnected.Text = string.Format(LanguageManager.Strings.XClientsConnected, MacroDeckServer.Clients.Count) )); } - private void BtnDeck_Click(object sender, EventArgs e) + private void BtnDeck_Click(object? sender, EventArgs e) { SetView(DeckView); DeckView.UpdateButtons(); } - private void BtnExtensions_Click(object sender, EventArgs e) + private void BtnExtensions_Click(object? sender, EventArgs e) { SetView(new ExtensionsView()); } - private void BtnSettings_Click(object sender, EventArgs e) + private void BtnSettings_Click(object? sender, EventArgs e) { SetView(new SettingsView()); } - private void BtnDeviceManager_Click(object sender, EventArgs e) + private void BtnDeviceManager_Click(object? sender, EventArgs e) { SetView(new DeviceManagerView()); } - public void OnFormClosing(object sender, EventArgs e) + public void OnFormClosing(object? sender, EventArgs e) { foreach (Control control in contentPanel.Controls) { @@ -248,37 +257,37 @@ public void OnFormClosing(object sender, EventArgs e) } } - private void BtnVariables_Click(object sender, EventArgs e) + private void BtnVariables_Click(object? sender, EventArgs e) { SetView(new VariablesView()); } - private void BtnNotifications_Click(object sender, EventArgs e) + private void BtnNotifications_Click(object? sender, EventArgs e) { - if (notificationsList == null || notificationsList.IsDisposed) + if (_notificationsList == null || _notificationsList.IsDisposed) { - notificationsList = new NotificationsList + _notificationsList = new NotificationsList { - Location = new Point(btnNotifications.Location.X, btnNotifications.Location.Y + btnNotifications.Height) + Location = btnNotifications.Location with { Y = btnNotifications.Location.Y + btnNotifications.Height } }; - notificationsList.OnCloseRequested += (sender, e) => + _notificationsList.OnCloseRequested += (_, _) => { - Controls.Remove(notificationsList); + Controls.Remove(_notificationsList); }; } - if (Controls.Contains(notificationsList)) + if (Controls.Contains(_notificationsList)) { - Controls.Remove(notificationsList); + Controls.Remove(_notificationsList); } else { - Controls.Add(notificationsList); - notificationsList.BringToFront(); + Controls.Add(_notificationsList); + _notificationsList.BringToFront(); } } - private void Hosts_SelectedIndexChanged(object sender, EventArgs e) + private void Hosts_SelectedIndexChanged(object? sender, EventArgs e) { MacroDeck.Configuration.HostAddress = hosts.Text; MacroDeck.Configuration.Save(ApplicationPaths.MainConfigFilePath); diff --git a/MacroDeck/MacroDeck.cs b/MacroDeck/MacroDeck.cs index 3b6a2970..f3aa8416 100644 --- a/MacroDeck/MacroDeck.cs +++ b/MacroDeck/MacroDeck.cs @@ -101,7 +101,7 @@ internal static void Start(StartParameters startParameters) IconManager.Initialize(); ProfileManager.Load(); - PrintNetworkInterfaces(); + SearchNetworkInterfaces(); MacroDeckServer.Start(StartParameters.Port <= 0 ? Configuration.HostPort : StartParameters.Port); BroadcastServer.Start(); ADBServerHelper.Initialize(); @@ -162,25 +162,39 @@ private static void OnUpdateAvailable(object? sender, UpdateApiVersionInfo e) } - private static void PrintNetworkInterfaces() + private static void SearchNetworkInterfaces() { StringBuilder sb = new(); + var foundNetworkInterfaces = 0; try { foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces()) { - var address = adapter.GetIPProperties().UnicastAddresses - .FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork)?.Address; - sb.AppendLine(); - sb.Append($"{adapter.Name} - {address}"); + var address = adapter + .GetIPProperties() + .UnicastAddresses + .FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork)? + .Address? + .ToString(); + if (!string.IsNullOrWhiteSpace(address)) + { + sb.AppendLine($"{adapter.Name} - {address}"); + foundNetworkInterfaces++; + } } } - catch + catch (Exception ex) { - // ignored + MacroDeckLogger.Warning($"Error while searching for network interfaces\n{ex.Message}"); + } + + if (foundNetworkInterfaces == 0) + { + MacroDeckLogger.Error("No network interfaces were found"); + } else + { + MacroDeckLogger.Info($"Found network interfaces:\n{sb}"); } - MacroDeckLogger.Info($"Network interfaces: {sb}"); - sb.Clear(); } private static void StartInitialSetup() diff --git a/MacroDeck/MacroDeck.csproj b/MacroDeck/MacroDeck.csproj index 57b690d7..b72e47ae 100644 --- a/MacroDeck/MacroDeck.csproj +++ b/MacroDeck/MacroDeck.csproj @@ -318,4 +318,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +