Skip to content

Commit d1802de

Browse files
committed
Remote Desktop - Disconnect on tab close
1 parent 1a5e926 commit d1802de

6 files changed

Lines changed: 73 additions & 26 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
3+
namespace NETworkManager.Controls
4+
{
5+
public class DragablzRemoteDesktopTabItem : IDisposable
6+
{
7+
public string Header { get; set; }
8+
public RemoteDesktopControl Control { get; set; }
9+
10+
public DragablzRemoteDesktopTabItem(string header, RemoteDesktopControl control)
11+
{
12+
Header = header;
13+
Control = control;
14+
}
15+
16+
private bool _disposed;
17+
18+
protected virtual void Dispose(bool disposing)
19+
{
20+
if (_disposed)
21+
return;
22+
23+
if (disposing)
24+
Control.OnClose();
25+
26+
_disposed = true;
27+
}
28+
29+
public void Dispose()
30+
{
31+
Dispose(true);
32+
GC.SuppressFinalize(this);
33+
}
34+
}
35+
}

Source/NETworkManager/Controls/DragablzTabContent.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

Source/NETworkManager/Controls/RemoteDesktopControl.xaml.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace NETworkManager.Controls
1212
{
13+
1314
public partial class RemoteDesktopControl : UserControl, INotifyPropertyChanged
1415
{
1516
#region PropertyChangedEventHandler
@@ -117,7 +118,7 @@ private void UserControl_Loaded(object sender, RoutedEventArgs e)
117118
{
118119
if (!Connected)
119120
Connect();
120-
}
121+
}
121122
#endregion
122123

123124
#region ICommands & Actions
@@ -168,7 +169,7 @@ private void Connect()
168169
// Events
169170
rdpClient.OnConnected += RdpClient_OnConnected;
170171
rdpClient.OnDisconnected += RdpClient_OnDisconnected;
171-
172+
172173
rdpClient.Connect();
173174
}
174175

@@ -185,6 +186,17 @@ private void Reconnect()
185186
rdpClient.Connect();
186187
}
187188

189+
private void Disconnect()
190+
{
191+
rdpClient.Disconnect();
192+
}
193+
194+
public void OnClose()
195+
{
196+
if (Connected)
197+
Disconnect();
198+
}
199+
188200
private void ReconnectAdjustScreen()
189201
{
190202
rdpClient.Reconnect((uint)rdpGrid.ActualWidth, (uint)rdpGrid.ActualHeight);
@@ -336,7 +348,7 @@ private void ReconnectAdjustScreenTimer_Tick(object sender, EventArgs e)
336348

337349
// Reconnect with new resulution
338350
ReconnectAdjustScreen();
339-
}
340-
#endregion
351+
}
352+
#endregion
341353
}
342354
}

Source/NETworkManager/NETworkManager.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
<Compile Include="3rdParty\Heijden.DNS\RR.cs" />
180180
<Compile Include="3rdParty\Heijden.DNS\Structs.cs" />
181181
<Compile Include="Controls\DragablzMainInterTabClient.cs" />
182-
<Compile Include="Controls\DragablzTabContent.cs" />
182+
<Compile Include="Controls\DragablzRemoteDesktopTabItem.cs" />
183183
<Compile Include="Converters\NullOrEmptyToBoolConverter.cs" />
184184
<Compile Include="Converters\TimestampToStringConverter.cs" />
185185
<Compile Include="Converters\NullableDateTimeToStringConverter.cs" />

Source/NETworkManager/ViewModels/Applications/RemoteDesktopViewModel.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Windows.Data;
1212
using System;
1313
using System.Linq;
14+
using System.Collections.Specialized;
1415

