Skip to content

Commit 1f9a6bf

Browse files
committed
Implement panel sort order for gnome extension
1 parent 394a726 commit 1f9a6bf

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

gnome-extension/extension.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,29 +352,42 @@ const RabbitForexIndicator = GObject.registerClass(
352352
}
353353

354354
_updatePanelLabel() {
355-
const panelItems = [];
356355
const maxPanelItems = this._settings.get_int("max-panel-items");
357356
const showCurrencyInPanel = this._settings.get_boolean("show-currency-in-panel");
358357
const panelSeparator = this._settings.get_string("panel-separator");
359358
const panelItemTemplate = this._settings.get_string("panel-item-template");
359+
const sortOrder = this._settings.get_string("panel-sort-order");
360+
361+
const allPanelItems = [];
360362

361363
for (const category of CATEGORIES) {
362364
if (!this._rates[category]) continue;
363365

364366
const showInPanel = this._getPanelCategory(category);
365367

366368
for (const symbol of showInPanel) {
367-
if (panelItems.length >= maxPanelItems) break;
368-
369369
if (this._rates[category][symbol] !== undefined) {
370370
const rate = this._rates[category][symbol];
371+
const price = this._getRawPrice(rate, category);
371372
const formattedRate = this._formatPanelRate(rate, category, symbol, showCurrencyInPanel);
372373
const panelItem = panelItemTemplate.replace("{symbol}", symbol).replace("{rate}", formattedRate);
373-
panelItems.push(panelItem);
374+
allPanelItems.push({ symbol, price, panelItem });
374375
}
375376
}
376377
}
377378

379+
if (sortOrder === "symbol-asc") {
380+
allPanelItems.sort((a, b) => a.symbol.localeCompare(b.symbol));
381+
} else if (sortOrder === "symbol-desc") {
382+
allPanelItems.sort((a, b) => b.symbol.localeCompare(a.symbol));
383+
} else if (sortOrder === "price-asc") {
384+
allPanelItems.sort((a, b) => a.price - b.price);
385+
} else if (sortOrder === "price-desc") {
386+
allPanelItems.sort((a, b) => b.price - a.price);
387+
}
388+
389+
const panelItems = allPanelItems.slice(0, maxPanelItems).map((item) => item.panelItem);
390+
378391
if (panelItems.length === 0) {
379392
this._panelLabel.text = "Rabbit Forex";
380393
} else {

gnome-extension/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Monitor exchange rates for fiat currencies, precious metals, cryptocurrencies and stocks.\nClick on any rate to copy it to clipboard.",
44
"uuid": "[email protected]",
55
"shell-version": ["47", "48", "49"],
6-
"version-name": "1.3.0",
6+
"version-name": "1.4.0",
77
"settings-schema": "org.gnome.shell.extensions.rabbitforex",
88
"url": "https://github.com/Rabbit-Company/RabbitForexAPI",
99
"donations": {

gnome-extension/prefs.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ const SYMBOL_POSITIONS = [
212212
{ id: "after", label: "After price (100 $)" },
213213
];
214214

215+
const PANEL_SORT_OPTIONS = [
216+
{ id: "none", label: "No sorting" },
217+
{ id: "symbol-asc", label: "Symbol (A → Z)" },
218+
{ id: "symbol-desc", label: "Symbol (Z → A)" },
219+
{ id: "price-asc", label: "Price (Low → High)" },
220+
{ id: "price-desc", label: "Price (High → Low)" },
221+
];
222+
215223
export default class RabbitForexPreferences extends ExtensionPreferences {
216224
fillPreferencesWindow(window) {
217225
const settings = this.getSettings();
@@ -290,6 +298,28 @@ export default class RabbitForexPreferences extends ExtensionPreferences {
290298
});
291299
panelGroup.add(maxPanelRow);
292300

301+
// Panel sort order dropdown
302+
const sortModel = new Gtk.StringList();
303+
for (const option of PANEL_SORT_OPTIONS) {
304+
sortModel.append(option.label);
305+
}
306+
307+
const sortRow = new Adw.ComboRow({
308+
title: "Sort Order",
309+
subtitle: "How to sort items displayed in the panel",
310+
model: sortModel,
311+
});
312+
313+
const currentSort = settings.get_string("panel-sort-order");
314+
const sortIndex = PANEL_SORT_OPTIONS.findIndex((o) => o.id === currentSort);
315+
sortRow.selected = sortIndex >= 0 ? sortIndex : 0;
316+
317+
sortRow.connect("notify::selected", () => {
318+
const selected = PANEL_SORT_OPTIONS[sortRow.selected].id;
319+
settings.set_string("panel-sort-order", selected);
320+
});
321+
panelGroup.add(sortRow);
322+
293323
// Panel separator
294324
const separatorRow = new Adw.EntryRow({
295325
title: "Panel Separator",

gnome-extension/schemas/org.gnome.shell.extensions.rabbitforex.gschema.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
<description>Maximum number of rates to display in the panel</description>
3636
</key>
3737

38+
<key name="panel-sort-order" type="s">
39+
<default>'none'</default>
40+
<summary>Panel sort order</summary>
41+
<description>How to sort panel items: none (as configured), symbol-asc, symbol-desc, price-asc, price-desc</description>
42+
</key>
43+
3844
<key name="panel-separator" type="s">
3945
<default>' | '</default>
4046
<summary>Panel separator</summary>

0 commit comments

Comments
 (0)