diff options
| author | Felix (xq) Queißner <git@mq32.de> | 2021-03-06 15:26:07 +0100 |
|---|---|---|
| committer | Felix (xq) Queißner <git@mq32.de> | 2021-03-06 15:26:07 +0100 |
| commit | 9457e720f24d8365ed6a1e82b016078b0bc17eb6 (patch) | |
| tree | d810dcd8189ce508a9a0e666f7d819f3c63bcbca /src/main.cpp | |
| parent | e16c85887a58381f225d706a8d740bf96a5fdf6a (diff) | |
| download | kristall-9457e720f24d8365ed6a1e82b016078b0bc17eb6.tar.gz | |
Implements proper multi-window support.
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 98 |
1 files changed, 74 insertions, 24 deletions
diff --git a/src/main.cpp b/src/main.cpp index 5bc2426..e1098bd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -115,10 +115,27 @@ void forAllAppWindows(F const & f) MainWindow * getFocusedAppWindow() { assert(qApp != nullptr); + // first, check if we have currently active window: auto * main_window = qobject_cast<MainWindow *>(qApp->activeWindow()); if(main_window != nullptr) return main_window; - return last_focused_window; + + // if not, fall back to the window we focused last: + if(::last_focused_window != nullptr) + return ::last_focused_window; + + // and if we have none, we just take the first window we can find if + // any: + forAllAppWindows([&main_window](MainWindow * w) { + if(main_window == nullptr) + main_window = w; + }); + if(main_window != nullptr) + return main_window; + + qCritical() << "could not find a focused/foreground window!"; + + return main_window; } @@ -234,18 +251,17 @@ namespace ipc break; } case Message::open_in_window: { - auto * const window = getFocusedAppWindow(); + QVector<QUrl> urls; for(auto const & data : payload.split('\n')) { QUrl url { QString::fromUtf8(data) }; if(url.isValid()) { - // TODO: Implement opening these urls in a new - // window instead of a new tab! - if(window != nullptr) { - window->addNewTab(true, url); - } + urls.append(url); } } + if(urls.size() > 0) { + kristall::openNewWindow(urls); + } break; } @@ -311,6 +327,36 @@ void kristall::registerAppWindow(MainWindow * window) }); } +MainWindow * kristall::openNewWindow(bool load_default) +{ + auto * const window = openNewWindow(QVector<QUrl>{}); + window->addEmptyTab(true, load_default); + return window; +} + +//! Opens a new window with the given url. +MainWindow * kristall::openNewWindow(QUrl const & url) +{ + return openNewWindow(QVector<QUrl>{url}); +} + +//! Opens a new window with the given list of urls. +//! If the list is empty, no new tab will spawned. +MainWindow * kristall::openNewWindow(QVector<QUrl> const & urls) +{ + MainWindow * const window = new MainWindow(qApp); + + for(int i = 0; i < urls.length(); i++) + { + window->addNewTab((i == 0), urls.at(i)); + } + + window->show(); + + return window; +} + + int main(int argc, char *argv[]) { QApplication app(argc, argv); @@ -461,7 +507,7 @@ int main(int argc, char *argv[]) QSettings app_settings { kristall::dirs::config_root.absoluteFilePath("config.ini"), - QSettings::IniFormat + QSettings::IniFormat }; app_settings_ptr = &app_settings; @@ -613,8 +659,6 @@ int main(int argc, char *argv[]) kristall::setTheme(kristall::options.theme); - MainWindow w(&app); - if(ipc_server != nullptr) { QObject::connect(ipc_server.get(), &QLocalServer::newConnection, [&ipc_server]() { auto * const socket = ipc_server->nextPendingConnection(); @@ -632,24 +676,20 @@ int main(int argc, char *argv[]) // Open all URLs in the new window if(urls.size() > 0) { - for(const auto & url : urls) { - w.addNewTab(false, url); - } + kristall::openNewWindow(urls); } else { - w.addEmptyTab(true, true); + kristall::openNewWindow(true); } - app_settings.beginGroup("Window State"); - if(app_settings.contains("geometry")) { - w.restoreGeometry(app_settings.value("geometry").toByteArray()); - } - if(app_settings.contains("state")) { - w.restoreState(app_settings.value("state").toByteArray()); - } - app_settings.endGroup(); - - w.show(); + //app_settings.beginGroup("Window State"); + //if(app_settings.contains("geometry")) { + // w.restoreGeometry(app_settings.value("geometry").toByteArray()); + //} + //if(app_settings.contains("state")) { + // w.restoreState(app_settings.value("state").toByteArray()); + //} + //app_settings.endGroup(); int exit_code = app.exec(); @@ -780,6 +820,16 @@ void GenericSettings::save(QSettings &settings) const } } +void kristall::applySettings() +{ + kristall::setTheme(kristall::options.theme); + kristall::setUiDensity(kristall::options.ui_density, false); + + forAllAppWindows([](MainWindow * window) + { + window->applySettings(); + }); +} void kristall::saveSettings() { |
