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

Feature/web preferences #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion cl-electron.asd
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
((:file "package")
(:file "condition" :depends-on ("package"))
(:file "core" :depends-on ("package"))
(:file "web-preferences" :depends-on ("package"))
(:file "window" :depends-on ("package" "core"))
(:file "view" :depends-on ("package" "core"))
(:file "view" :depends-on ("package" "core" "web-preferences"))
(:file "web-contents" :depends-on ("package" "core"))
(:file "protocol" :depends-on ("package" "core")))))
:in-order-to ((test-op (test-op "cl-electron/tests"))))
Expand Down
6 changes: 4 additions & 2 deletions examples/example-web-preferences.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
(setf electron:*interface* (make-instance 'electron:interface))
(electron:launch)
;; Note: WebPreferences can only be set during object creation!
(let ((win (make-instance 'electron:window)))
(make-instance 'example-view :window win :options "{webPreferences: {images: false}}")
(let ((win (make-instance 'electron:window))
(preferences (make-instance 'electron:web-preferences)))
(electron:add-preference preferences "images" nil)
(make-instance 'example-view :window win :web-preferences preferences)
win))
13 changes: 8 additions & 5 deletions source/core.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,15 @@ Particularly useful to avoid errors on already terminated threads."
:type (or web-contents null)
:documentation "The `web-contents' object bound to the view.")
(options
""
nil
:accessor nil
:type (or string null)
:documentation "A string that specifies the views's behavior.")
(web-preferences
nil
:export t
:reader t
:writer nil
:type string
:documentation "A string that specifies the views's behavior."))
:type (or web-preferences null)
:documentation "An object that encodes the web preferences for a given view."))
(:export-class-name-p t)
(:export-predicate-name-p t)
(:export-accessor-names-p t)
Expand Down
10 changes: 10 additions & 0 deletions source/view.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

(in-package :electron)

(defmethod options ((view view))
;; If `options' is explicitly set, use it.
(if (slot-value view 'options)
(slot-value view 'options)
(format nil "{~@[webPreferences:~a,~] ~@[webContents:~a~]}"
(when (web-preferences view)
(encode-json (web-preferences view)))
(when (slot-value view 'web-contents)
(remote-symbol (slot-value view 'web-contents))))))

(defmethod initialize-instance :after ((view view) &key)
(message
view
Expand Down
30 changes: 30 additions & 0 deletions source/web-preferences.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
;;;; SPDX-FileCopyrightText: Atlas Engineer LLC
;;;; SPDX-License-Identifier: BSD-3-Clause

;;;; Electron web preferences object.

(in-package :electron)

(define-class web-preferences ()
((properties
(make-hash-table :test 'equal)
:type (or hash-table null)
:documentation "The table of properties set within the web-preferences object. This value will
be encoded into JSON for creating WebContentsViews."))
(:export-class-name-p t)
(:export-predicate-name-p t)
(:export-accessor-names-p t)
(:documentation "This object represents the Electron analog of a WebPreferences object. It is
used to specify the behavior of a WebContentsView."))

(export-always 'add-preference)
(defmethod add-preference ((web-preferences web-preferences) preference value)
(setf (gethash preference (properties web-preferences)) value))

(export-always 'remove-preference)
(defmethod remove-preference ((web-preferences web-preferences) preference)
(remhash preference (properties web-preferences)))

(export-always 'encode-json)
(defmethod encode-json ((web-preferences web-preferences))
(cl-json:encode-json-to-string (properties web-preferences)))