+ if (!m_searchBar) {
+ m_searchBar = new Search::Bar(std::make_shared<const Search::DolphinQuery>(m_urlNavigator->locationUrl(), QUrl{} /** will be set below. */), this);
+ connect(m_searchBar, &Search::Bar::urlChangeRequested, this, [this](const QUrl &url) {
+ m_view->setViewPropertiesContext(isSearchUrl(url) ? QStringLiteral("search") : QString());
+ setGrabFocusOnUrlChange(false); // Prevent loss of focus while typing or refining a search.
+ setUrl(url);
+ setGrabFocusOnUrlChange(true);
+ });
+ connect(m_searchBar, &Search::Bar::focusViewRequest, this, &DolphinViewContainer::requestFocus);
+ connect(m_searchBar, &Search::Bar::showMessage, this, [this](const QString &message, KMessageWidget::MessageType messageType) {
+ showMessage(message, messageType);
+ });
+ connect(m_searchBar,
+ &Search::Bar::showInstallationProgress,
+ m_statusBar,
+ [this](const QString ¤tlyRunningTaskTitle, int installationProgressPercent) {
+ m_statusBar->showProgress(currentlyRunningTaskTitle, installationProgressPercent, DolphinStatusBar::CancelLoading::Disallowed);
+ });
+ connect(m_searchBar, &Search::Bar::visibilityChanged, this, &DolphinViewContainer::searchBarVisibilityChanged);
+ m_topLayout->addWidget(m_searchBar, positionFor.searchBar, 0);
+ }
+
+ m_searchBar->setVisible(true, WithAnimation);
+
+ // The Search::Bar has been set visible but its state does not yet match with this view container or view.
+ // The view might for example already be searching because it was opened with a search URL. The Search::Bar needs to be updated to show the parameters of
+ // that search. And even if there is no search URL loaded in the view currently, we still need to figure out where the Search::Bar should be searching if
+ // the user starts a search from there. Let's figure out the search location in this method and let the DolphinQuery constructor figure out the rest from
+ // the current m_urlNavigator->locationUrl().
+ for (int i = m_urlNavigator->historyIndex(); i < m_urlNavigator->historySize(); i++) {
+ QUrl url = m_urlNavigator->locationUrl(i);
+ if (isSearchUrl(url)) {
+ // The previous location was a search URL. Try to see if that search URL has a valid search path so we keep searching in the same location.
+ const auto searchPath = Search::DolphinQuery(url, QUrl{}).searchPath(); // DolphinQuery is great at extracting the search path from a search URL.
+ if (searchPath.isValid()) {
+ m_searchBar->updateStateToMatch(std::make_shared<const Search::DolphinQuery>(m_urlNavigator->locationUrl(), searchPath));
+ return;
+ }
+ } else if (url.scheme() == QLatin1String("tags")) {
+ continue; // We avoid setting a tags url as the backup search path because a DolphinQuery constructed from a tags url will already search tagged
+ // items everywhere.
+ } else {
+ m_searchBar->updateStateToMatch(std::make_shared<const Search::DolphinQuery>(m_urlNavigator->locationUrl(), url));
+ return;
+ }
+ }
+ // We could not find any URL fit for searching in the history. This might happen because this view only ever loaded a search which searches "Everywhere"
+ // and therefore there is no specific search path to choose from. But the Search::Bar *needs* to know a search path because the user might switch from
+ // searching "Everywhere" to "Here" and it is everybody's guess what "Here" is supposed to mean in that context… We'll simply fall back to the user's home
+ // path for lack of a better option.
+ m_searchBar->updateStateToMatch(std::make_shared<const Search::DolphinQuery>(m_urlNavigator->locationUrl(), QUrl::fromUserInput(QDir::homePath())));
+}
+
+bool DolphinViewContainer::isSearchBarVisible() const
+{
+ return m_searchBar && m_searchBar->isVisible() && m_searchBar->isEnabled();
+}
+
+void DolphinViewContainer::setFocusToSearchBar()
+{
+ Q_ASSERT(isSearchBarVisible());
+ m_searchBar->selectAll();
+}
+
+void DolphinViewContainer::setSelectionModeEnabled(bool enabled, KActionCollection *actionCollection, SelectionMode::BottomBar::Contents bottomBarContents)
+{
+ const bool wasEnabled = m_view->selectionMode();
+ m_view->setSelectionModeEnabled(enabled);
+
+ if (!enabled) {
+ if (!wasEnabled) {
+ return; // nothing to do here
+ }
+ Q_CHECK_PTR(m_selectionModeTopBar); // there is no point in disabling selectionMode when it wasn't even enabled once.
+ Q_CHECK_PTR(m_selectionModeBottomBar);
+ m_selectionModeTopBar->setVisible(false, WithAnimation);
+ m_selectionModeBottomBar->setVisible(false, WithAnimation);
+ Q_EMIT selectionModeChanged(false);
+
+ if (!QApplication::focusWidget() || m_selectionModeTopBar->isAncestorOf(QApplication::focusWidget())
+ || m_selectionModeBottomBar->isAncestorOf(QApplication::focusWidget())) {
+ m_view->setFocus();
+ }
+ return;
+ }
+
+ if (!m_selectionModeTopBar) {
+ // Changing the location will disable selection mode.
+ connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlChanged, this, [this]() {
+ setSelectionModeEnabled(false);
+ });
+
+ m_selectionModeTopBar = new SelectionMode::TopBar(this); // will be created hidden
+ connect(m_selectionModeTopBar, &SelectionMode::TopBar::selectionModeLeavingRequested, this, [this]() {
+ setSelectionModeEnabled(false);
+ });
+ m_topLayout->addWidget(m_selectionModeTopBar, positionFor.selectionModeTopBar, 0);
+ }
+
+ if (!m_selectionModeBottomBar) {
+ m_selectionModeBottomBar = new SelectionMode::BottomBar(actionCollection, this);
+ connect(m_view, &DolphinView::selectionChanged, this, [this](const KFileItemList &selection) {
+ m_selectionModeBottomBar->slotSelectionChanged(selection, m_view->url());
+ });
+ connect(m_selectionModeBottomBar, &SelectionMode::BottomBar::error, this, &DolphinViewContainer::showErrorMessage);
+ connect(m_selectionModeBottomBar, &SelectionMode::BottomBar::selectionModeLeavingRequested, this, [this]() {
+ setSelectionModeEnabled(false);
+ });
+ m_topLayout->addWidget(m_selectionModeBottomBar, positionFor.selectionModeBottomBar, 0);
+ }
+ m_selectionModeBottomBar->resetContents(bottomBarContents);
+ if (bottomBarContents == SelectionMode::BottomBar::GeneralContents) {
+ m_selectionModeBottomBar->slotSelectionChanged(m_view->selectedItems(), m_view->url());
+ }
+
+ if (!wasEnabled) {
+ m_selectionModeTopBar->setVisible(true, WithAnimation);
+ m_selectionModeBottomBar->setVisible(true, WithAnimation);
+ Q_EMIT selectionModeChanged(true);
+ }
+}
+
+bool DolphinViewContainer::isSelectionModeEnabled() const
+{
+ const bool isEnabled = m_view->selectionMode();
+ Q_ASSERT((!isEnabled
+ // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
+ && (!m_selectionModeBottomBar || !m_selectionModeBottomBar->isEnabled() || !m_selectionModeBottomBar->isVisible()
+ || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::PasteContents))
+ || (isEnabled && m_selectionModeTopBar
+ && m_selectionModeTopBar->isVisible()
+ // The bottom bar is either visible or was hidden because it has nothing to show in GeneralContents mode e.g. because no items are selected.
+ && m_selectionModeBottomBar
+ && (m_selectionModeBottomBar->isVisible() || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::GeneralContents)));
+ return isEnabled;
+}
+
+void DolphinViewContainer::slotSplitTabDisabled()
+{
+ if (m_selectionModeBottomBar) {
+ m_selectionModeBottomBar->slotSplitTabDisabled();
+ }
+}
+
+void DolphinViewContainer::showMessage(const QString &message, KMessageWidget::MessageType messageType, std::initializer_list<QAction *> buttonActions)
+{
+ if (message.isEmpty()) {
+ return;
+ }
+
+ m_messageWidget->setText(message);