diff --git a/src/filewidgets/knewfilemenu.h b/src/filewidgets/knewfilemenu.h --- a/src/filewidgets/knewfilemenu.h +++ b/src/filewidgets/knewfilemenu.h @@ -67,6 +67,9 @@ * @note If you want the "Create directory..." action shortcut to show up next to its text, * make sure to have an action with name "create_dir" (and shortcut set) in @p collection. * This will only work with KIO >= 5.27. + * From KIO >= 5.52, an action named "create_file" (and shortcut set) in @p collection + * will be linked to the creation of the first file template (either from XDG_TEMPLATES_DIR + * or from :/kio5/newfile-templates) */ KNewFileMenu(KActionCollection *collection, const QString &name, QObject *parent); @@ -148,6 +151,17 @@ */ void createDirectory(); + /** + * Call this to create a new file as if the user had done it using + * a popupmenu. This is useful to make sure that creating a directory with + * a key shortcut (e.g. Shift-F10) triggers the exact same code as when using + * the New menu. + * Requirements: call setPopupFiles first, and keep this KNewFileMenu instance + * alive (the copy is async). + * @since 5.52 + */ + void createFile(); + Q_SIGNALS: /** * Emitted once the file (or symlink) @p url has been successfully created diff --git a/src/filewidgets/knewfilemenu.cpp b/src/filewidgets/knewfilemenu.cpp --- a/src/filewidgets/knewfilemenu.cpp +++ b/src/filewidgets/knewfilemenu.cpp @@ -242,6 +242,7 @@ : m_menuItemsVersion(0), m_modal(true), m_viewShowsHiddenFiles(false), + m_firstFileEntry(nullptr), q(qq) {} @@ -372,6 +373,8 @@ QString m_text; bool m_viewShowsHiddenFiles; + KNewFileMenuSingleton::Entry *m_firstFileEntry; + KNewFileMenu *q; KNewFileMenuCopyData m_copyData; @@ -749,6 +752,20 @@ menu->addAction(act); } } else { + if (!m_firstFileEntry) { + m_firstFileEntry = &entry; + // If there is a shortcut available in the action collection, use it. + QAction *act2 = m_actionCollection->action(QStringLiteral("create_file")); + if (act2) { + act->setShortcuts(act2->shortcuts()); + // Both actions have now the same shortcut, so this will prevent the "Ambiguous shortcut detected" dialog. + act->setShortcutContext(Qt::WidgetShortcut); + // We also need to react to shortcut changes. + QObject::connect(act2, &QAction::changed, act, [=]() { + act->setShortcuts(act2->shortcuts()); + }); + } + } menu->addAction(act); } } @@ -1226,6 +1243,20 @@ lineEdit->setFocus(); } +void KNewFileMenu::createFile() +{ + if (d->m_popupFiles.isEmpty()) { + return; + } + + checkUpToDate(); + if (!d->m_firstFileEntry) { + return; + } + + d->executeRealFileOrDir(*d->m_firstFileEntry); +} + bool KNewFileMenu::isModal() const { return d->m_modal;