Update to spice2x-23-06-08

This commit is contained in:
[ ]
2023-09-20 02:09:17 +09:00
parent 8957d9d446
commit d03cca1fe2
98 changed files with 21957 additions and 4977 deletions
+206 -42
View File
@@ -3,11 +3,13 @@
#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cstring>
#include <filesystem>
#include <memory>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#ifndef IMGUI_VERSION
@@ -41,6 +43,10 @@ namespace ImGui
FileBrowser &operator=(const FileBrowser &copyFrom);
// set the window position (in pixels)
// default is centered
void SetWindowPos(int posx, int posy) noexcept;
// set the window size (in pixels)
// default is (700, 450)
void SetWindowSize(int width, int height) noexcept;
@@ -90,24 +96,37 @@ namespace ImGui
// set currently applied type filter
// default value is 0 (the first type filter)
void SetCurrentTypeFilterIndex(int index);
// when ImGuiFileBrowserFlags_EnterNewFilename is set
// this function will pre-fill the input dialog with a filename.
void SetInputName(std::string_view input);
private:
template <class Functor>
struct ScopeGuard
{
ScopeGuard(Functor&& t) : func(std::move(t)) { }
~ScopeGuard()
{
func();
}
~ScopeGuard() { func(); }
private:
Functor func;
};
struct FileRecord
{
bool isDir = false;
std::filesystem::path name;
std::string showName;
std::filesystem::path extension;
};
static std::string ToLower(const std::string &s);
void UpdateFileRecords();
void SetPwdUncatched(const std::filesystem::path &pwd);
bool IsExtensionMatched(const std::filesystem::path &extension) const;
@@ -125,6 +144,8 @@ namespace ImGui
int width_;
int height_;
int posX_;
int posY_;
ImGuiFileBrowserFlags flags_;
std::string title_;
@@ -134,23 +155,17 @@ namespace ImGui
bool closeFlag_;
bool isOpened_;
bool ok_;
bool posIsSet_;
std::string statusStr_;
std::vector<std::string> typeFilters_;
int typeFilterIndex_;
unsigned int typeFilterIndex_;
bool hasAllFilter_;
std::filesystem::path pwd_;
std::set<std::filesystem::path> selectedFilenames_;
struct FileRecord
{
bool isDir = false;
std::filesystem::path name;
std::string showName;
std::filesystem::path extension;
};
std::vector<FileRecord> fileRecords_;
// IMPROVE: truncate when selectedFilename_.length() > inputNameBuf_.size() - 1
@@ -167,20 +182,20 @@ namespace ImGui
} // namespace ImGui
inline ImGui::FileBrowser::FileBrowser(ImGuiFileBrowserFlags flags)
: width_(700), height_(450), flags_(flags),
openFlag_(false), closeFlag_(false), isOpened_(false), ok_(false),
: width_(700), height_(450), posX_(0), posY_(0), flags_(flags),
openFlag_(false), closeFlag_(false), isOpened_(false), ok_(false), posIsSet_(false),
inputNameBuf_(std::make_unique<std::array<char, INPUT_NAME_BUF_SIZE>>())
{
if(flags_ & ImGuiFileBrowserFlags_CreateNewDir)
newDirNameBuf_ = std::make_unique<
std::array<char, INPUT_NAME_BUF_SIZE>>();
{
newDirNameBuf_ =
std::make_unique<std::array<char, INPUT_NAME_BUF_SIZE>>();
}
inputNameBuf_->front() = '\0';
inputNameBuf_->back() = '\0';
SetTitle("File Browser");
try {
SetPwd(std::filesystem::current_path());
} catch (std::filesystem::filesystem_error&) {}
SetTitle("file browser");
SetPwd(std::filesystem::current_path());
typeFilters_.clear();
typeFilterIndex_ = 0;
@@ -203,6 +218,9 @@ inline ImGui::FileBrowser &ImGui::FileBrowser::operator=(
width_ = copyFrom.width_;
height_ = copyFrom.height_;
posX_ = copyFrom.posX_;
posY_ = copyFrom.posY_;
flags_ = copyFrom.flags_;
SetTitle(copyFrom.title_);
@@ -210,7 +228,8 @@ inline ImGui::FileBrowser &ImGui::FileBrowser::operator=(
closeFlag_ = copyFrom.closeFlag_;
isOpened_ = copyFrom.isOpened_;
ok_ = copyFrom.ok_;
posIsSet_ = copyFrom.posIsSet_;
statusStr_ = "";
typeFilters_ = copyFrom.typeFilters_;
@@ -224,16 +243,28 @@ inline ImGui::FileBrowser &ImGui::FileBrowser::operator=(
*inputNameBuf_ = *copyFrom.inputNameBuf_;
openNewDirLabel_ = copyFrom.openNewDirLabel_;
if(flags_ & ImGuiFileBrowserFlags_CreateNewDir)
{
newDirNameBuf_ = std::make_unique<
std::array<char, INPUT_NAME_BUF_SIZE>>();
std::array<char, INPUT_NAME_BUF_SIZE>>();
*newDirNameBuf_ = *copyFrom.newDirNameBuf_;
}
#ifdef _WIN32
drives_ = copyFrom.drives_;
#endif
return *this;
}
inline void ImGui::FileBrowser::SetWindowPos(int posx, int posy) noexcept
{
posX_ = posx;
posY_ = posy;
posIsSet_ = true;
}
inline void ImGui::FileBrowser::SetWindowSize(int width, int height) noexcept
{
assert(width > 0 && height > 0);
@@ -253,6 +284,7 @@ inline void ImGui::FileBrowser::SetTitle(std::string title)
inline void ImGui::FileBrowser::Open()
{
ClearSelected();
UpdateFileRecords();
statusStr_ = std::string();
openFlag_ = true;
closeFlag_ = false;
@@ -282,18 +314,27 @@ inline void ImGui::FileBrowser::Display()
});
if(openFlag_)
{
OpenPopup(openLabel_.c_str());
}
isOpened_ = false;
// open the popup window
if(openFlag_ && (flags_ & ImGuiFileBrowserFlags_NoModal))
{
if (posIsSet_)
SetNextWindowPos(
ImVec2(static_cast<float>(posX_), static_cast<float>(posY_)));
SetNextWindowSize(
ImVec2(static_cast<float>(width_), static_cast<float>(height_)));
}
else
{
if (posIsSet_)
SetNextWindowPos(
ImVec2(static_cast<float>(posX_), static_cast<float>(posY_)),
ImGuiCond_FirstUseEver);
SetNextWindowSize(
ImVec2(static_cast<float>(width_), static_cast<float>(height_)),
ImGuiCond_FirstUseEver);
@@ -301,7 +342,9 @@ inline void ImGui::FileBrowser::Display()
if(flags_ & ImGuiFileBrowserFlags_NoModal)
{
if(!BeginPopup(openLabel_.c_str()))
{
return;
}
}
else if(!BeginPopupModal(openLabel_.c_str(), nullptr,
flags_ & ImGuiFileBrowserFlags_NoTitleBar ?
@@ -309,6 +352,7 @@ inline void ImGui::FileBrowser::Display()
{
return;
}
isOpened_ = true;
ScopeGuard endPopup([] { EndPopup(); });
@@ -322,13 +366,18 @@ inline void ImGui::FileBrowser::Display()
if(BeginCombo("##select_drive", driveStr))
{
ScopeGuard guard([&] { EndCombo(); });
for(int i = 0; i < 26; ++i)
{
if(!(drives_ & (1 << i)))
{
continue;
}
char driveCh = static_cast<char>('A' + i);
char selectableStr[] = { driveCh, ':', '\0' };
bool selected = currentDrive == driveCh;
if(Selectable(selectableStr, selected) && !selected)
{
char newPwd[] = { driveCh, ':', '\\', '\0' };
@@ -342,7 +391,7 @@ inline void ImGui::FileBrowser::Display()
#endif
int secIdx = 0, newPwdLastSecIdx = -1;
for(auto &sec : pwd_)
for(const auto &sec : pwd_)
{
#ifdef _WIN32
if(secIdx == 1)
@@ -351,12 +400,18 @@ inline void ImGui::FileBrowser::Display()
continue;
}
#endif
PushID(secIdx);
if(secIdx > 0)
{
SameLine();
}
if(SmallButton(u8StrToStr(sec.u8string()).c_str()))
{
newPwdLastSecIdx = secIdx;
}
PopID();
++secIdx;
}
@@ -364,23 +419,52 @@ inline void ImGui::FileBrowser::Display()
{
int i = 0;
std::filesystem::path newPwd;
for(auto &sec : pwd_)
for(const auto &sec : pwd_)
{
if(i++ > newPwdLastSecIdx)
{
break;
}
newPwd /= sec;
}
#ifdef _WIN32
if(newPwdLastSecIdx == 0)
{
newPwd /= "\\";
}
#endif
SetPwd(newPwd);
}
SameLine();
if(SmallButton("*"))
SetPwd(pwd_);
{
UpdateFileRecords();
std::set<std::filesystem::path> newSelectedFilenames;
for(auto &name : selectedFilenames_)
{
auto it = std::find_if(
fileRecords_.begin(), fileRecords_.end(),
[&](const FileRecord &record)
{
return name == record.name;
});
if(it != fileRecords_.end())
{
newSelectedFilenames.insert(name);
}
}
if(inputNameBuf_ && (*inputNameBuf_)[0])
{
newSelectedFilenames.insert(inputNameBuf_->data());
}
}
if(newDirNameBuf_)
{
@@ -402,7 +486,9 @@ inline void ImGui::FileBrowser::Display()
{
ScopeGuard closeNewDirPopup([] { CloseCurrentPopup(); });
if(create_directory(pwd_ / newDirNameBuf_->data()))
SetPwd(pwd_);
{
UpdateFileRecords();
}
else
{
statusStr_ = "failed to create " +
@@ -428,10 +514,14 @@ inline void ImGui::FileBrowser::Display()
for(auto &rsc : fileRecords_)
{
if(!rsc.isDir && !IsExtensionMatched(rsc.extension))
{
continue;
}
if(!rsc.name.empty() && rsc.name.c_str()[0] == '$')
{
continue;
}
bool selected = selectedFilenames_.find(rsc.name)
!= selectedFilenames_.end();
@@ -447,9 +537,13 @@ inline void ImGui::FileBrowser::Display()
if(selected)
{
if(!multiSelect)
{
selectedFilenames_.clear();
}
else
{
selectedFilenames_.erase(rsc.name);
}
(*inputNameBuf_)[0] = '\0';
}
@@ -458,9 +552,15 @@ inline void ImGui::FileBrowser::Display()
if((rsc.isDir && (flags_ & ImGuiFileBrowserFlags_SelectDirectory)) ||
(!rsc.isDir && !(flags_ & ImGuiFileBrowserFlags_SelectDirectory)))
{
if(!multiSelect)
selectedFilenames_.clear();
selectedFilenames_.insert(rsc.name);
if(multiSelect)
{
selectedFilenames_.insert(rsc.name);
}
else
{
selectedFilenames_ = { rsc.name };
}
if(!(flags_ & ImGuiFileBrowserFlags_SelectDirectory))
{
#ifdef _MSC_VER
@@ -478,7 +578,9 @@ inline void ImGui::FileBrowser::Display()
else
{
if(!multiSelect)
{
selectedFilenames_.clear();
}
}
}
@@ -501,7 +603,9 @@ inline void ImGui::FileBrowser::Display()
}
if(setNewPwd)
{
SetPwd(newPwd);
}
if(!(flags_ & ImGuiFileBrowserFlags_SelectDirectory) &&
(flags_ & ImGuiFileBrowserFlags_EnterNewFilename))
@@ -537,12 +641,15 @@ inline void ImGui::FileBrowser::Display()
SameLine();
int escIdx = GetIO().KeyMap[ImGuiKey_Escape];
if(Button("cancel") || closeFlag_ ||
bool shouldExit =
Button("cancel") || closeFlag_ ||
((flags_ & ImGuiFileBrowserFlags_CloseOnEsc) &&
IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) &&
escIdx >= 0 && IsKeyPressed(escIdx)))
IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) &&
IsKeyPressed(ImGuiKey_Escape));
if(shouldExit)
{
CloseCurrentPopup();
}
if(!statusStr_.empty() && !(flags_ & ImGuiFileBrowserFlags_NoStatusBar))
{
@@ -561,9 +668,11 @@ inline void ImGui::FileBrowser::Display()
for(size_t i = 0; i < typeFilters_.size(); ++i)
{
bool selected = static_cast<int>(i) == typeFilterIndex_;
bool selected = i == typeFilterIndex_;
if(Selectable(typeFilters_[i].c_str(), selected) && !selected)
typeFilterIndex_ = static_cast<int>(i);
{
typeFilterIndex_ = static_cast<unsigned int>(i);
}
}
}
PopItemWidth();
@@ -605,7 +714,9 @@ inline std::filesystem::path ImGui::FileBrowser::GetSelected() const
// when ok_ is true, selectedFilenames_ may be empty if SelectDirectory
// is enabled. return pwd in that case.
if(selectedFilenames_.empty())
{
return pwd_;
}
return pwd_ / *selectedFilenames_.begin();
}
@@ -613,11 +724,17 @@ inline std::vector<std::filesystem::path>
ImGui::FileBrowser::GetMultiSelected() const
{
if(selectedFilenames_.empty())
{
return { pwd_ };
}
std::vector<std::filesystem::path> ret;
ret.reserve(selectedFilenames_.size());
for(auto &s : selectedFilenames_)
{
ret.push_back(pwd_ / s);
}
return ret;
}
@@ -641,9 +758,11 @@ inline void ImGui::FileBrowser::SetTypeFilters(
for(auto &rawFilter : _typeFilters)
{
std::string lowerFilter = ToLower(rawFilter);
if(std::find(typeFilters.begin(), typeFilters.end(), lowerFilter) ==
typeFilters.end())
auto it = std::find(typeFilters.begin(), typeFilters.end(), lowerFilter);
if(it == typeFilters.end())
{
typeFilters.push_back(std::move(lowerFilter));
}
}
#else
@@ -667,12 +786,16 @@ inline void ImGui::FileBrowser::SetTypeFilters(
}
if(i > 0)
{
allFiltersName += ",";
}
allFiltersName += typeFilters[i];
}
if(hasAllFilter_)
{
typeFilters_.push_back(std::move(allFiltersName));
}
}
std::copy(
@@ -684,35 +807,60 @@ inline void ImGui::FileBrowser::SetTypeFilters(
inline void ImGui::FileBrowser::SetCurrentTypeFilterIndex(int index)
{
typeFilterIndex_ = index;
typeFilterIndex_ = static_cast<unsigned int>(index);
}
inline void ImGui::FileBrowser::SetInputName(std::string_view input)
{
if(flags_ & ImGuiFileBrowserFlags_EnterNewFilename)
{
if(input.size() >= static_cast<size_t>(INPUT_NAME_BUF_SIZE))
{
// If input doesn't fit trim off characters
input = input.substr(0, INPUT_NAME_BUF_SIZE - 1);
}
std::copy(input.begin(), input.end(), inputNameBuf_->begin());
inputNameBuf_->at(input.size()) = '\0';
selectedFilenames_ = { inputNameBuf_->data() };
}
}
inline std::string ImGui::FileBrowser::ToLower(const std::string &s)
{
std::string ret = s;
for(char &c : ret)
{
c = static_cast<char>(std::tolower(c));
}
return ret;
}
inline void ImGui::FileBrowser::SetPwdUncatched(const std::filesystem::path &pwd)
inline void ImGui::FileBrowser::UpdateFileRecords()
{
fileRecords_ = { FileRecord{ true, "..", "[D] ..", "" } };
for(auto &p : std::filesystem::directory_iterator(pwd))
for(auto &p : std::filesystem::directory_iterator(pwd_))
{
FileRecord rcd;
if(p.is_regular_file())
{
rcd.isDir = false;
}
else if(p.is_directory())
{
rcd.isDir = true;
}
else
{
continue;
}
rcd.name = p.path().filename();
if(rcd.name.empty())
{
continue;
}
rcd.extension = p.path().filename().extension();
@@ -726,8 +874,12 @@ inline void ImGui::FileBrowser::SetPwdUncatched(const std::filesystem::path &pwd
{
return (L.isDir ^ R.isDir) ? L.isDir : (L.name < R.name);
});
}
inline void ImGui::FileBrowser::SetPwdUncatched(const std::filesystem::path &pwd)
{
pwd_ = absolute(pwd);
UpdateFileRecords();
selectedFilenames_.clear();
(*inputNameBuf_)[0] = '\0';
}
@@ -743,11 +895,15 @@ inline bool ImGui::FileBrowser::IsExtensionMatched(
// no type filters
if(typeFilters_.empty())
{
return true;
}
// invalid type filter index
if(static_cast<size_t>(typeFilterIndex_) >= typeFilters_.size())
{
return true;
}
// all type filters
if(hasAllFilter_ && typeFilterIndex_ == 0)
@@ -755,14 +911,18 @@ inline bool ImGui::FileBrowser::IsExtensionMatched(
for(size_t i = 1; i < typeFilters_.size(); ++i)
{
if(extension == typeFilters_[i])
{
return true;
}
}
return false;
}
// universal filter
if(typeFilters_[typeFilterIndex_] == std::string_view(".*"))
{
return true;
}
// regular filter
return extension == typeFilters_[typeFilterIndex_];
@@ -807,11 +967,15 @@ inline std::uint32_t ImGui::FileBrowser::GetDrivesBitMask()
for(int i = 0; i < 26; ++i)
{
if(!(mask & (1 << i)))
{
continue;
}
char rootName[4] = { static_cast<char>('A' + i), ':', '\\', '\0' };
UINT type = GetDriveTypeA(rootName);
if(type == DRIVE_REMOVABLE || type == DRIVE_FIXED)
if(type == DRIVE_REMOVABLE || type == DRIVE_FIXED || type == DRIVE_REMOTE)
{
ret |= (1 << i);
}
}
return ret;
}