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 { enum CommandPaletteType {
CMDPAL_TYPE_MAIN = 0, CMDPAL_TYPE_MAIN = 0,
CMDPAL_TYPE_RECENT, CMDPAL_TYPE_RECENT,
// TODO // TODO: are there more?
CMDPAL_TYPE_MAX,
}; };
enum CommandPaletteExtraAction { enum CommandPaletteExtraAction {
CMDPAL_EXTRA_RECENT = 0, CMDPAL_EXTRA_RECENT = 0,
CMDPAL_EXTRA_MAX, CMDPAL_EXTRA_MAX,
}; };
@ -63,6 +66,12 @@ static inline bool matchFuzzy(const char* haystack,const char* needle) {
} }
void FurnaceGUI::drawPalette() { void FurnaceGUI::drawPalette() {
auto resetPalette = [](FurnaceGUI* g){
g->paletteFirstFrame=true;
g->paletteQuery="";
g->curPaletteChoice=0;
};
bool accepted=false; bool accepted=false;
if (paletteFirstFrame) if (paletteFirstFrame)
@ -164,9 +173,7 @@ void FurnaceGUI::drawPalette() {
} else { } else {
switch (i-GUI_ACTION_MAX) { switch (i-GUI_ACTION_MAX) {
case CMDPAL_EXTRA_RECENT: case CMDPAL_EXTRA_RECENT:
paletteFirstFrame=true; resetPalette(this);
paletteQuery="";
curPaletteChoice=0;
curPaletteType=CMDPAL_TYPE_RECENT; curPaletteType=CMDPAL_TYPE_RECENT;
break; break;
default: default:
@ -177,17 +184,7 @@ void FurnaceGUI::drawPalette() {
break; break;
case CMDPAL_TYPE_RECENT: case CMDPAL_TYPE_RECENT:
if (modified) { openRecentFile(recentFile[i]);
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));
}
}
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
break; break;

View file

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

View file

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