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

Quip print from NSMenuItem shows up as an empty page #2744

Closed
wants to merge 1 commit into from

Conversation

rr-codes
Copy link
Contributor

@rr-codes rr-codes commented Jul 26, 2022

30ffd1a

Quip print from NSMenuItem shows up as an empty page
https://bugs.webkit.org/show_bug.cgi?id=243218
rdar://90054391

Reviewed by NOBODY (OOPS!).

When a user pressed Cmd+P, some websites (Quip, Google Docs, etc)
intercept this event and do their own processing before sending a print
event. However, when doing `File > Print`, the default print behavior
is used. This causes discrepancies for how these websites are printed
between the two different methods.

This PR addresses this by synthesizing a Cmd+P event whenever
`File > Print` is invoked or when `Print` is selected from the context
menu, which allows for consistent behavior.

* Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _generateSyntheticCommandP:]):
* Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h:
* Source/WebKit/UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::generateSyntheticCommandP):
* Source/WebKit/UIProcess/WebPageProxy.h:
* Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::generateSyntheticEditingCommand): Deleted.
* Source/WebKit/WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::generateSyntheticCommandP):
* Source/WebKit/WebProcess/WebPage/WebPage.h:
* Source/WebKit/WebProcess/WebPage/WebPage.messages.in:

@rr-codes rr-codes requested a review from cdumez as a code owner July 26, 2022 18:17
@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label Jul 26, 2022
@rr-codes rr-codes force-pushed the eng/90054391 branch 2 times, most recently from 50b7213 to 2c7dbf8 Compare July 26, 2022 19:15
return;
}

sendWithAsyncReply(Messages::WebPage::GenerateSyntheticCommandP(), WTFMove(callbackFunction));
Copy link
Member

Choose a reason for hiding this comment

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

This seems to hard-code the command bindings of the web browser.

Is command-P the shortcut for printing worldwide in all the various translated versions of Safari?

I think it’s really sad that the websites hard code a keyboard equivalent, but it seems like this could entirely break printing for some Safari localizations.

We may need to do a more sophisticated fix that always looks like Command-P to the webpage, but then makes sure to actually do a print action once it gets back to the Mac menu system.

Here’s a testing idea: Customize the menu key shortcut for Print, changing it to something like Command-T, using the Keyboard Settings, Keyboard Shortcut, App Shortcuts, and see what happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is command-P the shortcut for printing worldwide in all the various translated versions of Safari?

Yes, it is (Command+P isn't localized in Safari)/

Here’s a testing idea: Customize the menu key shortcut for Print, changing it to something like Command-T, using the Keyboard Settings, Keyboard Shortcut, App Shortcuts, and see what happens.

Yup, I already tested this. Specifically, I tested:

  • if it works in the DVORAK keyboard layout
  • if it works for when the user changes the print command key binding (to Command+Option+L for example)
  • for a non-Latin system language and keyboard (specifically, Hebrew)

and all of these worked.

Note that I also have a corresponding Safari PR which is linked in the Radar. The Safari change will trigger the normal print flow if the command+P event was not handled

@@ -268,6 +268,8 @@ for this property.

- (void)_doAfterNextVisibleContentRectUpdate:(void (^)(void))updateBlock WK_API_AVAILABLE(macos(13.0), ios(16.0));

- (void)_generateSyntheticCommandP:(void (^)(BOOL))completionBlock;
Copy link
Member

@whsieh whsieh Jul 27, 2022

Choose a reason for hiding this comment

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

This should be marked as WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do the TBAs mean like a future release? If that's the case, will that affect the Safari PR's mergability?

@aproskuryakov
Copy link
Contributor

aproskuryakov commented Jul 27, 2022

This approach looks very surprising to me, based on years old memories of working in this area. We don't have to synthesize any other keyboard shortcuts. It's up to the embedding application to override those that shouldn't be handled by WebKit (like Cmd+Q or Cmd+W), and the normal behavior is that everything is routed through WebKit.

@rr-codes
Copy link
Contributor Author

This approach looks very surprising to me, based on years old memories of working in this area. We don't have to synthesize any other keyboard shortcuts. It's up to the embedding application to override those that shouldn't be handled by WebKit (like Cmd+Q or Cmd+W), and the normal behavior is that everything is routed through WebKit.

@aproskuryakov this is just a compat hack to make Print from the menu bar work for web apps that currently attempt to intercept all printing by handling Cmd+P. For sites such as Quip, the system print mechanism doesn't work at all and/or doesn't produce reasonable/accurate pages.

https://bugs.webkit.org/show_bug.cgi?id=243218
rdar://90054391

Reviewed by NOBODY (OOPS!).

When a user pressed Cmd+P, some websites (Quip, Google Docs, etc)
intercept this event and do their own processing before sending a print
event. However, when doing `File > Print`, the default print behavior
is used. This causes discrepancies for how these websites are printed
between the two different methods.

This PR addresses this by synthesizing a Cmd+P event whenever
`File > Print` is invoked or when `Print` is selected from the context
menu, which allows for consistent behavior.

* Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _generateSyntheticCommandP:]):
* Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h:
* Source/WebKit/UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::generateSyntheticCommandP):
* Source/WebKit/UIProcess/WebPageProxy.h:
* Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::generateSyntheticEditingCommand): Deleted.
* Source/WebKit/WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::generateSyntheticCommandP):
* Source/WebKit/WebProcess/WebPage/WebPage.h:
* Source/WebKit/WebProcess/WebPage/WebPage.messages.in:
@aproskuryakov
Copy link
Contributor

