mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-23 16:10:20 +00:00
ThemeEditor: Add generic RoleModel template class
This is to simplify the code, as Color, Alignment, Flag, Metric and Path RoleModel classes looked exactly the same. Additionally, I've added a try_create() function for error propagation. :^)
This commit is contained in:
parent
73552c1856
commit
8817d3ec58
Notes:
sideshowbarker
2024-07-17 11:43:14 +09:00
Author: https://github.com/krkk Commit: https://github.com/SerenityOS/serenity/commit/8817d3ec58 Pull-request: https://github.com/SerenityOS/serenity/pull/13739
1 changed files with 25 additions and 0 deletions
|
@ -33,6 +33,31 @@
|
|||
#include <LibMain/Main.h>
|
||||
#include <unistd.h>
|
||||
|
||||
template<typename T>
|
||||
class RoleModel final : public GUI::ItemListModel<T> {
|
||||
public:
|
||||
static ErrorOr<NonnullRefPtr<RoleModel>> try_create(Vector<T> const& data)
|
||||
{
|
||||
return adopt_nonnull_ref_or_enomem(new (nothrow) RoleModel<T>(data));
|
||||
}
|
||||
|
||||
virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
|
||||
{
|
||||
if (role == GUI::ModelRole::Display)
|
||||
return Gfx::to_string(this->m_data[index.row()]);
|
||||
if (role == GUI::ModelRole::Custom)
|
||||
return this->m_data[index.row()];
|
||||
|
||||
return GUI::ItemListModel<T>::data(index, role);
|
||||
}
|
||||
|
||||
private:
|
||||
explicit RoleModel(Vector<T> const& data)
|
||||
: GUI::ItemListModel<T>(data)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class ColorRoleModel final : public GUI::ItemListModel<Gfx::ColorRole> {
|
||||
public:
|
||||
explicit ColorRoleModel(Vector<Gfx::ColorRole> const& data)
|
||||
|
|
Loading…
Reference in a new issue