Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

Developer bar: Console and Network switch to their panel, and close DevTools only when it already shows (#138) (#148)

Victor Kildahl7b7edc485e6b
diff --git a/Sources/Cra/App/AppDelegate.swift b/Sources/Cra/App/AppDelegate.swift
index ac8b6df..853e7bb 100644
--- a/Sources/Cra/App/AppDelegate.swift
+++ b/Sources/Cra/App/AppDelegate.swift
@@ -876,7 +876,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
case "zoomOut": main.zoom(by: -0.5)
case "resetZoom": main.currentBrowser?.setZoomLevel(0)
case "viewSource": main.currentBrowser?.viewSource()
- // Like Chrome: ⌥⌘I, ⌥⌘J and Network close the dock when it is already open.
+ // Like Arc: ⌥⌘I closes open DevTools; ⌥⌘J and Network switch to their panel and close
+ // DevTools only when that panel already shows (DevToolsToggle.run).
case "devTools": main.browsers.runDevTools(.toggle)
case "jsConsole": main.browsers.runDevTools(.console)
case "networkPanel": main.browsers.runDevTools(.network)
diff --git a/Sources/Cra/Services/BrowserManager.swift b/Sources/Cra/Services/BrowserManager.swift
index 39ade0e..8cc3cc2 100644
--- a/Sources/Cra/Services/BrowserManager.swift
+++ b/Sources/Cra/Services/BrowserManager.swift
@@ -903,7 +903,8 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {
func runDevTools(_ command: DevToolsToggle.Command) {
guard let rt = visibleRuntime else { return }
let (effect, frontend) = rt.devTools.run(command)
- // A press made while DevTools close is done when they reopen (DevToolsToggle.didClose).
+ // A press made while DevTools close is done when they reopen (DevToolsToggle.didClose),
+ // one made while they open once the frontend is up.
rt.browser.devToolsAction = frontend
perform(effect, on: rt)
}
@@ -912,6 +913,7 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {
switch effect {
case .show: rt.browser.showDevTools()
case .close: rt.browser.closeDevTools()
+ case .frontend: rt.browser.applyDevToolsAction()
case .none: break
}
}
@@ -943,6 +945,11 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {
delegate?.browserManagerDevToolsChanged()
}
+ func browser(_ browser: CraBrowser, devToolsShowsPanel panel: String) {
+ guard let id = tabID(of: browser) else { return }
+ runtimes[id]?.devTools.didShowPanel(panel)
+ }
+
/// CEF's window for the visible tab's DevTools, if any.
var visibleDevToolsWindow: NSWindow? { visibleTabID.flatMap { devToolsViews[$0]?.window } }
diff --git a/Sources/Cra/Services/DevToolsToggle.swift b/Sources/Cra/Services/DevToolsToggle.swift
index c233073..37f557a 100644
--- a/Sources/Cra/Services/DevToolsToggle.swift
+++ b/Sources/Cra/Services/DevToolsToggle.swift
@@ -6,9 +6,13 @@ import Foundation
/// from Cra's requests and CEF's callbacks instead, and remembers a press made mid-close.
struct DevToolsToggle: Equatable {
enum State: Equatable { case closed, opening, open, closing(reopen: Bool) }
- enum Effect: Equatable { case show, close, none }
+ /// `frontend`: DevTools stay as they are and the open frontend takes the action now.
+ enum Effect: Equatable { case show, close, frontend, none }
private(set) var state = State.closed
+ /// The panel the frontend shows ("console", "network", "elements", …) as it last reported
+ /// it, nil while closed or before it reports one.
+ private(set) var panel: String?
/// The shortcut, the developer bar's button, the menu item.
mutating func toggle() -> Effect {
@@ -37,23 +41,43 @@ struct DevToolsToggle: Equatable {
}
}
- /// The DevTools commands, as Chrome's DevToolsToggleAction (Arc 1.170's developer bar runs
- /// Chrome's commands, GitHub #88): ⌥⌘I toggles; Console (⌥⌘J) and Network toggle too and
- /// open on their panel; Inspect (⌥⌘C) always ends open, with the element picker armed.
+ /// The DevTools commands of Arc 1.170's developer bar and menu (GitHub #88): ⌥⌘I toggles;
+ /// Console (⌥⌘J) and Network open on their panel, switch to it when DevTools show another,
+ /// and close them only when it already shows (#138); Inspect (⌥⌘C) always ends open, with
+ /// the element picker armed.
enum Command: Equatable { case toggle, console, network, inspect }
- /// The effect, and what the frontend is asked once open (CraBrowser.devToolsAction).
+ /// The effect, and what the frontend is asked (CraBrowser.devToolsAction).
mutating func run(_ command: Command) -> (effect: Effect, frontend: String?) {
- let effect = command == .inspect ? show() : toggle()
- guard effect != .close else { return (effect, nil) }
switch command {
- case .toggle: return (effect, nil)
- case .console: return (effect, "console")
- case .network: return (effect, "network")
- case .inspect: return (effect, "inspect")
+ case .toggle: return (toggle(), nil)
+ case .inspect: return (show(), "inspect")
+ case .console, .network:
+ let name = command == .console ? "console" : "network"
+ switch state {
+ case .closed, .closing:
+ let effect = toggle()
+ return (effect, state == .closing(reopen: false) ? nil : name)
+ case .opening:
+ // The frontend still loading takes the panel once it is up.
+ return (.none, name)
+ case .open:
+ if panel == name {
+ state = .closing(reopen: false)
+ return (.close, nil)
+ }
+ // Until the frontend reports the switch, a second press already closes.
+ panel = name
+ return (.frontend, name)
+ }
}
}
+ /// The frontend selected a panel, from these commands or its own tab bar.
+ mutating func didShowPanel(_ name: String) {
+ if state == .open || state == .opening { panel = name }
+ }
+
/// CEF made the DevTools view, also for DevTools opened from Chromium's own menu.
mutating func didOpen() {
if case .closing = state { return }
@@ -62,6 +86,7 @@ struct DevToolsToggle: Equatable {
/// CEF reports them gone; a press made while they closed opens them again now.
mutating func didClose() -> Effect {
+ panel = nil
if case .closing(reopen: true) = state {
state = .opening
return .show
diff --git a/Sources/CraCEF/CraCEF.mm b/Sources/CraCEF/CraCEF.mm
index 28dec43..4b44253 100644
--- a/Sources/CraCEF/CraCEF.mm
+++ b/Sources/CraCEF/CraCEF.mm
@@ -831,6 +831,13 @@ static const char kCraDevToolsHooks[] =
// (DevToolsWindow::DoAction); -runDevToolsAction queues on __craRun until then.
"var lc=h.loadCompleted;h.loadCompleted=function(){var r=lc&&lc.apply(h,arguments);h.__craLoaded=1;"
"var q=h.__craRun||[];h.__craRun=[];q.forEach(function(f){f();});return r;};"
+ // The panel on show, so Console and Network can tell a switch from a close (GitHub #138):
+ // the main tab bar is the one with Elements; its selected tab is #tab-<panel>.
+ "(function(){var now=null,n=0,w=setInterval(function(){var e=find(document,'#tab-elements'),tabs=e&&e.parentNode;"
+ "if(tabs){clearInterval(w);var report=function(){var t=tabs.querySelector('.tabbed-pane-header-tab.selected');"
+ "var p=t&&t.id.indexOf('tab-')==0?t.id.slice(4):null;if(p&&p!==now){now=p;console.log('__cra_devtools_panel:'+p);}};"
+ "report();new MutationObserver(report).observe(tabs,{attributes:true,subtree:true,attributeFilter:['class']});return;}"
+ "if(++n>200)clearInterval(w);},150);})();"
"if(!/[?&]can_dock=/.test(location.search)){installClose();return;}"
"var sent=false;"
"h.setInspectedPageBounds=function(b){sent=true;try{console.log('__cra_devtools_bounds:'+JSON.stringify({x:b.x,y:b.y,width:b.width,height:b.height}));}catch(e){}};"
@@ -921,6 +928,7 @@ public:
static const std::string kClose = "__cra_devtools_close";
static const std::string kBounds = "__cra_devtools_bounds:";
static const std::string kDocked = "__cra_devtools_docked:";
+ static const std::string kPanel = "__cra_devtools_panel:";
std::string m = message.ToString();
if (m == kClose) {
if (CraBrowser* o = owner_) [o closeDevTools];
@@ -936,6 +944,14 @@ public:
}
return true;
}
+ if (m.rfind(kPanel, 0) == 0) {
+ NSString* panel = [NSString stringWithUTF8String:m.c_str() + kPanel.size()];
+ CraDevToolsLog(@"panel %@", panel);
+ CraBrowser* o = owner_;
+ if (o && panel.length && [o.delegate respondsToSelector:@selector(browser:devToolsShowsPanel:)])
+ [o.delegate browser:o devToolsShowsPanel:panel];
+ return true;
+ }
if (m.rfind("__cra_dbg:", 0) == 0) { CraDevToolsLog(@"%s", m.c_str()); return true; }
if (m.rfind(kDocked, 0) == 0) {
BOOL docked = m.size() > kDocked.size() && m[kDocked.size()] == '1';
@@ -2044,6 +2060,10 @@ class CraDevToolsObserver : public CefDevToolsMessageObserver {
- (void)showDevTools { [self showDevToolsInspectingPoint:NSZeroPoint]; }
- (void)closeDevTools { if (_browser) _browser->GetHost()->CloseDevTools(); }
+- (void)applyDevToolsAction {
+ // A frontend still loading takes the action when DevToolsClient prepares it.
+ if (_devToolsBrowser && !_devToolsBrowser->IsLoading()) [self runDevToolsAction];
+}
// The frontend's own API, which Chrome's DevToolsWindow::DoAction calls through DevToolsAPI.
- (void)runDevToolsAction {
NSString* action = _devToolsAction;
diff --git a/Sources/CraCEF/include/CraCEF.h b/Sources/CraCEF/include/CraCEF.h
index 8734daa..42441be 100644
--- a/Sources/CraCEF/include/CraCEF.h
+++ b/Sources/CraCEF/include/CraCEF.h
@@ -109,6 +109,9 @@ typedef NS_ENUM(NSInteger, CraDownloadState) {
/// opens as a target=_blank tab) or Shift-clicked (YES, instead of a new window). Return
/// YES to show it in a Peek; the page then stays where it is.
- (BOOL)browser:(CraBrowser *)browser opensPeekForURL:(NSString *)url shiftClick:(BOOL)shiftClick;
+/// The DevTools frontend shows `panel` ("elements", "console", "network", …): on load, and
+/// on every switch, from Cra or the frontend's own tab bar.
+- (void)browser:(CraBrowser *)browser devToolsShowsPanel:(NSString *)panel;
@end
@interface CraBrowser : NSObject
@@ -164,6 +167,9 @@ typedef NS_ENUM(NSInteger, CraDownloadState) {
/// panel to select ("console", "network", "elements"), or "inspect" for the element picker. Done
/// once the frontend is up (at once when DevTools are already open), then cleared.
@property (nonatomic, copy, nullable) NSString *devToolsAction;
+/// Does devToolsAction in DevTools already open, without showing them again; a frontend
+/// still loading does it once up.
+- (void)applyDevToolsAction;
- (void)closeDevTools;
- (double)zoomLevel;
- (void)setZoomLevel:(double)level;
diff --git a/Tests/BrowserManagerLifecycleTests.swift b/Tests/BrowserManagerLifecycleTests.swift
index caf02bf..df258d9 100644
--- a/Tests/BrowserManagerLifecycleTests.swift
+++ b/Tests/BrowserManagerLifecycleTests.swift
@@ -261,7 +261,7 @@ enum BrowserManagerLifecycleTests {
}
/// ⌥⌘I pressed again while CEF is still closing DevTools opens them once they are gone,
- /// instead of closing them a second time.
+ /// instead of closing them a second time. Console and Network switch panels on open DevTools.
@MainActor
private static func devToolsCase() {
let store = SidebarStore()
@@ -281,8 +281,13 @@ enum BrowserManagerLifecycleTests {
manager.runDevTools(.inspect)
expect(browser.devToolsRequests.last == "show" && manager.runtime(for: id)!.devTools.state == .open
&& browser.devToolsAction == "inspect", "Inspect Element on open DevTools keeps them open and arms the picker")
+ manager.browser(browser, devToolsShowsPanel: "console")
manager.runDevTools(.network)
- expect(browser.devToolsRequests.last == "close" && browser.devToolsAction == nil, "Network on open DevTools closes them")
+ expect(browser.devToolsRequests.last == "apply network" && manager.runtime(for: id)!.devTools.state == .open,
+ "Network on the Console switches the open frontend to Network (#138)")
+ manager.browser(browser, devToolsShowsPanel: "network")
+ manager.runDevTools(.network)
+ expect(browser.devToolsRequests.last == "close" && browser.devToolsAction == nil, "Network on the Network panel closes them")
manager.closeAll()
}
diff --git a/Tests/BrowserManagerSupport/CraCEF.swift b/Tests/BrowserManagerSupport/CraCEF.swift
index 0ec6b16..09d3eea 100644
--- a/Tests/BrowserManagerSupport/CraCEF.swift
+++ b/Tests/BrowserManagerSupport/CraCEF.swift
@@ -44,11 +44,12 @@ public final class CraBrowser {
public func close() { closeRequested = true; onClose?() }
public func finishClose() { isClosed = true; delegate?.browserDidClose(self) }
- /// "show" / "close", in the order Cra asked.
+ /// "show" / "close" / "apply <action>", in the order Cra asked.
public var devToolsRequests: [String] = []
public func showDevTools() { hasDevTools = true; devToolsRequests.append("show") }
/// The frontend work asked of the next show (a panel, "inspect"); CEF clears it once done.
public var devToolsAction: String?
+ public func applyDevToolsAction() { devToolsRequests.append("apply \(devToolsAction ?? "")"); devToolsAction = nil }
public func closeDevTools() { hasDevTools = false; devToolsRequests.append("close") }
public func setAudioMuted(_ muted: Bool) {}
public private(set) var backgroundProbes = 0
diff --git a/Tests/PolicyTests.swift b/Tests/PolicyTests.swift
index ca1c2e4..d43afdf 100644
--- a/Tests/PolicyTests.swift
+++ b/Tests/PolicyTests.swift
@@ -89,13 +89,32 @@ enum PolicyTests {
userClosed.didOpen()
expect(userClosed.didClose() == .none && userClosed.toggle() == .show, "closed from DevTools' own button: the next press opens")
- // The developer bar and ⌥⌘J/⌥⌘C (#88): Chrome's toggle, with the frontend on a panel.
+ // The developer bar and ⌥⌘J/⌥⌘C (#88): Console and Network switch panels, close only their own (#138).
var bar = DevToolsToggle()
expect(bar.run(.console) == (.show, "console"), "Console on closed DevTools opens them on the Console")
+ expect(bar.run(.network) == (.none, "network") && bar.state == .opening, "pressed while they open: they open on Network instead")
bar.didOpen()
- expect(bar.run(.network) == (.close, nil), "Network on open DevTools closes them, as Chrome's toggle does")
+ bar.didShowPanel("console")
+ expect(bar.run(.network) == (.frontend, "network") && bar.state == .open, "Network on the Console switches panel, DevTools stay open")
+ expect(bar.run(.network) == (.close, nil) && bar.state == .closing(reopen: false),
+ "pressed again before the frontend reports the switch: Network shows, so it closes")
expect(bar.run(.network) == (.none, "network") && bar.state == .closing(reopen: true), "pressed while closing: reopened on Network")
- expect(bar.didClose() == .show, "once closed")
+ expect(bar.run(.console) == (.none, nil) && bar.state == .closing(reopen: false), "a second press takes the reopen back")
+ expect(bar.run(.console) == (.none, "console"), "a third asks again, on the Console")
+ expect(bar.didClose() == .show && bar.panel == nil, "once closed they reopen, the old panel forgotten")
+ bar.didOpen()
+ bar.didShowPanel("elements")
+ expect(bar.run(.console) == (.frontend, "console"), "Console on Elements switches")
+ bar.didShowPanel("console")
+ expect(bar.run(.console) == (.close, nil), "Console on the Console closes")
+ expect(bar.didClose() == .none && bar.state == .closed && bar.panel == nil, "and stays closed")
+ bar.didShowPanel("network")
+ expect(bar.panel == nil, "a report from a closed frontend is dropped")
+ var unknown = DevToolsToggle()
+ unknown.didOpen()
+ expect(unknown.run(.console) == (.frontend, "console"), "a frontend that never said its panel: switch, never close")
+ bar = DevToolsToggle()
+ _ = bar.run(.console)
bar.didOpen()
expect(bar.run(.inspect) == (.show, "inspect") && bar.state == .open, "Inspect never closes: the picker on open DevTools")
expect(bar.run(.toggle) == (.close, nil), "⌥⌘I closes")