Skip to content

Unlocking via Kee ignores "Minimize main window after opening a database" #158

Description

@blacksun80

Problem

With the KeePass option "Minimize main window after opening a database" enabled, unlocking the database through Kee (e.g. the "Open KeePass" button while the database is locked) leaves the KeePass main window open in the foreground. Unlocking the same database directly in KeePass (tray icon, hotkey) minimizes the window as expected.

Steps to reproduce

  1. KeePass options → Interface: enable "Minimize main window after opening a database" and "Minimize to tray instead of taskbar".
  2. Lock the workspace (KeePass sits in the tray).
  3. In Kee, click the KeePass button and unlock the database.
  4. Expected: KeePass goes back to the tray after unlocking. Actual: the main window stays open and focused.

Without "Minimize to tray" the window briefly minimizes and is then restored again.

Cause

Both effects come from PromptUserToOpenDb in KeePassRPCService.cs:

  1. After the unlock, the window state is set back explicitly:

    // Set the program state back to what is was unless the user has
    // configured "lock on minimise" in which case we always set it to Normal
    if (!Program.Config.Security.WorkspaceLocking.LockOnWindowMinimize)
    {
        minimised = false;
        trayed = false;
    }
    
    Program.MainForm.WindowState = minimised ? FormWindowState.Minimized : FormWindowState.Normal;

    This overrides the minimize that MainForm.OpenDatabase performs when MinimizeAfterOpeningDatabase is set. (Side note: the comment describes the opposite of what the condition does.)

  2. When KeePass is in the tray, Native.AttachToActiveAndBringToForeground shows the window via the native ShowWindow(SW_RESTORE). That does not update Form.Visible, which stays false. While Visible is false, WinForms ignores WindowState changes, so KeePass's own minimize after opening the database has no effect either.

I confirmed the sequence with temporary logging of the window state and Form.Visible (Windows 11, KeePass 2.61.1, KeePassRPC 2.0.2, KeePassWinHello for unlocking). The unlock prompt runs synchronously inside the native restore, so the database is already open when PromptUserToOpenDb finishes.

Proposed fix

At the end of PromptUserToOpenDb, if the database was closed or locked at the start and is open now, and the user has enabled MinimizeAfterOpeningDatabase, minimize the main window (deferred with BeginInvoke, after the state restore). If Form.Visible is out of sync, set it to true first so WinForms applies the state change. KeePass's own OnFormResize then decides between tray and taskbar as usual.

Nothing changes for users without that option. The window is also left alone when the database was already open (showing KeePass stays possible), when the unlock is cancelled, and when minimizing would lock the workspace again (LockOnWindowMinimize, or LockOnWindowMinimizeToTray together with MinimizeToTray). No new setting, no platform-specific code.

I have a small patch (one file, ~30 added lines) and can open a pull request if you're interested.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions