Skip to content

Commit 5e28465

Browse files
authored
Feature: RDP session resize event in dragged out tab / split view (#2976)
* Feature: RDP session resize event in dragged out tab / split view * Docs: #2976
1 parent 0cc4bb6 commit 5e28465

10 files changed

+107
-35
lines changed

Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4+
using System.Diagnostics;
45
using System.Linq;
56
using System.Runtime.CompilerServices;
67
using System.Threading.Tasks;
78
using System.Windows;
89
using System.Windows.Forms;
910
using System.Windows.Input;
11+
using System.Windows.Interop;
1012
using Dragablz;
1113
using MahApps.Metro.Controls.Dialogs;
1214
using NETworkManager.Localization;
@@ -60,21 +62,16 @@ private async void FocusEmbeddedWindow()
6062
if (!_embeddedWindowApplicationNames.Contains(ApplicationName))
6163
return;
6264

63-
var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
64-
65-
if (window == null)
66-
return;
67-
6865
// Find all TabablzControl in the active window
69-
foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren<TabablzControl>(window))
66+
foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren<TabablzControl>(this))
7067
{
7168
// Skip if no items
7269
if (tabablzControl.Items.Count == 0)
7370
continue;
7471

7572
// Focus embedded window in the selected tab
7673
(((DragablzTabItem)tabablzControl.SelectedItem)?.View as IEmbeddedWindow)?.FocusEmbeddedWindow();
77-
74+
7875
break;
7976
}
8077
}
@@ -212,7 +209,7 @@ private void RemoteDesktop_FullscreenAction(object view)
212209
private void RemoteDesktop_AdjustScreenAction(object view)
213210
{
214211
if (view is RemoteDesktopControl control)
215-
control.AdjustScreen(force:true);
212+
control.AdjustScreen(force: true);
216213
}
217214

218215
public ICommand RemoteDesktop_SendCtrlAltDelCommand =>
@@ -377,14 +374,8 @@ private void MetroWindow_Activated(object sender, EventArgs e)
377374

378375
private void DragablzTabHostWindow_OnClosing(object sender, CancelEventArgs e)
379376
{
380-
// Close all tabs properly when the window is closing
381-
var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
382-
383-
if (window == null)
384-
return;
385-
386377
// Find all TabablzControl in the active window
387-
foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren<TabablzControl>(window))
378+
foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren<TabablzControl>(this))
388379
foreach (var tabItem in tabablzControl.Items.OfType<DragablzTabItem>())
389380
((IDragablzTabItem)tabItem.View).CloseTab();
390381

@@ -439,4 +430,72 @@ private void TabablzControl_OnIsDraggingWindowChanged(object sender, RoutedPrope
439430
}
440431

441432
#endregion
433+
434+
#region Handle WndProc messages (handle window size events)
435+
436+
private HwndSource _hwndSource;
437+
438+
private const int WmExitSizeMove = 0x232;
439+
private const int WmSysCommand = 0x0112;
440+
private const int ScMaximize = 0xF030;
441+
private const int ScRestore = 0xF120;
442+
443+
protected override void OnSourceInitialized(EventArgs e)
444+
{
445+
base.OnSourceInitialized(e);
446+
447+
_hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
448+
_hwndSource?.AddHook(HwndHook);
449+
}
450+
451+
[DebuggerStepThrough]
452+
private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
453+
{
454+
// Window size events
455+
switch (msg)
456+
{
457+
// Handle window resize and move events
458+
case WmExitSizeMove:
459+
UpdateOnWindowResize();
460+
break;
461+
462+
// Handle system commands (like maximize and restore)
463+
case WmSysCommand:
464+
465+
switch (wParam.ToInt32())
466+
{
467+
// Window is maximized
468+
case ScMaximize:
469+
// Window is restored (back to normal size from maximized state)
470+
case ScRestore:
471+
UpdateOnWindowResize();
472+
break;
473+
}
474+
475+
break;
476+
}
477+
478+
handled = false;
479+
480+
return IntPtr.Zero;
481+
}
482+
483+
private void UpdateOnWindowResize()
484+
{
485+
// Find all TabablzControl
486+
foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren<TabablzControl>(this))
487+
{
488+
// Skip if no items
489+
if (tabablzControl.Items.Count == 0)
490+
continue;
491+
492+
foreach (var item in tabablzControl.Items.OfType<DragablzTabItem>())
493+
{
494+
if (item.View is RemoteDesktopControl control)
495+
control.UpdateOnWindowResize();
496+
}
497+
}
498+
}
499+
500+
#endregion
442501
}

Source/NETworkManager/Controls/IEmbeddedWindow.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public interface IEmbeddedWindow
1111
/// </summary>
1212
public void FocusEmbeddedWindow()
1313
{
14+
1415
}
1516
}

Source/NETworkManager/Controls/RemoteDesktopControl.xaml.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ private set
100100
OnPropertyChanged();
101101
}
102102
}
103-
104103
#endregion
105104

