diff options
author | Andreas Kling <kling@serenityos.org> | 2020-12-31 01:12:16 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-31 01:13:52 +0100 |
commit | 4f1db41a6ce2a4262eab024815072c2b1717bb38 (patch) | |
tree | 91a7b590bfa7d9fb5da4ab1757dac525526c3faa /Libraries/LibGUI | |
parent | 28bad49ed474c686b9caaa639f5fe7304f1aeed0 (diff) | |
download | serenity-4f1db41a6ce2a4262eab024815072c2b1717bb38.zip |
LibGUI: Show font weight names instead of numeric weights in FontPicker
Map font weights to their names from the OpenType specification.
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r-- | Libraries/LibGUI/FontPicker.cpp | 53 | ||||
-rw-r--r-- | Libraries/LibGUI/ItemListModel.h | 4 |
2 files changed, 53 insertions, 4 deletions
diff --git a/Libraries/LibGUI/FontPicker.cpp b/Libraries/LibGUI/FontPicker.cpp index f1a82046e5..74adea59e8 100644 --- a/Libraries/LibGUI/FontPicker.cpp +++ b/Libraries/LibGUI/FontPicker.cpp @@ -37,6 +37,55 @@ namespace GUI { +struct FontWeightNameMapping { + constexpr FontWeightNameMapping(int w, const char* n) + : weight(w) + , name(n) + { + } + int weight { 0 }; + StringView name; +}; + +static constexpr FontWeightNameMapping font_weight_names[] = { + { 100, "Thin" }, + { 200, "Extra Light" }, + { 300, "Light" }, + { 400, "Regular" }, + { 500, "Medium" }, + { 600, "Semi Bold" }, + { 700, "Bold" }, + { 800, "Extra Bold" }, + { 900, "Black" }, + { 950, "Extra Black" }, +}; + +static constexpr StringView weight_to_name(int weight) +{ + for (auto& it : font_weight_names) { + if (it.weight == weight) + return it.name; + } + return {}; +} + +class FontWeightListModel : public ItemListModel<int> { +public: + FontWeightListModel(const Vector<int>& weights) + : ItemListModel(weights) + { + } + + virtual Variant data(const ModelIndex& index, ModelRole role) const override + { + if (role == ModelRole::Custom) + return m_data.at(index.row()); + if (role == ModelRole::Display) + return String(weight_to_name(m_data.at(index.row()))); + return ItemListModel::data(index, role); + } +}; + FontPicker::FontPicker(Window* parent_window, const Gfx::Font* current_font, bool fixed_width_only) : Dialog(parent_window) , m_fixed_width_only(fixed_width_only) @@ -54,7 +103,7 @@ FontPicker::FontPicker(Window* parent_window, const Gfx::Font* current_font, boo m_family_list_view->horizontal_scrollbar().set_visible(false); m_weight_list_view = static_cast<ListView&>(*widget.find_descendant_by_name("weight_list_view")); - m_weight_list_view->set_model(ItemListModel<int>::create(m_weights)); + m_weight_list_view->set_model(adopt(*new FontWeightListModel(m_weights))); m_weight_list_view->horizontal_scrollbar().set_visible(false); m_size_list_view = static_cast<ListView&>(*widget.find_descendant_by_name("size_list_view")); @@ -93,7 +142,7 @@ FontPicker::FontPicker(Window* parent_window, const Gfx::Font* current_font, boo }; m_weight_list_view->on_selection = [this](auto& index) { - m_weight = index.data().to_i32(); + m_weight = index.data(ModelRole::Custom).to_i32(); m_sizes.clear(); Gfx::FontDatabase::the().for_each_font([&](auto& font) { if (m_fixed_width_only && !font.is_fixed_width()) diff --git a/Libraries/LibGUI/ItemListModel.h b/Libraries/LibGUI/ItemListModel.h index 1546dce35d..2290bd82de 100644 --- a/Libraries/LibGUI/ItemListModel.h +++ b/Libraries/LibGUI/ItemListModel.h @@ -33,7 +33,7 @@ namespace GUI { template<typename T> -class ItemListModel final : public Model { +class ItemListModel : public Model { public: static NonnullRefPtr<ItemListModel> create(const Vector<T>& data) { return adopt(*new ItemListModel<T>(data)); } @@ -69,7 +69,7 @@ public: did_update(); } -private: +protected: explicit ItemListModel(const Vector<T>& data) : m_data(data) { |