1
0
mirror of synced 2024-12-13 16:31:07 +01:00
ImHex/plugins/builtin/include/content/popups/popup_text_input.hpp

73 lines
2.3 KiB
C++
Raw Normal View History

2023-11-10 20:47:08 +01:00
#pragma once
#include <hex/ui/popup.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/localization_manager.hpp>
#include <functional>
#include <string>
#include <fonts/codicons_font.h>
namespace hex::plugin::builtin {
class PopupTextInput : public Popup<PopupTextInput> {
public:
PopupTextInput(UnlocalizedString unlocalizedName, UnlocalizedString message, std::function<void(std::string)> function)
: hex::Popup<PopupTextInput>(std::move(unlocalizedName), false),
m_message(std::move(message)), m_function(std::move(function)) { }
void drawContent() override {
ImGuiExt::TextFormattedWrapped("{}", Lang(this->m_message));
ImGui::NewLine();
ImGui::PushItemWidth(-1);
if (this->m_justOpened) {
ImGui::SetKeyboardFocusHere();
this->m_justOpened = false;
}
ImGuiExt::InputTextIcon("##input", ICON_VS_SYMBOL_KEY, this->m_input);
ImGui::PopItemWidth();
ImGui::NewLine();
ImGui::Separator();
auto width = ImGui::GetWindowWidth();
ImGui::SetCursorPosX(width / 9);
if (ImGui::Button("hex.builtin.common.okay"_lang, ImVec2(width / 3, 0)) || ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Enter))) {
this->m_function(this->m_input);
this->close();
}
ImGui::SameLine();
ImGui::SetCursorPosX(width / 9 * 5);
if (ImGui::Button("hex.builtin.common.cancel"_lang, ImVec2(width / 3, 0)) || ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape))) {
this->close();
}
ImGui::SetWindowPos((ImHexApi::System::getMainWindowSize() - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
}
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
return ImGuiWindowFlags_AlwaysAutoResize;
}
[[nodiscard]] ImVec2 getMinSize() const override {
return scaled({ 400, 100 });
}
[[nodiscard]] ImVec2 getMaxSize() const override {
return scaled({ 600, 300 });
}
private:
std::string m_input;
UnlocalizedString m_message;
std::function<void(std::string)> m_function;
bool m_justOpened = true;
};
}