106105
#region Constructor, load

Source/NETworkManager/MainWindow.xaml.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
using System.Windows.Interop;
3939
using System.Windows.Markup;
4040
using System.Windows.Threading;
41+
using Dragablz;
4142
using Application = System.Windows.Application;
4243
using ContextMenu = System.Windows.Controls.ContextMenu;
4344
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
@@ -1590,19 +1591,23 @@ private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref
15901591
// Window size events
15911592
switch (msg)
15921593
{
1594+
// Handle window resize and move events
15931595
case WmExitSizeMove:
1594-
_remoteDesktopHostView?.UpdateOnWindowResize();
1596+
UpdateOnWindowResize();
15951597
break;
15961598

1599+
// Handle system commands (like maximize and restore)
15971600
case WmSysCommand:
1598-
// Handle system commands (like maximize and restore)
1599-
if (wParam.ToInt32() == ScMaximize)
1601+
1602+
switch (wParam.ToInt32())
1603+
{
16001604
// Window is maximized
1601-
_remoteDesktopHostView?.UpdateOnWindowResize();
1602-
1603-
if (wParam.ToInt32() == ScRestore)
1605+
case ScMaximize:
16041606
// Window is restored (back to normal size from maximized state)
1605-
_remoteDesktopHostView?.UpdateOnWindowResize();
1607+
case ScRestore:
1608+
UpdateOnWindowResize();
1609+
break;
1610+
}
16061611

16071612
break;
16081613
}
@@ -1611,6 +1616,22 @@ private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref
16111616

16121617
return IntPtr.Zero;
16131618
}
1619+
1620+
private void UpdateOnWindowResize()
1621+
{
1622+
foreach (var tabablzControl in VisualTreeHelper.FindVisualChildren<TabablzControl>(this))
1623+
{
1624+
// Skip if no items
1625+
if (tabablzControl.Items.Count == 0)
1626+
continue;
1627+
1628+
foreach (var item in tabablzControl.Items.OfType<DragablzTabItem>())
1629+
{
1630+
if (item.View is RemoteDesktopControl control)
1631+
control.UpdateOnWindowResize();
1632+
}
1633+
}
1634+
}
16141635

16151636
#endregion
16161637

Source/NETworkManager/ViewModels/AWSSessionManagerHostViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ public void FocusEmbeddedWindow()
943943

944944
// Focus embedded window in the selected tab
945945
(((DragablzTabItem)tabablzControl.SelectedItem)?.View as IEmbeddedWindow)?.FocusEmbeddedWindow();
946+
946947
break;
947948
}
948949
}

Source/NETworkManager/ViewModels/PowerShellHostViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ public void FocusEmbeddedWindow()
594594

595595
// Focus embedded window in the selected tab
596596
(((DragablzTabItem)tabablzControl.SelectedItem)?.View as IEmbeddedWindow)?.FocusEmbeddedWindow();
597+
597598
break;
598599
}
599600
}

Source/NETworkManager/ViewModels/PuTTYHostViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ public void FocusEmbeddedWindow()
678678

679679
// Focus embedded window in the selected tab
680680
(((DragablzTabItem)tabablzControl.SelectedItem)?.View as IEmbeddedWindow)?.FocusEmbeddedWindow();
681+
681682
break;
682683
}
683684
}

Source/NETworkManager/ViewModels/RemoteDesktopHostViewModel.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,6 @@ public void OnViewHide()
574574
_isViewActive = false;
575575
}
576576

577-
public void UpdateOnWindowResize()
578-
{
579-
foreach (var tab in TabItems)
580-
(tab.View as RemoteDesktopControl)?.UpdateOnWindowResize();
581-
}
582-
583577
private void SetProfilesView(ProfileInfo profile = null)
584578
{
585579
Profiles = new CollectionViewSource

Source/NETworkManager/Views/RemoteDesktopHostView.xaml.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,4 @@ public void OnViewVisible()
5454
{
5555
_viewModel.OnViewVisible();
5656
}
57-
58-
public void UpdateOnWindowResize()
59-
{
60-
_viewModel.UpdateOnWindowResize();
61-
}
6257
}

Website/docs/changelog/next-release.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ Release date: **xx.xx.2024**
6363
## Dependencies, Refactoring & Documentation
6464

6565
- 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)
66-
- Code cleanup & refactoring [#2940](https://github.com/BornToBeRoot/NETworkManager/pull/2940)
66+
- Code cleanup & refactoring [#2940](https://github.com/BornToBeRoot/NETworkManager/pull/2940) [#2976](https://github.com/BornToBeRoot/NETworkManager/pull/2976)
6767
- Language files updated via [#transifex](https://github.com/BornToBeRoot/NETworkManager/pulls?q=author%3Aapp%2Ftransifex-integration)
6868
- Dependencies updated via [#dependabot](https://github.com/BornToBeRoot/NETworkManager/pulls?q=author%3Aapp%2Fdependabot)

0 commit comments

Comments
 (0)