1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 | #include "fsShareSheetServiceWindows.h"
#include "../fsSharePlatform.h"
#include "../save/fsShareSheetServiceSave.h"
#include <windows.h>
#include <shlobj_core.h>
#include <shobjidl_core.h>
#include <winrt/Windows.ApplicationModel.DataTransfer.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.h>
#include <atomic>
namespace DataTransfer = winrt::Windows::ApplicationModel::DataTransfer;
namespace Storage = winrt::Windows::Storage;
namespace
{
// The game's own top-level window; the share UI is anchored to it.
HWND gameWindowGet()
{
HWND window = GetActiveWindow();
if (!window)
{
window = GetForegroundWindow();
DWORD processId = 0;
GetWindowThreadProcessId(window, &processId);
if (processId != GetCurrentProcessId())
{
window = nullptr;
}
}
return window;
}
}
struct fsShareSheetServiceWindows::Impl
{
DataTransfer::DataTransferManager manager{nullptr};
winrt::event_token requestedToken{};
fsShareRequest pending;
};
fsShareSheetServiceWindows::fsShareSheetServiceWindows(std::string saveFolderName, const bool saveReveal) :
mImpl(std::make_unique<Impl>()),
mSave(std::make_unique<fsShareSheetServiceSave>(std::move(saveFolderName), saveReveal))
{
}
fsShareSheetServiceWindows::~fsShareSheetServiceWindows()
{
if (mImpl->manager)
{
mImpl->manager.DataRequested(mImpl->requestedToken);
}
}
std::vector<fsShareTarget> fsShareSheetServiceWindows::targetsGet() const
{
std::vector<fsShareTarget> targets{{"system", "shareTarget_system", ".png"}};
for (const auto& target : mSave->targetsGet())
{
targets.push_back(target);<--- Consider using std::copy algorithm instead of a raw loop.
}
return targets;
}
void fsShareSheetServiceWindows::share(const fsShareRequest& request, const std::string& targetId, DoneCallback done)
{
if (targetId == fsShareSheetServiceSave::kTargetId)
{
mSave->share(request, targetId, std::move(done));
return;
}
const HWND window = gameWindowGet();
if (!window)
{
mSave->share(request, fsShareSheetServiceSave::kTargetId, std::move(done));
return;
}
try
{
// Harmless if the thread already has an apartment.
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
auto interop = winrt::get_activation_factory<DataTransfer::DataTransferManager, IDataTransferManagerInterop>();
if (!mImpl->manager)
{
winrt::check_hresult(interop->GetForWindow(window, winrt::guid_of<DataTransfer::DataTransferManager>(),
winrt::put_abi(mImpl->manager)));
// Windows asks for the data once the player opens the sheet; fill it from the pending request.
Impl* const impl = mImpl.get();
mImpl->requestedToken = mImpl->manager.DataRequested(
[impl](DataTransfer::DataTransferManager const&, DataTransfer::DataRequestedEventArgs const& args)
{
const fsShareRequest& pending = impl->pending;
DataTransfer::DataRequest dataRequest = args.Request();
DataTransfer::DataPackage data = dataRequest.Data();
data.Properties().Title(winrt::to_hstring(pending.subject.empty() ? std::string(" ") : pending.subject));
if (!pending.text.empty())
{
data.SetText(winrt::to_hstring(pending.text));
}
if (pending.files.empty())
{
return;
}
// Files load asynchronously; the deferral keeps the request open until they are in.
auto deferral = dataRequest.GetDeferral();
auto items = winrt::single_threaded_vector<Storage::IStorageItem>();
auto remaining = std::make_shared<std::atomic<int>>(static_cast<int>(pending.files.size()));
for (const auto& file : pending.files)
{
Storage::StorageFile::GetFileFromPathAsync(winrt::to_hstring(file.path)).Completed(
[data, deferral, items, remaining](auto const& operation, winrt::Windows::Foundation::AsyncStatus status)
{
if (status == winrt::Windows::Foundation::AsyncStatus::Completed)
{
items.Append(operation.GetResults());
}
if (--*remaining == 0)
{
if (items.Size() > 0)
{
data.SetStorageItems(items.GetView());
}
deferral.Complete();
}
});
}
});
}
mImpl->pending = request;
winrt::check_hresult(interop->ShowShareUIForWindow(window));
// Windows doesn't report a cancel on every version, so the outcome is unknown.
mCompletions.push(done, fsShareResult::eDISMISSED);
}
catch (...)
{
// No share UI (older Windows, or WinRT unavailable): save to Pictures instead.
mSave->share(request, fsShareSheetServiceSave::kTargetId, std::move(done));
}
}
void fsShareSheetServiceWindows::update()
{
mSave->update();
mCompletions.deliver();
}
std::filesystem::path fsSharePlatform::picturesFolderGet()
{
PWSTR folder = nullptr;
std::filesystem::path path;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Pictures, 0, nullptr, &folder)))
{
path = folder;
}
CoTaskMemFree(folder);
return path;
}
bool fsSharePlatform::revealInFolder(const std::filesystem::path& file)
{
PIDLIST_ABSOLUTE item = ILCreateFromPathW(file.wstring().c_str());
if (!item)
{
return false;
}
const HRESULT result = SHOpenFolderAndSelectItems(item, 0, nullptr, 0);
ILFree(item);
return SUCCEEDED(result);
}
|