Skip to content

Commit

Permalink
[PT Run] Fix some keyboard issues on plugin (#13490)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jaimecbernardo committed Sep 28, 2021
1 parent 7874b77 commit c85fa98
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ internal static List<ContextMenuResult> GetContextMenu(Result result, string ass
list.Add(new ContextMenuResult
{
AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ => TryToOpenInRegistryEditor(entry),
FontFamily = "Segoe MDL2 Assets",
Glyph = "\xE8A7", // E8A7 => Symbol: OpenInNewWindow
PluginName = assemblyName,
Title = $"{Resources.OpenKeyInRegistryEditor} (Enter)",
Title = $"{Resources.OpenKeyInRegistryEditor} (Ctrl+Enter)",
});

return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Threading.Tasks;
using Microsoft.PowerToys.Run.Plugin.Service.Properties;
using Wox.Infrastructure;
using Wox.Plugin;
Expand All @@ -18,18 +19,41 @@ namespace Microsoft.PowerToys.Run.Plugin.Service.Helpers
{
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();

return services
.Where(s => s.DisplayName.StartsWith(search, StringComparison.OrdinalIgnoreCase) || s.ServiceName.StartsWith(search, StringComparison.OrdinalIgnoreCase))
.Select(s => new Result
.Select(s =>
{
Title = GetResultTitle(s),
SubTitle = GetResultSubTitle(s),
IcoPath = icoPath,
ContextData = new ServiceResult(s),
ServiceResult serviceResult = new ServiceResult(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,
ContextData = serviceResult,
Action = serviceAction,
};
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
Glyph = "\xE71A",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ =>
{
Task.Run(() => ServiceHelper.ChangeStatus(serviceResult, Action.Stop, _context.API));
Expand Down Expand Up @@ -84,6 +85,7 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
Glyph = "\xEDB5",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ =>
{
Task.Run(() => ServiceHelper.ChangeStatus(serviceResult, Action.Start, _context.API));
Expand Down Expand Up @@ -114,7 +116,7 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
public List<Result> Query(Query query)
{
var search = query?.Search ?? string.Empty;
return ServiceHelper.Search(search, _icoPath).ToList();
return ServiceHelper.Search(search, _icoPath, _context).ToList();
}

public string GetTranslatedPluginTitle()
Expand Down
27 changes: 19 additions & 8 deletions src/modules/launcher/PowerLauncher/ResultList.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,25 @@ private void ContextMenuListView_SelectionChanged(object sender, SelectionChange
if (listView.SelectedItem != null)
{
ListBoxItem listBoxItem = (ListBoxItem)listView.ItemContainerGenerator.ContainerFromItem(listView.SelectedItem);
ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
DataTemplate dataTemplate = contentPresenter.ContentTemplate;
Button button = (Button)dataTemplate.FindName("commandButton", contentPresenter);
ToolTip tooltip = button.ToolTip as ToolTip;
tooltip.PlacementTarget = button;
tooltip.Placement = System.Windows.Controls.Primitives.PlacementMode.Right;
tooltip.PlacementRectangle = new Rect(0, button.Height, 0, 0);
tooltip.IsOpen = true;
if (listBoxItem != null)
{
ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
DataTemplate dataTemplate = contentPresenter.ContentTemplate;
Button button = (Button)dataTemplate.FindName("commandButton", contentPresenter);
ToolTip tooltip = button.ToolTip as ToolTip;
tooltip.PlacementTarget = button;
tooltip.Placement = System.Windows.Controls.Primitives.PlacementMode.Right;
tooltip.PlacementRectangle = new Rect(0, button.Height, 0, 0);
tooltip.IsOpen = true;
}
else
{
HideCurrentToolTip();
}
}
else
{
HideCurrentToolTip();
}
}

Expand Down

0 comments on commit c85fa98

Please sign in to comment.