[PT Run] Fix some keyboard issues on plugin (#13490)

* Don't use enter for accelerator keys in results

Enter is already used to select commands in the context menu in the
result entries.

* Don't crash when trying to show a tooltip

* Clear hanging tooltips when keyboard navigating

* Starting/stopping service on Enter
This commit is contained in:
Jaime Bernardo
2021-09-28 15:30:44 +01:00
committed by GitHub
parent 7874b77104
commit c85fa98bcc
4 changed files with 54 additions and 16 deletions

View File

@@ -75,11 +75,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Registry.Helper
list.Add(new ContextMenuResult list.Add(new ContextMenuResult
{ {
AcceleratorKey = Key.Enter, AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ => TryToOpenInRegistryEditor(entry), Action = _ => TryToOpenInRegistryEditor(entry),
FontFamily = "Segoe MDL2 Assets", FontFamily = "Segoe MDL2 Assets",
Glyph = "\xE8A7", // E8A7 => Symbol: OpenInNewWindow Glyph = "\xE8A7", // E8A7 => Symbol: OpenInNewWindow
PluginName = assemblyName, PluginName = assemblyName,
Title = $"{Resources.OpenKeyInRegistryEditor} (Enter)", Title = $"{Resources.OpenKeyInRegistryEditor} (Ctrl+Enter)",
}); });
return list; return list;

View File

@@ -9,6 +9,7 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.ServiceProcess; using System.ServiceProcess;
using System.Threading.Tasks;
using Microsoft.PowerToys.Run.Plugin.Service.Properties; using Microsoft.PowerToys.Run.Plugin.Service.Properties;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Plugin; using Wox.Plugin;
@@ -18,18 +19,41 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Helpers
{ {
public static class ServiceHelper public static class ServiceHelper
{ {
public static IEnumerable<Result> Search(string search, string icoPath) public static IEnumerable<Result> Search(string search, string icoPath, PluginInitContext context)
{ {
var services = ServiceController.GetServices(); var services = ServiceController.GetServices();
return services return services
.Where(s => s.DisplayName.StartsWith(search, StringComparison.OrdinalIgnoreCase) || s.ServiceName.StartsWith(search, StringComparison.OrdinalIgnoreCase)) .Where(s => s.DisplayName.StartsWith(search, StringComparison.OrdinalIgnoreCase) || s.ServiceName.StartsWith(search, StringComparison.OrdinalIgnoreCase))
.Select(s => new Result .Select(s =>
{ {
Title = GetResultTitle(s), ServiceResult serviceResult = new ServiceResult(s);
SubTitle = GetResultSubTitle(s), Func<ActionContext, bool> serviceAction;
if (serviceResult.IsRunning)
{
serviceAction = _ =>
{
Task.Run(() => ServiceHelper.ChangeStatus(serviceResult, Action.Stop, context.API));
return true;
};
}
else
{
serviceAction = _ =>
{
Task.Run(() => ServiceHelper.ChangeStatus(serviceResult, Action.Start, context.API));
return true;
};
}
return new Result
{
Title = ServiceHelper.GetResultTitle(s),
SubTitle = ServiceHelper.GetResultSubTitle(s),
IcoPath = icoPath, IcoPath = icoPath,
ContextData = new ServiceResult(s), ContextData = serviceResult,
Action = serviceAction,
};
}); });
} }

View File

@@ -51,6 +51,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Service
Glyph = "\xE71A", Glyph = "\xE71A",
FontFamily = "Segoe MDL2 Assets", FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Enter, AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ => Action = _ =>
{ {
Task.Run(() => ServiceHelper.ChangeStatus(serviceResult, Action.Stop, _context.API)); Task.Run(() => ServiceHelper.ChangeStatus(serviceResult, Action.Stop, _context.API));
@@ -84,6 +85,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Service
Glyph = "\xEDB5", Glyph = "\xEDB5",
FontFamily = "Segoe MDL2 Assets", FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Enter, AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ => Action = _ =>
{ {
Task.Run(() => ServiceHelper.ChangeStatus(serviceResult, Action.Start, _context.API)); Task.Run(() => ServiceHelper.ChangeStatus(serviceResult, Action.Start, _context.API));
@@ -114,7 +116,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Service
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
var search = query?.Search ?? string.Empty; var search = query?.Search ?? string.Empty;
return ServiceHelper.Search(search, _icoPath).ToList(); return ServiceHelper.Search(search, _icoPath, _context).ToList();
} }
public string GetTranslatedPluginTitle() public string GetTranslatedPluginTitle()

View File

@@ -58,6 +58,8 @@ namespace PowerLauncher
if (listView.SelectedItem != null) if (listView.SelectedItem != null)
{ {
ListBoxItem listBoxItem = (ListBoxItem)listView.ItemContainerGenerator.ContainerFromItem(listView.SelectedItem); ListBoxItem listBoxItem = (ListBoxItem)listView.ItemContainerGenerator.ContainerFromItem(listView.SelectedItem);
if (listBoxItem != null)
{
ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(listBoxItem); ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
DataTemplate dataTemplate = contentPresenter.ContentTemplate; DataTemplate dataTemplate = contentPresenter.ContentTemplate;
Button button = (Button)dataTemplate.FindName("commandButton", contentPresenter); Button button = (Button)dataTemplate.FindName("commandButton", contentPresenter);
@@ -67,6 +69,15 @@ namespace PowerLauncher
tooltip.PlacementRectangle = new Rect(0, button.Height, 0, 0); tooltip.PlacementRectangle = new Rect(0, button.Height, 0, 0);
tooltip.IsOpen = true; tooltip.IsOpen = true;
} }
else
{
HideCurrentToolTip();
}
}
else
{
HideCurrentToolTip();
}
} }
private void ToolTip_Opened(object sender, RoutedEventArgs e) private void ToolTip_Opened(object sender, RoutedEventArgs e)