command palette: reducing code repetition

This commit is contained in:
YohananDiamond 2023-06-18 23:46:52 -03:00
parent 56874ddc2e
commit 91e7b90a48
3 changed files with 28 additions and 23 deletions

View file

@ -28,11 +28,14 @@
enum CommandPaletteType {
CMDPAL_TYPE_MAIN = 0,
CMDPAL_TYPE_RECENT,
// TODO
// TODO: are there more?
CMDPAL_TYPE_MAX,
};
enum CommandPaletteExtraAction {
CMDPAL_EXTRA_RECENT = 0,
CMDPAL_EXTRA_MAX,
};
@ -63,6 +66,12 @@ static inline bool matchFuzzy(const char* haystack,const char* needle) {
}
void FurnaceGUI::drawPalette() {
auto resetPalette = [](FurnaceGUI* g){
g->paletteFirstFrame=true;
g->paletteQuery="";
g->curPaletteChoice=0;
};
bool accepted=false;
if (paletteFirstFrame)
@ -164,9 +173,7 @@ void FurnaceGUI::drawPalette() {
} else {
switch (i-GUI_ACTION_MAX) {
case CMDPAL_EXTRA_RECENT:
paletteFirstFrame=true;
paletteQuery="";
curPaletteChoice=0;
resetPalette(this);
curPaletteType=CMDPAL_TYPE_RECENT;
break;
default:
@ -177,17 +184,7 @@ void FurnaceGUI::drawPalette() {
break;
case CMDPAL_TYPE_RECENT:
if (modified) {
nextFile=recentFile[i];
showWarning("Unsaved changes! Save changes before opening file?",GUI_WARN_OPEN_DROP);
} else {
String item=recentFile[i];
recentFile.erase(recentFile.begin()+i);
i--;
if (load(item)>0) {
showError(fmt::sprintf("Error while loading file! (%s)",lastError));
}
}
openRecentFile(recentFile[i]);
ImGui::CloseCurrentPopup();
break;

View file

@ -2151,6 +2151,17 @@ int FurnaceGUI::load(String path) {
return 0;
}
void FurnaceGUI::openRecentFile(String path) {
if (modified) {
nextFile=path;
showWarning("Unsaved changes! Save changes before opening file?",GUI_WARN_OPEN_DROP);
} else {
if (load(path)>0) {
showError(fmt::sprintf("Error while loading file! (%s)",lastError));
}
}
}
void FurnaceGUI::pushRecentFile(String path) {
if (path.empty()) return;
if (path.find(backupPath)==0) return;
@ -3802,17 +3813,13 @@ bool FurnaceGUI::loop() {
exitDisabledTimer=1;
for (int i=0; i<(int)recentFile.size(); i++) {
String item=recentFile[i];
if (ImGui::MenuItem(item.c_str())) {
if (modified) {
nextFile=item;
showWarning("Unsaved changes! Save changes before opening file?",GUI_WARN_OPEN_DROP);
} else {
if (ImGui::MenuItem(recentFile[i].c_str())) {
String item=recentFile[i];
if (!modified) {
recentFile.erase(recentFile.begin()+i);
i--;
if (load(item)>0) {
showError(fmt::sprintf("Error while loading file! (%s)",lastError));
}
}
openRecentFile(item);
}
}
if (recentFile.empty()) {

View file

@ -2240,6 +2240,7 @@ class FurnaceGUI {
int save(String path, int dmfVersion);
int load(String path);
int loadStream(String path);
void openRecentFile(String path);
void pushRecentFile(String path);
void exportAudio(String path, DivAudioExportModes mode);
void delFirstBackup(String name);