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

Help Browser code execution #4883

Merged
merged 5 commits into from
Oct 25, 2020
Merged

Help Browser code execution #4883

merged 5 commits into from
Oct 25, 2020

Conversation

elgiano
Copy link
Contributor

@elgiano elgiano commented Apr 19, 2020

Purpose and Motivation

Fixes #4542 and #4000

Explained more in detail in an RFC and open for discussion. Please let's discuss it, I don't want to push this in any way. I also would really benefit from some other pairs of eyes on it. And it needs testing on Windows.
RFC: https://github.com/elgiano/rfcs/blob/helpbrowser/rfcs/0009-helpbrowser-code-evaluation.md

  • fixed code execution in both HelpBrowser class and Widget + in WebView, if the rendered webpage has selectLine()/selectRegion() javascript functions. It works with selected text/line, and with selected text/region.
  • code execution in webviews is more consistently delegated to javascript, calling a function to get selected text, receiving it in a callback and then sending it to the interpreter
  • a workaround WebView's "funny" keypress behavior, now keyDownAction works on WebView only when an url is set. It gets all events (including the ones swallowed by the renderer) without duplication. WebView parent widgets also get all keystrokes, except the ones swallowed by the web renderer when focusing an input field (like Shift+Enter).
  • Using QcWebView in ScIDE Help Browser Widget

Types of changes

  • Bug fix

To-do list

  • Code is tested
    It works on Linux and Mac.Missing testing on Windows. Also on Windows, thanks @dyfer
  • All tests are passing
  • Updated documentation
  • This PR is ready for review

@elgiano elgiano changed the title Webview Help Browser code execution Apr 19, 2020
@elgiano elgiano force-pushed the webview branch 2 times, most recently from 681b0a1 to cfa4c63 Compare April 20, 2020 00:31
@dyfer
Copy link
Member

dyfer commented Apr 20, 2020

I've built it on Windows and tested code execution in the Help Browser - works fine. I haven't had a chance to look deeper yet, but doesn't seem like there are any apparent issues with Windows at first glance.

@elgiano
Copy link
Contributor Author

elgiano commented Apr 20, 2020

thanks for testing it @dyfer!

@elgiano elgiano force-pushed the webview branch 3 times, most recently from 4876831 to dd568e7 Compare July 1, 2020 22:36
@elgiano elgiano force-pushed the webview branch 3 times, most recently from 6d8dd76 to 5ef0990 Compare July 21, 2020 12:09
@elgiano elgiano changed the base branch from develop to 3.11 August 24, 2020 14:13
@elgiano
Copy link
Contributor Author

elgiano commented Aug 24, 2020

Rebased on 3.11

@mossheim mossheim mentioned this pull request Aug 25, 2020
Copy link
Contributor

@mossheim mossheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! i haven't tested it yet but i have a few questions about the code before reviewing further