1516
namespace NETworkManager.ViewModels.Applications
1617
{
@@ -20,7 +21,7 @@ public class RemoteDesktopViewModel : ViewModelBase
2021
private IDialogCoordinator dialogCoordinator;
2122

2223
public IInterTabClient InterTabClient { get; private set; }
23-
public ObservableCollection<DragablzTabContent> TabContents { get; private set; }
24+
public ObservableCollection<DragablzRemoteDesktopTabItem> TabItems { get; private set; }
2425

2526
private const string tagIdentifier = "tag=";
2627

@@ -103,7 +104,9 @@ public RemoteDesktopViewModel(IDialogCoordinator instance)
103104
dialogCoordinator = instance;
104105

105106
InterTabClient = new DragablzMainInterTabClient();
106-
TabContents = new ObservableCollection<DragablzTabContent>();
107+
TabItems = new ObservableCollection<DragablzRemoteDesktopTabItem>();
108+
109+
TabItems.CollectionChanged += TabItems_CollectionChanged;
107110

108111
// Load sessions
109112
if (RemoteDesktopSessionManager.Sessions == null)
@@ -131,7 +134,7 @@ public RemoteDesktopViewModel(IDialogCoordinator instance)
131134
else
132135
{
133136
return info.Name.IndexOf(search, StringComparison.OrdinalIgnoreCase) > -1;
134-
}
137+
}
135138
};
136139

137140
LoadSettings();
@@ -159,8 +162,8 @@ private void ConnectSession(Models.RemoteDesktop.RemoteDesktopSessionInfo sessio
159162
sessionInfo.RedirectPorts = SettingsManager.Current.RemoteDesktop_RedirectPorts;
160163
sessionInfo.RedirectSmartCards = SettingsManager.Current.RemoteDesktop_RedirectSmartCards;
161164

162-
TabContents.Add(new DragablzTabContent(Header ?? sessionInfo.Hostname, new RemoteDesktopControl(sessionInfo)));
163-
SelectedTabIndex = TabContents.Count - 1;
165+
TabItems.Add(new DragablzRemoteDesktopTabItem(Header ?? sessionInfo.Hostname, new RemoteDesktopControl(sessionInfo)));
166+
SelectedTabIndex = TabItems.Count - 1;
164167
}
165168

166169
private void RemoteDesktopSession_Search(object sender, FilterEventArgs e)
@@ -394,5 +397,16 @@ private async void DeleteSessionAction()
394397
await dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
395398
}
396399
#endregion
400+
401+
#region Events
402+
private void TabItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
403+
{
404+
if (e.Action == NotifyCollectionChangedAction.Remove)
405+
{
406+
DragablzRemoteDesktopTabItem item = (DragablzRemoteDesktopTabItem)e.OldItems[0];
407+
item.Dispose();
408+
}
409+
}
410+
#endregion
397411
}
398412
}

Source/NETworkManager/Views/Applications/RemoteDesktopView.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<ColumnDefinition Width="*" />
2020
<ColumnDefinition Width="Auto" />
2121
</Grid.ColumnDefinitions>
22-
<dragablz:TabablzControl Grid.Column="0" Grid.Row="0" Margin="-0,-2,0,0" ItemsSource="{Binding TabContents}" SelectedIndex="{Binding SelectedTabIndex}">
22+
<dragablz:TabablzControl Grid.Column="0" Grid.Row="0" Margin="-0,-2,0,0" ItemsSource="{Binding TabItems}" SelectedIndex="{Binding SelectedTabIndex}">
2323
<dragablz:TabablzControl.InterTabController>
2424
<dragablz:InterTabController InterTabClient="{Binding InterTabClient}" />
2525
</dragablz:TabablzControl.InterTabController>
@@ -51,7 +51,7 @@
5151
<Setter Property="ContentTemplate">
5252
<Setter.Value>
5353
<DataTemplate>
54-
<ContentPresenter Content="{Binding Content}" Visibility="{Binding Source={x:Static ConfigurtationManager:ConfigurationManager.Current}, Path=FixAirspace, Converter={StaticResource BooleanReverseToVisibilityConverter}}" />
54+
<ContentPresenter Content="{Binding Control}" Visibility="{Binding Source={x:Static ConfigurtationManager:ConfigurationManager.Current}, Path=FixAirspace, Converter={StaticResource BooleanReverseToVisibilityConverter}}" />
5555
</DataTemplate>
5656
</Setter.Value>
5757
</Setter>

0 commit comments

Comments
 (0)