Skip to content

Commit 05985b5

Browse files
committed
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.
1 parent 34c232d commit 05985b5

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Userland/Libraries/LibWebView/DOMTreeModel.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
namespace WebView {
1616

17-
DOMTreeModel::DOMTreeModel(JsonObject dom_tree, GUI::TreeView& tree_view)
17+
DOMTreeModel::DOMTreeModel(JsonObject dom_tree, GUI::TreeView* tree_view)
1818
: m_tree_view(tree_view)
1919
, m_dom_tree(move(dom_tree))
2020
{
21+
// FIXME: Get these from the outside somehow instead of hard-coding paths here.
22+
#ifdef __serenity__
2123
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());
2224
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());
2325
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());
26+
#endif
2427

2528
map_dom_nodes_to_parent(nullptr, &m_dom_tree);
2629
}
@@ -119,18 +122,23 @@ GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
119122
auto node_name = node.get("name"sv).as_string();
120123
auto type = node.get("type"sv).as_string_or("unknown"sv);
121124

125+
// FIXME: This FIXME can go away when we fix the one below.
126+
#ifdef __serenity__
122127
if (role == GUI::ModelRole::ForegroundColor) {
123128
// FIXME: Allow models to return a foreground color *role*.
124129
// Then we won't need to have a GUI::TreeView& member anymore.
125130
if (type == "comment"sv)
126-
return m_tree_view.palette().syntax_comment();
131+
return m_tree_view->palette().syntax_comment();
127132
if (type == "pseudo-element"sv)
128-
return m_tree_view.palette().syntax_type();
133+
return m_tree_view->palette().syntax_type();
129134
if (!node.get("visible"sv).to_bool(true))
130-
return m_tree_view.palette().syntax_comment();
135+
return m_tree_view->palette().syntax_comment();
131136
return {};
132137
}
138+
#endif
133139

140+
// FIXME: This FIXME can go away when the icons are provided from the outside (see constructor).
141+
#ifdef __serenity__
134142
if (role == GUI::ModelRole::Icon) {
135143
if (type == "document")
136144
return m_document_icon;
@@ -139,6 +147,8 @@ GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
139147
// FIXME: More node type icons?
140148
return m_text_icon;
141149
}
150+
#endif
151+
142152
if (role == GUI::ModelRole::Display) {
143153
if (type == "text")
144154
return with_whitespace_collapsed(node.get("text"sv).as_string());

Userland/Libraries/LibWebView/DOMTreeModel.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ class DOMTreeModel final : public GUI::Model {
2020
static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree, GUI::TreeView& tree_view)
2121
{
2222
auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
23-
return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), tree_view));
23+
return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), &tree_view));
24+
}
25+
26+
static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree)
27+
{
28+
auto json_or_error = JsonValue::from_string(dom_tree).release_value_but_fixme_should_propagate_errors();
29+
return adopt_ref(*new DOMTreeModel(json_or_error.as_object(), nullptr));
2430
}
2531

2632
virtual ~DOMTreeModel() override;
@@ -34,7 +40,7 @@ class DOMTreeModel final : public GUI::Model {
3440
GUI::ModelIndex index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element) const;
3541

3642
private:
37-
DOMTreeModel(JsonObject, GUI::TreeView&);
43+
DOMTreeModel(JsonObject, GUI::TreeView*);
3844

3945
ALWAYS_INLINE JsonObject const* get_parent(JsonObject const& o) const
4046
{
@@ -52,7 +58,7 @@ class DOMTreeModel final : public GUI::Model {
5258

5359
void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child);
5460

55-
GUI::TreeView& m_tree_view;
61+
GUI::TreeView* m_tree_view { nullptr };
5662
GUI::Icon m_document_icon;
5763
GUI::Icon m_element_icon;
5864
GUI::Icon m_text_icon;

0 commit comments

Comments
 (0)