From 4881c0af02ea0e840454d54d0849ad2c5d7be4dc Mon Sep 17 00:00:00 2001 From: Cameron Kaiser Date: Tue, 12 Feb 2019 18:47:45 -0800 Subject: [PATCH] #164: implement rw current tab prop ; improve closing ; fix various glitches --- .../Contents/Resources/scripting.sdef.in | 2 +- browser/components/nsBrowserGlue.js | 19 +++++++++-- .../public/nsIApplescriptService.idl | 32 +++++++++++++++++ .../applescript/src/MacScripting.mm | 34 +++++++++++++++++-- .../applescript/src/nsApplescriptService.cpp | 19 +++++++++++ 5 files changed, 100 insertions(+), 6 deletions(-) diff --git a/browser/app/macbuild/Contents/Resources/scripting.sdef.in b/browser/app/macbuild/Contents/Resources/scripting.sdef.in index 59eabe66a..4727ed8c5 100644 --- a/browser/app/macbuild/Contents/Resources/scripting.sdef.in +++ b/browser/app/macbuild/Contents/Resources/scripting.sdef.in @@ -274,7 +274,7 @@ --> - + diff --git a/browser/components/nsBrowserGlue.js b/browser/components/nsBrowserGlue.js index 9f3f576b8..5d220ffbd 100644 --- a/browser/components/nsBrowserGlue.js +++ b/browser/components/nsBrowserGlue.js @@ -907,8 +907,9 @@ BrowserGlue.prototype = { createWindowAtIndex : function(index) { var handler = Cc["@mozilla.org/browser/clh;1"].getService(Ci.nsIBrowserHandler); var defaultArgs = handler.defaultArgs; - var topWindow = Services.wm.getMostRecentWindow(''); - topWindow.openDialog("chrome://browser/content/", "_blank", "chrome,all,dialog=no,non-remote", defaultArgs); + // Use the hidden window to open in case the script closes all the things. + Services.appShell.hiddenDOMWindow.openDialog("chrome://browser/content/", + "_blank", "chrome,all,dialog=no,non-remote", defaultArgs); }, getWindows : function() { var array = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray); @@ -946,6 +947,20 @@ BrowserGlue.prototype = { var tab = win.gBrowser.tabs[index]; win.gBrowser.removeTab(tab); } + }, + setCurrentTabInWindow : function(index, window_index) { + let win = this.getWindow(window_index); + if (win != null) { + win.gBrowser.selectedTab = win.gBrowser.tabs[index]; + } + }, + closeWindowAtIndex : function(index) { + let win = this.getWindow(index); + if (win != null) { + // Use the soop3r s3kr1t don't ask when closing flag. + win.skipNextCanClose = true; + win.close(); + } } } diff --git a/toolkit/components/applescript/public/nsIApplescriptService.idl b/toolkit/components/applescript/public/nsIApplescriptService.idl index 2e0733156..9d6f9e984 100644 --- a/toolkit/components/applescript/public/nsIApplescriptService.idl +++ b/toolkit/components/applescript/public/nsIApplescriptService.idl @@ -71,6 +71,13 @@ interface nsIApplescriptService : nsISupports * @param index The tab's index. */ void createWindowAtIndex(in unsigned long index); + + /** + * Closes a window (without verification). + * + * @param index The tab's index. + */ + void closeWindowAtIndex(in unsigned long index); /** * Returns an array of nsIDOMWindows representing the tabs in @@ -89,6 +96,15 @@ interface nsIApplescriptService : nsISupports */ nsIDOMWindow getCurrentTabInWindow(in unsigned long index, out unsigned long tab_index); + /** + * Sets the current tab in + * the window to the given index. + * + * @param index The window's index. + * @param window_index The window's index. + */ + void setCurrentTabInWindow(in unsigned long index, in unsigned long window_index); + /** * Creates a tab in the window at the given index. * @@ -121,6 +137,13 @@ interface nsIApplescriptWindowCallback : nsISupports * @param index The tab's index. */ void createWindowAtIndex(in unsigned long index); + + /** + * Closes a window (without verification). + * + * @param index The tab's index. + */ + void closeWindowAtIndex(in unsigned long index); }; [scriptable, uuid(a433c084-ffc7-4264-90fa-82c1e0100b46)] @@ -143,6 +166,15 @@ interface nsIApplescriptTabCallback : nsISupports */ nsIDOMWindow getCurrentTabInWindow(in unsigned long index, out unsigned long tab_index); + /** + * Sets the current tab in + * the window to the given index. + * + * @param index The tab's index. + * @param window_index The window's index. + */ + void setCurrentTabInWindow(in unsigned long index, in unsigned long window_index); + /** * Creates a tab in the window at the given index. * diff --git a/toolkit/components/applescript/src/MacScripting.mm b/toolkit/components/applescript/src/MacScripting.mm index cb16493d7..f65d68be3 100644 --- a/toolkit/components/applescript/src/MacScripting.mm +++ b/toolkit/components/applescript/src/MacScripting.mm @@ -150,6 +150,7 @@ typedef unsigned int NSUInteger; - (NSArray*)scriptTabs; - (void)insertInScriptTabs:(id)value; - (GeckoTab*)selectedScriptTab; +- (void)setSelectedScriptTab:(id)value; // Helper Methods - (void)_setIndex:(NSUInteger)index; @@ -183,6 +184,7 @@ typedef unsigned int NSUInteger; // Helper Methods - (void)_setWindow:(GeckoWindow*)window; - (void)_setIndex:(NSUInteger)index; +- (NSUInteger)_index; @end @@ -482,6 +484,16 @@ static GeckoScriptingRoot *sharedScriptingRoot = nil; } - (id)handleCloseScriptCommand:(NSCloseCommand*)command { + NS_WARNING("AppleScript: window handleCloseScriptCommand"); + // Try to close with the scripted handler, since it's faster. If not, + // fallback on Cocoa. + nsCOMPtr applescriptService(do_GetService("@mozilla.org/applescript-service;1")); + if (applescriptService) { + if (NS_SUCCEEDED(applescriptService->CloseWindowAtIndex(mIndex))) { + return nil; + } + } + NS_WARNING("window close from Gecko failed; trying from NSWindow"); NSWindow *window = [self window]; if (window) { return [window handleCloseScriptCommand:command]; @@ -536,16 +548,28 @@ static GeckoScriptingRoot *sharedScriptingRoot = nil; if (!applescriptService) { return nil; } - + nsCOMPtr contentWindow; - uint32_t tabIndex = 0; + uint32_t tabIndex = 0; if (NS_FAILED(applescriptService->GetCurrentTabInWindow(mIndex, &tabIndex, getter_AddRefs(contentWindow))) || !contentWindow) { return nil; } - + return [GeckoTab tabWithIndex:tabIndex andContentWindow:contentWindow andWindow:self]; } +- (void)setSelectedScriptTab:(id)value { + NS_WARNING("AppleScript: window setSelectedScriptTab"); + if (![(NSObject*)value isKindOfClass:[GeckoTab class]]) { + return; + } + nsCOMPtr applescriptService(do_GetService("@mozilla.org/applescript-service;1")); + if (!applescriptService) { + return; + } + (void)applescriptService->SetCurrentTabInWindow([(GeckoTab*)value _index], mIndex); +} + - (void)insertObject:(NSObject*)object inScriptTabsAtIndex:(NSUInteger)index { NS_WARNING("AppleScript: window insertObject:inScriptTabsAtIndex"); if (![object isKindOfClass:[GeckoTab class]]) { @@ -627,6 +651,10 @@ static GeckoScriptingRoot *sharedScriptingRoot = nil; mIndex = index; } +- (NSUInteger)_index { + return mIndex; +} + - (NSScriptObjectSpecifier*)objectSpecifier { if (!mWindow) { diff --git a/toolkit/components/applescript/src/nsApplescriptService.cpp b/toolkit/components/applescript/src/nsApplescriptService.cpp index 50994e131..9251ad5c1 100644 --- a/toolkit/components/applescript/src/nsApplescriptService.cpp +++ b/toolkit/components/applescript/src/nsApplescriptService.cpp @@ -20,6 +20,7 @@ * * Contributor(s): * Scott Greenlay + * Cameron Kaiser * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -68,12 +69,22 @@ nsApplescriptService::GetWindows(nsIArray **windows) NS_IMETHODIMP nsApplescriptService::CreateWindowAtIndex(uint32_t index) { + NS_ASSERTION(index == 0, "Not implemented for window indices other than zero"); if (windowCallback) { return windowCallback->CreateWindowAtIndex(index); } return NS_OK; } +NS_IMETHODIMP +nsApplescriptService::CloseWindowAtIndex(uint32_t index) +{ + if (windowCallback) { + return windowCallback->CloseWindowAtIndex(index); + } + return NS_OK; +} + NS_IMETHODIMP nsApplescriptService::GetTabsInWindow(uint32_t index, nsIArray **tabs) { if (tabCallback) { @@ -95,6 +106,14 @@ nsApplescriptService::GetCurrentTabInWindow(uint32_t index, uint32_t *tab_index, return NS_OK; } +NS_IMETHODIMP +nsApplescriptService::SetCurrentTabInWindow(uint32_t index, uint32_t window_index) { + if (tabCallback) { + return tabCallback->SetCurrentTabInWindow(index, window_index); + } + return NS_OK; +} + NS_IMETHODIMP nsApplescriptService::CreateTabAtIndexInWindow(uint32_t index, uint32_t window_index) { if (tabCallback) {