Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nyxt-native dialogs! #1465

Merged
merged 5 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions source/browser.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ issued by an external program or issued by Control+<button1> in a new window.")
:reader ready-p
:documentation "If non-nil, the browser is ready for operation (make
buffers, load data files, open prompt buffer, etc).")
(native-dialogs t
:type boolean
:documentation "Whether to use prompt-buffer-reliant script dialogs and file-chooser.
If nil, renderer-provided dialogs are used.")
(session-restore-prompt :always-ask
:documentation "Ask whether to restore the session.
The possible values are `:always-ask', `:always-restore' and `:never-restore'.")
Expand Down
71 changes: 71 additions & 0 deletions source/renderer-gtk.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,71 @@ See `gtk-browser's `modifier-translator' slot."
(setf (gtk:gtk-widget-size-request (message-container window))
(list -1 height)))

(defun process-file-chooser-request (web-view file-chooser-request)
(declare (ignore web-view))
(when (native-dialogs *browser*)
(gobject:g-object-ref (gobject:pointer file-chooser-request))
(run-thread
(let ((files (mapcar
#'namestring
(prompt :prompt (format
nil "File~@[s~*~] to input"
(webkit:webkit-file-chooser-request-select-multiple
file-chooser-request))
:input (or
(and
(webkit:webkit-file-chooser-request-selected-files
file-chooser-request)
(first
(webkit:webkit-file-chooser-request-selected-files
file-chooser-request)))
(namestring (uiop:getcwd)))
:sources (list (make-instance 'file-source))))))
(if files
(webkit:webkit-file-chooser-request-select-files
file-chooser-request
(cffi:foreign-alloc :string
:initial-contents (if (webkit:webkit-file-chooser-request-select-multiple
file-chooser-request)
(mapcar #'cffi:foreign-string-alloc files)
(list (cffi:foreign-string-alloc (first files))))
:count (if (webkit:webkit-file-chooser-request-select-multiple
file-chooser-request)
(length files)
1)
:null-terminated-p t))
(webkit:webkit-file-chooser-request-cancel file-chooser-request))
t))))

(defun process-script-dialog (web-view dialog)
(declare (ignore web-view))
(when (native-dialogs *browser*)
(let ((dialog (gobject:pointer dialog)))
(webkit:webkit-script-dialog-ref dialog)
(run-thread
(case (webkit:webkit-script-dialog-get-dialog-type dialog)
(:webkit-script-dialog-alert (echo (webkit:webkit-script-dialog-get-message dialog)))
(:webkit-script-dialog-prompt
(let ((text (first (handler-case
(prompt
:input (webkit:webkit-script-dialog-prompt-get-default-text dialog)
:prompt (webkit:webkit-script-dialog-get-message dialog)
:sources (list (make-instance 'prompter:raw-source)))
(nyxt-prompt-buffer-canceled (c) (declare (ignore c)) nil)))))
(if text
(webkit:webkit-script-dialog-prompt-set-text dialog text)
(progn
(webkit:webkit-script-dialog-prompt-set-text dialog (cffi:null-pointer))
(webkit:webkit-script-dialog-close dialog)))))
((:webkit-script-dialog-confirm :webkit-script-dialog-before-unload-confirm)
(webkit:webkit-script-dialog-confirm-set-confirmed
dialog (if-confirm
((webkit:webkit-script-dialog-get-message dialog))
t nil))))
(webkit:webkit-script-dialog-close dialog)
(webkit:webkit-script-dialog-unref dialog))
t)))

(define-ffi-method ffi-buffer-make ((buffer gtk-buffer))
"Initialize BUFFER's GTK web view."
(setf (gtk-object buffer) (make-web-view
Expand Down Expand Up @@ -820,6 +885,12 @@ See `gtk-browser's `modifier-translator' slot."
(gtk-object buffer) "scroll-event"
(lambda (web-view event) (declare (ignore web-view))
(on-signal-scroll-event buffer event)))
(gobject:g-signal-connect
(gtk-object buffer) "script-dialog"
#'process-script-dialog)
(gobject:g-signal-connect
(gtk-object buffer) "run-file-chooser"
#'process-file-chooser-request)
;; TLS certificate handling
(gobject:g-signal-connect
(gtk-object buffer) "load-failed-with-tls-errors"
Expand Down