Skip to content

Commit

Permalink
Inspector+UI: Close inspector on inspector.js' window.close()
Browse files Browse the repository at this point in the history
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 and Ctrl+Shift+I now close the inspector when the inspector
window is focused.

Resolves #972
  • Loading branch information
tyzoid committed Dec 4, 2024
1 parent 7e78d7d commit 3d0565b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Base/res/ladybird/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,8 @@ document.addEventListener("DOMContentLoaded", () => {
const LEFT_ARROW_KEYCODE = 37;
const RETURN_KEYCODE = 13;
const SPACE_KEYCODE = 32;
const F12_KEYCODE = 123;
const LETTER_I_KEYCODE = 73;

const move = delta => {
let selectedIndex = visibleDOMNodes.indexOf(selectedDOMNode);
Expand All @@ -785,6 +787,16 @@ document.addEventListener("DOMContentLoaded", () => {
}
};

let isF12Close = event.keyCode == F12_KEYCODE;
let isCtrlShiftIClose =
event.keyCode == LETTER_I_KEYCODE
&& event.composed
&& event.ctrlKey
&& event.shiftKey;

if (isF12Close || isCtrlShiftIClose)
window.close();

if (document.activeElement.tagName !== "INPUT") {
const isSummary = selectedDOMNode.parentNode.tagName === "SUMMARY";
const isDiv = selectedDOMNode.parentNode.tagName === "DIV";
Expand Down
2 changes: 1 addition & 1 deletion UI/Qt/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow

auto* inspector_action = new QAction("Open &Inspector", this);
inspector_action->setIcon(load_icon_from_uri("resource://icons/browser/dom-tree.png"sv));
inspector_action->setShortcuts({ QKeySequence("Ctrl+Shift+I"), QKeySequence("F12") });
inspector_action->setShortcuts({ QKeySequence("Ctrl+Shift+I"), QKeySequence("Ctrl+Shift+C"), QKeySequence("F12") });
inspect_menu->addAction(inspector_action);
QObject::connect(inspector_action, &QAction::triggered, this, [this] {
if (m_current_tab) {
Expand Down
4 changes: 4 additions & 0 deletions UI/Qt/InspectorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ InspectorWidget::InspectorWidget(QWidget* tab, WebContentView& content_view)
: QWidget(tab, Qt::Window)
{
m_inspector_view = new WebContentView(this);
m_inspector_view->on_close = [this] {
close();
emit closed();
};

if (is_using_dark_system_theme(*this))
m_inspector_view->update_palette(WebContentView::PaletteMode::Dark);
Expand Down
3 changes: 3 additions & 0 deletions UI/Qt/InspectorWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class InspectorWidget final : public QWidget {
public slots:
void device_pixel_ratio_changed(qreal dpi);

signals:
void closed();

private:
virtual bool event(QEvent*) override;
void closeEvent(QCloseEvent*) override;
Expand Down
15 changes: 14 additions & 1 deletion UI/Qt/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,10 +889,23 @@ void Tab::recreate_toolbar_icons()
m_hamburger_button->setIcon(create_tvg_icon_with_theme_colors("hamburger", palette()));
}

void Tab::recreate_inspector()
{
if (m_inspector_widget)
m_inspector_widget->deleteLater();

m_inspector_widget = new InspectorWidget(this, view());

QObject::connect(m_inspector_widget, &InspectorWidget::closed, [this] {
m_inspector_widget->deleteLater();
m_inspector_widget = nullptr;
});
}

void Tab::show_inspector_window(InspectorTarget inspector_target)
{
if (!m_inspector_widget)
m_inspector_widget = new InspectorWidget(this, view());
recreate_inspector();
else
m_inspector_widget->inspect();

Expand Down
2 changes: 2 additions & 0 deletions UI/Qt/Tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public slots:

void close_sub_widgets();

void recreate_inspector();

QBoxLayout* m_layout { nullptr };
QToolBar* m_toolbar { nullptr };
QToolButton* m_hamburger_button { nullptr };
Expand Down

0 comments on commit 3d0565b

Please sign in to comment.