From d16cb902b3b51ea3a676ae0f42c9483ce8976493 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Sat, 16 Aug 2014 15:10:11 +0200 Subject: [PATCH] Improved Selector's handling of the folder icon Renamed fontHeight to lineHeight, since it is computed using both the font's line spacing and the height of the folder icon. For the latter, use the actual icon height plus two pixels spacing instead of the hardcoded value of 20 (16 height + 4 pixels spacing). Fixed recently introduced bug where "top" is added to the folder icon y-coordinate twice. Also center the icon vertically when a large font is used. Don't crash if the folder icon is missing. This could only happen on broken installations though. Also don't search for the icon twice: SurfaceCollection::skinRes already calls addSkinRes internally when there is no cached surface for the key. --- src/selector.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/selector.cpp b/src/selector.cpp index 2f37063..09862ba 100644 --- a/src/selector.cpp +++ b/src/selector.cpp @@ -73,22 +73,19 @@ int Selector::exec(int startSelection) { unsigned int top, height; tie(top, height) = gmenu2x->getContentArea(); - int fontHeight = gmenu2x->font->getLineSpacing(); - if (showDirectories) { - fontHeight = constrain(fontHeight, 20, 40); + auto folderIcon = gmenu2x->sc.skinRes("imgs/folder.png"); + + int lineHeight = gmenu2x->font->getLineSpacing(); + if (showDirectories && folderIcon) { + lineHeight = max(lineHeight, folderIcon->height() + 2); } - unsigned int nb_elements = height / fontHeight; + unsigned int nb_elements = max(height / lineHeight, 1u); bg.convertToDisplayFormat(); unsigned int firstElement = 0; unsigned int selected = constrain(startSelection, 0, fl.size() - 1); - auto folderIcon = gmenu2x->sc.skinRes("imgs/folder.png"); - if (!folderIcon) { - folderIcon = gmenu2x->sc.addSkinRes("imgs/folder.png"); - } - bool close = false, result = true; while (!close) { OutputSurface& s = *gmenu2x->s; @@ -110,23 +107,28 @@ int Selector::exec(int startSelection) { } //Selection - unsigned int iY = top + (selected - firstElement) * fontHeight; + int iY = top + (selected - firstElement) * lineHeight; if (selectedskinConfColors[COLOR_SELECTION_BG]); + s.box(1, iY, 309, lineHeight, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); //Files & Dirs s.setClipRect(0, top, 311, height); for (unsigned int i = firstElement; i < fl.size() && i < firstElement + nb_elements; i++) { - iY = top + (i - firstElement) * fontHeight; + iY = top + (i - firstElement) * lineHeight; + x = 4; if (fl.isDirectory(i)) { - folderIcon->blit(s, 4, top + iY); + if (folderIcon) { + folderIcon->blit(s, + x, iY + (lineHeight - folderIcon->height()) / 2); + x += folderIcon->width() + 2; + } gmenu2x->font->write(s, fl[i], - 21, iY + (fontHeight / 2), + x, iY + lineHeight / 2, Font::HAlignLeft, Font::VAlignMiddle); } else { gmenu2x->font->write(s, trimExtension(fl[i]), - 4, iY + (fontHeight / 2), + x, iY + lineHeight / 2, Font::HAlignLeft, Font::VAlignMiddle); } }