Files
Keys/src/App/UI/MainWindow/MainWindow.cs

112 lines
3.5 KiB
C#

using Adw;
using Keychain.ViewModels;
using Logic;
namespace Keychain.UI;
public partial class MainWindow
{
public Window Window { get; private set; }
private PasswordList passwordCollection;
private PreferencesGroup passwordList;
private Gtk.ToggleButton searchToggleButton;
private Gtk.Stack titleStack;
private Gtk.SearchEntry searchEntry;
private const string windowId = "main_window";
private const string searchToggleButtonId = "search_button";
private const string titleStackId = "title_stack";
private const string searchEntryId = "search_entry";
private const string passwordListId = "password_list";
public MainWindow(IPasswordStoreService passwordStoreService)
{
this.passwordStoreService = passwordStoreService;
BindUIElements();
// Initialize the observable collection with property binding
shortcuts = new PasswordStoreShortcutCollection(shortcutsGroup, passwordStoreService);
if (shortcuts.Count == 0)
{
LoadDefaultShortcuts();
}
passwordCollection = new PasswordList(passwordList);
}
private void SetSearchBarVisible(object sender, EventArgs e)
{
if (searchToggleButton.Active)
{
titleStack.SetVisibleChildName("Search");
searchEntry.GrabFocus();
}
else
{
titleStack.SetVisibleChildName("Passwords");
}
}
private void BindUIElements()
{
var builder = new Gtk.Builder("Keychain.UI.MainWindow.MainWindow.xml");
Window = builder.GetObject(windowId) as Window;
if (Window == null)
{
throw new Exception("Failed to load embedded resource MainWindow.xml");
}
try
{
shortcutsGroup = builder.GetObject(shortcutsGroupId) as PreferencesGroup;
if (shortcutsGroup == null)
{
throw new Exception(shortcutsGroupId);
}
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;
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);
passwordList = builder.GetObject(passwordListId) as PreferencesGroup;
if (passwordList == null)
{
throw new Exception(passwordListId);
}
}
catch (Exception e)
{
throw new Exception("Failed to load UI element with ID: " + e.Message);
}
}
}