2025-09-17 12:49:05 +02:00
|
|
|
using Adw;
|
2025-09-22 15:35:31 +02:00
|
|
|
using Keychain.ViewModels;
|
2025-09-30 11:31:38 +02:00
|
|
|
using Logic;
|
2025-09-13 17:36:46 +02:00
|
|
|
|
2025-09-22 13:58:59 +02:00
|
|
|
namespace Keychain.UI;
|
|
|
|
|
|
|
|
|
|
public class MainWindow
|
|
|
|
|
{
|
|
|
|
|
public Window Window { get; }
|
|
|
|
|
private PreferencesGroup shortcutsGroup;
|
2025-09-30 19:05:53 +02:00
|
|
|
private PasswordList passwordCollection;
|
2025-09-18 14:46:25 +02:00
|
|
|
private PasswordStoreShortcutCollection shortcuts;
|
2025-09-22 13:58:59 +02:00
|
|
|
private Gtk.ToggleButton searchToggleButton;
|
|
|
|
|
private Gtk.Stack titleStack;
|
|
|
|
|
private Gtk.SearchEntry searchEntry;
|
2025-09-30 19:05:53 +02:00
|
|
|
private PreferencesGroup passwordList;
|
2025-09-22 13:58:59 +02:00
|
|
|
|
2025-09-30 11:31:38 +02:00
|
|
|
private readonly IPasswordStoreService passwordStoreService;
|
|
|
|
|
|
2025-09-30 19:05:53 +02:00
|
|
|
private const string windowId = "main_window";
|
|
|
|
|
private const string shortcutsGroupId = "shortcuts_group";
|
|
|
|
|
private const string addShortcutButtonId = "add_shortcut_button";
|
|
|
|
|
private const string searchToggleButtonId = "search_button";
|
|
|
|
|
private const string titleStackId = "title_stack";
|
|
|
|
|
private const string searchEntryId = "search_entry";
|
|
|
|
|
private const string passwordListId = "password_list";
|
2025-09-22 13:58:59 +02:00
|
|
|
|
2025-09-30 11:31:38 +02:00
|
|
|
public MainWindow(IPasswordStoreService passwordStoreService)
|
2025-09-18 14:46:25 +02:00
|
|
|
{
|
2025-09-30 11:31:38 +02:00
|
|
|
this.passwordStoreService = passwordStoreService;
|
2025-09-18 14:46:25 +02:00
|
|
|
var builder = new Gtk.Builder("Keychain.UI.MainWindow.MainWindow.xml");
|
2025-09-13 17:36:46 +02:00
|
|
|
|
2025-09-22 13:58:59 +02:00
|
|
|
|
|
|
|
|
Window = builder.GetObject(windowId) as Window;
|
|
|
|
|
if (Window == null)
|
2025-09-17 12:49:05 +02:00
|
|
|
{
|
|
|
|
|
throw new Exception("Failed to load embedded resource MainWindow.xml");
|
|
|
|
|
}
|
2025-09-18 14:46:25 +02:00
|
|
|
|
2025-09-22 13:58:59 +02:00
|
|
|
try
|
2025-09-17 12:49:05 +02:00
|
|
|
{
|
2025-09-22 13:58:59 +02:00
|
|
|
shortcutsGroup = builder.GetObject(shortcutsGroupId) as PreferencesGroup;
|
|
|
|
|
if (shortcutsGroup == null)
|
2025-09-30 19:05:53 +02:00
|
|
|
{
|
2025-09-22 13:58:59 +02:00
|
|
|
throw new Exception(shortcutsGroupId);
|
2025-09-30 19:05:53 +02:00
|
|
|
}
|
2025-09-22 13:58:59 +02:00
|
|
|
|
|
|
|
|
var addButton = builder.GetObject(addShortcutButtonId) as Gtk.Button;
|
|
|
|
|
if (addButton == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(addShortcutButtonId);
|
|
|
|
|
}
|
|
|
|
|
addButton.OnClicked += OnAddShortcutClicked;
|
|
|
|
|
|
|
|
|
|
searchToggleButton = builder.GetObject(searchToggleButtonId) as Gtk.ToggleButton;
|
|
|
|
|
if (searchToggleButton == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(searchToggleButtonId);
|
|
|
|
|
}
|
|
|
|
|
searchToggleButton.OnToggled += SetSearchBarVisible;
|
|
|
|
|
|
2025-09-18 14:46:25 +02:00
|
|
|
|
2025-09-22 13:58:59 +02:00
|
|
|
titleStack = builder.GetObject(titleStackId) as Gtk.Stack;
|
|
|
|
|
if (titleStack == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(titleStackId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
searchEntry = builder.GetObject(searchEntryId) as Gtk.SearchEntry;
|
|
|
|
|
if (searchEntry == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(searchEntryId);
|
|
|
|
|
}
|
|
|
|
|
var focusController = new Gtk.EventControllerFocus();
|
|
|
|
|
focusController.OnLeave += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
searchToggleButton.Active = false;
|
|
|
|
|
};
|
|
|
|
|
searchEntry.AddController(focusController);
|
2025-09-30 19:05:53 +02:00
|
|
|
|
|
|
|
|
passwordList = builder.GetObject(passwordListId) as PreferencesGroup;
|
|
|
|
|
if (passwordList == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(passwordListId);
|
|
|
|
|
}
|
2025-09-22 13:58:59 +02:00
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2025-09-17 12:49:05 +02:00
|
|
|
{
|
2025-09-22 13:58:59 +02:00
|
|
|
throw new Exception("Failed to load UI element with ID: " + e.Message);
|
2025-09-18 14:46:25 +02:00
|
|
|
}
|
2025-09-17 12:49:05 +02:00
|
|
|
|
2025-09-30 19:05:53 +02:00
|
|
|
// Initialize the observable collection with property binding
|
|
|
|
|
shortcuts = new PasswordStoreShortcutCollection(shortcutsGroup, passwordStoreService);
|
|
|
|
|
if (shortcuts.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
LoadDefaultShortcuts();
|
|
|
|
|
}
|
2025-09-18 14:46:25 +02:00
|
|
|
|
2025-09-30 19:05:53 +02:00
|
|
|
passwordCollection = new PasswordList(passwordList);
|
2025-09-18 15:36:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnAddShortcutClicked(object sender, EventArgs e)
|
|
|
|
|
{
|
2025-09-30 11:31:38 +02:00
|
|
|
var dialog = new AddShortcutWindow(shortcuts).Dialog;
|
2025-09-18 15:36:30 +02:00
|
|
|
dialog.Present(Window);
|
2025-09-22 13:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadDefaultShortcuts()
|
|
|
|
|
{
|
2025-09-30 11:31:38 +02:00
|
|
|
shortcuts.Add(new PasswordStoreViewModel(displayName: "Default", path: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/.password_store"));
|
2025-09-22 13:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 11:31:38 +02:00
|
|
|
// private void UpdateShortcutName(PasswordStoreViewModel shortcut, string newName)
|
|
|
|
|
// {
|
|
|
|
|
// shortcut.DisplayName = newName; // This will automatically update the UI row
|
|
|
|
|
// }
|
2025-09-22 13:58:59 +02:00
|
|
|
|
|
|
|
|
private void SetSearchBarVisible(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (searchToggleButton.Active)
|
|
|
|
|
{
|
|
|
|
|
titleStack.SetVisibleChildName("Search");
|
|
|
|
|
searchEntry.GrabFocus();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
titleStack.SetVisibleChildName("Passwords");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-13 17:36:46 +02:00
|
|
|
}
|