Skip to content

Commit 1e7fd77

Browse files
committed
feat: Offering quick connect in the launcher by press Tab key #313
[WIP] working on multi credentials #301
1 parent 71154cc commit 1e7fd77

13 files changed

+183
-453
lines changed

Ui/Model/GlobalEventHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace _1RM.Model
1010
{
1111
public static class GlobalEventHelper
1212
{
13-
public delegate void OnRequestQuickConnectDelegate(ProtocolBase server, string assignTabToken = "", string assignRunnerName = "", string via = "");
13+
public delegate void OnRequestQuickConnectDelegate(ProtocolBase server, string assignTabToken = "", string assignRunnerName = "", string fromView = "", string assignCredentialName = "");
1414
public static OnRequestQuickConnectDelegate? OnRequestQuickConnect { get; set; } = null;
1515

1616

17-
public delegate void OnRequestServerConnectDelegate(string serverId, string assignTabToken = "", string assignRunnerName = "", string via = "");
17+
public delegate void OnRequestServerConnectDelegate(string serverId, string assignTabToken = "", string assignRunnerName = "", string fromView = "", string assignCredentialName = "");
1818
/// <summary>
1919
/// Invoke notify to open a new remote session to Tab with assignTabToken (if assignTabToken != null).
2020
/// </summary>

Ui/Model/ProtocolAction.cs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,42 @@ public static List<ProtocolAction> GetActions(this ProtocolBase server)
4040
var actions = new List<ProtocolAction>();
4141
{
4242
if (IoC.Get<SessionControlService>().TabWindowCount > 0)
43+
{
4344
actions.Add(new ProtocolAction(
4445
actionName: IoC.Get<ILanguageService>().Translate("Connect (New window)"),
45-
action: () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, DateTime.Now.Ticks.ToString(), via: nameof(LauncherWindowView)); }
46+
action: () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, DateTime.Now.Ticks.ToString(), fromView: nameof(LauncherWindowView)); }
4647
));
48+
}
4749

50+
if (server is ProtocolBaseWithAddressPortUserPwd { Credentials.Count: > 0 } protocol)
51+
{
52+
foreach (var credential in protocol.Credentials)
53+
{
54+
actions.Add(new ProtocolAction(
55+
actionName: IoC.Get<ILanguageService>().Translate("Connect") + $" (TXT:with credential {credential.Name})",
56+
action: () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, DateTime.Now.Ticks.ToString(), fromView: nameof(LauncherWindowView), assignCredentialName: credential.Name); }
57+
));
58+
}
59+
}
4860

4961
// external runners
5062
var protocolConfigurationService = IoC.Get<ProtocolConfigurationService>();
5163
if (protocolConfigurationService.ProtocolConfigs.ContainsKey(server.Protocol)
5264
&& protocolConfigurationService.ProtocolConfigs[server.Protocol].Runners.Count > 1)
5365
{
54-
actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Connect") + $" (Internal)", () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, assignRunnerName: protocolConfigurationService.ProtocolConfigs[server.Protocol].Runners.First().Name, via: nameof(LauncherWindowView)); }));
66+
//actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Connect") + $" (Internal)", () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, assignRunnerName: protocolConfigurationService.ProtocolConfigs[server.Protocol].Runners.First().Name, fromView: nameof(LauncherWindowView)); }));
5567
foreach (var runner in protocolConfigurationService.ProtocolConfigs[server.Protocol].Runners)
5668
{
5769
if (runner is InternalDefaultRunner) continue;
5870
if (runner is ExternalRunner er && er.IsExeExisted == false) continue;
59-
actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Connect") + $" ({runner.Name})", () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, assignRunnerName: runner.Name, via: nameof(LauncherWindowView)); }));
71+
actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Connect") + $" (via {runner.Name})", () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, assignRunnerName: runner.Name, fromView: nameof(LauncherWindowView)); }));
6072
}
6173
}
62-
else
63-
{
64-
actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Connect"), () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, via: nameof(LauncherWindowView)); }));
65-
}
74+
75+
//else
76+
//{
77+
// actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Connect"), () => { GlobalEventHelper.OnRequestServerConnect?.Invoke(server.Id, fromView: nameof(LauncherWindowView)); }));
78+
//}
6679

6780
if (writable)
6881
{
@@ -72,14 +85,16 @@ public static List<ProtocolAction> GetActions(this ProtocolBase server)
7285
IoC.Get<MainWindowViewModel>()?.ShowMe();
7386
GlobalEventHelper.OnRequestGoToServerEditPage?.Invoke(server: server, showAnimation: false);
7487
}));
75-
actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("server_card_operate_duplicate"), () =>
76-
{
77-
if (GlobalEventHelper.OnRequestGoToServerEditPage == null)
78-
IoC.Get<MainWindowViewModel>()?.ShowMe();
79-
GlobalEventHelper.OnRequestGoToServerDuplicatePage?.Invoke(server: server, showAnimation: false);
80-
}));
88+
//actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("server_card_operate_duplicate"), () =>
89+
//{
90+
// if (GlobalEventHelper.OnRequestGoToServerEditPage == null)
91+
// IoC.Get<MainWindowViewModel>()?.ShowMe();
92+
// GlobalEventHelper.OnRequestGoToServerDuplicatePage?.Invoke(server: server, showAnimation: false);
93+
//}));
8194
}
8295
};
96+
97+
8398
if (server is ProtocolBaseWithAddressPort protocolServerWithAddrPortBase)
8499
{
85100
actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("server_card_operate_copy_address"),
@@ -132,10 +147,10 @@ public static List<ProtocolAction> GetActions(this ProtocolBase server)
132147
}
133148
}
134149

