// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Collections.Generic; using System.Windows; using System.Windows.Input; using Microsoft.PowerToys.Run.Plugin.Registry.Classes; using Microsoft.PowerToys.Run.Plugin.Registry.Properties; using Wox.Plugin; using Wox.Plugin.Logger; namespace Microsoft.PowerToys.Run.Plugin.Registry.Helper { /// /// Helper class to easier work with context menu entries /// internal static class ContextMenuHelper { /// /// Return a list with all context menu entries for the given /// Symbols taken from /// /// The result for the context menu entires /// The name of the this assembly /// A list with context menu entries internal static List GetContextMenu(Result result, string assemblyName) { if (!(result?.ContextData is RegistryEntry entry)) { return new List(0); } var list = new List(); if (string.IsNullOrEmpty(entry.ValueName)) { list.Add(new ContextMenuResult { AcceleratorKey = Key.C, AcceleratorModifiers = ModifierKeys.Control, Action = _ => TryToCopyToClipBoard(entry.GetRegistryKey()), FontFamily = "Segoe MDL2 Assets", Glyph = "\xE8C8", // E8C8 => Symbol: Copy PluginName = assemblyName, Title = $"{Resources.CopyKeyNamePath} (Ctrl+C)", }); } else { list.Add(new ContextMenuResult { AcceleratorKey = Key.C, AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift, Action = _ => TryToCopyToClipBoard(entry.GetValueData()), FontFamily = "Segoe MDL2 Assets", Glyph = "\xF413", // F413 => Symbol: CopyTo PluginName = assemblyName, Title = $"{Resources.CopyValueData} (Ctrl+Shift+C)", }); list.Add(new ContextMenuResult { AcceleratorKey = Key.C, AcceleratorModifiers = ModifierKeys.Control, Action = _ => TryToCopyToClipBoard(entry.GetValueNameWithKey()), FontFamily = "Segoe MDL2 Assets", Glyph = "\xE8C8", // E8C8 => Symbol: Copy PluginName = assemblyName, Title = $"{Resources.CopyValueName} (Ctrl+C)", }); } 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} (Ctrl+Enter)", }); return list; } #pragma warning disable CA1031 // Do not catch general exception types /// /// Open the Windows registry editor and jump to registry key inside the given key (inside the /// /// The to jump in /// if the registry editor was successful open, otherwise internal static bool TryToOpenInRegistryEditor(in RegistryEntry entry) { try { RegistryHelper.OpenRegistryKey(entry.Key?.Name ?? entry.KeyPath); return true; } catch (System.ComponentModel.Win32Exception) { MessageBox.Show( Resources.OpenInRegistryEditorAccessExceptionText, Resources.OpenInRegistryEditorAccessExceptionTitle, MessageBoxButton.OK, MessageBoxImage.Error); return false; } catch (Exception exception) { Log.Exception("Error on opening Windows registry editor", exception, typeof(Main)); return false; } } /// /// Copy the given text to the clipboard /// /// The text to copy to the clipboard /// The text successful copy to the clipboard, otherwise private static bool TryToCopyToClipBoard(in string text) { try { Clipboard.Clear(); Clipboard.SetText(text); return true; } catch (Exception exception) { Log.Exception("Can't copy to clipboard", exception, typeof(Main)); return false; } } #pragma warning restore CA1031 // Do not catch general exception types } }