Application: reorganise profile manager UI code.

This commit is contained in:
Guillaume Jacquemin 2024-03-10 14:03:51 +01:00
parent 387647669f
commit e9fad7600b
Signed by: williamjcm
SSH key fingerprint: SHA256:AYLOg+iTV0ElElnlu4vqM4edFazVdRiuQB0Y5LoKc4A
2 changed files with 114 additions and 112 deletions

View file

@ -113,9 +113,11 @@ class Application: public Platform::Sdl2Application, public efsw::FileWatchListe
void drawInitialisation();
void drawProfileManager();
auto drawBackupListPopup() -> ImGuiID;
auto drawBackupProfilePopup(std::size_t profile_index) -> ImGuiID;
auto drawDeleteProfilePopup(std::size_t profile_index) -> ImGuiID;
void drawBackupListPopup();
void drawBackupRestorePopup(std::size_t backup_index);
void drawBackupDeletePopup(std::size_t backup_index);
void drawBackupProfilePopup(std::size_t profile_index);
void drawDeleteProfilePopup(std::size_t profile_index);
void drawManager();
bool drawIntEditPopup(int* value_to_edit, int max);

View file

@ -40,10 +40,6 @@ Application::drawProfileManager() {
ImGui::EndMenuBar();
}
static ImGuiID backup_list_popup_id = drawBackupListPopup();
static ImGuiID backup_popup_id = drawBackupProfilePopup(profile_index);
static ImGuiID delete_popup_id = drawDeleteProfilePopup(profile_index);
if(ImGui::BeginTable("##ManagerLayout", 2)) {
ImGui::TableSetupColumn("##Label", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("##Refresh", ImGuiTableColumnFlags_WidthFixed);
@ -63,8 +59,9 @@ Application::drawProfileManager() {
ImGui::SameLine();
if(ImGui::SmallButton("Backups")) {
_profileManager->refreshBackups();
ImGui::OpenPopup(backup_list_popup_id);
ImGui::OpenPopup("Backups##BackupsModal");
}
drawBackupListPopup();
ImGui::EndTable();
}
@ -104,15 +101,17 @@ Application::drawProfileManager() {
ImGui::TableSetColumnIndex(2);
if(ImGui::SmallButton(ICON_FA_FILE_ARCHIVE)) {
profile_index = i;
ImGui::OpenPopup(backup_popup_id);
ImGui::OpenPopup("Include builds ?##IncludeBuildsDialog");
}
drawTooltip("Backup");
drawBackupProfilePopup(profile_index);
ImGui::SameLine(0.0f, 2.0f);
if(drawUnsafeWidget(ImGui::SmallButton, ICON_FA_TRASH_ALT)) {
profile_index = i;
ImGui::OpenPopup(delete_popup_id);
ImGui::OpenPopup("Confirmation##DeleteProfileConfirmation");
}
drawTooltip("Delete");
drawDeleteProfilePopup(profile_index);
ImGui::PopID();
}
ImGui::EndTable();
@ -121,108 +120,20 @@ Application::drawProfileManager() {
ImGui::TextUnformatted("Click a profile to manage it.");
}
drawBackupListPopup();
drawBackupProfilePopup(profile_index);
drawDeleteProfilePopup(profile_index);
ImGui::End();
}
ImGuiID
void
Application::drawBackupListPopup() {
ImGui::SetNextWindowPos(ImVec2{Vector2{windowSize() / 2.0f} / dpiScaling()}, ImGuiCond_Always, center_pivot);
if(!ImGui::BeginPopupModal("Backups##BackupsModal", nullptr,
ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoMove))
{
return ImGui::GetID("Backups##BackupsModal");
return;
}
static std::size_t backup_index;
if(ImGui::BeginPopupModal("Restore backup", nullptr,
ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::PushTextWrapPos(float(windowSize().x()) * 0.40f);
ImGui::Text("Are you sure you want to restore the %s backup from %.4i-%.2i-%.2i %.2i:%.2i:%.2i ? Any existing data will be overwritten.",
_profileManager->backups()[backup_index].company.data(),
_profileManager->backups()[backup_index].timestamp.year,
_profileManager->backups()[backup_index].timestamp.month,
_profileManager->backups()[backup_index].timestamp.day,
_profileManager->backups()[backup_index].timestamp.hour,
_profileManager->backups()[backup_index].timestamp.minute,
_profileManager->backups()[backup_index].timestamp.second);
ImGui::PopTextWrapPos();
if(ImGui::BeginTable("##RestoreBackupLayout", 2)) {
ImGui::TableSetupColumn("##Dummy", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("##YesNo", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(1);
if(ImGui::Button("Yes")) {
if(!_profileManager->restoreBackup(backup_index)) {
_queue.addToast(Toast::Type::Error, _profileManager->lastError());
}
if(!_profileManager->refreshProfiles()) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",
_profileManager->lastError().data(), window());
exit(EXIT_FAILURE);
}
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if(ImGui::Button("No", ImGui::GetItemRectSize())) {
ImGui::CloseCurrentPopup();
}
ImGui::EndTable();
}
ImGui::EndPopup();
}
if(ImGui::BeginPopupModal("Delete backup", nullptr,
ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::PushTextWrapPos(float(windowSize().x()) * 0.40f);
ImGui::Text("Are you sure you want to delete the %s backup from %.4i-%.2i-%.2i %.2i:%.2i:%.2i ? This operation is irreversible.",
_profileManager->backups()[backup_index].company.data(),
_profileManager->backups()[backup_index].timestamp.year,
_profileManager->backups()[backup_index].timestamp.month,
_profileManager->backups()[backup_index].timestamp.day,
_profileManager->backups()[backup_index].timestamp.hour,
_profileManager->backups()[backup_index].timestamp.minute,
_profileManager->backups()[backup_index].timestamp.second);
ImGui::PopTextWrapPos();
if(ImGui::BeginTable("##DeleteBackupLayout", 2)) {
ImGui::TableSetupColumn("##Dummy", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("##YesNo", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(1);
if(ImGui::Button("Yes")) {
if(!_profileManager->deleteBackup(backup_index)) {
_queue.addToast(Toast::Type::Error, _profileManager->lastError());
}
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if(ImGui::Button("No", ImGui::GetItemRectSize())) {
ImGui::CloseCurrentPopup();
}
ImGui::EndTable();
}
ImGui::EndPopup();
}
static ImGuiID restore_backup_popup_id = ImGui::GetID("Restore backup");
static ImGuiID delete_backup_popup_id = ImGui::GetID("Delete backup");
if(ImGui::BeginTable("##BackupsLabelLayout", 2)) {
ImGui::TableSetupColumn("##Label", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("##Refresh", ImGuiTableColumnFlags_WidthFixed);
@ -290,15 +201,17 @@ Application::drawBackupListPopup() {
ImGui::PushID(int(i));
if(ImGui::SmallButton(ICON_FA_UNDO)) {
backup_index = i;
ImGui::OpenPopup(restore_backup_popup_id);
ImGui::OpenPopup("Restore backup##RestoreBackupModal");
}
drawTooltip("Restore");
drawBackupRestorePopup(backup_index);
ImGui::SameLine(0.0f, 2.0f);
if(ImGui::SmallButton(ICON_FA_TRASH_ALT)) {
backup_index = i;
ImGui::OpenPopup(delete_backup_popup_id);
ImGui::OpenPopup("Delete backup##DeleteBackupModal");
}
drawTooltip("Delete");
drawBackupDeletePopup(backup_index);
ImGui::PopID();
}
ImGui::EndTable();
@ -323,16 +236,107 @@ Application::drawBackupListPopup() {
}
ImGui::EndPopup();
return 0;
}
ImGuiID
void
Application::drawBackupRestorePopup(std::size_t backup_index) {
if(!ImGui::BeginPopupModal("Restore backup##RestoreBackupModal", nullptr,
ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_AlwaysAutoResize))
{
return;
}
ImGui::PushTextWrapPos(float(windowSize().x()) * 0.50f);
ImGui::Text("Are you sure you want to restore the %s backup from %.4i-%.2i-%.2i %.2i:%.2i:%.2i ?\n\n"
"Any existing data will be overwritten.",
_profileManager->backups()[backup_index].company.data(),
_profileManager->backups()[backup_index].timestamp.year,
_profileManager->backups()[backup_index].timestamp.month,
_profileManager->backups()[backup_index].timestamp.day,
_profileManager->backups()[backup_index].timestamp.hour,
_profileManager->backups()[backup_index].timestamp.minute,
_profileManager->backups()[backup_index].timestamp.second);
ImGui::PopTextWrapPos();
if(ImGui::BeginTable("##RestoreBackupLayout", 2)) {
ImGui::TableSetupColumn("##Dummy", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("##YesNo", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(1);
if(ImGui::Button("Yes")) {
if(!_profileManager->restoreBackup(backup_index)) {
_queue.addToast(Toast::Type::Error, _profileManager->lastError());
}
if(!_profileManager->refreshProfiles()) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",
_profileManager->lastError().data(), window());
exit(EXIT_FAILURE);
}
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if(ImGui::Button("No", ImGui::GetItemRectSize())) {
ImGui::CloseCurrentPopup();
}
ImGui::EndTable();
}
ImGui::EndPopup();
}
void
Application::drawBackupDeletePopup(std::size_t backup_index) {
if(!ImGui::BeginPopupModal("Delete backup##DeleteBackupModal", nullptr,
ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_AlwaysAutoResize))
{
return;
}
ImGui::PushTextWrapPos(float(windowSize().x()) * 0.50f);
ImGui::Text("Are you sure you want to delete the %s backup from %.4i-%.2i-%.2i %.2i:%.2i:%.2i ?\n\n"
"This operation is irreversible.",
_profileManager->backups()[backup_index].company.data(),
_profileManager->backups()[backup_index].timestamp.year,
_profileManager->backups()[backup_index].timestamp.month,
_profileManager->backups()[backup_index].timestamp.day,
_profileManager->backups()[backup_index].timestamp.hour,
_profileManager->backups()[backup_index].timestamp.minute,
_profileManager->backups()[backup_index].timestamp.second);
ImGui::PopTextWrapPos();
if(ImGui::BeginTable("##DeleteBackupLayout", 2)) {
ImGui::TableSetupColumn("##Dummy", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("##YesNo", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(1);
if(ImGui::Button("Yes")) {
if(!_profileManager->deleteBackup(backup_index)) {
_queue.addToast(Toast::Type::Error, _profileManager->lastError());
}
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if(ImGui::Button("No", ImGui::GetItemRectSize())) {
ImGui::CloseCurrentPopup();
}
ImGui::EndTable();
}
ImGui::EndPopup();
}
void
Application::drawBackupProfilePopup(std::size_t profile_index) {
if(!ImGui::BeginPopupModal("Include builds ?##IncludeBuildsDialog", nullptr,
ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoMove))
{
return ImGui::GetID("Include builds ?##IncludeBuildsDialog");
return;
}
ImGui::TextUnformatted("Should builds be added to the backup ?");
@ -368,16 +372,14 @@ Application::drawBackupProfilePopup(std::size_t profile_index) {
}
ImGui::EndPopup();
return 0;
}
ImGuiID
void
Application::drawDeleteProfilePopup(std::size_t profile_index) {
if(!ImGui::BeginPopupModal("Confirmation##DeleteProfileConfirmation", nullptr,
ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoMove))
{
return ImGui::GetID("Confirmation##DeleteProfileConfirmation");
return;
}
static bool delete_builds = false;
@ -415,8 +417,6 @@ Application::drawDeleteProfilePopup(std::size_t profile_index) {
}
ImGui::EndPopup();
return 0;
}
}