135-
if (writable)
136-
{
137-
actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Delete"), () => { GlobalEventHelper.OnRequestDeleteServer?.Invoke(server); }));
138-
}
150+
//if (writable)
151+
//{
152+
// actions.Add(new ProtocolAction(IoC.Get<ILanguageService>().Translate("Delete"), () => { GlobalEventHelper.OnRequestDeleteServer?.Invoke(server); }));
153+
//}
139154

140155
#endregion Build Actions
141156

Ui/Service/SessionControlService.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
using ProtocolHostStatus = _1RM.View.Host.ProtocolHosts.ProtocolHostStatus;
2525
using Screen = System.Windows.Forms.Screen;
2626
using _1RM.Service.DataSource;
27-
using _1RM.View.ServerList;
2827

2928
namespace _1RM.Service
3029
{
@@ -39,8 +38,8 @@ public SessionControlService(DataSourceService sourceService, ConfigurationServi
3938
_sourceService = sourceService;
4039
_configurationService = configurationService;
4140
_appData = appData;
42-
GlobalEventHelper.OnRequestServerConnect += this.ShowRemoteHost;
43-
GlobalEventHelper.OnRequestQuickConnect += this.ShowRemoteHost;
41+
GlobalEventHelper.OnRequestServerConnect += this.ShowRemoteHostById;
42+
GlobalEventHelper.OnRequestQuickConnect += this.ShowRemoteHostByObject;
4443
}
4544

4645
public void Release()
@@ -306,15 +305,25 @@ private void ConnectWithTab(ProtocolBase server, Runner runner, string assignTab
306305
}
307306
}
308307

309-
private void ShowRemoteHost(ProtocolBase server, string? assignTabToken, string? assignRunnerName, string? via)
308+
private void ShowRemoteHostByObject(ProtocolBase serverOrg, string? assignTabToken, string? assignRunnerName, string? fromView, string assignCredentialName = "")
310309
{
311310
// if is OnlyOneInstance server and it is connected now, activate it and return.
312-
if (this.ActivateOrReConnIfServerSessionIsOpened(server))
311+
if (this.ActivateOrReConnIfServerSessionIsOpened(serverOrg))
313312
return;
314313

315314

316-
if (string.IsNullOrEmpty(via) == false)
317-
MsAppCenterHelper.TraceSessionOpen(server.Protocol, via);
315+
if (string.IsNullOrEmpty(fromView) == false)
316+
MsAppCenterHelper.TraceSessionOpen(serverOrg.Protocol, fromView);
317+
318+
var server = serverOrg.Clone();
319+
if (server is ProtocolBaseWithAddressPortUserPwd protocol
320+
&& protocol.Credentials?.Count > 0
321+
&& protocol.Credentials.Any(x => x.Name == assignCredentialName))
322+
{
323+
var c = protocol.Credentials.First(x => x.Name == assignCredentialName);
324+
if (!string.IsNullOrEmpty(c.Address))
325+
protocol.Address = c.Address;
326+
}
318327

319328
// run script before connected
320329
server.RunScriptBeforeConnect();
@@ -353,7 +362,7 @@ private void ShowRemoteHost(ProtocolBase server, string? assignTabToken, string?
353362
PrintCacheCount();
354363
}
355364

356-
private void ShowRemoteHost(string serverId, string? assignTabToken, string? assignRunnerName, string via)
365+
private void ShowRemoteHostById(string serverId, string? assignTabToken, string? assignRunnerName, string fromView, string assignCredentialName = "")
357366
{
358367
#region START MULTIPLE SESSION
359368
// if serverId <= 0, then start multiple sessions
@@ -362,7 +371,7 @@ private void ShowRemoteHost(string serverId, string? assignTabToken, string? ass
362371
var list = _appData.VmItemList.Where(x => x.IsSelected).ToArray();
363372
foreach (var item in list)
364373
{
365-
this.ShowRemoteHost(item.Id, assignTabToken, assignRunnerName, "");
374+
this.ShowRemoteHostById(item.Id, assignTabToken, assignRunnerName, fromView);
366375
}
367376
return;
368377
}
@@ -384,7 +393,7 @@ private void ShowRemoteHost(string serverId, string? assignTabToken, string? ass
384393
ConnectTimeRecorder.UpdateAndSave(vmServer.Server);
385394
vmServer.LastConnectTime = ConnectTimeRecorder.Get(vmServer.Server);
386395

387-
ShowRemoteHost(vmServer.Server, assignTabToken, assignRunnerName, via);
396+
ShowRemoteHostByObject(vmServer.Server, assignTabToken, assignRunnerName, fromView, assignCredentialName);
388397
}
389398

390399
public void AddTab(TabWindowBase tab)

Ui/Service/TaskTrayService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private void ReloadTaskTrayContextMenu()
143143
}
144144

145145
var button = new System.Windows.Forms.ToolStripMenuItem(text);
146-
button.Click += (sender, args) => { GlobalEventHelper.OnRequestServerConnect?.Invoke(protocolBaseViewModel.Id, via: "Tray"); };
146+
button.Click += (sender, args) => { GlobalEventHelper.OnRequestServerConnect?.Invoke(protocolBaseViewModel.Id, fromView: "Tray"); };
147147
_taskTrayIcon.ContextMenuStrip.Items.Add(button);
148148
}
149149
}

0 commit comments

Comments
 (0)