Skip to content

Commit 1a7c172

Browse files
authored
Merge pull request #663 from themike10452/dev
run as administration when holding ctrl and shift
2 parents 03166ec + 3452ecf commit 1a7c172

File tree

9 files changed

+145
-11
lines changed

9 files changed

+145
-11
lines changed

Flow.Launcher/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<KeyBinding Key="Home" Modifiers="Alt" Command="{Binding SelectFirstResultCommand}"></KeyBinding>
4747
<KeyBinding Key="O" Modifiers="Ctrl" Command="{Binding LoadContextMenuCommand}"></KeyBinding>
4848
<KeyBinding Key="H" Modifiers="Ctrl" Command="{Binding LoadHistoryCommand}"></KeyBinding>
49+
<KeyBinding Key="Enter" Modifiers="Ctrl+Shift" Command="{Binding OpenResultCommand}"></KeyBinding>
4950
<KeyBinding Key="Enter" Modifiers="Shift" Command="{Binding LoadContextMenuCommand}"></KeyBinding>
5051
<KeyBinding Key="Enter" Command="{Binding OpenResultCommand}"></KeyBinding>
5152
<KeyBinding Key="Enter" Modifiers="Ctrl" Command="{Binding OpenResultCommand}"></KeyBinding>

Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Flow.Launcher.Infrastructure;
22
using Flow.Launcher.Plugin.SharedCommands;
33
using System;
4+
using System.Diagnostics;
45
using System.IO;
56
using System.Linq;
7+
using System.Threading.Tasks;
68
using System.Windows;
79

810
namespace Flow.Launcher.Plugin.Explorer.Search
@@ -127,7 +129,26 @@ internal static Result CreateFileResult(string filePath, Query query, int score
127129
{
128130
try
129131
{
130-
if (c.SpecialKeyState.CtrlPressed)
132+
if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed)
133+
{
134+
Task.Run(() =>
135+
{
136+
try
137+
{
138+
Process.Start(new ProcessStartInfo
139+
{
140+
FileName = filePath,
141+
UseShellExecute = true,
142+
Verb = "runas",
143+
});
144+
}
145+
catch (Exception e)
146+
{
147+
MessageBox.Show(e.Message, "Could not start " + filePath);
148+
}
149+
});
150+
}
151+
else if (c.SpecialKeyState.CtrlPressed)
131152
{
132153
FilesFolders.OpenContainingFolder(filePath);
133154
}

Plugins/Flow.Launcher.Plugin.Program/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@
5353
<!--Dialogs-->
5454
<system:String x:Key="flowlauncher_plugin_program_disable_dlgtitle_success">Success</system:String>
5555
<system:String x:Key="flowlauncher_plugin_program_disable_dlgtitle_success_message">Successfully disabled this program from displaying in your query</system:String>
56+
<system:String x:Key="flowlauncher_plugin_program_run_as_administrator_not_supported_message">This app is not intended to be run as administrator</system:String>
5657

5758
</ResourceDictionary>

Plugins/Flow.Launcher.Plugin.Program/Languages/zh-tw.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333

3434
<system:String x:Key="flowlauncher_plugin_program_plugin_name">程式</system:String>
3535
<system:String x:Key="flowlauncher_plugin_program_plugin_description">在 Flow Launcher 中搜尋程式</system:String>
36+
3637
</ResourceDictionary>

Plugins/Flow.Launcher.Plugin.Program/Programs/UWP.cs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ public class Application : IProgram
267267
public string Location => Package.Location;
268268

269269
public bool Enabled { get; set; }
270+
public bool CanRunElevated { get; set; }
270271

271272
public string LogoUri { get; set; }
272273
public string LogoPath { get; set; }
@@ -320,7 +321,29 @@ public Result Result(string query, IPublicAPI api)
320321
ContextData = this,
321322
Action = e =>
322323
{
323-
Launch(api);
324+
var elevated = (
325+
e.SpecialKeyState.CtrlPressed &&
326+
e.SpecialKeyState.ShiftPressed &&
327+
!e.SpecialKeyState.AltPressed &&
328+
!e.SpecialKeyState.WinPressed
329+
);
330+
331+
if (elevated && CanRunElevated)
332+
{
333+
LaunchElevated();
334+
}
335+
else
336+
{
337+
Launch(api);
338+
339+
if (elevated)
340+
{
341+
var title = "Plugin: Program";
342+
var message = api.GetTranslation("flowlauncher_plugin_program_run_as_administrator_not_supported_message");
343+
api.ShowMsg(title, message, string.Empty);
344+
}
345+
}
346+
324347
return true;
325348
}
326349
};
@@ -354,6 +377,21 @@ public List<Result> ContextMenus(IPublicAPI api)
354377
IcoPath = "Images/folder.png"
355378
}
356379
};
380+
381+
if (CanRunElevated)
382+
{
383+
contextMenus.Add(new Result
384+
{
385+
Title = api.GetTranslation("flowlauncher_plugin_program_run_as_administrator"),
386+
Action = _ =>
387+
{
388+
LaunchElevated();
389+
return true;
390+
},
391+
IcoPath = "Images/cmd.png"
392+
});
393+
}
394+
357395
return contextMenus;
358396
}
359397

@@ -377,6 +415,20 @@ await Task.Run(() =>
377415
});
378416
}
379417