Right, what I'm saying is that ALL shortcuts go to the website, and if Cmd+P doesn't, then it's an exception that needs to be aligned with the norm. There shouldn't be a need to make it even more exceptional.

@rr-codes
Copy link
Contributor Author

Right, what I'm saying is that ALL shortcuts go to the website, and if Cmd+P doesn't, then it's an exception that needs to be aligned with the norm. There shouldn't be a need to make it even more exceptional.

Yes, all shortcuts (including Cmd+P) do indeed go through the website. It's just that when using the menu item (File > Print), the website is unaware of this, and unable to intercept it to do its own handling

@aproskuryakov
Copy link
Contributor

Ah, now I understand why you took this path.

Yet, I'm very torn on faking keypresses when there are none, and it doesn't appear to be necessary anyway. Chrome doesn't do this, and yet quip pages print fine from it. What is the actual cause of quip pages being blank when printed in Safari?

And if we do this after all, doing this with an SPI seems like a liability, as embedders that don't use the SPI don't get the fix, and embedders that use it even slightly incorrectly still fail when printing via Apple Events.

@darinadler
Copy link
Member

Chrome doesn't do this, and yet quip pages print fine from it. What is the actual cause of quip pages being blank when printed in Safari?

I would like to know more about this.

@rr-codes
Copy link
Contributor Author

@aproskuryakov @darinadler Chrome has this same issue as well. Using Command+P on a Quip page in Chrome works as expected, but File > Print has the same issue as Safari (it doesn't get processed by the site and so it renders incorrectly like Safari)

@aproskuryakov
Copy link
Contributor

I see a completely blank page in print preview in Safari, and Chrome works. Yes, it's not the same content as with Cmd+P, but I wouldn't say that Chrome has an issue here, much less "the same issue".

@aproskuryakov
Copy link
Contributor

There are multiple other approaches for making printing work that I've seen in practice:

  • print stylesheets;
  • a "Print" button that opens a popup window formatted for printing, and executes window.print();
  • generate a PDF, potentially with embedded JS code that also executes window.print().

Intercepting Cmd+P does not catch printing via menu or automation (like Apple Events) in other browsers. Thinking about this as an innovation that other browsers might pick up in the future, we need to consider localizability issues not just on macOS, but on other platforms too, as well as resistance for standardizing such a hacky API.

@rr-codes
Copy link
Contributor Author

Even if we fix whatever caused the preview in Safari to be blank (such that our behavior would match Chrome’s) that’s still a user-facing bug, since the user would probably expect cmd+P to match File > Print.

Do you have any alternative solutions in mind @aproskuryakov ?

@aproskuryakov
Copy link
Contributor

Personally, I expect the menu to bypass whatever bugs (or intentional "DRM" features) the site has, and print what I see. But that's likely me not being a normal user.

But anyone would expect some kind of consciously aligned behavior between "Print", "Share" and "Export as PDF" Safari menus, although I'm not sure what exactly it should be.

One solution that doesn't seem like a total hack is having the Print menu dispatch an equivalent of "window.print()" on the frontmost HTML document. That wouldn't fix Quip automatically, but this is something that can be adopted by websites AFAICT. I'm not sure what forums we have for aligning with other browsers on a behavior like this.

@rr-codes rr-codes closed this Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merging-blocked Applied to prevent a change from being merged
Projects
None yet
5 participants