From 60e109632fd63b335ca1cc037c5d5c3e291349f5 Mon Sep 17 00:00:00 2001 From: Méven Car Date: Sun, 2 Nov 2025 15:49:47 +0100 Subject: context menu: use selected item as containing folder for New file menu Use current view url as fallback. Don't update the selection after a directory is created, except if there was no selection. CCBUG: 508196 BUG: 512020 --- src/views/dolphinview.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) (limited to 'src/views/dolphinview.cpp') diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index 4c909caca..70b32968a 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -113,6 +113,7 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent) // get selected and it must be assured that the item will get visible. As the // creation is done asynchronously, several signals must be checked: connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::itemCreated, this, &DolphinView::observeCreatedItem); + connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::directoryCreated, this, &DolphinView::observeCreatedDirectory); m_selectionChangedTimer = new QTimer(this); m_selectionChangedTimer->setSingleShot(true); @@ -431,6 +432,15 @@ KFileItemList DolphinView::selectedItems() const return selectedItems; } +std::optional DolphinView::firstSelectedItem() const +{ + const KItemListSelectionManager *selectionManager = m_container->controller()->selectionManager(); + if (selectionManager->selectedItems().count() == 1) { + return {m_model->fileItem(selectionManager->selectedItems().first())}; + } + return std::nullopt; +} + int DolphinView::selectedItemsCount() const { const KItemListSelectionManager *selectionManager = m_container->controller()->selectionManager(); @@ -1780,11 +1790,80 @@ void DolphinView::resetZoomLevel() setZoomLevel(ZoomLevelInfo::zoomLevelForIconSize(QSize(userDefaultIconSize, userDefaultIconSize))); } +void DolphinView::selectFileOnceAvailable(const QUrl &url, std::function condition) +{ + // need to wait for the item to be added to the model + QMetaObject::Connection *connection = new QMetaObject::Connection; + *connection = connect(m_model, &KFileItemModel::itemsInserted, this, [this, url, connection, condition](const KItemRangeList &ranges) { + bool found = false; + for (const KItemRange &it : ranges) { + for (int i = 0; i < it.count; ++i) { + if (m_model->fileItem(it.index + i).url() == url) { + found = true; + break; + } + } + if (found) { + break; + } + } + // check whether the selection should be changed + if (condition()) { + forceUrlsSelection(url, {url}); + } + if (found) { + disconnect(*connection); + delete connection; + } + }); +} + +void DolphinView::observeCreatedDirectory(const QUrl &url) +{ + if (!m_active) { + return; + } + + // if there was no selection but a new directory was created + if (m_container->controller()->selectionManager()->hasSelection()) { + return; + } + + // select the new directory + if (!m_model->fileItem(url).isNull()) { + forceUrlsSelection(url, {url}); + return; + } + + // since this is async make sure the selection state hasn't change in the meantime + std::function condition([this]() { + return !m_container->controller()->selectionManager()->hasSelection(); + }); + + // need to wait for the item to be added to the model + selectFileOnceAvailable(url, condition); +} + void DolphinView::observeCreatedItem(const QUrl &url) { - if (m_active) { + if (!m_active) { + return; + } + + // select the new file + if (!m_model->fileItem(url).isNull()) { forceUrlsSelection(url, {url}); + return; } + + // since this is async make sure the selection state hasn't change in the meantime + auto selection = m_container->controller()->selectionManager()->selectedItems(); + std::function condition([this, selection]() { + return selection == m_container->controller()->selectionManager()->selectedItems(); + }); + + // need to wait for the item to be added to the model + selectFileOnceAvailable(url, condition); } void DolphinView::slotDirectoryRedirection(const QUrl &oldUrl, const QUrl &newUrl) -- cgit v1.3