Closed svfat closed 4 months ago
Hi, and thanks for the report!
Indeed, it would be very nice to be able to use StyledStrings there! Especially to use bold or underscores to highlight shortcuts.
Right now the menu doesn't support these, mostly because it was written before styled strings were a thing. Shouldn't be too hard to add that!
I can take this on if you could point me in the right direction. I'm not yet familiar with the sources
Sure!
There's two parts to the menubar:
StyledStrings
- yay!draw
implementation: first we call item.label()
which ignores the style, and here we only call printer.print((offset, 0), &format!(" {label} "));
, which is not style-aware.So to fix this last point, I would recommend making 2 changes:
menu::Item::styled_label(&self) -> SpannedStr<Style>
, which returns either the styled string stored from the item, or an unstyled "|"
if it's a delimiter:
// It's a bit cumbersome, but you can create a static slice of spans:
const PLAIN_1CHAR_SPAN: &'static [IndexedSpan<Style>] = &[IndexedSpan {
content:
attr: Style {
effects: EnumSet::EMPTY, // This needs a recent enough `enumset` dependency, we should bump the minimum version to 1.1.0
color: ColorStyle {
front: ColorType::InheritParent,
back: ColorType::InheritParent,
},
},
width: 1,
}];
// ...
// ... and here you can create a `SpannedStr<'static, Style>`:
SpannedStr::new("|", PLAIN_1CHAR_SPAN)
(Note for later: we should really try to make more functions const to help here! Right now we're often using the Into
trait so that limits what we can const-ify, but hopefully it'll get better eventually...)
Menubar::draw
implementation to call this new styled_label()
method to get the label, and use Printer::print_styled
to print it.
format!(" {label} ")
to add spaces around the label. It may be a better idea to instead make 3 print calls: one (regular Printer::print
) for each space, and one (Printer::print_styled
) for the styled label.Wow, thanks for the thorough instructions. The fix seems to work, I will test it and prepare a proper PR soon
Is your feature request related to a problem? Please describe. I'm frustrated because the menubar doesn't render colours from StyledString. Basically, I would like to have ability to highlight keyboard shortcuts (e.g. "F" in "File")
Describe the solution you'd like I would like the menubar to render the colours specified in the StyledString. For example, when passing a StyledString with specific colours, those colours should appear on the menubar.
Describe alternatives you've considered I considered using plain strings instead, but this doesn't allow for the use of colours or styles, which limits the visual customisation options.
Additional context Here is a minimal example demonstrating the issue: