diff --git a/Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs b/Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs index c17732bc69..8558a9b368 100644 --- a/Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs +++ b/Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; using System.Windows.Input; +using System.Windows.Interop; using Dragablz; using MahApps.Metro.Controls.Dialogs; using NETworkManager.Localization; @@ -60,13 +62,8 @@ private async void FocusEmbeddedWindow() if (!_embeddedWindowApplicationNames.Contains(ApplicationName)) return; - var window = Application.Current.Windows.OfType().FirstOrDefault(x => x.IsActive); - - if (window == null) - return; - // Find all TabablzControl in the active window - foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren(window)) + foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren(this)) { // Skip if no items if (tabablzControl.Items.Count == 0) @@ -74,7 +71,7 @@ private async void FocusEmbeddedWindow() // Focus embedded window in the selected tab (((DragablzTabItem)tabablzControl.SelectedItem)?.View as IEmbeddedWindow)?.FocusEmbeddedWindow(); - + break; } } @@ -212,7 +209,7 @@ private void RemoteDesktop_FullscreenAction(object view) private void RemoteDesktop_AdjustScreenAction(object view) { if (view is RemoteDesktopControl control) - control.AdjustScreen(force:true); + control.AdjustScreen(force: true); } public ICommand RemoteDesktop_SendCtrlAltDelCommand => @@ -377,14 +374,8 @@ private void MetroWindow_Activated(object sender, EventArgs e) private void DragablzTabHostWindow_OnClosing(object sender, CancelEventArgs e) { - // Close all tabs properly when the window is closing - var window = Application.Current.Windows.OfType().FirstOrDefault(x => x.IsActive); - - if (window == null) - return; - // Find all TabablzControl in the active window - foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren(window)) + foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren(this)) foreach (var tabItem in tabablzControl.Items.OfType()) ((IDragablzTabItem)tabItem.View).CloseTab(); @@ -439,4 +430,72 @@ private void TabablzControl_OnIsDraggingWindowChanged(object sender, RoutedPrope } #endregion + + #region Handle WndProc messages (handle window size events) + + private HwndSource _hwndSource; + + private const int WmExitSizeMove = 0x232; + private const int WmSysCommand = 0x0112; + private const int ScMaximize = 0xF030; + private const int ScRestore = 0xF120; + + protected override void OnSourceInitialized(EventArgs e) + { + base.OnSourceInitialized(e); + + _hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); + _hwndSource?.AddHook(HwndHook); + } + + [DebuggerStepThrough] + private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + { + // Window size events + switch (msg) + { + // Handle window resize and move events + case WmExitSizeMove: + UpdateOnWindowResize(); + break; + + // Handle system commands (like maximize and restore) + case WmSysCommand: + + switch (wParam.ToInt32()) + { + // Window is maximized + case ScMaximize: + // Window is restored (back to normal size from maximized state) + case ScRestore: + UpdateOnWindowResize(); + break; + } + + break; + } + + handled = false; + + return IntPtr.Zero; + } + + private void UpdateOnWindowResize() + { + // Find all TabablzControl + foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren(this)) + { + // Skip if no items + if (tabablzControl.Items.Count == 0) + continue; + + foreach (var item in tabablzControl.Items.OfType()) + { + if (item.View is RemoteDesktopControl control) + control.UpdateOnWindowResize(); + } + } + } + + #endregion } \ No newline at end of file diff --git a/Source/NETworkManager/Controls/IEmbeddedWindow.cs b/Source/NETworkManager/Controls/IEmbeddedWindow.cs index 66ecdf95f1..bed847fdd5 100644 --- a/Source/NETworkManager/Controls/IEmbeddedWindow.cs +++ b/Source/NETworkManager/Controls/IEmbeddedWindow.cs @@ -11,5 +11,6 @@ public interface IEmbeddedWindow /// public void FocusEmbeddedWindow() { + } } \ No newline at end of file diff --git a/Source/NETworkManager/Controls/RemoteDesktopControl.xaml.cs b/Source/NETworkManager/Controls/RemoteDesktopControl.xaml.cs index c2ff6f4ed4..073ec85c39 100644 --- a/Source/NETworkManager/Controls/RemoteDesktopControl.xaml.cs +++ b/Source/NETworkManager/Controls/RemoteDesktopControl.xaml.cs @@ -100,7 +100,6 @@ private set OnPropertyChanged(); } } - #endregion #region Constructor, load diff --git a/Source/NETworkManager/MainWindow.xaml.cs b/Source/NETworkManager/MainWindow.xaml.cs index 846e21c74c..d5e596d283 100644 --- a/Source/NETworkManager/MainWindow.xaml.cs +++ b/Source/NETworkManager/MainWindow.xaml.cs @@ -38,6 +38,7 @@ using System.Windows.Interop; using System.Windows.Markup; using System.Windows.Threading; +using Dragablz; using Application = System.Windows.Application; using ContextMenu = System.Windows.Controls.ContextMenu; using MouseEventArgs = System.Windows.Forms.MouseEventArgs; @@ -1590,19 +1591,23 @@ private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref // Window size events switch (msg) { + // Handle window resize and move events case WmExitSizeMove: - _remoteDesktopHostView?.UpdateOnWindowResize(); + UpdateOnWindowResize(); break; + // Handle system commands (like maximize and restore) case WmSysCommand: - // Handle system commands (like maximize and restore) - if (wParam.ToInt32() == ScMaximize) + + switch (wParam.ToInt32()) + { // Window is maximized - _remoteDesktopHostView?.UpdateOnWindowResize(); - - if (wParam.ToInt32() == ScRestore) + case ScMaximize: // Window is restored (back to normal size from maximized state) - _remoteDesktopHostView?.UpdateOnWindowResize(); + case ScRestore: + UpdateOnWindowResize(); + break; + } break; } @@ -1611,6 +1616,22 @@ private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref return IntPtr.Zero; } + + private void UpdateOnWindowResize() + { + foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren(this)) + { + // Skip if no items + if (tabablzControl.Items.Count == 0) + continue; + + foreach (var item in tabablzControl.Items.OfType()) + { + if (item.View is RemoteDesktopControl control) + control.UpdateOnWindowResize(); + } + } + } #endregion diff --git a/Source/NETworkManager/ViewModels/AWSSessionManagerHostViewModel.cs b/Source/NETworkManager/ViewModels/AWSSessionManagerHostViewModel.cs index bd2ed275c9..e8a4bc95b3 100644 --- a/Source/NETworkManager/ViewModels/AWSSessionManagerHostViewModel.cs +++ b/Source/NETworkManager/ViewModels/AWSSessionManagerHostViewModel.cs @@ -943,6 +943,7 @@ public void FocusEmbeddedWindow() // Focus embedded window in the selected tab (((DragablzTabItem)tabablzControl.SelectedItem)?.View as IEmbeddedWindow)?.FocusEmbeddedWindow(); + break; } } diff --git a/Source/NETworkManager/ViewModels/PowerShellHostViewModel.cs b/Source/NETworkManager/ViewModels/PowerShellHostViewModel.cs index c2214ce002..d0147e2bde 100644 --- a/Source/NETworkManager/ViewModels/PowerShellHostViewModel.cs +++ b/Source/NETworkManager/ViewModels/PowerShellHostViewModel.cs @@ -594,6 +594,7 @@ public void FocusEmbeddedWindow() // Focus embedded window in the selected tab (((DragablzTabItem)tabablzControl.SelectedItem)?.View as IEmbeddedWindow)?.FocusEmbeddedWindow(); + break; } } diff --git a/Source/NETworkManager/ViewModels/PuTTYHostViewModel.cs b/Source/NETworkManager/ViewModels/PuTTYHostViewModel.cs index 1af436f6f6..29b92c1bf8 100644 --- a/Source/NETworkManager/ViewModels/PuTTYHostViewModel.cs +++ b/Source/NETworkManager/ViewModels/PuTTYHostViewModel.cs @@ -678,6 +678,7 @@ public void FocusEmbeddedWindow() // Focus embedded window in the selected tab (((DragablzTabItem)tabablzControl.SelectedItem)?.View as IEmbeddedWindow)?.FocusEmbeddedWindow(); + break; } } diff --git a/Source/NETworkManager/ViewModels/RemoteDesktopHostViewModel.cs b/Source/NETworkManager/ViewModels/RemoteDesktopHostViewModel.cs index 0d097bdc2c..66d4ec1058 100644 --- a/Source/NETworkManager/ViewModels/RemoteDesktopHostViewModel.cs +++ b/Source/NETworkManager/ViewModels/RemoteDesktopHostViewModel.cs @@ -574,12 +574,6 @@ public void OnViewHide() _isViewActive = false; } - public void UpdateOnWindowResize() - { - foreach (var tab in TabItems) - (tab.View as RemoteDesktopControl)?.UpdateOnWindowResize(); - } - private void SetProfilesView(ProfileInfo profile = null) { Profiles = new CollectionViewSource diff --git a/Source/NETworkManager/Views/RemoteDesktopHostView.xaml.cs b/Source/NETworkManager/Views/RemoteDesktopHostView.xaml.cs index 45140ed077..35a87953f1 100644 --- a/Source/NETworkManager/Views/RemoteDesktopHostView.xaml.cs +++ b/Source/NETworkManager/Views/RemoteDesktopHostView.xaml.cs @@ -54,9 +54,4 @@ public void OnViewVisible() { _viewModel.OnViewVisible(); } - - public void UpdateOnWindowResize() - { - _viewModel.UpdateOnWindowResize(); - } } \ No newline at end of file diff --git a/Website/docs/changelog/next-release.md b/Website/docs/changelog/next-release.md index 01063109fd..f21450391a 100644 --- a/Website/docs/changelog/next-release.md +++ b/Website/docs/changelog/next-release.md @@ -63,6 +63,6 @@ Release date: **xx.xx.2024** ## Dependencies, Refactoring & Documentation - Migrated code for some loading indicators from the library [LoadingIndicators.WPF] (https://github.com/zeluisping/LoadingIndicators.WPF) to the NETworkManager repo, as the original repo looks unmaintained and has problems with MahApps.Metro version 2 and later. [#2963](https://github.com/BornToBeRoot/NETworkManager/pull/2963) -- Code cleanup & refactoring [#2940](https://github.com/BornToBeRoot/NETworkManager/pull/2940) +- Code cleanup & refactoring [#2940](https://github.com/BornToBeRoot/NETworkManager/pull/2940) [#2976](https://github.com/BornToBeRoot/NETworkManager/pull/2976) - Language files updated via [#transifex](https://github.com/BornToBeRoot/NETworkManager/pulls?q=author%3Aapp%2Ftransifex-integration) - Dependencies updated via [#dependabot](https://github.com/BornToBeRoot/NETworkManager/pulls?q=author%3Aapp%2Fdependabot)