Draw popup confirm/cancel rows with one helper

popup_buttons_draw takes its labels, tooltips and enabled state as a struct. Three hand-written rows use it.
This commit is contained in:
shweet committed 2026-11-22 18:48:58 -05:00
1 parent 8d3ebf2329
commit a9177b13d0
9 files changed
+50 -62

No files matched your search

+4 -3
View File
@@ -529,9 +529,10 @@ static void animations_merge_popup_update(Panel& panel, AnimationsPanel& animati
ImGui::Checkbox(localize.get(LABEL_DELETE_ANIMATIONS_AFTER), &settings.mergeIsDeleteAnimationsAfter);
ImGui::EndChild();
auto result =
popup_buttons_draw(manager, localize.get(LABEL_MERGE),
!animation_merge_indices_get(document, mergeSelection, mergeReference).empty(), LABEL_CLOSE);
auto result = popup_buttons_draw(
manager, {.confirm = LABEL_MERGE,
.cancel = LABEL_CLOSE,
.isConfirmEnabled = !animation_merge_indices_get(document, mergeSelection, mergeReference).empty()});
if (result == PopupButton::CONFIRM)
command_push(
panel, [options = AnimationMergeOptions{.selection = mergeSelection,
+6 -14
View File
@@ -150,11 +150,11 @@ bool ItemProperties::update(Manager& manager, Settings& settings, Document& docu
}
ImGui::EndChild();
auto widgetSize = widget_size_with_row_get(2);
ImGui::BeginDisabled(source == SOURCE_EXISTING && edit.id == -1);
shortcut(manager.chords[SHORTCUT_CONFIRM]);
if (ImGui::Button(localize.get(BASIC_ADD), widgetSize))
auto result = popup_buttons_draw(manager, {.confirm = BASIC_ADD,
.confirmTooltip = TOOLTIP_ADD_ITEM,
.cancelTooltip = TOOLTIP_CANCEL_ADD_ITEM,
.isConfirmEnabled = source != SOURCE_EXISTING || edit.id != -1});
if (result == PopupButton::CONFIRM)
{
auto queuedType = type;
auto queuedDestination = destination;
@@ -185,17 +185,9 @@ bool ItemProperties::update(Manager& manager, Settings& settings, Document& docu
}});
isAdded = true;
close();
}
ImGui::EndDisabled();
tooltip_set(TOOLTIP_ADD_ITEM);
ImGui::SameLine();
shortcut(manager.chords[SHORTCUT_CANCEL]);
if (ImGui::Button(localize.get(BASIC_CANCEL), widgetSize))
if (result != PopupButton::NONE)
close();
tooltip_set(TOOLTIP_CANCEL_ADD_ITEM);
ImGui::EndPopup();
}
+1 -1
View File
@@ -124,7 +124,7 @@ void item_properties_update(Panel& panel, int popupIndex, int rows, SelectionKin
}
ImGui::EndChild();
auto result = popup_buttons_draw(manager, localize.get(edit.id == -1 ? BASIC_ADD : BASIC_CONFIRM));
auto result = popup_buttons_draw(manager, {.confirm = edit.id == -1 ? BASIC_ADD : BASIC_CONFIRM});
if (result == PopupButton::CONFIRM)
{
Item item = {};
+9 -6
View File
@@ -424,7 +424,7 @@ static void content_remove_popup_draw(Panel& panel)
ImGui::BulletText("%s", localize_format(FORMAT_REMOVE_MORE, (int)(lines.size() - REMOVE_LINES_MAX)).c_str());
ImGui::Spacing();
ImGui::TextUnformatted(localize.get(TEXT_REMOVE_CONFIRM));
auto result = popup_buttons_draw(panel.manager, localize.get(BASIC_REMOVE));
auto result = popup_buttons_draw(panel.manager, {.confirm = BASIC_REMOVE});
if (result == PopupButton::CONFIRM)
content_remove_apply(panel);
if (result != PopupButton::NONE)
@@ -521,21 +521,24 @@ void directory_open(Dialog& dialog, Document& document, const std::filesystem::p
dialog.file_explorer_open(target);
}
PopupButton popup_buttons_draw(Manager& manager, const char* confirmLabel, bool isConfirmEnabled,
StringType cancelLabel)
PopupButton popup_buttons_draw(Manager& manager, const PopupButtons& buttons)
{
auto widgetSize = widget_size_with_row_get(2);
auto result = PopupButton::NONE;
ImGui::BeginDisabled(!isConfirmEnabled);
ImGui::BeginDisabled(!buttons.isConfirmEnabled);
shortcut(manager.chords[SHORTCUT_CONFIRM]);
if (ImGui::Button(confirmLabel, widgetSize))
if (ImGui::Button(localize.get(buttons.confirm), widgetSize))
result = PopupButton::CONFIRM;
ImGui::EndDisabled();
if (buttons.confirmTooltip != STRING_UNDEFINED)
tooltip_set(buttons.confirmTooltip);
ImGui::SameLine();
shortcut(manager.chords[SHORTCUT_CANCEL]);
if (ImGui::Button(localize.get(cancelLabel), widgetSize))
if (ImGui::Button(localize.get(buttons.cancel), widgetSize))
result = PopupButton::CANCEL;
if (buttons.cancelTooltip != STRING_UNDEFINED)
tooltip_set(buttons.cancelTooltip);
return result;
}
+11 -1
View File
@@ -111,7 +111,17 @@ void tooltip_name_draw(Resources&, const std::string&);
void tooltip_image_draw(const CardImage&, const std::function<void()>&);
void directory_open(Dialog&, Document&, const std::filesystem::path&);
std::filesystem::path asset_path_get(Document&, const std::filesystem::path&);
PopupButton popup_buttons_draw(Manager&, const char*, bool = true, StringType = BASIC_CANCEL);
// The confirm and cancel buttons that end a popup, side by side, on the confirm and cancel shortcuts.
struct PopupButtons
{
StringType confirm = BASIC_CONFIRM;
StringType cancel = BASIC_CANCEL;
StringType confirmTooltip = STRING_UNDEFINED;
StringType cancelTooltip = STRING_UNDEFINED;
bool isConfirmEnabled = true;
};
PopupButton popup_buttons_draw(Manager&, const PopupButtons&);
void content_remove_action_add(Actions&, Panel&, ElementType, SelectionKind, int = -1);
struct SpritesheetsPanel
+2 -2
View File
@@ -286,7 +286,7 @@ static void region_properties_popup_update(Panel& panel, RegionsPanel& regions)
}
ImGui::EndChild();
auto result = popup_buttons_draw(panel.manager, localize.get(regions.editId == -1 ? BASIC_ADD : BASIC_CONFIRM));
auto result = popup_buttons_draw(panel.manager, {.confirm = regions.editId == -1 ? BASIC_ADD : BASIC_CONFIRM});
if (result == PopupButton::CONFIRM)
{
auto& state = panel.state;
@@ -368,7 +368,7 @@ static void region_export_popup_update(Panel& panel, RegionsPanel& regions)
auto texture = document.texture_get(spritesheetId);
auto isValid = region && texture && texture->is_valid() && !texture->pixels.empty() &&
(settings.isExportRegionMakeSpritesheet || !settings.exportRegionPath.empty());
auto result = popup_buttons_draw(manager, localize.get(BASIC_CONFIRM), isValid);
auto result = popup_buttons_draw(manager, {.confirm = BASIC_CONFIRM, .isConfirmEnabled = isValid});
if (result == PopupButton::CONFIRM)
{
if (!settings.exportRegionPath.empty())
+5 -4
View File
@@ -149,7 +149,8 @@ static void spritesheets_merge_popup_update(Panel& panel, SpritesheetsPanel& spr
}
ImGui::EndChild();
auto result = popup_buttons_draw(manager, localize.get(BASIC_MERGE), spritesheets.mergeIds.size() > 1);
auto result =
popup_buttons_draw(manager, {.confirm = BASIC_MERGE, .isConfirmEnabled = spritesheets.mergeIds.size() > 1});
if (result == PopupButton::CONFIRM)
command_push(panel,
[ids = spritesheets.mergeIds, isAppendRight = settings.mergeSpritesheetsOrigin == APPEND_RIGHT,
@@ -187,8 +188,8 @@ static void spritesheets_pack_popup_update(Panel& panel, SpritesheetsPanel& spri
ImGui::DragInt(localize.get(LABEL_PACK_PADDING), &settings.packPadding, DRAG_SPEED, 0, PADDING_MAX);
ImGui::EndChild();
auto result =
popup_buttons_draw(manager, localize.get(BASIC_PACK), is_spritesheet_regions(document, spritesheets.packId));
auto result = popup_buttons_draw(
manager, {.confirm = BASIC_PACK, .isConfirmEnabled = is_spritesheet_regions(document, spritesheets.packId)});
if (result == PopupButton::CONFIRM)
command_push(panel, [id = spritesheets.packId, padding = settings.packPadding](Document& document) {
spritesheet_textures_edit(
@@ -208,7 +209,7 @@ static void spritesheets_overwrite_popup_update(Panel& panel, SpritesheetsPanel&
return popup.end();
ImGui::TextUnformatted(localize.get(LABEL_OVERWRITE_CONFIRMATION));
auto result = popup_buttons_draw(panel.manager, localize.get(BASIC_YES), true, BASIC_NO);
auto result = popup_buttons_draw(panel.manager, {.confirm = BASIC_YES, .cancel = BASIC_NO});
if (result == PopupButton::CONFIRM)
command_push(panel, [ids = spritesheets.saveIds](Document& document) { spritesheets_save(document, ids); });
if (result != PopupButton::NONE)
+11 -30
View File
@@ -70,7 +70,7 @@ void TimelineContext::popups_update()
input_text_string(localize.get(LABEL_FORMAT), &settings.generateRegionNameFormat);
ImGui::Checkbox(localize.get(LABEL_MAP_FRAMES_TO_REGIONS), &isMakeManyRegionsMapFrames);
auto result = popup_buttons_draw(manager, localize.get(LABEL_MAKE_MANY_REGIONS));
auto result = popup_buttons_draw(manager, {.confirm = LABEL_MAKE_MANY_REGIONS});
if (result == PopupButton::CONFIRM)
edit_push(EDIT_GENERATE_REGIONS_FROM_ANIMATIONS,
[targetFrames = makeManyRegionReferences, format = settings.generateRegionNameFormat,
@@ -101,22 +101,13 @@ void TimelineContext::popups_update()
ImGui::Checkbox(localize.get(LABEL_ROUND_SCALE), &isRoundScale);
tooltip_set(TOOLTIP_ROUND_SCALE);
auto widgetSize = widget_size_with_row_get(2);
shortcut(manager.chords[SHORTCUT_CONFIRM]);
if (ImGui::Button(localize.get(LABEL_BAKE), widgetSize))
{
auto result = popup_buttons_draw(manager, {.confirm = LABEL_BAKE,
.confirmTooltip = TOOLTIP_BAKE_FRAMES_OPTIONS,
.cancelTooltip = TOOLTIP_CANCEL_BAKE_FRAMES});
if (result == PopupButton::CONFIRM)
frames_bake();
if (result != PopupButton::NONE)
bakePopup.close();
}
tooltip_set(TOOLTIP_BAKE_FRAMES_OPTIONS);
ImGui::SameLine();
shortcut(manager.chords[SHORTCUT_CANCEL]);
if (ImGui::Button(localize.get(BASIC_CANCEL), widgetSize))
bakePopup.close();
tooltip_set(TOOLTIP_CANCEL_BAKE_FRAMES);
ImGui::EndPopup();
}
@@ -155,23 +146,13 @@ void TimelineContext::popups_update()
ImGui::Checkbox(localize.get(LABEL_BAKE_USE_ROOT_PIVOT), &isUseRootPivot);
tooltip_set(TOOLTIP_BAKE_USE_ROOT_PIVOT);
auto widgetSize = widget_size_with_row_get(2);
shortcut(manager.chords[SHORTCUT_CONFIRM]);
ImGui::BeginDisabled(!is_bake_into_other_frames_ready());
if (ImGui::Button(localize.get(LABEL_BAKE), widgetSize))
{
auto result = popup_buttons_draw(manager, {.confirm = LABEL_BAKE,
.cancelTooltip = TOOLTIP_CANCEL_BAKE_FRAMES,
.isConfirmEnabled = is_bake_into_other_frames_ready()});
if (result == PopupButton::CONFIRM)
bake_into_other_frames();
if (result != PopupButton::NONE)
bakeIntoOtherFramesPopup.close();
}
ImGui::EndDisabled();
ImGui::SameLine();
shortcut(manager.chords[SHORTCUT_CANCEL]);
if (ImGui::Button(localize.get(BASIC_CANCEL), widgetSize))
bakeIntoOtherFramesPopup.close();
tooltip_set(TOOLTIP_CANCEL_BAKE_FRAMES);
ImGui::EndPopup();
}
+1 -1
View File
@@ -252,7 +252,7 @@ void TimelineContext::group_properties_update()
}
ImGui::EndChild();
auto result = popup_buttons_draw(manager, localize.get(BASIC_CONFIRM));
auto result = popup_buttons_draw(manager, {.confirm = BASIC_CONFIRM});
if (result == PopupButton::CONFIRM)
{
auto targetName = groupName;