From 4dd98e97c1664aee7c0900829af5ef6c8ccd74a4 Mon Sep 17 00:00:00 2001
From: matt1432 <matt@nelim.org>
Date: Fri, 19 Apr 2024 20:31:17 -0400
Subject: [PATCH] fix(ags): only show one entry for lockscreen if !greetdDupe

---
 .gitignore                               |  1 +
 modules/ags/config/ts/lockscreen/main.ts | 95 ++++++++++++++----------
 modules/ags/default.nix                  | 17 ++++-
 modules/hyprland/security.nix            |  1 +
 4 files changed, 73 insertions(+), 41 deletions(-)

diff --git a/.gitignore b/.gitignore
index d4751348..a8ac590d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,4 @@ result*
 *config.js
 *icons
 *.direnv/
+modules/ags/config/ts/lockscreen/vars.ts
diff --git a/modules/ags/config/ts/lockscreen/main.ts b/modules/ags/config/ts/lockscreen/main.ts
index 35f9c74f..035f3b00 100644
--- a/modules/ags/config/ts/lockscreen/main.ts
+++ b/modules/ags/config/ts/lockscreen/main.ts
@@ -4,6 +4,9 @@ import Gdk from 'gi://Gdk?version=3.0';
 import Gtk from 'gi://Gtk?version=3.0';
 import Lock from 'gi://GtkSessionLock?version=0.1';
 
+// This file is generated by Nix
+import Vars from './vars.ts';
+
 import Separator from '../misc/separator.ts';
 
 /* Types */
@@ -18,6 +21,7 @@ const transition_duration = 1000;
 const WINDOW_MARGINS = -2;
 const ENTRY_SPACING = 20;
 const CLOCK_SPACING = 60;
+
 const bgCSS = ({ w = 1, h = 1 } = {}) => `
     border: 2px solid rgba(189, 147, 249, 0.8);
     background: rgba(0, 0, 0, 0.2);
@@ -59,7 +63,7 @@ const Clock = () => Label({ class_name: 'clock' })
             .replace('p.m.', 'PM');
     });
 
-const PasswordPrompt = (monitor: Gdk.Monitor) => {
+const PasswordPrompt = (monitor: Gdk.Monitor, visible: boolean) => {
     const rev = Box({
         css: bgCSS(),
         attribute: {
@@ -108,57 +112,68 @@ const PasswordPrompt = (monitor: Gdk.Monitor) => {
     const label = Label('Enter password:');
 
     return new Gtk.Window({
-        child: Box({
-            vertical: true,
-            vpack: 'center',
-            hpack: 'center',
-            spacing: 16,
+        child: visible ?
+            Box({
+                vertical: true,
+                vpack: 'center',
+                hpack: 'center',
+                spacing: 16,
 
-            children: [
-                Clock(),
+                children: [
+                    Clock(),
 
-                Separator(CLOCK_SPACING, { vertical: true }),
+                    Separator(CLOCK_SPACING, { vertical: true }),
 
-                Box({
-                    hpack: 'center',
-                    class_name: 'avatar',
-                }),
+                    Box({
+                        hpack: 'center',
+                        class_name: 'avatar',
+                    }),
 
-                Box({
-                    class_name: 'entry-box',
-                    vertical: true,
-                    children: [
-                        label,
+                    Box({
+                        class_name: 'entry-box',
+                        vertical: true,
+                        children: [
+                            label,
 
-                        Separator(ENTRY_SPACING, { vertical: true }),
+                            Separator(ENTRY_SPACING, { vertical: true }),
 
-                        Entry({
-                            hpack: 'center',
-                            xalign: 0.5,
-                            visibility: false,
-                            placeholder_text: 'password',
+                            Entry({
+                                hpack: 'center',
+                                xalign: 0.5,
+                                visibility: false,
+                                placeholder_text: 'password',
 
-                            on_accept: (self) => {
-                                self.sensitive = false;
+                                on_accept: (self) => {
+                                    self.sensitive = false;
 
-                                Utils.authenticate(self.text ?? '')
-                                    .then(() => unlock())
-                                    .catch((e) => {
-                                        self.text = '';
-                                        label.label = e.message;
-                                        self.sensitive = true;
-                                    });
-                            },
-                        }).on('realize', (entry) => entry.grab_focus()),
-                    ],
-                }),
-            ],
-        }),
+                                    Utils.authenticate(self.text ?? '')
+                                        .then(() => unlock())
+                                        .catch((e) => {
+                                            self.text = '';
+                                            label.label = e.message;
+                                            self.sensitive = true;
+                                        });
+                                },
+                            }).on('realize', (entry) => entry.grab_focus()),
+                        ],
+                    }),
+                ],
+            }) :
+            Box(),
     });
 };
 
+const getHyprlandMonitorDesc = (monitor: Gdk.Monitor) => {
+    const manufacturer = monitor.manufacturer?.replace(',', '');
+    const model = monitor.model?.replace(',', '');
+
+    return `desc:${manufacturer} ${model}`;
+};
+
 const createWindow = (monitor: Gdk.Monitor) => {
-    const win = PasswordPrompt(monitor);
+    const hyprDesc = getHyprlandMonitorDesc(monitor);
+    const entryVisible = Vars.mainMonitor.startsWith(hyprDesc) || Vars.dupeLockscreen;
+    const win = PasswordPrompt(monitor, entryVisible);
 
     windows.push(win);
     lock.new_surface(win, monitor);
diff --git a/modules/ags/default.nix b/modules/ags/default.nix
index 6412db28..9a5e5c41 100644
--- a/modules/ags/default.nix
+++ b/modules/ags/default.nix
@@ -3,10 +3,12 @@
   astal,
   config,
   gtk-session-lock,
+  lib,
   pkgs,
   ...
 }: let
-  inherit (config.vars) mainUser hostName;
+  inherit (lib) boolToString;
+  inherit (config.vars) mainUser hostName mainMonitor greetdDupe;
 
   flakeDir = config.environment.variables.FLAKE;
   isTouchscreen = config.hardware.sensor.iio.enable;
@@ -83,6 +85,17 @@ in {
             };
             "${agsConfigDir}/config/types/gtk-session-lock".source = pkgs.callPackage ./gtk-session-lock-types {inherit gtkSessionLock;};
             "${agsConfigDir}/config/config.js".text = configJs;
+
+            "${agsConfigDir}/config/ts/lockscreen/vars.ts".text =
+              /*
+              javascript
+              */
+              ''
+                export default {
+                    mainMonitor: '${mainMonitor}',
+                    dupeLockscreen: ${boolToString greetdDupe},
+                };
+              '';
           }
           // (import ./icons.nix {inherit pkgs agsConfigDir;});
 
@@ -119,6 +132,8 @@ in {
 
           layerrule = [
             "noanim, ^(?!win-).*"
+
+            # Lockscreen blur
             "blur, ^(blur-bg.*)"
             "ignorealpha 0.19, ^(blur-bg.*)"
           ];
diff --git a/modules/hyprland/security.nix b/modules/hyprland/security.nix
index bc1149d7..59257819 100644
--- a/modules/hyprland/security.nix
+++ b/modules/hyprland/security.nix
@@ -35,6 +35,7 @@ in {
 
     home.packages = with pkgs; [
       gnome.seahorse
+      lockPkg
     ];
 
     services.hypridle = mkIf isLaptop {