Skip to content

Commit

Permalink
refactor: method to automatically align text
Browse files Browse the repository at this point in the history
  • Loading branch information
klonyyy committed Sep 21, 2024
1 parent ea07e03 commit a32b87b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
8 changes: 8 additions & 0 deletions src/Gui/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,3 +1110,11 @@ void Gui::drawCenteredText(std::string&& text)
ImGui::SetCursorPosX((ImGui::GetWindowSize().x - ImGui::CalcTextSize(text.c_str()).x) * 0.5f);
ImGui::Text("%s", text.c_str());
}

void Gui::drawTextAlignedToSize(std::string&& text, size_t alignTo)
{
size_t currentLength = text.length();
size_t spacesToAdd = (currentLength < alignTo) ? (alignTo - currentLength) : 0;
std::string alignedText = text + std::string(spacesToAdd, ' ');
ImGui::Text("%s", alignedText.c_str());
}
1 change: 1 addition & 0 deletions src/Gui/Gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class Gui
std::optional<std::string> showDeletePopup(const char* text, const std::string& name);
std::string intToHexString(uint32_t i);
void drawCenteredText(std::string&& text);
void drawTextAlignedToSize(std::string&& text, size_t alignTo);

bool openWebsite(const char* url);

Expand Down
42 changes: 22 additions & 20 deletions src/Gui/GuiAcqusition.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "Gui.hpp"

static constexpr size_t alignment = 30;

void Gui::acqusitionSettingsViewer()
{
ImGui::Dummy(ImVec2(-1, 5));
drawCenteredText("Project");
ImGui::Separator();

ImGui::Text("*.elf file: ");
drawTextAlignedToSize("*.elf file:", alignment);
ImGui::SameLine();
ImGui::InputText("##", &projectElfPath, 0, NULL, NULL);
ImGui::SameLine();
Expand All @@ -15,15 +17,15 @@ void Gui::acqusitionSettingsViewer()

PlotHandler::Settings settings = plotHandler->getSettings();

ImGui::Text("Refresh vars on *.elf change: ");
drawTextAlignedToSize("Refresh vars on *.elf change:", alignment);
ImGui::SameLine();
ImGui::Checkbox("##refresh", &settings.refreshAddressesOnElfChange);

ImGui::Text("Stop on *.elf change: ");
drawTextAlignedToSize("Stop on *.elf change:", alignment);
ImGui::SameLine();
ImGui::Checkbox("##stop", &settings.stopAcqusitionOnElfChange);

ImGui::Text("Sampling [Hz]: ");
drawTextAlignedToSize("Sampling [Hz]:", alignment);
ImGui::SameLine();
ImGui::InputScalar("##sample", ImGuiDataType_U32, &settings.sampleFrequencyHz, NULL, NULL, "%u");
ImGui::SameLine();
Expand All @@ -32,14 +34,14 @@ void Gui::acqusitionSettingsViewer()

const uint32_t minPoints = 100;
const uint32_t maxPoints = 20000;
ImGui::Text("Max points: ");
drawTextAlignedToSize("Max points:", alignment);
ImGui::SameLine();
ImGui::InputScalar("##maxPoints", ImGuiDataType_U32, &settings.maxPoints, NULL, NULL, "%u");
ImGui::SameLine();
ImGui::HelpMarker("Max points used for a single series after which the oldest points will be overwritten.");
settings.maxPoints = std::clamp(settings.maxPoints, minPoints, maxPoints);

ImGui::Text("Max view points: ");
drawTextAlignedToSize("Max view points:", alignment);
ImGui::SameLine();
ImGui::InputScalar("##maxViewportPoints", ImGuiDataType_U32, &settings.maxViewportPoints, NULL, NULL, "%u");
ImGui::SameLine();
Expand All @@ -66,7 +68,7 @@ void Gui::drawDebugProbes()
ImGui::HelpMarker("Select the debug probe type and the serial number of the probe to unlock the START button.");
ImGui::Separator();

ImGui::Text("Debug probe: ");
drawTextAlignedToSize("Debug probe:", alignment);
ImGui::SameLine();

const char* debugProbes[] = {"STLINK", "JLINK"};
Expand All @@ -90,7 +92,7 @@ void Gui::drawDebugProbes()
}
SNptr = 0;
}
ImGui::Text("Debug probe S/N: ");
drawTextAlignedToSize("Debug probe S/N:", alignment);
ImGui::SameLine();

if (ImGui::Combo("##debugProbeSN", &SNptr, devicesList))
Expand All @@ -113,15 +115,15 @@ void Gui::drawDebugProbes()
shouldListDevices = false;
}

