Skip to content

Commit d358b02

Browse files
committed
feat: add fav toggle to commands in settings
1 parent 01cd7cb commit d358b02

8 files changed

Lines changed: 83 additions & 2 deletions

File tree

src/server/src/config/config.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct ProviderItemData {
2525

2626
struct ProviderData {
2727
std::optional<bool> enabled;
28+
std::optional<bool> favorite;
2829
std::optional<glz::generic::object_t> preferences;
2930
std::map<std::string, ProviderItemData> entrypoints;
3031
};
@@ -53,6 +54,7 @@ template <> struct Partial<ThemeConfig> {
5354

5455
template <> struct Partial<ProviderData> {
5556
std::optional<bool> enabled;
57+
std::optional<bool> favorite;
5658
std::optional<glz::generic::object_t> preferences;
5759
std::optional<std::map<std::string, ProviderItemData>> entrypoints;
5860
};

src/server/src/qml/extension-settings-model.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ QVariant ExtensionSettingsModel::data(const QModelIndex &index, int role) const
4040
return e.indent;
4141
case EnabledRole:
4242
return e.enabled;
43+
case FavoriteRole:
44+
return e.favorite;
4345
case AliasRole:
4446
return e.alias;
4547
case EntrypointIdRole:
@@ -64,6 +66,7 @@ QHash<int, QByteArray> ExtensionSettingsModel::roleNames() const {
6466
{IsProviderRole, "isProvider"},
6567
{IndentRole, "indent"},
6668
{EnabledRole, "enabled"},
69+
{FavoriteRole, "favorite"},
6770
{AliasRole, "alias"},
6871
{EntrypointIdRole, "entrypointId"},
6972
{ExpandedRole, "expanded"},
@@ -168,6 +171,26 @@ void ExtensionSettingsModel::setEnabled(int row, bool value) {
168171
if (row == m_selectedRow) emit selectedChanged();
169172
}
170173

174+
void ExtensionSettingsModel::setFavorite(int row, bool value) {
175+
if (row < 0 || std::cmp_greater_equal(row, m_visibleIndices.size())) return;
176+
auto &e = m_allEntries[m_visibleIndices[row]];
177+
auto *manager = ServiceRegistry::instance()->rootItemManager();
178+
179+
if (e.isProvider) {
180+
manager->setProviderFavorite(e.providerId, value);
181+
e.favorite = value;
182+
auto idx = index(row);
183+
emit dataChanged(idx, idx, {FavoriteRole});
184+
} else {
185+
manager->setItemAsFavorite(e.entrypointId, value);
186+
e.favorite = value;
187+
auto idx = index(row);
188+
emit dataChanged(idx, idx, {FavoriteRole});
189+
}
190+
191+
if (row == m_selectedRow) emit selectedChanged();
192+
}
193+
171194
void ExtensionSettingsModel::setAlias(int row, const QString &alias) {
172195
if (row < 0 || std::cmp_greater_equal(row, m_visibleIndices.size())) return;
173196
auto &e = m_allEntries[m_visibleIndices[row]];
@@ -309,6 +332,7 @@ void ExtensionSettingsModel::rebuild(const QString &filter) {
309332
ie.isProvider = false;
310333
ie.indent = 1;
311334
ie.enabled = metadata.enabled;
335+
ie.favorite = metadata.favorite;
312336
ie.alias = QString::fromStdString(metadata.alias.value_or(""));
313337
ie.shortcut = QString::fromStdString(metadata.shortcut.value_or(""));
314338
ie.entrypointId = item->uniqueId();
@@ -395,8 +419,8 @@ void ExtensionSettingsModel::loadCommandsForProvider(const QString &providerId)
395419
const auto &e = m_allEntries[j];
396420
auto *item = manager->findItemById(e.entrypointId);
397421
bool const hasPrefs = item && !item->preferences().empty();
398-
commands.push_back({e.name, e.type, e.iconSource, e.description, e.enabled, hasPrefs, e.alias,
399-
QString::fromStdString(e.entrypointId), e.shortcut});
422+
commands.push_back({e.name, e.type, e.iconSource, e.description, e.enabled, e.favorite, hasPrefs,
423+
e.alias, QString::fromStdString(e.entrypointId), e.shortcut});
400424
}
401425
break;
402426
}
@@ -437,6 +461,14 @@ void ExtensionSettingsModel::setEnabledByEntrypointId(const QString &id, bool va
437461
}
438462
}
439463

464+
void ExtensionSettingsModel::setFavoriteByEntrypointId(const QString &id, bool value) {
465+
int const row = findVisibleEntryByEntrypointId(id);
466+
if (row >= 0) {
467+
setFavorite(row, value);
468+
m_commandModel->setFavorite(id, value);
469+
}
470+
}
471+
440472
void ExtensionSettingsModel::setAliasByEntrypointId(const QString &id, const QString &alias) {
441473
int const row = findVisibleEntryByEntrypointId(id);
442474
if (row >= 0) {
@@ -473,6 +505,7 @@ void ExtensionSettingsModel::loadCommandPreferences(const QString &entrypointId)
473505
for (auto &e : m_allEntries) {
474506
if (!e.isProvider && QString::fromStdString(e.entrypointId) == entrypointId) {
475507
auto *item = manager->findItemById(e.entrypointId);
508+
476509
m_cmdPrefModel->load(e.entrypointId, item ? item->preferences() : std::vector<Preference>{});
477510
return;
478511
}

src/server/src/qml/extension-settings-model.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ExtensionSettingsModel : public QAbstractListModel {
4040
IsProviderRole,
4141
IndentRole,
4242
EnabledRole,
43+
FavoriteRole,
4344
AliasRole,
4445
EntrypointIdRole,
4546
ExpandedRole,
@@ -72,13 +73,15 @@ class ExtensionSettingsModel : public QAbstractListModel {
7273
Q_INVOKABLE void setFilter(const QString &text);
7374
Q_INVOKABLE void select(int row);
7475
Q_INVOKABLE void setEnabled(int row, bool value);
76+
Q_INVOKABLE void setFavorite(int row, bool value);
7577
Q_INVOKABLE void setAlias(int row, const QString &alias);
7678
Q_INVOKABLE void toggleExpanded(int row);
7779
Q_INVOKABLE void moveUp();
7880
Q_INVOKABLE void moveDown();
7981
Q_INVOKABLE void activate();
8082
Q_INVOKABLE void selectProviderById(const QString &providerId);
8183
Q_INVOKABLE void setEnabledByEntrypointId(const QString &id, bool value);
84+
Q_INVOKABLE void setFavoriteByEntrypointId(const QString &id, bool value);
8285
Q_INVOKABLE void setAliasByEntrypointId(const QString &id, const QString &alias);
8386
Q_INVOKABLE void setShortcutByEntrypointId(const QString &id, const QString &shortcut);
8487
Q_INVOKABLE void clearShortcutByEntrypointId(const QString &id);
@@ -92,6 +95,7 @@ class ExtensionSettingsModel : public QAbstractListModel {
9295
QString description;
9396
bool isProvider;
9497
bool enabled;
98+
bool favorite;
9599
QString alias;
96100
QString shortcut;
97101
EntrypointId entrypointId;

src/server/src/qml/provider-command-model.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ QVariant ProviderCommandModel::data(const QModelIndex &index, int role) const {
2020
return cmd.iconSource;
2121
case EnabledRole:
2222
return cmd.enabled;
23+
case FavoriteRole:
24+
return cmd.favorite;
2325
case AliasRole:
2426
return cmd.alias;
2527
case EntrypointIdRole:
@@ -40,6 +42,7 @@ QHash<int, QByteArray> ProviderCommandModel::roleNames() const {
4042
{TypeRole, "type"},
4143
{IconSourceRole, "iconSource"},
4244
{EnabledRole, "enabled"},
45+
{FavoriteRole, "favorite"},
4346
{AliasRole, "alias"},
4447
{EntrypointIdRole, "entrypointId"},
4548
{DescriptionRole, "description"},
@@ -106,6 +109,20 @@ bool ProviderCommandModel::setEnabled(const QString &entrypointId, bool value) {
106109
return false;
107110
}
108111

112+
bool ProviderCommandModel::setFavorite(const QString &entrypointId, bool value) {
113+
for (int i = 0; std::cmp_less(i, m_allCommands.size()); ++i) {
114+
if (m_allCommands[i].entrypointId != entrypointId) continue;
115+
m_allCommands[i].favorite = value;
116+
int const row = visibleRowFor(i);
117+
if (row >= 0) {
118+
auto idx = index(row);
119+
emit dataChanged(idx, idx, {FavoriteRole});
120+
}
121+
return true;
122+
}
123+
return false;
124+
}
125+
109126
void ProviderCommandModel::setAllEnabled(bool value) {
110127
for (auto &cmd : m_allCommands) {
111128
cmd.enabled = value;

src/server/src/qml/provider-command-model.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ProviderCommandModel : public QAbstractListModel {
1919
TypeRole,
2020
IconSourceRole,
2121
EnabledRole,
22+
FavoriteRole,
2223
AliasRole,
2324
EntrypointIdRole,
2425
DescriptionRole,
@@ -32,6 +33,7 @@ class ProviderCommandModel : public QAbstractListModel {
3233
QString iconSource;
3334
QString description;
3435
bool enabled;
36+
bool favorite;
3537
bool hasPreferences;
3638
QString alias;
3739
QString entrypointId;
@@ -51,6 +53,7 @@ class ProviderCommandModel : public QAbstractListModel {
5153
void clear();
5254

5355
bool setEnabled(const QString &entrypointId, bool value);
56+
bool setFavorite(const QString &entrypointId, bool value);
5457
void setAllEnabled(bool value);
5558
bool setAlias(const QString &entrypointId, const QString &alias);
5659
bool setShortcut(const QString &entrypointId, const QString &shortcut);

src/server/src/qml/qml/ExtensionSettingsPage.qml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Item {
156156
required property string iconSource
157157
required property string description
158158
required property bool enabled
159+
required property bool favorite
159160
required property string alias
160161
required property string entrypointId
161162
required property bool hasPreferences
@@ -271,6 +272,20 @@ Item {
271272
onClicked: root.extModel.setEnabledByEntrypointId(cmdDelegate.entrypointId, !cmdDelegate.enabled)
272273
}
273274
}
275+
276+
// Favorite toggle
277+
ViciImage {
278+
source: cmdDelegate.favorite ? Img.builtin("star").withFillColor(Theme.accent) : Img.builtin("star-disabled").withFillColor(Theme.textMuted)
279+
// source: Img.builtin(cmdDelegate.favorite ? "star" : "star-disabled").withFillColor(Theme.textMuted)
280+
Layout.preferredWidth: 16
281+
Layout.preferredHeight: 16
282+
283+
MouseArea {
284+
anchors.fill: parent
285+
cursorShape: Qt.PointingHandCursor
286+
onClicked: root.extModel.setFavoriteByEntrypointId(cmdDelegate.entrypointId, !cmdDelegate.favorite)
287+
}
288+
}
274289
}
275290
}
276291

src/server/src/services/root-item-manager/root-item-manager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,11 @@ bool RootItemManager::disableItem(const EntrypointId &id) { return setItemEnable
532532

533533
bool RootItemManager::enableItem(const EntrypointId &id) { return setItemEnabled(id, true); }
534534

535+
bool RootItemManager::setProviderFavorite(const QString &providerId, bool value) {
536+
m_cfg.mergeProviderWithUser(providerId.toStdString(), {.favorite = value});
537+
return true;
538+
}
539+
535540
std::vector<RootProvider *> RootItemManager::providers() const {
536541
std::vector<RootProvider *> providers;
537542

src/server/src/services/root-item-manager/root-item-manager.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ class RootItemManager : public QObject {
302302

303303
bool enableItem(const EntrypointId &id);
304304

305+
bool setProviderFavorite(const QString &providerId, bool value);
306+
305307
std::vector<RootProvider *> providers() const;
306308
std::vector<ExtensionRootProvider *> extensions() const;
307309

0 commit comments

Comments
 (0)