HelpSource/editor.js Show resolved Hide resolved
HelpSource/editor.js Outdated Show resolved Hide resolved
editors/sc-ide/widgets/help_browser.cpp Outdated Show resolved Hide resolved
QtCollider/widgets/QcWebView.cpp Outdated Show resolved Hide resolved
@@ -159,7 +158,8 @@ void WebView::toPlainText(QcCallback* cb) const {
void WebView::runJavaScript(const QString& script, QcCallback* cb) {
if (page()) {
if (cb) {
page()->runJavaScript(script, cb->asFunctor());
// convert QVariant to string until we deal with QVariants
page()->runJavaScript(script, [cb](const QVariant& t) { cb->call(t.toString()); });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't understand this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough :) Sorry for being criptic, now I also have to look it up again. We have a problem when passing a QVariant back into sclang:

  • runJavascript takes a callback function, which gets converted to this QcCallback
  • when the callback is called, sclang throws an error:
    ERROR: Qt: Failed to write a slot when trying to run interpreter!

If anyone feels like trying it out, this code errors without this PR:

WebView().runJavaScript("1+1"){|res,e|res.class.postln}

This problem does not happen when we convert to string before calling the QCallback. I can definitely look deeper into this, and suggestions are very welcome :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've investigated a bit more and I think this maybe deserves another PR. I'll try to explain here:

  • JavaScript can return a variety of types to sclang, so it gives a QVariant as argument to the callback.
  • The callback fires, emitting a signal that is caught by a QSignalSpy
  • QSignalSpy gets the argument as a void* (but it also gets its type), and throws it into a QVariant. Now we have a QVariant<QVariant>
  • QSignalSpy sends the arguments to QtCollider::runLang, which calls QtCollider::set, which fails for a QVariant<QVariant>

I have a fix for QSignalSpy to avoid creating a QVariant<QVariant> which makes possible to return Numbers, Strings and Arrays. The only type that still fails is QMap. I haven't tested if this code has consequences on any other call, it looks like a legit bugfix for me, I'm tempted to include it here... what do you think would be best?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for looking into it! since this is the last PR before the 3.11.2 release, let's just leave this as it is. if you're interested you can make another PR for it, i'd also be happy to take a look myself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, we keep it like it is then and I'll open another PR! Just to be extra clear, now all javascript calls from sclang will always return a string only

QtCollider/widgets/QcWebView.h Show resolved Hide resolved
editors/sc-ide/CMakeLists.txt Show resolved Hide resolved
editors/sc-ide/widgets/help_browser.cpp Outdated Show resolved Hide resolved
editors/sc-ide/widgets/help_browser.cpp Outdated Show resolved Hide resolved
@mossheim
Copy link
Contributor

thanks for the quick update @elgiano . if you can address https://github.com/supercollider/supercollider/pull/4883/files#r509325107 i'm ready to review the rest of the PR after that :)

@elgiano
Copy link
Contributor Author

elgiano commented Oct 22, 2020

Yeah, thanks for looking at it @brianlheim!
The one you point out is the one I don't really understand. What do you suggest I do about it? Shall I make the class hierarchy (my idea), or could you explain a bit more what your proposal is?
Thankssssssssssssss

@elgiano
Copy link
Contributor Author

elgiano commented Oct 22, 2020

I think I got what you meant now! I pushed all changes for your pre-review, I hope it's fine now :)

Copy link
Contributor

@mossheim mossheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this approach makes sense overall. i have a few questions and then some comments assuming this is the way we want to proceed

QtCollider/widgets/QcWebView.cpp Show resolved Hide resolved
QtCollider/widgets/QcWebView.h Show resolved Hide resolved
QtCollider/widgets/QcWebView.h Show resolved Hide resolved
QtCollider/widgets/QcWebView.h Outdated Show resolved Hide resolved
editors/sc-ide/widgets/help_browser.cpp Show resolved Hide resolved
editors/sc-ide/widgets/help_browser.cpp Outdated Show resolved Hide resolved
SCClassLibrary/Common/GUI/Base/QWebView.sc Show resolved Hide resolved
SCClassLibrary/Common/GUI/Base/QWebView.sc Outdated Show resolved Hide resolved
QtCollider/widgets/QcWebView.cpp Outdated Show resolved Hide resolved
QtCollider/widgets/QcWebView.h Outdated Show resolved Hide resolved
@elgiano elgiano force-pushed the webview branch 2 times, most recently from 8fbbb3a to cd869fa Compare October 23, 2020 02:24
Copy link
Contributor

@mossheim mossheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @elgiano ! the code all looks good except for one whitespace thing. i want to test it out before approving/merging.

<onSelectionChanged, <onTitleChanged, <onUrlChanged, <onScrollPositionChanged, <onContentsSizeChanged, <onAudioMutedChanged,
<onRecentlyAudibleChanged;
<onSelectionChanged, <onTitleChanged, <onUrlChanged, <onScrollPositionChanged, <onContentsSizeChanged, <onAudioMutedChanged,
<onRecentlyAudibleChanged, <enterInterpretsSelection = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please undo the whitespace changes

@@ -159,7 +158,8 @@ void WebView::toPlainText(QcCallback* cb) const {
void WebView::runJavaScript(const QString& script, QcCallback* cb) {
if (page()) {
if (cb) {
page()->runJavaScript(script, cb->asFunctor());
// convert QVariant to string until we deal with QVariants
page()->runJavaScript(script, [cb](const QVariant& t) { cb->call(t.toString()); });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for looking into it! since this is the last PR before the 3.11.2 release, let's just leave this as it is. if you're interested you can make another PR for it, i'd also be happy to take a look myself.

QtCollider/widgets/QcWebView.h Show resolved Hide resolved
QtCollider/widgets/QcWebView.h Show resolved Hide resolved
@@ -26,6 +26,11 @@

QC_DECLARE_QWIDGET_FACTORY(QLabel);

#ifdef SC_USE_QTWEBENGINE
# include "widgets/QcWebView.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aghh, you need to add a space here for linting

Suggested change
# include "widgets/QcWebView.h"
# include "widgets/QcWebView.h"

@mossheim
Copy link
Contributor

@elgiano do we use websockets for anything at all after this PR?

@elgiano
Copy link
Contributor Author

elgiano commented Oct 24, 2020

do we use websockets for anything at all after this PR?

After a little grep I haven't found any place where we are using them, which I also suspected.

@@ -38,7 +38,10 @@ const init = () => {
lineWrapping: true,
viewportMargin: Infinity,
extraKeys: {
'Shift-Enter': evalLine
// noop: prevent both codemirror and the browser to handle Shift-Enter
'Shift-Enter': ()=>false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing a comma here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yesss, thanks for catching it! btw I changed to ()=>{} which looks like a more proper noop

Copy link
Contributor

@mossheim mossheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks again! i think this is all good. now that we are not using the websocket code, i would like to remove that too, but that can be done on the develop branch after the 3.11.2 release.

i also tested the latest version myself on macOS 10.14, windows 10, and arch linux, all fine.

@mossheim mossheim merged commit 0c16681 into supercollider:3.11 Oct 25, 2020
@elgiano elgiano deleted the webview branch October 25, 2020 09:06
@elgiano
Copy link
Contributor Author

elgiano commented Oct 25, 2020

Thanks a lot @brianlheim and @dyfer!!!

@mossheim mossheim mentioned this pull request Oct 28, 2020
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

help browser: shift+enter doesn't evaluate code
3 participants