Responsive Scrolling - setWantsLayer Webkit problems - macos

My project is on base SDK 10.6 built in Xcode 3 - it’s important that it is a universal binary that also works with PPC.
In 10.9 my application has really jerky scrolling compared to Safari - if I add
[self setWantsLayer:YES]; the scrolling is super fast like Safari, great!
However I get visual glitches when scrolling, see screenshot of eBay where the legal image which was supposed to be fixed at the bottom of the page keeps repeating.
I know setWantsLayer isn’t supposed to be used with a webKit, but is there anyway to stop these visual glitches or is there another way to get the smooth scrolling without using setWantsLayer.
Above is an issue with eBay when scrolling and below is an issue with Twitter while scrolling, happens on various websites especially with fixed objects.
Issue with Twitter

Actually I originally thought the below solution was a solution without realising that I was back to the horrible jumpy scrolling. I leave it for reference.
I had the same problem you seems to have here.
I found that if I implemented the canDrawSubviewsIntoLayer in the subclass my glitches with the rendering were fixed.
so in the initWithCoder/initWithFrame method call:
[self setWantsLayer:YES];
then implement canDrawSubviewsIntoLayer
- (BOOL)canDrawSubviewsIntoLayer {
return YES;
}
You need to subclass the webView. I also found that works equally well if instead you subclass the NSView below and do the same in the NSView subclass but since I don't understand why that works my suggestion is to subclass the webView.
I hope it helps you and helps others.

Related

Disable All Interaction with a Cocoa WebView

I'm having difficulty in using a WebView that is being used purely to display a preview of a website - and I want to ignore all/any interactions the user may try to make with it.
I've already tried Cocoa webView - Disable all interaction to no avail; you can still easily spam-click and it will recognise the presses. (at least there's no context menu)
This one seems overkill; Disable a WebKit WebView - there must be an easier way.
In iOS I know exactly how to solve this, but as a Cocoa newcomer I am stumped - does anyone have any suggestions or better ways to achieve this? Or dump a transparent NSView on top and gobble up interactions? (tried this as well by subclassing an NSView, also to no effect)
Whole project is in IB currently, if this makes any difference.
I think you want to implement a WebPolicy Delegate and have it deny navigation events.

AS3 OSX's quirky scroll effect ruins mousewheel controls

I'm building a web app to design and animate simple 3d scenes using the Away3D library, but the design of the interface itself is built around a scrolling menu which takes up the whole height of the screen.
The problem is, on OSX browsers have a silly quirk where if the page is scrolled when it has already gone as far as it can, the page can be dragged slightly further, revealing a brushed metal background. This looks nice and whatnot, but it pretty much ruins scrolling in a swf object. I use flash builder and export to safari, which is just about useable, if pretty annoying (especially with a mac touchpad, which can give a much higher scroll delta than a mousewheel can), but when I open my app in firefox/chrome the same effect happens, and causes the app and browser to slow down drastically.
I've found code which uses ExternalInterface to stop the swf sending mouse events to the page, but they all seem to disable detection in the swf as well, and I can't find anything else which help. If anyone knows of a solution you'll be saving me from throwing a few months' work away for what seems like a suspiciously unnecessary drawback to Flash on OSX!
Thanks in advance if anyone can help
Figured it out, in case anyone else gets stuck with this:
The feature's called elastic scrolling, if you're running a flash/flex app which takes up all the browser space you can simply set body{overflow: hidden;} in page CSS and it'll work just fine. Obviously, this disables any kind of page-scrolling - I believe you can turn it back on inside nested elements though.
Hope this helps someone anyway!

Autolayout warning in Mac OS X

