Files
Keys/App/ViewModels/PasswordStoreShortcutCollection.cs

94 lines
3.0 KiB
C#
Raw Normal View History

2025-09-22 13:08:08 +02:00
namespace Keychain.ViewModels;
2025-09-17 12:49:05 +02:00
using Adw;
2025-09-17 13:30:09 +02:00
using Gtk;
2025-09-17 12:49:05 +02:00
using System.Collections.ObjectModel;
using System.Collections.Specialized;
public class PasswordStoreShortcutCollection : ObservableCollection<PasswordStoreViewModel>
2025-09-17 12:49:05 +02:00
{
private readonly PreferencesGroup shortcutsGroup;
private readonly Dictionary<PasswordStoreViewModel, ActionRow> itemToRowMap = new();
2025-09-17 12:49:05 +02:00
public PasswordStoreShortcutCollection(PreferencesGroup shortcutsGroup)
{
this.shortcutsGroup = shortcutsGroup;
CollectionChanged += OnCollectionChanged;
}
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (PasswordStoreViewModel item in e.NewItems)
2025-09-17 12:49:05 +02:00
{
var row = CreateShortcutRow(item);
itemToRowMap[item] = row;
shortcutsGroup.Add(row);
// Subscribe to property changes for reactive updates
item.PropertyChanged += (sender, args) => UpdateRowFromItem(item, ref row);
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (PasswordStoreViewModel item in e.OldItems)
2025-09-17 12:49:05 +02:00
{
if (itemToRowMap.TryGetValue(item, out var row))
{
shortcutsGroup.Remove(row);
itemToRowMap.Remove(item);
}
}
break;
case NotifyCollectionChangedAction.Reset:
foreach (var row in itemToRowMap.Values)
{
shortcutsGroup.Remove(row);
}
itemToRowMap.Clear();
break;
}
}
private ActionRow CreateShortcutRow(PasswordStoreViewModel shortcut)
2025-09-17 12:49:05 +02:00
{
var row = new ActionRow();
UpdateRowFromItem(shortcut, ref row);
2025-09-17 13:30:09 +02:00
row.SetActivatable(true);
2025-09-17 12:49:05 +02:00
row.OnActivated += (sender, args) => {
Console.WriteLine($"[DEBUG] Opening: {shortcut.Path}");
};
return row;
2025-09-17 13:30:09 +02:00
}
private void UpdateRowFromItem(PasswordStoreViewModel shortcut, ref ActionRow row)
2025-09-17 13:30:09 +02:00
{
row.SetTitle(shortcut.DisplayName);
row.SetSubtitle(shortcut.Path);
2025-09-17 12:49:05 +02:00
//Update icon
2025-09-17 13:30:09 +02:00
var existingIcon = row.GetFirstChild() as Gtk.Image;
if (existingIcon == null)
{
var icon = new Gtk.Image();
icon.SetFromIconName(shortcut.IconName);
row.AddPrefix(icon);
}
else
{
existingIcon.SetFromIconName(shortcut.IconName);
}
// Edit button
var edit = new Button();
edit.AddCssClass("flat");
edit.SetValign(Align.Center);
edit.IconName = "document-edit-symbolic";
row.AddSuffix(edit);
2025-09-17 12:49:05 +02:00
}
}