From 2f4c5e6737c4b139e2a30564306f93792a6385b5 Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Tue, 17 Jun 2025 01:09:06 +0200 Subject: [PATCH] Codefix: outlined font and use shorter names --- src/hotkeys.cpp | 67 +++++++++++++++++++++----------------- src/widget.cpp | 86 +++++++++++++++++++++++++++++++------------------ 2 files changed, 92 insertions(+), 61 deletions(-) diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 9db098f315..523e2f6fdd 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -27,8 +27,16 @@ static std::vector *_hotkey_lists = nullptr; /** String representation of a keycode */ struct KeycodeNames { - const std::string_view name; ///< Name of the keycode + std::string_view name; ///< Name of the keycode WindowKeyCodes keycode; ///< The keycode + std::string_view short_name; + + KeycodeNames(const std::string_view name, const WindowKeyCodes keycode, const std::string_view short_name = "") + { + this->name = name; + this->keycode = keycode; + this->short_name = short_name.empty() ? name : short_name; + } }; /** Array of non-standard keycodes that can be used in the hotkeys config file. */ @@ -38,16 +46,17 @@ static const std::initializer_list _keycode_to_name = { {"ALT", WKC_ALT}, {"META", WKC_META}, {"GLOBAL", WKC_GLOBAL_HOTKEY}, - {"ESC", WKC_ESC}, - {"BACKSPACE", WKC_BACKSPACE}, - {"INS", WKC_INSERT}, - {"DEL", WKC_DELETE}, - {"PAGEUP", WKC_PAGEUP}, - {"PAGEDOWN", WKC_PAGEDOWN}, - {"END", WKC_END}, - {"HOME", WKC_HOME}, - {"RETURN", WKC_RETURN}, - {"SPACE", WKC_SPACE}, + {"ESC", WKC_ESC, "Esc"}, + {"TAB", WKC_TAB, "Tab"}, + {"BACKSPACE", WKC_BACKSPACE, "BS"}, + {"INS", WKC_INSERT, "Ins"}, + {"DEL", WKC_DELETE, "Del"}, + {"PAGEUP", WKC_PAGEUP, "Page up"}, + {"PAGEDOWN", WKC_PAGEDOWN, "Page down"}, + {"END", WKC_END, "End"}, + {"HOME", WKC_HOME, "Home"}, + {"RETURN", WKC_RETURN, "Enter"}, + {"SPACE", WKC_SPACE, "Space"}, {"F1", WKC_F1}, {"F2", WKC_F2}, {"F3", WKC_F3}, @@ -61,31 +70,31 @@ static const std::initializer_list _keycode_to_name = { {"F11", WKC_F11}, {"F12", WKC_F12}, {"BACKQUOTE", WKC_BACKQUOTE}, - {"PAUSE", WKC_PAUSE}, - {"NUM_DIV", WKC_NUM_DIV}, - {"NUM_MUL", WKC_NUM_MUL}, - {"NUM_MINUS", WKC_NUM_MINUS}, - {"NUM_PLUS", WKC_NUM_PLUS}, - {"NUM_ENTER", WKC_NUM_ENTER}, - {"NUM_DOT", WKC_NUM_DECIMAL}, - {"SLASH", WKC_SLASH}, + {"PAUSE", WKC_PAUSE, "Pause"}, + {"NUM_DIV", WKC_NUM_DIV, "Num /"}, + {"NUM_MUL", WKC_NUM_MUL, "Num *"}, + {"NUM_MINUS", WKC_NUM_MINUS, "Num -"}, + {"NUM_PLUS", WKC_NUM_PLUS, "Num +"}, + {"NUM_ENTER", WKC_NUM_ENTER, "Num Enter"}, + {"NUM_DOT", WKC_NUM_DECIMAL, "Num ."}, + {"SLASH", WKC_SLASH, "/"}, {"/", WKC_SLASH}, /* deprecated, use SLASH */ - {"SEMICOLON", WKC_SEMICOLON}, + {"SEMICOLON", WKC_SEMICOLON, ";"}, {";", WKC_SEMICOLON}, /* deprecated, use SEMICOLON */ - {"EQUALS", WKC_EQUALS}, + {"EQUALS", WKC_EQUALS, "="}, {"=", WKC_EQUALS}, /* deprecated, use EQUALS */ - {"L_BRACKET", WKC_L_BRACKET}, + {"L_BRACKET", WKC_L_BRACKET, "["}, {"[", WKC_L_BRACKET}, /* deprecated, use L_BRACKET */ - {"BACKSLASH", WKC_BACKSLASH}, + {"BACKSLASH", WKC_BACKSLASH, "\\"}, {"\\", WKC_BACKSLASH}, /* deprecated, use BACKSLASH */ - {"R_BRACKET", WKC_R_BRACKET}, + {"R_BRACKET", WKC_R_BRACKET, "]"}, {"]", WKC_R_BRACKET}, /* deprecated, use R_BRACKET */ - {"SINGLEQUOTE", WKC_SINGLEQUOTE}, + {"SINGLEQUOTE", WKC_SINGLEQUOTE, "'"}, {"'", WKC_SINGLEQUOTE}, /* deprecated, use SINGLEQUOTE */ - {"COMMA", WKC_COMMA}, - {"PERIOD", WKC_PERIOD}, + {"COMMA", WKC_COMMA, ","}, + {"PERIOD", WKC_PERIOD, "."}, {".", WKC_PERIOD}, /* deprecated, use PERIOD */ - {"MINUS", WKC_MINUS}, + {"MINUS", WKC_MINUS, "-"}, {"-", WKC_MINUS}, /* deprecated, use MINUS */ }; @@ -221,7 +230,7 @@ std::string KeycodeToShortString(uint16_t keycode) for (const auto &kn : _keycode_to_name) { if (kn.keycode == keycode) { - str += kn.name; + str += kn.short_name; return str; } } diff --git a/src/widget.cpp b/src/widget.cpp index 0b82f623dd..7261c7a8f0 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -3124,50 +3124,72 @@ void NWidgetLeaf::DrawHotkeyHint(const Window* w) { // TODO: Rect is wrong for edit boxes, and the kind of hint we're showing // will look bad anyway. - Rect r = this->GetCurrentRect().Shrink(1); + const uint o = ScaleGUITrad(1); + Rect r = this->GetCurrentRect().Translate(o, 0); + std::string hint; + auto fontsize = FS_NORMAL; + uint16_t keycode = 0; + if (w->window_desc.cls == WC_MAIN_TOOLBAR && this->index == WID_TN_FAST_FORWARD) { // Special-case hint text for Fast-forwards because it's not really a hotkey - DrawStringMultiLine(r, "Tab", TC_WHITE, SA_LEFT | SA_BOTTOM, false, FS_NORMAL); + hint = "Tab"; } else if (w->window_desc.hotkeys != nullptr) { // Widget IDs can coincidentally overlap with hotkey IDs if // they aren't assigned properly. Avoid this. auto hk = w->window_desc.hotkeys->GetHotkeyByNum(this->index); - if (hk != nullptr) { - if (hk->keycodes.size()) { - // Find the "best" of the available keycodes - auto keycode = *(hk->keycodes.begin()); - for (auto k : hk->keycodes) { - if (!(keycode & WKC_GLOBAL_HOTKEY) && (k & WKC_GLOBAL_HOTKEY)) { - keycode = k; - } else if ('A' <= (k & ~WKC_SPECIAL_KEYS) && (k & ~WKC_SPECIAL_KEYS) <= 'Z') { + if (hk != nullptr && hk->keycodes.size()) { + // Find the "best" of the available keycodes + keycode = *(hk->keycodes.begin()); + hint = KeycodeToShortString(keycode); + for (auto k : hk->keycodes) { + if (!(keycode & WKC_GLOBAL_HOTKEY) && (k & WKC_GLOBAL_HOTKEY)) { + keycode = k; + } else { + auto h = KeycodeToShortString(k); + if (hint.length() > h.length()) { + Debug(misc, 1, "shorter hint: {}; old hint {}", h, hint); keycode = k; + hint = h; } } - - // Convert to a string - // TODO: Glyphs for Shift, Alt, etc. Colour for global. - // - On screen keyboard has a sprite for shift. - auto s = KeycodeToShortString(keycode); - - // Choose the font-size - auto availableSize = Dimension(r.right - r.left, r.bottom - r.top); - auto desiredSize = GetStringBoundingBox(s, FS_NORMAL); - auto fontsize = FS_NORMAL; - if (availableSize < desiredSize) { - fontsize = FS_SMALL; - } - - // Display the hints! - // TODO: not as readable as my mockup :( - // - Outline or a bolder font could help. - auto colour = TC_WHITE; - if (keycode & WKC_GLOBAL_HOTKEY) { - colour = TC_YELLOW; - } - DrawStringMultiLine(r, s, colour, SA_LEFT | SA_BOTTOM, false, fontsize); } + + // Convert to a string + // TODO: Glyphs for Shift, Alt, etc. Colour for global. + // - On screen keyboard has a sprite for shift. + hint = KeycodeToShortString(keycode); } } + + if (hint.empty()) { + return; + } + + // Choose the font-size + auto availableSize = Dimension(r.right - r.left, r.bottom - r.top); + auto desiredSize = GetStringBoundingBox(hint, FS_NORMAL); + if (availableSize < desiredSize) { + fontsize = FS_SMALL; + } + + // Display the hints! + // TODO: not as readable as my mockup :( + auto colour = TC_WHITE; + if (keycode & WKC_GLOBAL_HOTKEY) { + colour = TC_LIGHT_BLUE; + } + // Draw a slightly shoddy outline + DrawStringMultiLine(r.Translate( o, o), hint, TC_BLACK | TC_NO_SHADE, SA_LEFT | SA_BOTTOM, false, fontsize); + DrawStringMultiLine(r.Translate( o, -o), hint, TC_BLACK | TC_NO_SHADE, SA_LEFT | SA_BOTTOM, false, fontsize); + DrawStringMultiLine(r.Translate(-o, o), hint, TC_BLACK | TC_NO_SHADE, SA_LEFT | SA_BOTTOM, false, fontsize); + DrawStringMultiLine(r.Translate(-o, -o), hint, TC_BLACK | TC_NO_SHADE, SA_LEFT | SA_BOTTOM, false, fontsize); + DrawStringMultiLine(r.Translate( o, 0), hint, TC_BLACK | TC_NO_SHADE, SA_LEFT | SA_BOTTOM, false, fontsize); + DrawStringMultiLine(r.Translate(-o, 0), hint, TC_BLACK | TC_NO_SHADE, SA_LEFT | SA_BOTTOM, false, fontsize); + DrawStringMultiLine(r.Translate( 0, o), hint, TC_BLACK | TC_NO_SHADE, SA_LEFT | SA_BOTTOM, false, fontsize); + DrawStringMultiLine(r.Translate( 0, -o), hint, TC_BLACK | TC_NO_SHADE, SA_LEFT | SA_BOTTOM, false, fontsize); + + // Draw the hint text. + DrawStringMultiLine(r, hint, colour | TC_NO_SHADE, SA_LEFT | SA_BOTTOM, false, fontsize); } /**