geode/loader/src/utils/windows/util.cpp

118 lines
2.6 KiB
C++
Raw Normal View History

2022-07-30 12:24:03 -04:00
#include <Geode/utils/platform.hpp>
2022-10-13 06:07:37 -04:00
#include <fs/filesystem.hpp>
2022-07-30 12:24:03 -04:00
#ifdef GEODE_IS_WINDOWS
USE_GEODE_NAMESPACE();
2022-10-30 14:56:36 -04:00
#include "nfdwin.hpp"
#include <Windows.h>
#include <iostream>
#include <shlwapi.h>
#include <shobjidl.h>
#include <sstream>
2022-07-30 12:24:03 -04:00
bool utils::clipboard::write(std::string const& data) {
2022-10-30 14:56:36 -04:00
if (!OpenClipboard(nullptr)) return false;
2022-07-30 12:24:03 -04:00
if (!EmptyClipboard()) {
CloseClipboard();
return false;
}
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, data.size() + 1);
2022-10-30 14:56:36 -04:00
if (!hg) {
CloseClipboard();
return false;
}
auto dest = GlobalLock(hg);
2022-07-30 12:24:03 -04:00
2022-10-30 14:56:36 -04:00
if (!dest) {
CloseClipboard();
return false;
}
2022-07-30 12:24:03 -04:00
2022-10-30 14:56:36 -04:00
memcpy(dest, data.c_str(), data.size() + 1);
2022-07-30 12:24:03 -04:00
2022-10-30 14:56:36 -04:00
GlobalUnlock(hg);
2022-07-30 12:24:03 -04:00
2022-10-30 14:56:36 -04:00
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
2022-07-30 12:24:03 -04:00
2022-10-30 14:56:36 -04:00
GlobalFree(hg);
2022-07-30 12:24:03 -04:00
return true;
}
std::string utils::clipboard::read() {
2022-10-30 14:56:36 -04:00
if (!OpenClipboard(nullptr)) return "";
2022-07-30 12:24:03 -04:00
HANDLE hData = GetClipboardData(CF_TEXT);
if (hData == nullptr) {
CloseClipboard();
return "";
}
2022-10-30 14:56:36 -04:00
char* pszText = static_cast<char*>(GlobalLock(hData));
2022-07-30 12:24:03 -04:00
if (pszText == nullptr) {
CloseClipboard();
return "";
}
std::string text(pszText);
GlobalUnlock(hData);
CloseClipboard();
return text;
}
ghc::filesystem::path utils::file::geodeRoot() {
2022-07-30 12:24:03 -04:00
return ghc::filesystem::path(CCFileUtils::sharedFileUtils()->getWritablePath2().c_str());
}
bool utils::file::openFolder(ghc::filesystem::path const& path) {
2022-10-30 14:56:36 -04:00
ShellExecuteA(NULL, "open", path.string().c_str(), NULL, NULL, SW_SHOWDEFAULT);
return true;
2022-07-30 12:24:03 -04:00
}
Result<ghc::filesystem::path> utils::file::pickFile(
2022-10-30 14:56:36 -04:00
file::PickMode mode, file::FilePickOptions const& options
) {
#define TURN_INTO_NFDMODE(mode) \
case file::PickMode::mode: nfdMode = NFDMode::mode; break;
NFDMode nfdMode;
switch (mode) {
TURN_INTO_NFDMODE(OpenFile);
TURN_INTO_NFDMODE(SaveFile);
TURN_INTO_NFDMODE(OpenFolder);
default: return Err("Unknown open mode");
}
ghc::filesystem::path path;
auto r = nfdPick(nfdMode, options, &path);
if (!r) {
return Err(r.error());
}
return Ok(path);
}
Result<std::vector<ghc::filesystem::path>> utils::file::pickFiles(
file::FilePickOptions const& options
) {
std::vector<ghc::filesystem::path> paths;
auto r = nfdPick(NFDMode::OpenFolder, options, &paths);
if (!r) {
return Err(r.error());
}
return Ok(paths);
}
void utils::web::openLinkInBrowser(std::string const& url) {
2022-07-30 12:24:03 -04:00
ShellExecuteA(0, 0, url.c_str(), 0, 0, SW_SHOW);
}
#endif