From 05985b51f27dd466a58cda82e5b6467f5175705d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 25 Sep 2022 12:11:02 +0200 Subject: [PATCH] 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. --- Userland/Libraries/LibWebView/DOMTreeModel.cpp | 18 ++++++++++++++---- Userland/Libraries/LibWebView/DOMTreeModel.h | 12 +++++++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWebView/DOMTreeModel.cpp b/Userland/Libraries/LibWebView/DOMTreeModel.cpp index c3ff3f016a3..295cfdaa095 100644 --- a/Userland/Libraries/LibWebView/DOMTreeModel.cpp +++ b/Userland/Libraries/LibWebView/DOMTreeModel.cpp @@ -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()); diff --git a/Userland/Libraries/LibWebView/DOMTreeModel.h b/Userland/Libraries/LibWebView/DOMTreeModel.h index 90cfb6ca8fe..bcca61cc9d7 100644 --- a/Userland/Libraries/LibWebView/DOMTreeModel.h +++ b/Userland/Libraries/LibWebView/DOMTreeModel.h @@ -20,7 +20,13 @@ public: static NonnullRefPtr 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 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 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;