Преглед на файлове

Browser: Reset the DOM Inspector's state when re-opening it

This resets the DOM Inspector to a default state when re-opening it,
instead of it displaying the previous selection and properties, which
may be outdated. This is closer to how Chrome and Firefox behave.
Eventually, it probably makes sense to copy their behavior of selecting
the `<body>` element by default.
Sam Atkins преди 3 години
родител
ревизия
73c95bcd5f
променени са 3 файла, в които са добавени 28 реда и са изтрити 4 реда
  1. 23 4
      Userland/Applications/Browser/InspectorWidget.cpp
  2. 2 0
      Userland/Applications/Browser/InspectorWidget.h
  3. 3 0
      Userland/Applications/Browser/Tab.cpp

+ 23 - 4
Userland/Applications/Browser/InspectorWidget.cpp

@@ -52,10 +52,7 @@ void InspectorWidget::set_inspected_node(GUI::ModelIndex const index)
         auto inspected_node_properties = maybe_inspected_node_properties.value();
         load_style_json(inspected_node_properties.specified_values_json, inspected_node_properties.computed_values_json);
     } else {
-        m_inspected_node_specified_values_json.clear();
-        m_inspected_node_computed_values_json.clear();
-        m_style_table_view->set_model(nullptr);
-        m_computed_style_table_view->set_model(nullptr);
+        clear_style_json();
     }
 }
 
@@ -88,6 +85,18 @@ InspectorWidget::~InspectorWidget()
 {
 }
 
+void InspectorWidget::select_default_node()
+{
+    clear_style_json();
+
+    // FIXME: Select the <body> element, or else the root node.
+    m_dom_tree_view->collapse_tree();
+    m_dom_tree_view->set_cursor({}, GUI::AbstractView::SelectionUpdate::ClearIfNotSelected);
+
+    m_layout_tree_view->collapse_tree();
+    m_layout_tree_view->set_cursor({}, GUI::AbstractView::SelectionUpdate::ClearIfNotSelected);
+}
+
 void InspectorWidget::set_dom_json(String json)
 {
     if (m_dom_json.has_value() && m_dom_json.value() == json)
@@ -103,6 +112,8 @@ void InspectorWidget::set_dom_json(String json)
         i32 node_id = m_pending_inspect_node_id.value();
         m_pending_inspect_node_id.clear();
         set_inspected_node(node_id);
+    } else {
+        select_default_node();
     }
 }
 
@@ -124,4 +135,12 @@ void InspectorWidget::load_style_json(String specified_values_json, String compu
     m_computed_style_table_view->set_model(Web::StylePropertiesModel::create(m_inspected_node_computed_values_json.value().view()));
 }
 
+void InspectorWidget::clear_style_json()
+{
+    m_inspected_node_specified_values_json.clear();
+    m_inspected_node_computed_values_json.clear();
+    m_style_table_view->set_model(nullptr);
+    m_computed_style_table_view->set_model(nullptr);
+}
+
 }

+ 2 - 0
Userland/Applications/Browser/InspectorWidget.h

@@ -22,12 +22,14 @@ public:
     void set_dom_node_properties_json(i32 node_id, String specified_values_json, String computed_values_json);
 
     void set_inspected_node(i32 node_id);
+    void select_default_node();
 
 private:
     InspectorWidget();
 
     void set_inspected_node(GUI::ModelIndex);
     void load_style_json(String specified_values_json, String computed_values_json);
+    void clear_style_json();
 
     RefPtr<Web::OutOfProcessWebView> m_web_view;
 

+ 3 - 0
Userland/Applications/Browser/Tab.cpp

@@ -485,6 +485,9 @@ void Tab::show_inspector_window(Browser::Tab::InspectorTarget inspector_target)
         Optional<i32> hovered_node = m_web_content_view->get_hovered_node_id();
         VERIFY(hovered_node.has_value());
         m_dom_inspector_widget->set_inspected_node(hovered_node.value());
+    } else {
+        VERIFY(inspector_target == InspectorTarget::Document);
+        m_dom_inspector_widget->select_default_node();
     }
 
     auto* window = m_dom_inspector_widget->window();