diff options
| author | Walker Thompson <walker.thompson@urz.uni-heidelberg.de> | 2026-04-17 11:09:37 +0000 |
|---|---|---|
| committer | Walker Thompson <walker.thompson@urz.uni-heidelberg.de> | 2026-04-17 11:09:37 +0000 |
| commit | 822877c6d6d019f725e7696855cf1996b2138ab0 (patch) | |
| tree | 7b4949b53761234f818d7e6d3e958acf687512fa | |
| parent | fdda56c95ae249c90b47c1a060d081db66a730da (diff) | |
Add DPI scaling for TOTP text field under GTK+
| -rw-r--r-- | totpgen.h | 2 | ||||
| -rw-r--r-- | wxgui.cpp | 38 |
2 files changed, 37 insertions, 3 deletions
@@ -16,6 +16,7 @@ #include <wx/dir.h> #include <wx/clipbrd.h> #include <wx/regex.h> +#include <wx/font.h> static const std::string base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; @@ -50,6 +51,7 @@ class TOTPGen : public wxFrame { wxBoxSizer *TextSizer; wxStaticText *TOTPField; + void ResizeTextToFit(wxStaticText *text_field); wxString SecretFile; @@ -19,7 +19,7 @@ bool TOTPGenGUI::OnInit() TOTPGen::TOTPGen() : wxFrame(nullptr, wxID_ANY, wxT("TOTP Generator"), wxDefaultPosition, wxSize(320,240), - wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER) + wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX)) { MainWindow = new wxPanel(this, wxID_ANY); MainSizer = new wxBoxSizer(wxVERTICAL); @@ -43,6 +43,7 @@ TOTPGen::TOTPGen() : wxFrame(nullptr, wxID_ANY, wxT("TOTP Generator"), wxFONTWEIGHT_NORMAL); TOTPField->SetFont(LabelFont); TOTPField->SetLabel(" "); + ResizeTextToFit(TOTPField); TextSizer = new wxBoxSizer(wxHORIZONTAL); TextSizer->Add(TOTPField, 2, wxCENTER); @@ -152,7 +153,7 @@ void TOTPGen::UpdateSecret(wxCommandEvent& WXUNUSED(event)) Secret = Secret_New; EncryptSecret(Secret); - if (Locked) { + if (Locked == true) { Locked = false; MainWindow->Connect(CountdownTimer->GetId(), wxEVT_TIMER, @@ -213,4 +214,35 @@ void TOTPGen::CopyText(wxCommandEvent& WXUNUSED(event)) wxTheClipboard->SetData(new wxTextDataObject(ToCopy)); wxTheClipboard->Close(); } -}
\ No newline at end of file +} + +void TOTPGen::ResizeTextToFit(wxStaticText* text_field) +{ + wxString text = text_field->GetLabel(); + wxSize text_size = text_field->GetClientSize(); +#if defined(__WXGTK__) + double GTK_scale = std::stod(getenv("GDK_DPI_SCALE")); + text_size.x /= GTK_scale; + text_size.y /= GTK_scale; +#endif + + wxFont font = text_field->GetFont(); + int font_size = font.GetPointSize(); + + wxClientDC device_ctx(text_field); + device_ctx.SetFont(font); + + int text_width, text_height; + device_ctx.GetTextExtent(text, &text_width, &text_height); + + while (text_width > (text_size.x - 12) + || text_height > (text_size.y - 12)) { + font_size--; + font.SetPointSize(font_size); + device_ctx.SetFont(font); + device_ctx.GetTextExtent(text, &text_width, &text_height); + } + + text_field->SetFont(font); + text_field->Refresh(); +} |
