Skip to content

Commit

Permalink
Add Command Palette UI #214
Browse files Browse the repository at this point in the history
  • Loading branch information
rodlie committed Jul 25, 2024
1 parent b9e060c commit f83306c
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/app/friction.qss
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,13 @@ QTabWidget#TabWidgetWide QTabBar::tab:bottom {
QTabWidget#TabWidgetWide QTabBar {
border: 0;
}

QLineEdit#CommandPaletteInput {
border-radius: 10%;
border: 2px solid %4;
padding: .5em;
}

QListWidget#CommandPaletteSuggestions {
border: 2px solid %2;
}
2 changes: 2 additions & 0 deletions src/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(
SOURCES
dialogs/adjustscenedialog.cpp
dialogs/applyexpressiondialog.cpp
dialogs/commandpalette.cpp
dialogs/durationrectsettingsdialog.cpp
dialogs/exportsvgdialog.cpp
dialogs/qrealpointvaluedialog.cpp
Expand Down Expand Up @@ -104,6 +105,7 @@ set(
ui_global.h
dialogs/adjustscenedialog.h
dialogs/applyexpressiondialog.h
dialogs/commandpalette.h
dialogs/durationrectsettingsdialog.h
dialogs/exportsvgdialog.h
dialogs/qrealpointvaluedialog.h
Expand Down
109 changes: 109 additions & 0 deletions src/ui/dialogs/commandpalette.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
#
# Friction - https://friction.graphics
#
# Copyright (c) Friction contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See 'README.md' for more information.
#
*/

#include "commandpalette.h"

#include <QVBoxLayout>
#include <QKeyEvent>

#include "Private/esettings.h"

CommandPalette::CommandPalette(QWidget *parent)
: QDialog(parent)
, mUserInput(nullptr)
, mSuggestions(nullptr)
{
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
setWindowTitle(tr("Command Palette"));

setMinimumWidth(350);

const auto lay = new QVBoxLayout(this);

mSuggestions = new QListWidget(this);
mUserInput = new QLineEdit(this);

lay->addWidget(mUserInput);
lay->addWidget(mSuggestions);

mSuggestions->setObjectName("CommandPaletteSuggestions");
mSuggestions->setMinimumHeight(100);
mSuggestions->hide();

mUserInput->setObjectName("CommandPaletteInput");
mUserInput->setPlaceholderText(tr("Search for action ..."));
mUserInput->installEventFilter(this);
mUserInput->setFocus();

connect(mUserInput, &QLineEdit::textChanged, this, [this]() {
mSuggestions->clear();
mSuggestions->hide();
if (mUserInput->text().isEmpty()) { return; }
const auto actions = eSettings::instance().fCommandPalette;
int items = 0;
for (int i = 0; i < actions.count(); i++) {
const auto act = actions.at(i);
if (!act->text().startsWith(mUserInput->text(), Qt::CaseInsensitive)) { continue; }
const auto item = new QListWidgetItem(act->text(), mSuggestions);
item->setData(Qt::UserRole, i);
mSuggestions->addItem(item);
items++;
}
if (mSuggestions->isHidden() && items > 0) { mSuggestions->show(); }
});

connect(mUserInput, &QLineEdit::returnPressed, this, [this]() {
if (mSuggestions->count() < 1) { return; }
const auto index = mSuggestions->item(0)->data(Qt::UserRole).toInt();
const auto act = eSettings::instance().fCommandPalette.at(index);
if (!act) { return; }
act->trigger();
close();
});

connect(mSuggestions, &QListWidget::itemActivated, this, [this](const auto item) {
const auto index = item->data(Qt::UserRole).toInt();
const auto act = eSettings::instance().fCommandPalette.at(index);
if (!act) { return; }
act->trigger();
close();
});
}

bool CommandPalette::eventFilter(QObject *obj, QEvent *e)
{
if (obj == mUserInput) {
if (e->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(e);
if (keyEvent->key() == Qt::Key_Down) {
mSuggestions->setFocus();
return true;
}
}
return false;
}
return QDialog::eventFilter(obj, e);
}
49 changes: 49 additions & 0 deletions src/ui/dialogs/commandpalette.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
#
# Friction - https://friction.graphics
#
# Copyright (c) Friction contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See 'README.md' for more information.
#
*/

#ifndef COMMANDPALETTE_H
#define COMMANDPALETTE_H

#include "ui_global.h"

#include <QDialog>
#include <QLineEdit>
#include <QListWidget>
#include <QListWidgetItem>
#include <QList>
#include <QEvent>

class UI_EXPORT CommandPalette : public QDialog
{
public:
explicit CommandPalette(QWidget *parent = nullptr);

private:
QLineEdit *mUserInput;
QListWidget *mSuggestions;

protected:
bool eventFilter(QObject* obj, QEvent *e) override;
};

#endif // COMMANDPALETTE_H

0 comments on commit f83306c

Please sign in to comment.