LibWebView: Make DOMTreeModel usable outside of SerenityOS

Two issues made this class unusable on other platforms:
- Hardcoded /res paths to icons
- It required a GUI::TreeView for palette access

This patch simply patches out those features on non-Serenity systemsf
for now.
This commit is contained in:
Andreas Kling 2022-09-25 12:11:02 +02:00
parent 34c232def7
commit 05985b51f2
Notes: sideshowbarker 2024-07-18 05:37:06 +09:00
2 changed files with 23 additions and 7 deletions

View file

@ -14,13 +14,16 @@
namespace WebView {
DOMTreeModel::DOMTreeModel(JsonObject dom_tree, GUI::TreeView& tree_view)
DOMTreeModel::DOMTreeModel(JsonObject dom_tree, GUI::TreeView* tree_view)
: m_tree_view(tree_view)
, m_dom_tree(move(dom_tree))
{
// FIXME: Get these from the outside somehow instead of hard-coding paths here.
#ifdef __serenity__
m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-html.png"sv).release_value_but_fixme_should_propagate_errors());
m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png"sv).release_value_but_fixme_should_propagate_errors());
m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-unknown.png"sv).release_value_but_fixme_should_propagate_errors());
#endif
map_dom_nodes_to_parent(nullptr, &m_dom_tree);
}
@ -119,18 +122,23 @@ GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
auto node_name = node.get("name"sv).as_string();
auto type = node.get("type"sv).as_string_or("unknown"sv);
// FIXME: This FIXME can go away when we fix the one below.
#ifdef __serenity__
if (role == GUI::ModelRole::ForegroundColor) {
// FIXME: Allow models to return a foreground color *role*.
// Then we won't need to have a GUI::TreeView& member anymore.
if (type == "comment"sv)
return m_tree_view.palette().syntax_comment();
return m_tree_view->palette().syntax_comment();
if (type == "pseudo-element"sv)
return m_tree_view.palette().syntax_type();
return m_tree_view->palette().syntax_type();
if (!node.get("visible"sv).to_bool(true))
return m_tree_view.palette().syntax_comment();
return m_tree_view->palette().syntax_comment();
return {};
}
#endif
// FIXME: This FIXME can go away when the icons are provided from the outside (see constructor).
#ifdef __serenity__
if (role == GUI::ModelRole::Icon) {
if (type == "document")
return m_document_icon;
@ -139,6 +147,8 @@ GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
// FIXME: More node type icons?
return m_text_icon;
}
#endif
if (role == GUI::ModelRole::Display) {
if (type == "text")
return with_whitespace_collapsed(node.get("text"sv).as_string());

View file

@ -20,7 +20,13 @@ public:
static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree, GUI::TreeView& tree_view)
{
auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), tree_view));
return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), &tree_view));
}
static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree)
{
auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), nullptr));
}
virtual ~DOMTreeModel() override;
@ -34,7 +40,7 @@ public:
GUI::ModelIndex index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element) const;
private:
DOMTreeModel(JsonObject, GUI::TreeView&);
DOMTreeModel(JsonObject, GUI::TreeView*);
ALWAYS_INLINE JsonObject const* get_parent(JsonObject const& o) const
{
@ -52,7 +58,7 @@ private:
void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child);
GUI::TreeView& m_tree_view;
GUI::TreeView* m_tree_view { nullptr };
GUI::Icon m_document_icon;
GUI::Icon m_element_icon;
GUI::Icon m_text_icon;