From 0d3093e5d8e31fe0d7ea1e86de09633b0bcb6589 Mon Sep 17 00:00:00 2001 From: stippi Date: Sun, 28 Feb 2010 13:32:39 +0000 Subject: [PATCH] Implemented a neat new window management feature: When the app is asked to open a new file or url, check if there is already a window open on the current workspace, and if so, open a new tab in that window and bring it to front. ArgReceived() now reuses RefsReceived(), which has been extended to handle "url" string fields in the message, since ArgReceived() may also be asked to open urls and not only local files. The LOAD_AT_STARTING mechanism, which turned out to be broken for multiple args anyway, could be replaced, since RefsReceived() already buffers the message when it has been called earlier then ReadyToRun(). git-svn-id: http://svn.haiku-os.org/webpositive/webkit/trunk@244 94f232f2-1747-11df-bad5-a5bfde151594 --- src/apps/webpositive/BrowserApp.cpp | 74 ++++++++++++++--------------- src/apps/webpositive/BrowserApp.h | 1 + 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/apps/webpositive/BrowserApp.cpp b/src/apps/webpositive/BrowserApp.cpp index 3dd0d7a25a..c23e7cbd6b 100644 --- a/src/apps/webpositive/BrowserApp.cpp +++ b/src/apps/webpositive/BrowserApp.cpp @@ -49,11 +49,6 @@ const char* kApplicationSignature = "application/x-vnd.Haiku-WebPositive"; const char* kApplicationName = "WebPositive"; -enum { - LOAD_AT_STARTING = 'lost' -}; - - BrowserApp::BrowserApp() : BApplication(kApplicationSignature) , fWindowCount(0) @@ -85,17 +80,18 @@ BrowserApp::AboutRequested() void BrowserApp::ArgvReceived(int32 argc, char** argv) { + BMessage message(B_REFS_RECEIVED); for (int i = 1; i < argc; i++) { const char* url = argv[i]; BEntry entry(argv[i], true); BPath path; if (entry.Exists() && entry.GetPath(&path) == B_OK) url = path.Path(); - BMessage message(LOAD_AT_STARTING); message.AddString("url", url); - message.AddBool("new window", fInitialized || i > 1); - PostMessage(&message); } + // Upon program launch, it will buffer a copy of the message, since + // ArgReceived() is called before ReadyToRun(). + RefsReceived(&message); } @@ -141,37 +137,8 @@ void BrowserApp::MessageReceived(BMessage* message) { switch (message->what) { - case LOAD_AT_STARTING: { - BString url; - if (message->FindString("url", &url) != B_OK) - break; - bool openNewWindow = false; - message->FindBool("new window", &openNewWindow); - BrowserWindow* webWindow = NULL; - for (int i = 0; BWindow* window = WindowAt(i); i++) { - webWindow = dynamic_cast(window); - if (!webWindow) - continue; - if (!openNewWindow) { - // stop at the first window - break; - } - } - if (webWindow) { - // There should always be at least one window open. If not, maybe we are about - // to quit anyway... - if (openNewWindow) { - // open a new window with an offset to the last window - _CreateNewWindow(url); - } else { - // load the URL in the first window - webWindow->CurrentWebView()->LoadURL(url.String()); - } - } - break; - } case B_SILENT_RELAUNCH: - _CreateNewWindow(""); + _CreateNewPage(""); break; case NEW_WINDOW: { BString url; @@ -238,8 +205,12 @@ BrowserApp::RefsReceived(BMessage* message) continue; BString url; url << path.Path(); - _CreateNewWindow(url); + _CreateNewPage(url); } + + BString url; + for (int32 i = 0; message->FindString("url", i, &url) == B_OK; i++) + _CreateNewPage(url); } @@ -293,6 +264,31 @@ BrowserApp::_OpenSettingsFile(BFile& file, uint32 mode) } +void +BrowserApp::_CreateNewPage(const BString& url) +{ + uint32 workspace = 1 << current_workspace(); + + bool loadedInWindowOnCurrentWorkspace = false; + for (int i = 0; BWindow* window = WindowAt(i); i++) { + BrowserWindow* webWindow = dynamic_cast(window); + if (!webWindow) + continue; + if (webWindow->Lock()) { + if (webWindow->Workspaces() & workspace) { + webWindow->CreateNewTab(url, true); + webWindow->Activate(); + loadedInWindowOnCurrentWorkspace = true; + } + webWindow->Unlock(); + } + if (loadedInWindowOnCurrentWorkspace) + return; + } + _CreateNewWindow(url); +} + + void BrowserApp::_CreateNewWindow(const BString& url) { diff --git a/src/apps/webpositive/BrowserApp.h b/src/apps/webpositive/BrowserApp.h index f834d41f62..17ae54735a 100644 --- a/src/apps/webpositive/BrowserApp.h +++ b/src/apps/webpositive/BrowserApp.h @@ -51,6 +51,7 @@ public: private: bool _OpenSettingsFile(BFile& file, uint32 mode); + void _CreateNewPage(const BString& url); void _CreateNewWindow(const BString& url); void _CreateNewTab(BrowserWindow* window, const BString& url, bool select);