I am getting a warning I do not fully understand every time I run my app:
Layout still needs update after calling -[WebHTMLView layout]. WebHTMLView or one of its superclasses may have overridden -layout without calling super. Or, something may have dirtied layout in the middle of updating it. Both are programming errors in Cocoa Autolayout. The former is pretty likely to arise if some pre-Cocoa Autolayout class had a method called layout, but it should be fixed.
(I've seen this related question, yet the answer provided solves some other issue, thus my question.) The warning appears as soon as I add a web view to my app. What does it mean, and how should I fix the issue?
It appears that - (void)layout is implemented in WebHTMLView. The source for WebKit is available on Apple's open source page to review.
http://www.opensource.apple.com/source/WebKit/WebKit-7534.53.11/mac/WebView/WebHTMLView.mm
In the Auto Layout documentation, it states that you only need to implement "layout" if you need custom logic to layout subviews while using Auto Layout. It goes on to say that you should always call [super layout] in your custom implementation. That is not happening with WebHTMLView.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html%23//apple_ref/occ/instm/NSView/layout
This message can probably be safely ignored for your project. The same thing happens for me on a blank new project.
Besides the noted NSTableView with dragging I found out that if you open the NSFontManager this will always show this message. Not only with Swift but also with Apple's own demo program CoreTextArcCocoa. So it's definitely some bug in Apple's guts.

Safari 5.1 npapi issue

Since several days I am trying to resolve the folowing issue, reading all I found around the web about npapi on mac.
The goal is to have a npapi plugin which works for safari and firefox(mac).
My software (that I can not rewrite specialy for this purpose hase about 45000 lines of C code) is based on a NSView attached to a NSDocument....
I have a webkit version based plugin that I must trash (thanks to Apple!) based to the same NSView.
I have a npapi version plugin which works fine on firefox. In this npapi plugin, I take the carbon window ref, I make a NSWindow based on that:
NSWindow *browserWindow = [[[NSWindow alloc] initWithWindowRef:wind]autorelease];
and I put my NSView on this window and that works.
Now the pb is that I can not do the same thing on safari.
Look at attached picture, the window is not in the safari's window!
I tryed several ways... it dose not work.
Can a cocoa's gourou says where I am making something wrong? or is this a known issue?
NPError NPP_SetWindow(NPP instance, NPWindow* window){
NP_CGContext *ctx = window->window;
void *wind = ctx->window;
...
in the NSView init function:
NSWindow *browserWindow = [[NSWindow alloc] initWithWindowRef:wind];
self = [super initWithFrame:frame];
if( self )
{
[browserWindow makeFirstResponder: self];
[self setNextResponder: nil];
[browserWindow setContentView:self];
[self webPlugInInitialize];// my own initializing
}
return self;
In Safari 5.1, the web rendering is not done by Safari itself, but on a different process to enhance security. Open up the Activity Monitor, and you see that background process called "Safari Web Process" or something like that.
So, you can't and shouldn't create NSWindow based on the Carbon window ref which can be obtained within NPAPI plugin.
Read Apple's own documentation on this point. You should request the core graphics drawing method, and then the WindowRef field of NP_CGContext should have a NSWindow*, not the Carbon window ref.
If it works on Firefox, that's totally shocking and completely unsupported. Does it work in Firefox 4 and later?
If you absolutely have to use an NSView, the only way that I know to do it in a plugin is to render the NSView into your CGContext. Keep in mind that in newer NPAPI browsers with the Cocoa event system you get the CGContextRef as part of the draw event; to request a draw event you can call NPN_InvalidateWindow.
FireBreath has a completely experimental and not-fully-functional example of rendering an NSView (ans specifically a WebView) into a CGContextRef that you could look at as an example.
Other than using a CGContextRef your only other choice is to use a CALayer; if you can find a way to make a NSWindow or NSView in that you could be okay, but I don't know if there is one. Someone suggested that setting the CALayer as the rendering layer for the NSView might work. Either way you'll most likely have to forward all the events since you are basically hosting the NSView in an offscreen view.
Make no mistake; there is no supported way to get an NSView in the browser. There never has been -- methods that people have used were unsupported and depended on browser-specific implementations of the API. When you use things like that, you can reliably expect them to eventually break, such as in this case. For more information on the drawing models, you could read Stuart Morgan's blog post on the subject, check out the FireBreath mac drawing model docs, or read the Cocoa event model spec.
Given that you start with "take the carbon window ref", your approach is doomed, because it is based on the Carbon event model (and not just that, but assumptions about its internal implementation details). Anyone running Firefox on a 64-bit system will have to manually restart Firefox in 32-bit mode for your hack to work, and even then it will only work until Firefox completely removes Carbon support (which is planned for the foreseeable future).
As the other answers said, where you are going wrong is that your whole approach is completely unsupported, and the fact that it ever worked at all as an NPAPI plugin was luck. You simply cannot use an NSView directly in an NPAPI plugin.

WebView slow loading

In a cocoa / Mac app I am using a WebView to load a series of YouTube videos (5). When it is loading my whole application locks up for 5 seconds or so.
Is there a way I can load it so it doesn't freeze the rest of the application?
Thanks
eg:
[[webView mainFrame] loadHTMLString:html baseURL:[NSURL URLWithString:#"http://youtube.com"]];
Unfortunately, you cannot render a WebView except on the main thread. This is a basic limitation of the class. I would begin investigating why your page takes so long to render. My hunch: Javascript or a plugin.
I usually like to start by using Safari to see if it has the same issues. You can drag an HTML file onto Safari to load it easily. If Safari shows the same problem, then you can use Safari's Develop tools to profile it. (Preferences, Advanced, Show Develop menu in menu bar.)
If Safari doesn't have a problem, that's actually good, because it means your problem is definitely solvable (if Safari can do it, you should be able to achieve the same thing). Here are some things to try out:
Instruments. Try to see what WebView is taking its time on. Focus first on "all samples" for the main thread. This is usually what leads to the app hanging.
Simplification. Try stripping things out of the webview, especially javascript, until you find the piece that's causing the problem.
Implement the WebResourceLoadDelegate methods to see what piece seems to be causing the problem. The information given by this can be misleading, since it has to do with when things are downloaded, not when they are rendered, but it can give a sense of where in the page you're hanging.

Resources