LibGUI: Enable the use of font properties through GML

You can now specify the font, font_size, font_weight
and font_type (fixed_width/normal) through GML
This commit is contained in:
Edgar Araújo 2021-03-22 19:51:06 +00:00 committed by Andreas Kling
parent 243d7d9ecf
commit 532e0090fc
Notes: sideshowbarker 2024-07-18 20:58:56 +09:00
4 changed files with 76 additions and 0 deletions

View file

@ -347,4 +347,18 @@ T* Object::find_descendant_of_type_named(const String& name) requires IsBaseOf<O
{ Gfx::TextAlignment::CenterRight, "CenterRight" }, \
{ Gfx::TextAlignment::TopRight, "TopRight" }, \
{ Gfx::TextAlignment::BottomRight, "BottomRight" })
#define REGISTER_FONT_WEIGHT_PROPERTY(property_name, getter, setter) \
REGISTER_ENUM_PROPERTY( \
property_name, getter, setter, unsigned, \
{ Gfx::FontWeight::Thin, "Thin" }, \
{ Gfx::FontWeight::ExtraLight, "ExtraLight" }, \
{ Gfx::FontWeight::Light, "Light" }, \
{ Gfx::FontWeight::Regular, "Regular" }, \
{ Gfx::FontWeight::Medium, "Medium" }, \
{ Gfx::FontWeight::SemiBold, "SemiBold" }, \
{ Gfx::FontWeight::Bold, "Bold" }, \
{ Gfx::FontWeight::ExtraBold, "ExtraBold" }, \
{ Gfx::FontWeight::Black, "Black" }, \
{ Gfx::FontWeight::ExtraBlack, "ExtraBlack" })
}

View file

@ -110,6 +110,24 @@ Widget::Widget()
REGISTER_INT_PROPERTY("x", x, set_x);
REGISTER_INT_PROPERTY("y", y, set_y);
REGISTER_STRING_PROPERTY("font", m_font->family, set_font_family);
REGISTER_INT_PROPERTY("font_size", m_font->presentation_size, set_font_size);
REGISTER_FONT_WEIGHT_PROPERTY("font_weight", m_font->weight, set_font_weight);
register_property(
"font_type", [this] { return m_font->is_fixed_width() ? "FixedWidth" : "Normal"; },
[this](auto& value) {
if (value.to_string() == "FixedWidth") {
set_font_fixed_width(true);
return true;
}
if (value.to_string() == "Normal") {
set_font_fixed_width(false);
return true;
}
return false;
});
register_property(
"focus_policy", [this]() -> JsonValue {
auto policy = focus_policy();
@ -684,6 +702,29 @@ void Widget::set_font(const Gfx::Font* font)
update();
}
void Widget::set_font_family(const String& family)
{
set_font(Gfx::FontDatabase::the().get(family, m_font->presentation_size(), m_font->weight()));
}
void Widget::set_font_size(unsigned size)
{
set_font(Gfx::FontDatabase::the().get(m_font->family(), size, m_font->weight()));
}
void Widget::set_font_weight(unsigned weight)
{
set_font(Gfx::FontDatabase::the().get(m_font->family(), m_font->presentation_size(), weight));
}
void Widget::set_font_fixed_width(bool fixed_width)
{
if (fixed_width)
set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_fixed_width_font().family(), m_font->presentation_size(), m_font->weight()));
else
set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_font().family(), m_font->presentation_size(), m_font->weight()));
}
void Widget::set_global_cursor_tracking(bool enabled)
{
auto* win = window();

View file

@ -239,9 +239,15 @@ public:
bool fill_with_background_color() const { return m_fill_with_background_color; }
const Gfx::Font& font() const { return *m_font; }
void set_font(const Gfx::Font*);
void set_font(const Gfx::Font& font) { set_font(&font); }
void set_font_family(const String&);
void set_font_size(unsigned);
void set_font_weight(unsigned);
void set_font_fixed_width(bool);
void set_global_cursor_tracking(bool);
bool global_cursor_tracking() const;

View file

@ -34,6 +34,21 @@
namespace Gfx {
namespace FontWeight {
enum {
Thin = 100,
ExtraLight = 200,
Light = 300,
Regular = 400,
Medium = 500,
SemiBold = 600,
Bold = 700,
ExtraBold = 800,
Black = 900,
ExtraBlack = 950
};
}
class FontDatabase {
public:
static FontDatabase& the();