Fix New…/Open… window-creation/substition

Window-creation code has now been refactored.
Open…-ing a new vCard will, in a blank window,
populate said window; otherwise, a new one is
made.
This commit is contained in:
Jaidyn Ann 2024-02-12 22:46:37 -06:00
parent 205576d973
commit bf718e72c4

63
contact.scm Normal file → Executable file
View File

@ -35,8 +35,9 @@
qt-light) qt-light)
(define qt-app #f) ;; The <qt-application> object. (define *qt-app* #f) ;; The <qt-application> object.
(define qt-win #f) ;; The <qt-window> object. (define *qt-win* #f) ;; The <qt-window> object.
(define *vcard-pathname* #f) ;; Path to current vCard file.
;; Start & run the application. ;; Start & run the application.
@ -53,17 +54,21 @@
(thread-join! qt-thread)))) (thread-join! qt-thread))))
(define (last-free-arg cli-args)
(condition-case (last (car cli-args)) (var () #f)))
;; Set up some global variables (for easier live REPL use), prepare the QT app. ;; Set up some global variables (for easier live REPL use), prepare the QT app.
(define (init-qt cli-args) (define (init-qt cli-args)
(set! qt-app (qt:init)) (set! *qt-app* (qt:init))
(set! qt-win (create-window)) (set! *qt-win* (create-window))
(init-window qt-win) (init-window *qt-win*)
;; Kick off the QT thread, then open the cli free-arg vCard file, if provided. ;; Kick off the QT thread, then open the cli free-arg vCard file, if provided.
;; That is, like `$ contact freeArgFile.vcf`. ;; That is, like `$ contact freeArgFile.vcf`.
(let [(qt-thread (thread-start! qt-loop)) (let [(qt-thread (thread-start! qt-loop))
(last-free-arg (condition-case (last (car cli-args)) (var () #f)))] (last-arg (last-free-arg cli-args))]
(if (and last-free-arg (string? last-free-arg) (file-exists? last-free-arg)) (when (and (string? last-arg) (file-exists? last-arg))
(open-vcard-file qt-win last-free-arg)) (open-vcard-file *qt-win* last-arg))
qt-thread)) qt-thread))
@ -167,16 +172,7 @@
(if menu-file-new (if menu-file-new
(qt:connect (qt:connect
menu-file-new "triggered()" menu-file-new "triggered()"
(qt:receiver (qt:receiver create-new-window)))
(lambda ()
(let [(program-args
;; If we opened a pre-existing contact (last arg is a file),
;; remove that argument.
(drop-right (cdr (argv))
(if (file-exists? (last (argv)))
1
0)))]
(process-run (executable-pathname) program-args))))))
;; If they want to open a contact through the Open… dialogue, we should open ;; If they want to open a contact through the Open… dialogue, we should open
;; the contact in a new window. ;; the contact in a new window.
(if menu-file-open (if menu-file-open
@ -188,18 +184,35 @@
"/Contacts")) "/Contacts"))
(selected-file (qt:get-open-filename (selected-file (qt:get-open-filename
"Select a contact file to open…" "Select a contact file to open…"
contacts-dir)) contacts-dir))]
(program-args (append (cdr (argv)) (list selected-file)))]
(if (not (string-null? selected-file)) (if (not (string-null? selected-file))
(if *vcard-pathname*
(create-new-window selected-file)
(open-vcard-file window selected-file))))))))))
;; Executes a new instance of the program; optionally, with a contact-files
;; pathname as a new argument.
;; Functionally, opens a new contacts window.
(define (create-new-window . pathname)
(let* [(pathname (optional pathname #f))
(clean-cli-args
(drop-right (cdr (argv))
(length (command-line-arguments))))
(program-args
(if pathname
(append clean-cli-args (list pathname))
clean-cli-args))]
(process-run (process-run
(executable-pathname) (executable-pathname)
program-args))))))))) program-args)))
;; Parse a vCard file and populate the windows forms with its contents. ;; Parse a vCard file and populate the windows forms with its contents.
(define (open-vcard-file window file) (define (open-vcard-file window file)
(if (and (file-exists? file) (when (and (file-exists? file)
(file-readable? file)) (file-readable? file))
(set! *vcard-pathname* file)
(thread-start! (lambda () (populate-with-vcard (thread-start! (lambda () (populate-with-vcard
window window
(with-input-from-file file (with-input-from-file file
@ -240,8 +253,10 @@
[(and (eq? (car property) 'PHOTO) [(and (eq? (car property) 'PHOTO)
(list? (last property))) (list? (last property)))
(let* [(avatar (qt:find window "avatarLabel")) (let* [(avatar (qt:find window "avatarLabel"))
(old-pixmap (if avatar (qt:property avatar "pixmap"))) (old-pixmap
[new-pixmap (if avatar (u8vector->pixmap (cadr (last property))))]] (if avatar (qt:property avatar "pixmap")))
[new-pixmap
(if avatar (u8vector->pixmap (cadr (last property))))]]
(when avatar (when avatar
(set! (qt:property avatar "pixmap") new-pixmap)))]))) (set! (qt:property avatar "pixmap") new-pixmap)))])))
vcard-alist)) vcard-alist))