Format like smack-a-brother, and write comments as sentences
Four-space indents, braces on their own lines, and one-statement bodies without braces. Semicolons in comments became full stops. Designated initializers may leave fields out without a warning.
This commit is contained in:
1 parent
40f6b554b5
commit
7afdf709c3
176 files changed
+20500
-19048
No files matched your search
+65
-56
@@ -1,12 +1,12 @@
|
||||
#include "dialog.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <windows.h>
|
||||
#elif __unix__
|
||||
#else
|
||||
#include "log.h"
|
||||
#include "localization.h"
|
||||
#include "ui/toast.h"
|
||||
#include "log.h"
|
||||
#include "localization.h"
|
||||
#include "ui/toast.h"
|
||||
#endif
|
||||
|
||||
#include <cctype>
|
||||
@@ -16,45 +16,47 @@
|
||||
|
||||
#include "path.h"
|
||||
|
||||
static void callback(void* userData, const char* const* filelist, int filter)
|
||||
{
|
||||
static void callback(void* userData, const char* const* filelist, int filter)
|
||||
{
|
||||
auto self = (Dialog*)(userData);
|
||||
|
||||
if (filelist && filelist[0] && strlen(filelist[0]) > 0)
|
||||
{
|
||||
self->paths.clear();
|
||||
for (int i = 0; filelist[i] && strlen(filelist[i]) > 0; ++i)
|
||||
self->paths.push_back(path_from_utf8(filelist[i]));
|
||||
self->path = self->paths.empty() ? std::filesystem::path{} : self->paths.front();
|
||||
self->selectedFilter = filter;
|
||||
self->paths.clear();
|
||||
for (int i = 0; filelist[i] && strlen(filelist[i]) > 0; ++i)
|
||||
self->paths.push_back(path_from_utf8(filelist[i]));
|
||||
self->path = self->paths.empty() ? std::filesystem::path{} : self->paths.front();
|
||||
self->selectedFilter = filter;
|
||||
}
|
||||
else
|
||||
{
|
||||
self->selectedFilter = -1;
|
||||
self->path.clear();
|
||||
self->paths.clear();
|
||||
self->selectedFilter = -1;
|
||||
self->path.clear();
|
||||
self->paths.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Dialog::Dialog(SDL_Window* window)
|
||||
{
|
||||
Dialog::Dialog(SDL_Window* window)
|
||||
{
|
||||
*this = Dialog();
|
||||
this->window = window;
|
||||
}
|
||||
}
|
||||
|
||||
void Dialog::file_open(Type type, bool isMany)
|
||||
{
|
||||
if (type == Dialog::NONE) return;
|
||||
void Dialog::file_open(Type type, bool isMany)
|
||||
{
|
||||
if (type == Dialog::NONE)
|
||||
return;
|
||||
path.clear();
|
||||
paths.clear();
|
||||
SDL_ShowOpenFileDialog(callback, this, window, FILTERS[TYPE_FILTERS[type]], std::size(FILTERS[TYPE_FILTERS[type]]),
|
||||
nullptr, isMany);
|
||||
this->type = type;
|
||||
}
|
||||
}
|
||||
|
||||
void Dialog::file_save(Type type, const std::filesystem::path& defaultPath)
|
||||
{
|
||||
if (type == Dialog::NONE) return;
|
||||
void Dialog::file_save(Type type, const std::filesystem::path& defaultPath)
|
||||
{
|
||||
if (type == Dialog::NONE)
|
||||
return;
|
||||
path.clear();
|
||||
paths.clear();
|
||||
auto defaultPathString = path_to_utf8(defaultPath);
|
||||
@@ -62,57 +64,59 @@
|
||||
SDL_ShowSaveFileDialog(callback, this, window, FILTERS[TYPE_FILTERS[type]], std::size(FILTERS[TYPE_FILTERS[type]]),
|
||||
defaultPathCStr);
|
||||
this->type = type;
|
||||
}
|
||||
}
|
||||
|
||||
void Dialog::folder_open(Type type)
|
||||
{
|
||||
if (type == Dialog::NONE) return;
|
||||
void Dialog::folder_open(Type type)
|
||||
{
|
||||
if (type == Dialog::NONE)
|
||||
return;
|
||||
path.clear();
|
||||
paths.clear();
|
||||
SDL_ShowOpenFolderDialog(callback, this, window, nullptr, false);
|
||||
this->type = type;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __unix__
|
||||
// A string as one single-quoted shell word.
|
||||
static std::string shell_quote(std::string_view text)
|
||||
{
|
||||
// A string as one single-quoted shell word.
|
||||
static std::string shell_quote(std::string_view text)
|
||||
{
|
||||
std::string quoted = "'";
|
||||
for (auto character : text)
|
||||
quoted += character == '\'' ? std::string("'\\''") : std::string(1, character);
|
||||
quoted += character == '\'' ? std::string("'\\''") : std::string(1, character);
|
||||
return quoted + "'";
|
||||
}
|
||||
}
|
||||
|
||||
// A file:// URI; everything but unreserved characters and slashes is percent-encoded (commas too, which dbus-send
|
||||
// would otherwise take as array separators).
|
||||
static std::string file_uri_get(const std::filesystem::path& path)
|
||||
{
|
||||
// A file:// URI. Everything but unreserved characters and slashes is percent-encoded (commas too, which dbus-send
|
||||
// would otherwise take as array separators).
|
||||
static std::string file_uri_get(const std::filesystem::path& path)
|
||||
{
|
||||
std::string uri = "file://";
|
||||
for (unsigned char character : path_to_utf8(path))
|
||||
if (std::isalnum(character) || std::string_view("-._~/").contains((char)character))
|
||||
uri += (char)character;
|
||||
else
|
||||
uri += std::format("%{:02X}", character);
|
||||
if (std::isalnum(character) || std::string_view("-._~/").contains((char)character))
|
||||
uri += (char)character;
|
||||
else
|
||||
uri += std::format("%{:02X}", character);
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Shows a folder in the system's file manager; a file is shown selected in its folder.
|
||||
void Dialog::file_explorer_open(const std::filesystem::path& path)
|
||||
{
|
||||
if (path.empty()) return;
|
||||
// Shows a folder in the system's file manager. A file is shown selected in its folder.
|
||||
void Dialog::file_explorer_open(const std::filesystem::path& path)
|
||||
{
|
||||
if (path.empty())
|
||||
return;
|
||||
std::error_code ec;
|
||||
auto absolutePath = std::filesystem::absolute(path, ec);
|
||||
auto isFile = std::filesystem::is_regular_file(absolutePath, ec);
|
||||
#ifdef _WIN32
|
||||
if (isFile)
|
||||
ShellExecuteW(nullptr, L"open", L"explorer.exe", (L"/select,\"" + absolutePath.native() + L"\"").c_str(), nullptr,
|
||||
SW_SHOWNORMAL);
|
||||
ShellExecuteW(nullptr, L"open", L"explorer.exe", (L"/select,\"" + absolutePath.native() + L"\"").c_str(),
|
||||
nullptr, SW_SHOWNORMAL);
|
||||
else
|
||||
ShellExecuteW(nullptr, L"open", absolutePath.native().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
|
||||
ShellExecuteW(nullptr, L"open", absolutePath.native().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
|
||||
#elif __unix__
|
||||
// File managers that implement org.freedesktop.FileManager1 (Nautilus, Dolphin, Nemo, Caja, Thunar...) select the
|
||||
// file; anything else opens its folder.
|
||||
// file. Anything else opens its folder.
|
||||
auto folder = shell_quote(path_to_utf8(isFile ? absolutePath.parent_path() : absolutePath));
|
||||
auto command = isFile ? std::format("(dbus-send --session --print-reply --dest=org.freedesktop.FileManager1 "
|
||||
"--type=method_call /org/freedesktop/FileManager1 "
|
||||
@@ -124,9 +128,14 @@
|
||||
#else
|
||||
toast_log(Level::WARNING, TOAST_NOT_SUPPORTED);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void Dialog::reset() { *this = Dialog(this->window); }
|
||||
|
||||
bool Dialog::is_selected(Type type) const { return this->type == type && !paths.empty(); }
|
||||
void Dialog::reset()
|
||||
{
|
||||
*this = Dialog(this->window);
|
||||
}
|
||||
|
||||
bool Dialog::is_selected(Type type) const
|
||||
{
|
||||
return this->type == type && !paths.empty();
|
||||
}
|
||||
Reference in new issue
Block a user