Skip to content

Commit e0f0bed

Browse files
committed
Revert "Fix clipboard action under sta thread issue"
1 parent fd3576d commit e0f0bed

File tree

4 files changed

+20
-164
lines changed

4 files changed

+20
-164
lines changed

Flow.Launcher.Infrastructure/NativeMethods.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,4 @@ WM_KEYUP
1616
WM_SYSKEYDOWN
1717
WM_SYSKEYUP
1818

19-
EnumWindows
20-
21-
OleInitialize
22-
OleUninitialize
19+
EnumWindows

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public SearchPrecisionScore QuerySearchPrecision
230230
[JsonIgnore]
231231
public ObservableCollection<BuiltinShortcutModel> BuiltinShortcuts { get; set; } = new()
232232
{
233-
new BuiltinShortcutModel("{clipboard}", "shortcut_clipboard_description", () => Win32Helper.StartSTATaskAsync(Clipboard.GetText).Result),
233+
new BuiltinShortcutModel("{clipboard}", "shortcut_clipboard_description", Clipboard.GetText),
234234
new BuiltinShortcutModel("{active_explorer_path}", "shortcut_active_explorer_path", FileExplorerHelper.GetActiveExplorerPath)
235235
};
236236

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,12 @@
11
using System;
22
using System.Runtime.InteropServices;
3-
using System.Threading;
4-
using System.Threading.Tasks;
53
using System.Windows.Interop;
64
using System.Windows;
7-
using Windows.Win32;
85

96
namespace Flow.Launcher.Infrastructure
107
{
118
public static class Win32Helper
129
{
13-
#region STA Thread
14-
15-
/*
16-
Found on https://github.com/files-community/Files
17-
*/
18-
19-
public static Task StartSTATaskAsync(Action action)
20-
{
21-
var taskCompletionSource = new TaskCompletionSource();
22-
Thread thread = new(() =>
23-
{
24-
PInvoke.OleInitialize();
25-
26-
try
27-
{
28-
action();
29-
taskCompletionSource.SetResult();
30-
}
31-
catch (System.Exception)
32-
{
33-
taskCompletionSource.SetResult();
34-
}
35-
finally
36-
{
37-
PInvoke.OleUninitialize();
38-
}
39-
})
40-
{
41-
IsBackground = true,
42-
Priority = ThreadPriority.Normal
43-
};
44-
45-
thread.SetApartmentState(ApartmentState.STA);
46-
thread.Start();
47-
48-
return taskCompletionSource.Task;
49-
}
50-
51-
public static Task StartSTATaskAsync(Func<Task> func)
52-
{
53-
var taskCompletionSource = new TaskCompletionSource();
54-
Thread thread = new(async () =>
55-
{
56-
PInvoke.OleInitialize();
57-
58-
try
59-
{
60-
await func();
61-
taskCompletionSource.SetResult();
62-
}
63-
catch (System.Exception)
64-
{
65-
taskCompletionSource.SetResult();
66-
}
67-
finally
68-
{
69-
PInvoke.OleUninitialize();
70-
}
71-
})
72-
{
73-
IsBackground = true,
74-
Priority = ThreadPriority.Normal
75-
};
76-
77-
thread.SetApartmentState(ApartmentState.STA);
78-
thread.Start();
79-
80-
return taskCompletionSource.Task;
81-
}
82-
83-
public static Task<T?> StartSTATaskAsync<T>(Func<T> func)
84-
{
85-
var taskCompletionSource = new TaskCompletionSource<T?>();
86-
87-
Thread thread = new(() =>
88-
{
89-
PInvoke.OleInitialize();
90-
91-
try
92-
{
93-
taskCompletionSource.SetResult(func());
94-
}
95-
catch (System.Exception)
96-
{
97-
taskCompletionSource.SetResult(default);
98-
}
99-
finally
100-
{
101-
PInvoke.OleUninitialize();
102-
}
103-
})
104-
{
105-
IsBackground = true,
106-
Priority = ThreadPriority.Normal
107-
};
108-
109-
thread.SetApartmentState(ApartmentState.STA);
110-
thread.Start();
111-
112-
return taskCompletionSource.Task;
113-
}
114-
115-
public static Task<T?> StartSTATaskAsync<T>(Func<Task<T>> func)
116-
{
117-
var taskCompletionSource = new TaskCompletionSource<T?>();
118-
119-
Thread thread = new(async () =>
120-
{
121-
PInvoke.OleInitialize();
122-
try
123-
{
124-
taskCompletionSource.SetResult(await func());
125-
}
126-
catch (System.Exception)
127-
{
128-
taskCompletionSource.SetResult(default);
129-
}
130-
finally
131-
{
132-
PInvoke.OleUninitialize();
133-
}
134-
})
135-
{
136-
IsBackground = true,
137-
Priority = ThreadPriority.Normal
138-
};
139-
140-
thread.SetApartmentState(ApartmentState.STA);
141-
thread.Start();
142-
143-
return taskCompletionSource.Task;
144-
}
145-
146-
#endregion
147-
14810
#region Blur Handling
14911

15012
/*

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,38 +117,35 @@ public void ShellRun(string cmd, string filename = "cmd.exe")
117117
ShellCommand.Execute(startInfo);
118118
}
119119

120-
public async void CopyToClipboard(string stringToCopy, bool directCopy = false, bool showDefaultNotification = true)
120+
public void CopyToClipboard(string stringToCopy, bool directCopy = false, bool showDefaultNotification = true)
121121
{
122122
if (string.IsNullOrEmpty(stringToCopy))
123123
return;
124124

125-
await Win32Helper.StartSTATaskAsync(() =>
125+
var isFile = File.Exists(stringToCopy);
126+
if (directCopy && (isFile || Directory.Exists(stringToCopy)))
126127
{
127-
var isFile = File.Exists(stringToCopy);
128-
if (directCopy && (isFile || Directory.Exists(stringToCopy)))
129-
{
130-
var paths = new StringCollection
128+
var paths = new StringCollection
131129
{
132130
stringToCopy
133131
};
134132

135-
Clipboard.SetFileDropList(paths);
133+
Clipboard.SetFileDropList(paths);
136134

137-
if (showDefaultNotification)
138-
ShowMsg(
139-
$"{GetTranslation("copy")} {(isFile ? GetTranslation("fileTitle") : GetTranslation("folderTitle"))}",
140-
GetTranslation("completedSuccessfully"));
141-
}
142-
else
143-
{
144-
Clipboard.SetDataObject(stringToCopy);
135+
if (showDefaultNotification)
136+
ShowMsg(
137+
$"{GetTranslation("copy")} {(isFile ? GetTranslation("fileTitle") : GetTranslation("folderTitle"))}",
138+
GetTranslation("completedSuccessfully"));
139+
}
140+
else
141+
{
142+
Clipboard.SetDataObject(stringToCopy);
145143

146-
if (showDefaultNotification)
147-
ShowMsg(
148-
$"{GetTranslation("copy")} {GetTranslation("textTitle")}",
149-
GetTranslation("completedSuccessfully"));
150-
}
151-
});
144+
if (showDefaultNotification)
145+
ShowMsg(
146+
$"{GetTranslation("copy")} {GetTranslation("textTitle")}",
147+
GetTranslation("completedSuccessfully"));
148+
}
152149
}
153150

154151
public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible;

0 commit comments

Comments
 (0)