140 lines
4.5 KiB
C#
140 lines
4.5 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
|
using Wox.Plugin;
|
|
using Wox.Plugin.Logger;
|
|
|
|
namespace Community.PowerToys.Run.Plugin.YandexGPT;
|
|
|
|
public class Main : IDelayedExecutionPlugin, IContextMenu, ISettingProvider, IDisposable
|
|
{
|
|
#region Plugin Info
|
|
public static string PluginId => "C2218AB0D86F4345B55C60F9418A811C";
|
|
public string Name => "Yandex GPT";
|
|
public string Description => "Плагин для выполнения поисковых запросов через Yandex GPT";
|
|
public IEnumerable<PluginAdditionalOption> AdditionalOptions =>
|
|
[
|
|
new PluginAdditionalOption()
|
|
{
|
|
Key = nameof(Token),
|
|
DisplayLabel = "Токен",
|
|
DisplayDescription = "Ключ доступа к YandexGPT",
|
|
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox,
|
|
TextValue = Token
|
|
}
|
|
];
|
|
#endregion
|
|
|
|
private bool _disposed;
|
|
|
|
private string? Token { get; set; }
|
|
private PluginInitContext? Context { get; set; }
|
|
private string? IconPath { get; set; }
|
|
|
|
public void Init(PluginInitContext context)
|
|
{
|
|
Log.Info("Init", GetType());
|
|
Context = context ?? throw new ArgumentNullException(nameof(context));
|
|
//Context.API.ThemeChanged += OnThemeChanged;
|
|
//UpdateIconPath(Context.API.GetCurrentTheme());
|
|
}
|
|
|
|
public List<Result> Query(Query query, bool delayedExecution)
|
|
{
|
|
Log.Info("Query: " + query.Search, GetType());
|
|
|
|
if (query.ActionKeyword != Token)
|
|
return [];
|
|
|
|
var search = query.Search;
|
|
|
|
return [
|
|
new()
|
|
{
|
|
QueryTextDisplay = query.Search,
|
|
IcoPath = IconPath,
|
|
Title = $"YandexGPT: Чет такое {search}",
|
|
SubTitle = $"Ответ: иди туда!",
|
|
ToolTipData = new ToolTipData("Ответы", $"Тут ответ\nИ тут ответ"),
|
|
ContextData = Token,
|
|
}
|
|
];
|
|
}
|
|
|
|
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
|
|
{
|
|
Log.Info("LoadContextMenus", GetType());
|
|
|
|
if (selectedResult?.ContextData is (int words, TimeSpan transcription))
|
|
{
|
|
return
|
|
[
|
|
new ContextMenuResult
|
|
{
|
|
PluginName = Name,
|
|
Title = "Copy (Enter)",
|
|
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
|
|
Glyph = "\xE8C8", // Copy
|
|
AcceleratorKey = Key.Enter,
|
|
Action = _ => CopyToClipboard(words.ToString()),
|
|
},
|
|
new ContextMenuResult
|
|
{
|
|
PluginName = Name,
|
|
Title = "Copy time (Ctrl+Enter)",
|
|
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
|
|
Glyph = "\xE916", // Stopwatch
|
|
AcceleratorKey = Key.Enter,
|
|
AcceleratorModifiers = ModifierKeys.Control,
|
|
Action = _ => CopyToClipboard(transcription.ToString()),
|
|
},
|
|
];
|
|
}
|
|
|
|
if (selectedResult?.ContextData is int characters)
|
|
{
|
|
return
|
|
[
|
|
new ContextMenuResult
|
|
{
|
|
PluginName = Name,
|
|
Title = "Copy (Enter)",
|
|
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
|
|
Glyph = "\xE8C8", // Copy
|
|
AcceleratorKey = Key.Enter,
|
|
Action = _ => CopyToClipboard(characters.ToString()),
|
|
},
|
|
];
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
public Control CreateSettingPanel() => throw new NotImplementedException();
|
|
|
|
public void UpdateSettings(PowerLauncherPluginSettings settings)
|
|
{
|
|
var tokenSettings = settings.AdditionalOptions.SingleOrDefault(x => x.Key == nameof(Token));
|
|
if (tokenSettings is null)
|
|
return;
|
|
Token = tokenSettings.TextValue;
|
|
}
|
|
|
|
|
|
private static bool CopyToClipboard(string? value)
|
|
{
|
|
if (value != null)
|
|
Clipboard.SetText(value);
|
|
return true;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Log.Info("Dispose", GetType());
|
|
if (!_disposed)
|
|
return;
|
|
_disposed = true;
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
} |