diff --git a/App/UI/MainWindow/MainWindow.cs b/App/UI/MainWindow/MainWindow.cs index 7289370..c35dfa4 100644 --- a/App/UI/MainWindow/MainWindow.cs +++ b/App/UI/MainWindow/MainWindow.cs @@ -1,38 +1,78 @@ using Adw; using Keychain.UI.ViewModels; -namespace Keychain.UI; - -public class MainWindow -{ - public Window Window { get; } - private PreferencesGroup shortcutsGroup; +namespace Keychain.UI; + +public class MainWindow +{ + public Window Window { get; } + private PreferencesGroup shortcutsGroup; private PasswordStoreShortcutCollection shortcuts; - + private Gtk.ToggleButton searchToggleButton; + private Gtk.Stack titleStack; + private Gtk.SearchEntry searchEntry; + + private readonly string windowId = "main_window"; + private readonly string shortcutsGroupId = "shortcuts_group"; + private readonly string addShortcutButtonId = "add_shortcut_button"; + private readonly string searchToggleButtonId = "search_button"; + private readonly string titleStackId = "title_stack"; + private readonly string searchEntryId = "search_entry"; + public MainWindow() { var builder = new Gtk.Builder("Keychain.UI.MainWindow.MainWindow.xml"); - var window = builder.GetObject("main_window") as Window; - if (window == null) + + Window = builder.GetObject(windowId) as Window; + if (Window == null) { throw new Exception("Failed to load embedded resource MainWindow.xml"); } - Window = window; - var group = builder.GetObject("shortcuts_group") as PreferencesGroup; - if (group == null) + try { - throw new Exception("Failed to load UI element with ID: shortcuts_group"); - } - shortcutsGroup = group; + shortcutsGroup = builder.GetObject(shortcutsGroupId) as PreferencesGroup; + if (shortcutsGroup == null) + throw new Exception(shortcutsGroupId); - var addButton = builder.GetObject("add_shortcut_button") as Gtk.Button; - if (addButton == null) - { - throw new Exception("Failed to load UI element with ID: add_shortcut_button"); + 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); + } + catch (Exception e) + { + throw new Exception("Failed to load UI element with ID: " + e.Message); } - addButton.OnClicked += OnAddShortcutClicked; // Initialize the observable collection with property binding shortcuts = new PasswordStoreShortcutCollection(shortcutsGroup); @@ -44,26 +84,39 @@ public class MainWindow { var dialog = new AddShortcutWindow().Dialog; dialog.Present(Window); - } - - public void AddShortcut(string path) - { - var newShortcut = new PasswordStoreShortcut(path:path); + } + + private void AddShortcut(string path) + { + var newShortcut = new PasswordStoreShortcut(path: path); shortcuts.Add(newShortcut); // This will automatically update the UI - } - - public void RemoveShortcut(PasswordStoreShortcut shortcut) - { + } + + private void RemoveShortcut(PasswordStoreShortcut shortcut) + { shortcuts.Remove(shortcut); // This will automatically update the UI - } - - private void LoadDefaultShortcuts() - { - shortcuts.Add(new PasswordStoreShortcut(displayName:"Default", path:Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+"/.password_store")); - } - - public void UpdateShortcutName(PasswordStoreShortcut shortcut, string newName) - { + } + + private void LoadDefaultShortcuts() + { + shortcuts.Add(new PasswordStoreShortcut(displayName: "Default", path: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/.password_store")); + } + + private void UpdateShortcutName(PasswordStoreShortcut shortcut, string newName) + { shortcut.DisplayName = newName; // This will automatically update the UI row - } + } + + private void SetSearchBarVisible(object sender, EventArgs e) + { + if (searchToggleButton.Active) + { + titleStack.SetVisibleChildName("Search"); + searchEntry.GrabFocus(); + } + else + { + titleStack.SetVisibleChildName("Passwords"); + } + } } \ No newline at end of file diff --git a/App/UI/MainWindow/MainWindow.xml b/App/UI/MainWindow/MainWindow.xml index 7ab0623..f1c2a79 100644 --- a/App/UI/MainWindow/MainWindow.xml +++ b/App/UI/MainWindow/MainWindow.xml @@ -1,126 +1,160 @@ - - - - 350 - 300 - 800 - 500 - - - max-width: 500sp - True - True - - - - - 300 - 300 - - - - Stores - - - - - - - center - - - - list-add-symbolic - - - - - - - - - - - - - - - - - - - - - - Passwords + + + + 350 + 300 + 800 + 500 + + + max-width: 500sp + True + True + + + + + 300 + 300 + + + + Stores - - + + - - - horizontal - 6 + + + center + - - system-search-symbolic - + + list-add-symbolic - - - sidebar-show-symbolic - True - False - - - - - - - - - - - Default - /home/typo/.password-store - - - center - - - - list-add-symbolic - - - - - - - Sample password - - - - - Sample password 2 - - - - + + + + + + + + + + + - - - - - + + + + Passwords + + + + + + + horizontal + 6 + + + system-search-symbolic + + + + + sidebar-show-symbolic + True + False + + + + + + + slide-up-down + + + Passwords + + + Passwords + + + + + + + Search + + + 300 + 400 + + + True + Search passwords + + + + + + + + + + + + + + + + + + Default + /home/typo/.password-store + + + center + + + + list-add-symbolic + + + + + + + Sample password + + + + + Sample password 2 + + + + + + + + + + + + + + \ No newline at end of file