418+
private async void LaunchElevated()
419+
{
420+
string command = "shell:AppsFolder\\" + UniqueIdentifier;
421+
command = Environment.ExpandEnvironmentVariables(command.Trim());
422+
423+
var info = new ProcessStartInfo(command)
424+
{
425+
UseShellExecute = true,
426+
Verb = "runas",
427+
};
428+
429+
Main.StartProcess(Process.Start, info);
430+
}
431+
380432
public Application(AppxPackageHelper.IAppxManifestApplication manifestApp, UWP package)
381433
{
382434
// This is done because we cannot use the keyword 'out' along with a property
@@ -403,6 +455,28 @@ public Application(AppxPackageHelper.IAppxManifestApplication manifestApp, UWP p
403455
LogoPath = LogoPathFromUri(LogoUri);
404456

405457
Enabled = true;
458+
CanRunElevated = CanApplicationRunElevated();
459+
}
460+
461+
private bool CanApplicationRunElevated()
462+
{
463+
if (EntryPoint == "Windows.FullTrustApplication")
464+
{
465+
return true;
466+
}
467+
468+
var manifest = Package.Location + "\\AppxManifest.xml";
469+
if (File.Exists(manifest))
470+
{
471+
var file = File.ReadAllText(manifest);
472+
473+
if (file.Contains("TrustLevel=\"mediumIL\"", StringComparison.OrdinalIgnoreCase))
474+
{
475+
return true;
476+
}
477+
}
478+
479+
return false;
406480
}
407481

408482
internal string ResourceFromPri(string packageFullName, string packageName, string rawReferenceValue)

Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,24 @@ public Result Result(string query, IPublicAPI api)
102102
Score = matchResult.Score,
103103
TitleHighlightData = matchResult.MatchData,
104104
ContextData = this,
105-
Action = _ =>
105+
Action = c =>
106106
{
107+
var runAsAdmin = (
108+
c.SpecialKeyState.CtrlPressed &&
109+
c.SpecialKeyState.ShiftPressed &&
110+
!c.SpecialKeyState.AltPressed &&
111+
!c.SpecialKeyState.WinPressed
112+
);
113+
107114
var info = new ProcessStartInfo
108115
{
109116
FileName = LnkResolvedPath ?? FullPath,
110117
WorkingDirectory = ParentDirectory,
111-
UseShellExecute = true
118+
UseShellExecute = true,
119+
Verb = runAsAdmin ? "runas" : null
112120
};
113121

114-
Main.StartProcess(Process.Start, info);
122+
Task.Run(() => Main.StartProcess(Process.Start, info));
115123

116124
return true;
117125
}

Plugins/Flow.Launcher.Plugin.Program/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "Program",
55
"Description": "Search programs in Flow.Launcher",
66
"Author": "qianlifeng",
7-
"Version": "1.5.4",
7+
"Version": "1.6.0",
88
"Language": "csharp",
99
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
1010
"ExecuteFileName": "Flow.Launcher.Plugin.Program.dll",

Plugins/Flow.Launcher.Plugin.Shell/Main.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,14 @@ public List<Result> Query(Query query)
7373
IcoPath = Image,
7474
Action = c =>
7575
{
76-
Execute(Process.Start, PrepareProcessStartInfo(m, c.SpecialKeyState.CtrlPressed));
76+
var runAsAdministrator = (
77+
c.SpecialKeyState.CtrlPressed &&
78+
c.SpecialKeyState.ShiftPressed &&
79+
!c.SpecialKeyState.AltPressed &&
80+
!c.SpecialKeyState.WinPressed
81+
);
82+
83+
Execute(Process.Start, PrepareProcessStartInfo(m, runAsAdministrator));
7784
return true;
7885
}
7986
}));
@@ -106,7 +113,14 @@ private List<Result> GetHistoryCmds(string cmd, Result result)
106113
IcoPath = Image,
107114
Action = c =>
108115
{
109-
Execute(Process.Start, PrepareProcessStartInfo(m.Key));
116+
var runAsAdministrator = (
117+
c.SpecialKeyState.CtrlPressed &&
118+
c.SpecialKeyState.ShiftPressed &&
119+
!c.SpecialKeyState.AltPressed &&
120+
!c.SpecialKeyState.WinPressed
121+
);
122+
123+
Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator));
110124
return true;
111125
}
112126
};
@@ -129,7 +143,14 @@ private Result GetCurrentCmd(string cmd)
129143
IcoPath = Image,
130144
Action = c =>
131145
{
132-
Execute(Process.Start, PrepareProcessStartInfo(cmd));
146+
var runAsAdministrator = (
147+
c.SpecialKeyState.CtrlPressed &&
148+
c.SpecialKeyState.ShiftPressed &&
149+
!c.SpecialKeyState.AltPressed &&
150+
!c.SpecialKeyState.WinPressed
151+
);
152+
153+
Execute(Process.Start, PrepareProcessStartInfo(cmd, runAsAdministrator));
133154
return true;
134155
}
135156
};
@@ -147,7 +168,14 @@ private List<Result> ResultsFromlHistory()
147168
IcoPath = Image,
148169
Action = c =>
149170
{
150-
Execute(Process.Start, PrepareProcessStartInfo(m.Key));
171+
var runAsAdministrator = (
172+
c.SpecialKeyState.CtrlPressed &&
173+
c.SpecialKeyState.ShiftPressed &&
174+
!c.SpecialKeyState.AltPressed &&
175+
!c.SpecialKeyState.WinPressed
176+
);
177+
178+
Execute(Process.Start, PrepareProcessStartInfo(m.Key, runAsAdministrator));
151179
return true;
152180
}
153181
});

Plugins/Flow.Launcher.Plugin.Shell/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "Shell",
55
"Description": "Provide executing commands from Flow Launcher",
66
"Author": "qianlifeng",
7-
"Version": "1.4.3",
7+
"Version": "1.4.4",
88
"Language": "csharp",
99
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
1010
"ExecuteFileName": "Flow.Launcher.Plugin.Shell.dll",

0 commit comments

Comments
 (0)