[studio] Add confirmation for closing file with unsaved changes
This commit is contained in:
@@ -303,6 +303,34 @@ class FilePicker {
|
||||
|
||||
};
|
||||
|
||||
class QuestionPopup {
|
||||
private:
|
||||
enum class Stage {
|
||||
Closed,
|
||||
Opening,
|
||||
Open,
|
||||
};
|
||||
Stage m_stage = Stage::Closed;
|
||||
bool m_open{};
|
||||
ox::String m_title;
|
||||
ox::String m_question;
|
||||
|
||||
public:
|
||||
ox::Signal<ox::Error(ig::PopupResponse)> response;
|
||||
|
||||
QuestionPopup(ox::StringParam title, ox::StringParam question) noexcept;
|
||||
|
||||
void open() noexcept;
|
||||
|
||||
void close() noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
bool isOpen() const noexcept;
|
||||
|
||||
void draw(StudioContext &ctx, ImVec2 const &sz = {}) noexcept;
|
||||
|
||||
};
|
||||
|
||||
[[nodiscard]]
|
||||
bool mainWinHasFocus() noexcept;
|
||||
|
||||
|
||||
@@ -225,6 +225,59 @@ void FilePicker::show() noexcept {
|
||||
m_show = true;
|
||||
}
|
||||
|
||||
|
||||
QuestionPopup::QuestionPopup(ox::StringParam title, ox::StringParam question) noexcept:
|
||||
m_title{std::move(title)},
|
||||
m_question{std::move(question)} {
|
||||
}
|
||||
|
||||
void QuestionPopup::open() noexcept {
|
||||
m_stage = Stage::Opening;
|
||||
}
|
||||
|
||||
void QuestionPopup::close() noexcept {
|
||||
m_stage = Stage::Closed;
|
||||
m_open = false;
|
||||
}
|
||||
|
||||
bool QuestionPopup::isOpen() const noexcept {
|
||||
return m_open;
|
||||
}
|
||||
|
||||
void QuestionPopup::draw(StudioContext &ctx, ImVec2 const &sz) noexcept {
|
||||
switch (m_stage) {
|
||||
case Stage::Closed:
|
||||
break;
|
||||
case Stage::Opening:
|
||||
ImGui::OpenPopup(m_title.c_str());
|
||||
m_stage = Stage::Open;
|
||||
m_open = true;
|
||||
[[fallthrough]];
|
||||
case Stage::Open:
|
||||
centerNextWindow(ctx.tctx);
|
||||
ImGui::SetNextWindowSize(static_cast<ImVec2>(sz));
|
||||
constexpr auto modalFlags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;
|
||||
if (ImGui::BeginPopupModal(m_title.c_str(), &m_open, modalFlags)) {
|
||||
ImGui::Text("%s", m_question.c_str());
|
||||
auto const r = PopupControlsOkCancel(m_open, "Yes", "No");
|
||||
response.emit(r);
|
||||
switch (r) {
|
||||
case PopupResponse::None:
|
||||
break;
|
||||
case PopupResponse::OK:
|
||||
close();
|
||||
break;
|
||||
case PopupResponse::Cancel:
|
||||
close();
|
||||
break;
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool s_mainWinHasFocus{};
|
||||
bool mainWinHasFocus() noexcept {
|
||||
return s_mainWinHasFocus;
|
||||
|
||||
Reference in New Issue
Block a user