[webkit-gtk] Next: Adding items to the context menu (in Lisp)

Pierre Neidhardt mail at ambrevar.xyz
Tue May 5 03:00:11 PDT 2020


Hi!

I'm working on Next (https://github.com/atlas-engineer/next/), a Common
Lisp browser using WebKitGTK, and I'd like to populate the context menu
with custom items.

A simple attempt:

--8<---------------cut here---------------start------------->8---
; Helper to populate the browser "context-menu-entries" slot:
(defun make-context-menu-entries ()
  (let ((foo-action (gio:g-simple-action-new
                     "foo"
                     (glib:g-variant-type-new glib:+g-variant-type-maybe+))))

    (gobject:g-signal-connect
     foo-action "activate"
     (lambda (action parameter)
       (declare (ignore action parameter))
       (log:info "foo action")))

    (gio:g-simple-action-set-enabled foo-action t)

    (list :foo
          (webkit:webkit-context-menu-item-new-from-gaction
           foo-action
           "Do foo"
           (cffi:null-pointer)))))

; Initialize the web view:
; ...
(gobject:g-signal-connect (gtk-object buffer) "context-menu"

   (lambda (web-view context-menu event hit-test-result)
     (declare (ignore web-view event hit-test-result))

     (webkit:webkit-context-menu-append
      context-menu
      (getf (context-menu-entries *browser*) :foo))
     ;; Return nil to show the context menu.
     nil))
--8<---------------cut here---------------end--------------->8---


Translation for non-Lispers:

- Create a simple action "foo" with "maybe" variant type.
- Connect the "activate" signal to a lambda logging something.
- Return a dictionary of <:foo my-new-context-menu-item>.

When initializing the buffer:

- Connect the "context-menu" signal and append the mew-new-context-menu-item from above.

This shows a "Do foo" entry in the context menu.  But it's greyed
out, so it seems that g-simple-action-set-enabled does not do what I
think it does.

Any clue what I'm missing?

-- 
Pierre Neidhardt
https://ambrevar.xyz/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: not available
URL: <http://lists.webkit.org/pipermail/webkit-gtk/attachments/20200505/cd5a7c72/attachment.bin>


More information about the webkit-gtk mailing list