Skip to content

Commit

Permalink
fix: Do not create new files from the palette
Browse files Browse the repository at this point in the history
The palette is just a way to navigate around, it shouldn't be able to
create new files.
Also keep the palette open if the text does not match (like VS Code).
  • Loading branch information
narnaud committed Nov 17, 2024
1 parent ba939a5 commit e1d7e28
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/gui/palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,12 @@ void Palette::clickItem(const QModelIndex &index)
const auto text = ui->lineEdit->text().mid(selector.prefix.length()).simplified();
if (text.isEmpty())
return;
selector.selectionFunc(text);
// If there is a source model (some don't have, like the line selector), we need a match with one item from the
// model. If there are no matches (=proxy model empty, no current index selected), then we do nothing.
if (m_proxyModel->sourceModel() && !ui->treeView->currentIndex().isValid())
return;
if (!selector.selectionFunc(text))
return;
} else {
m_selectors.at(m_currentSelector).selectionFunc(index.data(Qt::UserRole));
}
Expand Down Expand Up @@ -482,6 +487,7 @@ void Palette::addFileSelector()
};
auto selectFile = [](const QVariant &path) {
Core::Project::instance()->open(path.toString());
return true;
};
m_selectors.emplace_back("", std::move(fileModel), selectFile, resetFiles);
}
Expand All @@ -497,7 +503,9 @@ void Palette::addLineSelector()
textDocument->textEdit()->setFocus(Qt::OtherFocusReason);
textDocument->textEdit()->centerCursor();
}
return true;
}
return false;
};
m_selectors.emplace_back(":", nullptr, gotoLine);
}
Expand All @@ -506,6 +514,7 @@ void Palette::addScriptSelector()
{
auto runScript = [](const QVariant &fileName) {
Core::Utils::runScript(fileName.toString(), true);
return true;
};
m_selectors.emplace_back(".", std::make_unique<ScriptModel>(), runScript);
}
Expand All @@ -521,7 +530,9 @@ void Palette::addSymbolSelector()
codeDocument->selectSymbol(symbolName.toString());
codeDocument->textEdit()->setFocus(Qt::OtherFocusReason);
codeDocument->textEdit()->centerCursor();
return true;
}
return false;
};
m_selectors.emplace_back("@", std::move(symbolModel), gotoSymbol, resetSymbols);
}
Expand All @@ -534,8 +545,11 @@ void Palette::addActionSelector()

auto runAction = [](const QVariant &action) {
auto val = action.value<QAction *>();
if (val && val->isEnabled())
if (val && val->isEnabled()) {
val->trigger();
return false;
}
return true;
};
m_selectors.emplace_back(">", std::move(actionModel), runAction);
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ class Palette : public QFrame
{
QString prefix;
std::unique_ptr<QAbstractItemModel> model;
std::function<void(const QVariant &)> selectionFunc;
std::function<bool(const QVariant &)> selectionFunc;
std::function<void()> resetFunc = {};

Selector(QString prefix, std::unique_ptr<QAbstractItemModel> model,
std::function<void(const QVariant &)> selectionFunc, std::function<void()> resetFunc = {})
std::function<bool(const QVariant &)> selectionFunc, std::function<void()> resetFunc = {})
: prefix(std::move(prefix))
, model(std::move(model))
, selectionFunc(std::move(selectionFunc))
Expand Down

0 comments on commit e1d7e28

Please sign in to comment.