mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 19:37:34 +00:00
LibGUI+LibGfx: Collapse the '&' from Alt shortcuts in tooltip texts
Also resolve a FIXME about using GUI::Label auto-sizing since we're changing this code and it simplifies what we're doing. Fixes #6219.
This commit is contained in:
parent
f27352dfdc
commit
a4992b5ece
3 changed files with 27 additions and 25 deletions
|
@ -44,12 +44,10 @@ class Application::TooltipWindow final : public Window {
|
||||||
C_OBJECT(TooltipWindow);
|
C_OBJECT(TooltipWindow);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void set_tooltip(String tooltip)
|
void set_tooltip(const String& tooltip)
|
||||||
{
|
{
|
||||||
// FIXME: Add some kind of GUI::Label auto-sizing feature.
|
m_label->set_text(Gfx::parse_ampersand_string(tooltip));
|
||||||
int text_width = m_label->font().width(tooltip);
|
set_rect(rect().x(), rect().y(), m_label->min_width() + 10, m_label->font().glyph_height() + 8);
|
||||||
set_rect(rect().x(), rect().y(), text_width + 10, m_label->font().glyph_height() + 8);
|
|
||||||
m_label->set_text(move(tooltip));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -63,6 +61,7 @@ private:
|
||||||
m_label->set_frame_thickness(1);
|
m_label->set_frame_thickness(1);
|
||||||
m_label->set_frame_shape(Gfx::FrameShape::Container);
|
m_label->set_frame_shape(Gfx::FrameShape::Container);
|
||||||
m_label->set_frame_shadow(Gfx::FrameShadow::Plain);
|
m_label->set_frame_shadow(Gfx::FrameShadow::Plain);
|
||||||
|
m_label->set_autosize(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Label> m_label;
|
RefPtr<Label> m_label;
|
||||||
|
|
|
@ -1806,29 +1806,30 @@ void Painter::blit_tiled(const IntRect& dst_rect, const Gfx::Bitmap& bitmap, con
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String parse_ampersand_string(const StringView& raw_text, Optional<size_t>* underline_offset)
|
||||||
|
{
|
||||||
|
if (raw_text.is_empty())
|
||||||
|
return String::empty();
|
||||||
|
|
||||||
|
StringBuilder builder;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < raw_text.length(); ++i) {
|
||||||
|
if (raw_text[i] == '&') {
|
||||||
|
if (i != (raw_text.length() - 1) && raw_text[i + 1] == '&')
|
||||||
|
builder.append(raw_text[i]);
|
||||||
|
else if (underline_offset && !(*underline_offset).has_value())
|
||||||
|
*underline_offset = i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
builder.append(raw_text[i]);
|
||||||
|
}
|
||||||
|
return builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
void Gfx::Painter::draw_ui_text(const Gfx::IntRect& rect, const StringView& text, const Gfx::Font& font, Gfx::TextAlignment text_alignment, Gfx::Color color)
|
void Gfx::Painter::draw_ui_text(const Gfx::IntRect& rect, const StringView& text, const Gfx::Font& font, Gfx::TextAlignment text_alignment, Gfx::Color color)
|
||||||
{
|
{
|
||||||
auto parse_ampersand_string = [](const StringView& raw_text, Optional<size_t>& underline_offset) -> String {
|
|
||||||
if (raw_text.is_empty())
|
|
||||||
return String::empty();
|
|
||||||
|
|
||||||
StringBuilder builder;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < raw_text.length(); ++i) {
|
|
||||||
if (raw_text[i] == '&') {
|
|
||||||
if (i != (raw_text.length() - 1) && raw_text[i + 1] == '&')
|
|
||||||
builder.append(raw_text[i]);
|
|
||||||
else if (!underline_offset.has_value())
|
|
||||||
underline_offset = i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
builder.append(raw_text[i]);
|
|
||||||
}
|
|
||||||
return builder.to_string();
|
|
||||||
};
|
|
||||||
|
|
||||||
Optional<size_t> underline_offset;
|
Optional<size_t> underline_offset;
|
||||||
auto name_to_draw = parse_ampersand_string(text, underline_offset);
|
auto name_to_draw = parse_ampersand_string(text, &underline_offset);
|
||||||
|
|
||||||
Gfx::IntRect text_rect { 0, 0, font.width(name_to_draw), font.glyph_height() };
|
Gfx::IntRect text_rect { 0, 0, font.width(name_to_draw), font.glyph_height() };
|
||||||
text_rect.align_within(rect, text_alignment);
|
text_rect.align_within(rect, text_alignment);
|
||||||
|
|
|
@ -171,4 +171,6 @@ private:
|
||||||
Painter& m_painter;
|
Painter& m_painter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
String parse_ampersand_string(const StringView&, Optional<size_t>* underline_offset = nullptr);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue