improved formatting and positioning of cursor labels

This commit is contained in:
2026-03-25 21:32:29 +01:00
parent c9b6b8d83d
commit a1b769336e
3 changed files with 38 additions and 55 deletions

View File

@@ -478,48 +478,21 @@ void Application::render() {
double binCenterFreq = fMin + (static_cast<double>(cursors_.hover.bin) + 0.5)
/ bins * (fMax - fMin);
// Fixed-width frequency string to prevent jumping
char freqBuf[48];
if (std::abs(binCenterFreq) >= 1e6)
std::snprintf(freqBuf, sizeof(freqBuf), "%12.6f MHz", binCenterFreq / 1e6);
else if (std::abs(binCenterFreq) >= 1e3)
std::snprintf(freqBuf, sizeof(freqBuf), "%9.3f kHz", binCenterFreq / 1e3);
else
std::snprintf(freqBuf, sizeof(freqBuf), "%7.1f Hz", binCenterFreq);
char valBuf[48];
char hoverBuf[128];
if (hoverPanel_ == HoverPanel::Spectrum) {
std::snprintf(valBuf, sizeof(valBuf), "%7.1f dB", cursors_.hover.dB);
fmtFreqDB(hoverBuf, sizeof(hoverBuf), "", binCenterFreq, cursors_.hover.dB);
} else if (hoverPanel_ == HoverPanel::Waterfall) {
if (hoverWfTimeOffset_ >= 10.0f)
std::snprintf(valBuf, sizeof(valBuf), "%7.1f s", -hoverWfTimeOffset_);
else if (hoverWfTimeOffset_ >= 1.0f)
std::snprintf(valBuf, sizeof(valBuf), "%7.2f s", -hoverWfTimeOffset_);
else
std::snprintf(valBuf, sizeof(valBuf), "%5.0f ms", -hoverWfTimeOffset_ * 1000.0f);
fmtFreqTime(hoverBuf, sizeof(hoverBuf), "", binCenterFreq, -hoverWfTimeOffset_);
} else {
valBuf[0] = '\0';
fmtFreq(hoverBuf, sizeof(hoverBuf), binCenterFreq);
}
// Draw as a single right-aligned line, fixed-width value column
// Right-align the text
ImU32 hoverTextCol = IM_COL32(100, 230, 130, 240);
float rightEdge = specPosX_ + specSizeX_ - 8;
float hy2 = specPosY_ + 4;
// Use a fixed-width reference for the value column to prevent jumping
ImVec2 refSz = ImGui::CalcTextSize("-000.0 dB");
ImVec2 freqSz = ImGui::CalcTextSize(freqBuf);
float sepW = ImGui::CalcTextSize(" ").x;
float valColX = rightEdge - refSz.x;
float freqX = valColX - sepW - freqSz.x;
if (valBuf[0]) {
ImVec2 valSz = ImGui::CalcTextSize(valBuf);
dlp->AddText({freqX, hy2}, hoverTextCol, freqBuf);
dlp->AddText({rightEdge - valSz.x, hy2}, hoverTextCol, valBuf);
} else {
dlp->AddText({rightEdge - freqSz.x, hy2}, hoverTextCol, freqBuf);
}
ImVec2 hSz = ImGui::CalcTextSize(hoverBuf);
dlp->AddText({rightEdge - hSz.x, hy2}, hoverTextCol, hoverBuf);
}
}
}

View File

@@ -97,7 +97,8 @@ void Cursors::draw(const SpectrumDisplay& specDisplay,
formatLabel(buf, sizeof(buf), label, c.freq, dispDB);
ImVec2 sz = ImGui::CalcTextSize(buf);
float lineH = ImGui::GetTextLineHeight();
float ty = posY + 4 + row * (lineH + 4);
// draw starting from the second line -- on the first line, we have cursor data
float ty = posY + 4 + (row + 1) * (lineH + 4);
// Place right of cursor line; flip left if it would overflow.
float tx;
@@ -118,25 +119,15 @@ void Cursors::draw(const SpectrumDisplay& specDisplay,
if (showDelta && cursorA.active && cursorB.active) {
double dFreq = cursorB.freq - cursorA.freq;
float dDB = bDB - aDB;
char val1[48], val2[48];
fmtFreq(val1, sizeof(val1), dFreq);
std::snprintf(val2, sizeof(val2), "%.1f dB", dDB);
char deltaBuf[128];
fmtFreqDB(deltaBuf, sizeof(deltaBuf), "D", dFreq, dDB);
ImVec2 labelSz = ImGui::CalcTextSize("dF = ");
ImVec2 v1Sz = ImGui::CalcTextSize(val1);
ImVec2 v2Sz = ImGui::CalcTextSize(val2);
float valW = std::max(v1Sz.x, v2Sz.x);
float lineH = labelSz.y;
float totalW = labelSz.x + valW;
float tx = posX + sizeX - totalW - 158;
ImVec2 dSz = ImGui::CalcTextSize(deltaBuf);
float tx = posX + sizeX - dSz.x - 178;
float lineH = ImGui::GetTextLineHeight();
float ty = posY + 4;
ImU32 col = IM_COL32(255, 200, 100, 255);
float eqX = tx + labelSz.x; // values start here (right of '= ')
dl->AddText({tx, ty}, col, "dF =");
dl->AddText({eqX + valW - v1Sz.x, ty}, col, val1);
dl->AddText({tx, ty + lineH + 2}, col, "dA =");
dl->AddText({eqX + valW - v2Sz.x, ty + lineH + 2}, col, val2);
dl->AddText({tx, ty}, col, deltaBuf);
}
// (Hover cursor line is drawn cross-panel by Application.)