Skip to content

Commit 8100eb8

Browse files
thinkyheadEvilGremlin
authored andcommitted
πŸ§‘β€πŸ’» MarlinUI menu tweaks
Changes in prep for MarlinFirmware#26339
1 parent 8fd7bef commit 8100eb8

File tree

9 files changed

+249
-149
lines changed

9 files changed

+249
-149
lines changed

β€ŽMarlin/src/lcd/HD44780/marlinui_HD44780.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1165,20 +1165,20 @@ void MarlinUI::draw_status_screen() {
11651165
#endif // ADVANCED_PAUSE_FEATURE
11661166

11671167
// Draw a static item with no left-right margin required. Centered by default.
1168-
void MenuItem_static::draw(const uint8_t row, FSTR_P const fstr, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
1168+
void MenuItem_static::draw(const uint8_t row, FSTR_P const ftpl, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
11691169
lcd_moveto(0, row);
11701170

11711171
int8_t n = LCD_WIDTH;
11721172
const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
1173-
const int8_t plen = fstr ? utf8_strlen(fstr) : 0,
1173+
const int8_t plen = ftpl ? utf8_strlen(ftpl) : 0,
11741174
vlen = vstr ? utf8_strlen(vstr) : 0;
11751175
int8_t pad = (center || full) ? n - plen - vlen : 0;
11761176

11771177
// SS_CENTER: Pad with half of the unused space first
11781178
if (center) for (int8_t lpad = pad / 2; lpad > 0; --lpad) { lcd_put_u8str(F(" ")); n--; }
11791179

11801180
// Draw as much of the label as fits
1181-
if (plen) n -= lcd_put_u8str(fstr, itemIndex, itemStringC, itemStringF, n - vlen);
1181+
if (plen) n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n - vlen);
11821182

11831183
if (vlen && n > 0) {
11841184
// SS_FULL: Pad with enough space to justify the value

β€ŽMarlin/src/lcd/TFTGLCD/marlinui_TFTGLCD.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ void MarlinUI::draw_status_screen() {
969969
#endif
970970

971971
// Draw a static item with no left-right margin required. Centered by default.
972-
void MenuItem_static::draw(const uint8_t row, FSTR_P const fstr, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
972+
void MenuItem_static::draw(const uint8_t row, FSTR_P const ftpl, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
973973
if (!PanelDetected) return;
974974
lcd_moveto(0, row);
975975

@@ -1004,25 +1004,25 @@ void MarlinUI::draw_status_screen() {
10041004
}
10051005

10061006
// Draw a generic menu item with pre_char (if selected) and post_char
1007-
void MenuItemBase::_draw(const bool sel, const uint8_t row, FSTR_P const fstr, const char pre_char, const char post_char) {
1007+
void MenuItemBase::_draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char pre_char, const char post_char) {
10081008
if (!PanelDetected) return;
10091009
lcd_moveto(0, row);
10101010
lcd.write(sel ? pre_char : ' ');
10111011
uint8_t n = LCD_WIDTH - 2;
1012-
n -= lcd_put_u8str(fstr, itemIndex, itemStringC, itemStringF, n);
1012+
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
10131013
for (; n; --n) lcd.write(' ');
10141014
lcd.write(post_char);
10151015
lcd.print_line();
10161016
}
10171017

10181018
// Draw a menu item with a (potentially) editable value
1019-
void MenuEditItemBase::draw(const bool sel, const uint8_t row, FSTR_P const fstr, const char * const inStr, const bool pgm) {
1019+
void MenuEditItemBase::draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char * const inStr, const bool pgm) {
10201020
if (!PanelDetected) return;
10211021
const uint8_t vlen = inStr ? (pgm ? utf8_strlen_P(inStr) : utf8_strlen(inStr)) : 0;
10221022
lcd_moveto(0, row);
10231023
lcd.write(sel ? LCD_STR_ARROW_RIGHT[0] : ' ');
10241024
uint8_t n = LCD_WIDTH - 2 - vlen;
1025-
n -= lcd_put_u8str(fstr, itemIndex, itemStringC, itemStringF, n);
1025+
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
10261026
if (vlen) {
10271027
lcd.write(':');
10281028
for (; n; --n) lcd.write(' ');

β€ŽMarlin/src/lcd/dogm/marlinui_DOGM.cpp

+53-54
Original file line numberDiff line numberDiff line change
@@ -411,63 +411,62 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
411411

412412
// Draw a static line of text in the same idiom as a menu item
413413
void MenuItem_static::draw(const uint8_t row, FSTR_P const ftpl, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
414-
415-
if (mark_as_selected(row, style & SS_INVERT)) {
416-
pixel_len_t n = LCD_PIXEL_WIDTH; // pixel width of string allowed
417-
418-
const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
419-
const int pwide = ftpl ? calculateWidth(ftpl) : 0,
420-
vlen = vstr ? utf8_strlen(vstr) : 0;
421-
int pad = (center || full) ? ((LCD_PIXEL_WIDTH) - pwide - vlen * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH) : 0;
422-
423-
// SS_CENTER: Pad with half of the unused space first
424-
if (center) for (int lpad = pad / 2; lpad > 0; --lpad) n -= lcd_put_u8str(F(" "));
425-
426-
// Draw as much of the label as fits
427-
if (pwide) n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n / (MENU_FONT_WIDTH)) * (MENU_FONT_WIDTH);
428-
429-
if (vlen) {
430-
// SS_FULL: Pad with enough space to justify the value
431-
if (full && !center && n > MENU_FONT_WIDTH) {
432-
// Move the leading colon from the value to the label
433-
if (*vstr == ':') { n -= lcd_put_u8str(F(":")); vstr++; }
434-
// Move spaces to the padding
435-
while (*vstr == ' ') { vstr++; pad++; }
436-
// Pad in-between
437-
for (; pad > 0; --pad) n -= lcd_put_u8str(F(" "));
438-
}
439-
n -= lcd_put_u8str_max(vstr, n);
414+
if (!mark_as_selected(row, style & SS_INVERT)) return;
415+
416+
pixel_len_t n = LCD_PIXEL_WIDTH; // pixel width of string allowed
417+
418+
const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
419+
const int pwide = ftpl ? calculateWidth(ftpl) : 0,
420+
vlen = vstr ? utf8_strlen(vstr) : 0;
421+
int pad = (center || full) ? ((LCD_PIXEL_WIDTH) - pwide - vlen * (MENU_FONT_WIDTH)) / (MENU_FONT_WIDTH) : 0;
422+
423+
// SS_CENTER: Pad with half of the unused space first
424+
if (center) for (int lpad = pad / 2; lpad > 0; --lpad) n -= lcd_put_u8str(F(" "));
425+
426+
// Draw as much of the label as fits
427+
if (pwide) n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n / (MENU_FONT_WIDTH)) * (MENU_FONT_WIDTH);
428+
429+
if (vlen) {
430+
// SS_FULL: Pad with enough space to justify the value
431+
if (full && !center && n > MENU_FONT_WIDTH) {
432+
// Move the leading colon from the value to the label
433+
if (*vstr == ':') { n -= lcd_put_u8str(F(":")); vstr++; }
434+
// Move spaces to the padding
435+
while (*vstr == ' ') { vstr++; pad++; }
436+
// Pad in-between
437+
for (; pad > 0; --pad) n -= lcd_put_u8str(F(" "));
440438
}
441-
while (n > MENU_FONT_WIDTH) n -= lcd_put_u8str(F(" "));
439+
n -= lcd_put_u8str_max(vstr, n);
442440
}
441+
while (n > MENU_FONT_WIDTH) n -= lcd_put_u8str(F(" "));
443442
}
444443

445444
// Draw a generic menu item
446445
void MenuItemBase::_draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char, const char post_char) {
447-
if (mark_as_selected(row, sel)) {
448-
uint8_t n = LCD_WIDTH - 1;
449-
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
450-
for (; n; --n) lcd_put_u8str(F(" "));
451-
lcd_put_lchar(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH), row_y2, post_char);
452-
lcd_put_u8str(F(" "));
453-
}
446+
if (!mark_as_selected(row, sel)) return;
447+
448+
uint8_t n = LCD_WIDTH - 1;
449+
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
450+
for (; n; --n) lcd_put_u8str(F(" "));
451+
lcd_put_lchar(LCD_PIXEL_WIDTH - (MENU_FONT_WIDTH), row_y2, post_char);
452+
lcd_put_u8str(F(" "));
454453
}
455454

456455
// Draw a menu item with an editable value
457456
void MenuEditItemBase::draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char * const inStr, const bool pgm) {
458-
if (mark_as_selected(row, sel)) {
459-
const uint8_t vallen = (pgm ? utf8_strlen_P(inStr) : utf8_strlen(inStr)),
460-
pixelwidth = (pgm ? uxg_GetUtf8StrPixelWidthP(u8g.getU8g(), inStr) : uxg_GetUtf8StrPixelWidth(u8g.getU8g(), inStr));
461-
const u8g_uint_t prop = USE_WIDE_GLYPH ? 2 : 1;
462-
463-
uint8_t n = LCD_WIDTH - 2 - vallen * prop;
464-
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
465-
if (vallen) {
466-
lcd_put_u8str(F(":"));
467-
for (; n; --n) lcd_put_u8str(F(" "));
468-
lcd_moveto(LCD_PIXEL_WIDTH - _MAX((MENU_FONT_WIDTH) * vallen, pixelwidth + 2), row_y2);
469-
if (pgm) lcd_put_u8str_P(inStr); else lcd_put_u8str(inStr);
470-
}
457+
if (!mark_as_selected(row, sel)) return;
458+
459+
const uint8_t vallen = (pgm ? utf8_strlen_P(inStr) : utf8_strlen(inStr)),
460+
pixelwidth = (pgm ? uxg_GetUtf8StrPixelWidthP(u8g.getU8g(), inStr) : uxg_GetUtf8StrPixelWidth(u8g.getU8g(), inStr));
461+
const u8g_uint_t prop = USE_WIDE_GLYPH ? 2 : 1;
462+
463+
uint8_t n = LCD_WIDTH - 2 - vallen * prop;
464+
n -= lcd_put_u8str(ftpl, itemIndex, itemStringC, itemStringF, n);
465+
if (vallen) {
466+
lcd_put_u8str(F(":"));
467+
for (; n; --n) lcd_put_u8str(F(" "));
468+
lcd_moveto(LCD_PIXEL_WIDTH - _MAX((MENU_FONT_WIDTH) * vallen, pixelwidth + 2), row_y2);
469+
if (pgm) lcd_put_u8str_P(inStr); else lcd_put_u8str(inStr);
471470
}
472471
}
473472

@@ -545,13 +544,13 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
545544
#if HAS_MEDIA
546545

547546
void MenuItem_sdbase::draw(const bool sel, const uint8_t row, FSTR_P const, CardReader &theCard, const bool isDir) {
548-
if (mark_as_selected(row, sel)) {
549-
const uint8_t maxlen = LCD_WIDTH - isDir;
550-
if (isDir) lcd_put_lchar(LCD_STR_FOLDER[0]);
551-
const pixel_len_t pixw = maxlen * (MENU_FONT_WIDTH);
552-
pixel_len_t n = pixw - lcd_put_u8str_max(ui.scrolled_filename(theCard, maxlen, row, sel), pixw);
553-
for (; n > MENU_FONT_WIDTH; n -= MENU_FONT_WIDTH) lcd_put_u8str(F(" "));
554-
}
547+
if (!mark_as_selected(row, sel)) return;
548+
549+
const uint8_t maxlen = LCD_WIDTH - isDir;
550+
if (isDir) lcd_put_lchar(LCD_STR_FOLDER[0]);
551+
const pixel_len_t pixw = maxlen * (MENU_FONT_WIDTH);
552+
pixel_len_t n = pixw - lcd_put_u8str_max(ui.scrolled_filename(theCard, maxlen, row, sel), pixw);
553+
for (; n > MENU_FONT_WIDTH; n -= MENU_FONT_WIDTH) lcd_put_u8str(F(" "));
555554
}
556555

557556
#endif // HAS_MEDIA

β€ŽMarlin/src/lcd/e3v2/marlinui/ui_common.cpp

+71-71
Original file line numberDiff line numberDiff line change
@@ -308,91 +308,91 @@ void MarlinUI::draw_status_message(const bool blink) {
308308
void MenuItem_static::draw(const uint8_t row, FSTR_P const ftpl, const uint8_t style/*=SS_DEFAULT*/, const char *vstr/*=nullptr*/) {
309309
// Call mark_as_selected to draw a bigger selection box
310310
// and draw the text without a background
311-
if (mark_as_selected(row, (bool)(style & SS_INVERT), true)) {
312-
ui.set_font(DWIN_FONT_MENU);
313-
dwin_font.solid = false;
314-
dwin_font.fg = COLOR_WHITE;
311+
if (!mark_as_selected(row, (bool)(style & SS_INVERT), true)) return;
315312

316-
dwin_string.set();
313+
ui.set_font(DWIN_FONT_MENU);
314+
dwin_font.solid = false;
315+
dwin_font.fg = COLOR_WHITE;
317316

318-
const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
319-
const int8_t plen = ftpl ? utf8_strlen(ftpl) : 0,
320-
vlen = vstr ? utf8_strlen(vstr) : 0;
321-
int8_t pad = (center || full) ? (LCD_WIDTH) - 1 - plen - vlen : 0;
317+
dwin_string.set();
322318

323-
// SS_CENTER: Pad with half of the unused space first
324-
if (center) for (int8_t lpad = pad / 2; lpad > 0; --lpad) dwin_string.add(' ');
319+
const bool center = bool(style & SS_CENTER), full = bool(style & SS_FULL);
320+
const int8_t plen = ftpl ? utf8_strlen(ftpl) : 0,
321+
vlen = vstr ? utf8_strlen(vstr) : 0;
322+
int8_t pad = (center || full) ? (LCD_WIDTH) - 1 - plen - vlen : 0;
325323

326-
// Append the templated label string
327-
if (plen) {
328-
dwin_string.add(ftpl, itemIndex, itemStringC, itemStringF);
329-
pad -= dwin_string.length - plen;
330-
}
324+
// SS_CENTER: Pad with half of the unused space first
325+
if (center) for (int8_t lpad = pad / 2; lpad > 0; --lpad) dwin_string.add(' ');
331326

332-
// SS_FULL: Pad with enough space to justify the value
333-
if (vlen) {
334-
if (full && !center) {
335-
// Move the leading colon from the value to the label
336-
if (*vstr == ':') { dwin_string.add(':'); vstr++; }
337-
// Move spaces to the padding
338-
while (*vstr == ' ') { vstr++; pad++; }
339-
// Pad in-between
340-
for (; pad > 0; --pad) dwin_string.add(' ');
341-
}
342-
// Append the value
343-
dwin_string.add(vstr);
327+
// Append the templated label string
328+
if (plen) {
329+
dwin_string.add(ftpl, itemIndex, itemStringC, itemStringF);
330+
pad -= dwin_string.length - plen;
331+
}
332+
333+
// SS_FULL: Pad with enough space to justify the value
334+
if (vlen) {
335+
if (full && !center) {
336+
// Move the leading colon from the value to the label
337+
if (*vstr == ':') { dwin_string.add(':'); vstr++; }
338+
// Move spaces to the padding
339+
while (*vstr == ' ') { vstr++; pad++; }
340+
// Pad in-between
341+
for (; pad > 0; --pad) dwin_string.add(' ');
344342
}
343+
// Append the value
344+
dwin_string.add(vstr);
345+
}
345346

346-
// SS_CENTER: Pad the rest of the string
347-
if (center) for (int8_t rpad = pad - (pad / 2); rpad > 0; --rpad) dwin_string.add(' ');
347+
// SS_CENTER: Pad the rest of the string
348+
if (center) for (int8_t rpad = pad - (pad / 2); rpad > 0; --rpad) dwin_string.add(' ');
348349

349-
lcd_moveto(1, row);
350-
lcd_put_dwin_string();
351-
}
350+
lcd_moveto(1, row);
351+
lcd_put_dwin_string();
352352
}
353353

354354
// Draw a generic menu item
355355
void MenuItemBase::_draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char, const char post_char) {
356-
if (mark_as_selected(row, sel)) {
357-
ui.set_font(DWIN_FONT_MENU);
358-
dwin_font.solid = false;
359-
dwin_font.fg = COLOR_WHITE;
356+
if (!mark_as_selected(row, sel)) return;
360357

361-
dwin_string.set(ftpl, itemIndex, itemStringC, itemStringF);
358+
ui.set_font(DWIN_FONT_MENU);
359+
dwin_font.solid = false;
360+
dwin_font.fg = COLOR_WHITE;
362361

363-
pixel_len_t n = LCD_WIDTH - 1 - dwin_string.length;
364-
while (--n > 1) dwin_string.add(' ');
362+
dwin_string.set(ftpl, itemIndex, itemStringC, itemStringF);
365363

366-
dwin_string.add(post_char);
364+
pixel_len_t n = LCD_WIDTH - 1 - dwin_string.length;
365+
while (--n > 1) dwin_string.add(' ');
367366

368-
lcd_moveto(1, row);
369-
lcd_put_dwin_string();
370-
}
367+
dwin_string.add(post_char);
368+
369+
lcd_moveto(1, row);
370+
lcd_put_dwin_string();
371371
}
372372

373373
//
374374
// Draw a menu item with an editable value
375375
//
376376
void MenuEditItemBase::draw(const bool sel, const uint8_t row, FSTR_P const ftpl, const char * const inStr, const bool pgm) {
377-
if (mark_as_selected(row, sel)) {
378-
ui.set_font(DWIN_FONT_MENU);
379-
dwin_font.solid = false;
380-
dwin_font.fg = COLOR_WHITE;
377+
if (!mark_as_selected(row, sel)) return;
381378

382-
const uint8_t vallen = (pgm ? utf8_strlen_P(inStr) : utf8_strlen(S(inStr)));
379+
ui.set_font(DWIN_FONT_MENU);
380+
dwin_font.solid = false;
381+
dwin_font.fg = COLOR_WHITE;
383382

384-
dwin_string.set(ftpl, itemIndex, itemStringC, itemStringF);
385-
if (vallen) dwin_string.add(':');
383+
const uint8_t vallen = (pgm ? utf8_strlen_P(inStr) : utf8_strlen(S(inStr)));
386384

387-
lcd_moveto(1, row);
388-
lcd_put_dwin_string();
385+
dwin_string.set(ftpl, itemIndex, itemStringC, itemStringF);
386+
if (vallen) dwin_string.add(':');
389387

390-
if (vallen) {
391-
dwin_font.fg = COLOR_YELLOW;
392-
dwin_string.set(inStr);
393-
lcd_moveto(LCD_WIDTH - vallen - 1, row);
394-
lcd_put_dwin_string();
395-
}
388+
lcd_moveto(1, row);
389+
lcd_put_dwin_string();
390+
391+
if (vallen) {
392+
dwin_font.fg = COLOR_YELLOW;
393+
dwin_string.set(inStr);
394+
lcd_moveto(LCD_WIDTH - vallen - 1, row);
395+
lcd_put_dwin_string();
396396
}
397397
}
398398

@@ -464,21 +464,21 @@ void MarlinUI::draw_status_message(const bool blink) {
464464
#if HAS_MEDIA
465465

466466
void MenuItem_sdbase::draw(const bool sel, const uint8_t row, FSTR_P const, CardReader &theCard, const bool isDir) {
467-
if (mark_as_selected(row, sel)) {
468-
dwin_string.set();
467+
if (!mark_as_selected(row, sel)) return;
469468

470-
uint8_t maxlen = LCD_WIDTH - 1;
471-
if (isDir) {
472-
dwin_string.add(LCD_STR_FOLDER " ");
473-
maxlen -= 2;
474-
}
469+
dwin_string.set();
475470

476-
dwin_string.add(ui.scrolled_filename(theCard, maxlen, row, sel), maxlen);
477-
uint8_t n = maxlen - dwin_string.length;
478-
while (n > 0) { dwin_string.add(' '); --n; }
479-
lcd_moveto(1, row);
480-
lcd_put_dwin_string();
471+
uint8_t maxlen = LCD_WIDTH - 1;
472+
if (isDir) {
473+
dwin_string.add(LCD_STR_FOLDER " ");
474+
maxlen -= 2;
481475
}
476+
477+
dwin_string.add(ui.scrolled_filename(theCard, maxlen, row, sel), maxlen);
478+
uint8_t n = maxlen - dwin_string.length;
479+
while (n > 0) { dwin_string.add(' '); --n; }
480+
lcd_moveto(1, row);
481+
lcd_put_dwin_string();
482482
}
483483

484484
#endif // HAS_MEDIA

0 commit comments

Comments
Β (0)