Skip to content

Commit 5cfed45

Browse files
tyzoidAtkinsSJ
authored andcommitted
Inspector+UI: Close inspector with shortcuts
This brings keyboard shortcuts for the inspector up with common convention in FF and Chrome: Ctrl+Shift+C now also opens the inspector, and F12, Ctrl+W, and Ctrl+Shift+I now close the inspector when the inspector window is focused. Resolves LadybirdBrowser#972
1 parent a24718c commit 5cfed45

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

UI/Qt/BrowserWindow.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow
355355

356356
auto* inspector_action = new QAction("Open &Inspector", this);
357357
inspector_action->setIcon(load_icon_from_uri("resource://icons/browser/dom-tree.png"sv));
358-
inspector_action->setShortcuts({ QKeySequence("Ctrl+Shift+I"), QKeySequence("F12") });
358+
inspector_action->setShortcuts({ QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_I),
359+
QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_C),
360+
QKeySequence(Qt::Key_F12) });
359361
inspect_menu->addAction(inspector_action);
360362
QObject::connect(inspector_action, &QAction::triggered, this, [this] {
361363
if (m_current_tab) {
@@ -472,7 +474,6 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow
472474
});
473475

474476
auto* clear_cache_action = new QAction("Clear &Cache", this);
475-
clear_cache_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_C));
476477
clear_cache_action->setIcon(load_icon_from_uri("resource://icons/browser/clear-cache.png"sv));
477478
debug_menu->addAction(clear_cache_action);
478479
QObject::connect(clear_cache_action, &QAction::triggered, this, [this] {

UI/Qt/InspectorWidget.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ InspectorWidget::InspectorWidget(QWidget* tab, WebContentView& content_view)
2525
: QWidget(tab, Qt::Window)
2626
{
2727
m_inspector_view = new WebContentView(this);
28+
m_inspector_view->on_close = [this] {
29+
close();
30+
};
2831

2932
if (is_using_dark_system_theme(*this))
3033
m_inspector_view->update_palette(WebContentView::PaletteMode::Dark);
3134

35+
auto* inspector_close_action = new QAction("Close Inspector", this);
36+
inspector_close_action->setShortcuts({ QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_I), QKeySequence(Qt::CTRL | Qt::Key_W), QKeySequence(Qt::Key_F12) });
37+
addAction(inspector_close_action);
38+
connect(inspector_close_action, &QAction::triggered, [this]() { close(); });
39+
3240
m_inspector_client = make<WebView::InspectorClient>(content_view, *m_inspector_view);
3341

3442
m_edit_node_action = new QAction("&Edit node", this);
@@ -206,6 +214,7 @@ void InspectorWidget::closeEvent(QCloseEvent* event)
206214
{
207215
event->accept();
208216
m_inspector_client->clear_selection();
217+
emit closed();
209218
}
210219

211220
}

UI/Qt/InspectorWidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class InspectorWidget final : public QWidget {
3535
public slots:
3636
void device_pixel_ratio_changed(qreal dpi);
3737

38+
signals:
39+
void closed();
40+
3841
private:
3942
virtual bool event(QEvent*) override;
4043
void closeEvent(QCloseEvent*) override;

UI/Qt/Tab.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,10 +899,23 @@ void Tab::recreate_toolbar_icons()
899899
m_hamburger_button->setIcon(create_tvg_icon_with_theme_colors("hamburger", palette()));
900900
}
901901

902+
void Tab::recreate_inspector()
903+
{
904+
if (m_inspector_widget)
905+
m_inspector_widget->deleteLater();
906+
907+
m_inspector_widget = new InspectorWidget(this, view());
908+
909+
QObject::connect(m_inspector_widget, &InspectorWidget::closed, [this] {
910+
m_inspector_widget->deleteLater();
911+
m_inspector_widget = nullptr;
912+
});
913+
}
914+
902915
void Tab::show_inspector_window(InspectorTarget inspector_target)
903916
{
904917
if (!m_inspector_widget)
905-
m_inspector_widget = new InspectorWidget(this, view());
918+
recreate_inspector();
906919
else
907920
m_inspector_widget->inspect();
908921

UI/Qt/Tab.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public slots:
125125

126126
void close_sub_widgets();
127127

128+
void recreate_inspector();
129+
128130
QBoxLayout* m_layout { nullptr };
129131
QToolBar* m_toolbar { nullptr };
130132
QToolButton* m_hamburger_button { nullptr };

0 commit comments

Comments
 (0)