Skip to content

Commit

Permalink
Implemented copy similar to the unit conversion plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
zziger committed Jun 30, 2022
1 parent e178980 commit 0536de5
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Windows;
Expand Down Expand Up @@ -154,11 +155,7 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
new()
{
Title = "Copy link (Ctrl + Enter)",
Action = _ =>
{
Clipboard.SetDataObject(link, true);
return false;
},
Action = _ => Copy(link),
Glyph = "\uE71B",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Return,
Expand All @@ -167,11 +164,7 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
new()
{
Title = "Copy name (Shift + Enter)",
Action = _ =>
{
Clipboard.SetDataObject(e.native.AltName, true);
return false;
},
Action = _ => Copy(e.native.AltName),
Glyph = "\uE8C8",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Return,
Expand All @@ -180,11 +173,7 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
new()
{
Title = "Copy capitalized name (Ctrl + Shift + Enter)",
Action = _ =>
{
Clipboard.SetDataObject(e.native.AltName[0].ToString().ToUpperInvariant() + e.native.AltName[1..], true);
return false;
},
Action = _ => Copy(e.native.AltName[0].ToString().ToUpperInvariant() + e.native.AltName[1..]),
Glyph = "\uE8C8",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Return,
Expand All @@ -193,6 +182,27 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
};
}

private bool Copy(string text)
{
var ret = false;
var thread = new Thread(_ =>
{
try
{
Clipboard.SetText(text);
ret = true;
}
catch (ExternalException)
{
MessageBox.Show("Failed to copy text");
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return ret;
}

public void Dispose()
{
}
Expand Down

0 comments on commit 0536de5

Please sign in to comment.