ImGui::Text("SWD speed [kHz]: ");
drawTextAlignedToSize("SWD speed [kHz]:", alignment);
ImGui::SameLine();

if (ImGui::InputScalar("##speed", ImGuiDataType_U32, &probeSettings.speedkHz, NULL, NULL, "%u"))
modified = true;

if (probeSettings.debugProbe == 1)
{
ImGui::Text("Target name: ");
drawTextAlignedToSize("Target name:", alignment);
ImGui::SameLine();

if (ImGui::InputText("##device", &probeSettings.device, 0, NULL, NULL))
Expand All @@ -134,7 +136,7 @@ void Gui::drawDebugProbes()
modified = true;
}

ImGui::Text("Mode: ");
drawTextAlignedToSize("Mode:", alignment);
ImGui::SameLine();

const char* probeModes[] = {"NORMAL", "HSS"};
Expand Down Expand Up @@ -174,13 +176,13 @@ void Gui::drawLoggingSettings(PlotHandlerBase* handler, Settings& settings)
ImGui::Separator();

/* CSV streamer */
ImGui::Text("Log to file: ");
drawTextAlignedToSize("Log to file:", alignment);
ImGui::SameLine();
ImGui::Checkbox("##logging", &settings.shouldLog);

ImGui::BeginDisabled(!settings.shouldLog);

ImGui::Text("Logfile directory: ");
drawTextAlignedToSize("Logfile directory:", alignment);
ImGui::SameLine();
ImGui::InputText("##", &settings.logFilePath, 0, NULL, NULL);
ImGui::SameLine();
Expand All @@ -195,21 +197,21 @@ void Gui::acqusitionSettingsTrace()
{
TracePlotHandler::Settings settings = tracePlotHandler->getSettings();

ImGui::Text("Max points: ");
drawTextAlignedToSize("Max points:", alignment);
ImGui::SameLine();
ImGui::InputScalar("##maxPoints", ImGuiDataType_U32, &settings.maxPoints, NULL, NULL, "%u");
ImGui::SameLine();
ImGui::HelpMarker("Max points used for a single series after which the oldest points will be overwritten.");
settings.maxPoints = std::clamp(settings.maxPoints, static_cast<uint32_t>(100), static_cast<uint32_t>(20000));

ImGui::Text("Viewport width [%%]: ");
drawTextAlignedToSize("Viewport width [%%]:", alignment);
ImGui::SameLine();
ImGui::InputScalar("##maxViewportPoints", ImGuiDataType_U32, &settings.maxViewportPointsPercent, NULL, NULL, "%u");
ImGui::SameLine();
ImGui::HelpMarker("The percentage of trace time visible during collect. Expressed in percent since the sample period is not constant.");
settings.maxViewportPointsPercent = std::clamp(settings.maxViewportPointsPercent, static_cast<uint32_t>(1), static_cast<uint32_t>(100));

ImGui::Text("Timeout [s]: ");
drawTextAlignedToSize("Timeout [s]:", alignment);
ImGui::SameLine();
ImGui::InputScalar("##timeout", ImGuiDataType_U32, &settings.timeout, NULL, NULL, "%u");
ImGui::SameLine();
Expand All @@ -236,7 +238,7 @@ void Gui::drawTraceProbes()
ImGui::HelpMarker("Select the debug probe type and the serial number of the probe to unlock the START button.");
ImGui::Separator();

ImGui::Text("Debug probe: ");
drawTextAlignedToSize("Debug probe:", alignment);
ImGui::SameLine();

const char* debugProbes[] = {"STLINK", "JLINK"};
Expand All @@ -260,7 +262,7 @@ void Gui::drawTraceProbes()
}
SNptr = 0;
}
ImGui::Text("Debug probe S/N: ");
drawTextAlignedToSize("Debug probe S/N:", alignment);
ImGui::SameLine();

if (ImGui::Combo("##debugProbeSN", &SNptr, devicesList))
Expand All @@ -283,15 +285,15 @@ void Gui::drawTraceProbes()
shouldListDevices = false;
}

ImGui::Text("SWD speed [kHz]: ");
drawTextAlignedToSize("SWD speed [kHz]:", alignment);
ImGui::SameLine();

if (ImGui::InputScalar("##speed", ImGuiDataType_U32, &probeSettings.speedkHz, NULL, NULL, "%u"))
modified = true;

if (probeSettings.debugProbe == 1)
{
ImGui::Text("Target name: ");
drawTextAlignedToSize("Target name:", alignment);
ImGui::SameLine();

if (ImGui::InputText("##device", &probeSettings.device, 0, NULL, NULL))
Expand Down

0 comments on commit a32b87b

Please sign in to comment.