updateTrackingAreas: override only works for the first 2 times? - objective-c

On El Capitan in Xcode 7 beta using Swift 2.0, I subclassed a NSView to use as a prototype view of NSCollectionView's item view, and override the updateTrackingAreas: method to do the mouse tracking. The NSCollectionView was inside a NSPopover.
It seems that only the first 2 times the updateTrackingAreas: will be called, as the debug log shows. The code was like the following:
override func updateTrackingAreas() {
Swift.print("updateTrackingAreas:")
if trackingArea != nil {
self.removeTrackingArea(trackingArea!)
}
trackingArea = NSTrackingArea(
rect: self.bounds,
options: [NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways],
owner: self,
userInfo: nil
)
if trackingArea != nil {
self.addTrackingArea(trackingArea!)
}
var mouseLocation = self.window?.mouseLocationOutsideOfEventStream
mouseLocation = self.convertPoint(mouseLocation!, fromView: nil)
if CGRectContainsPoint(self.bounds, mouseLocation!) {
mouseEntered(NSEvent())
} else {
mouseExited(NSEvent())
}
super.updateTrackingAreas()
}
The first 2 times when the popover was opened, I can see the console log shows updateTrackingAreas:. Then the tracking failed and no logs either.
EDIT:
When I commented out this part like:
//var mouseLocation = self.window?.mouseLocationOutsideOfEventStream
mouseLocation = self.convertPoint(mouseLocation!, fromView: nil)
if CGRectContainsPoint(self.bounds, mouseLocation!) {
mouseEntered(NSEvent())
} else {
mouseExited(NSEvent())
}
The problem will no longer exists.
And the following code makes no differences:
if let window = self.window {
var mouseLocation = window.mouseLocationOutsideOfEventStream
mouseLocation = self.convertPoint(mouseLocation, fromView: nil)
if let event = NSApplication.sharedApplication().currentEvent {
if NSPointInRect(mouseLocation, self.bounds) {
mouseEntered(event)
} else {
mouseExited(event)
}
}
}
EDIT 2:
Ok, I just reinstall OS X Yosemite and compiled it again with Xcode 7 beta 1. The problem no longer exists. Might just be a bug of El Capitan. I'll report it to Apple. Thank you all.

You're passing a newly-created event instance to mouseEntered() and mouseExited(). I realize that this is because you can't pass nil in the Swift variant of the method but perhaps you should pass the current event instead (NSApplication and NSWindow both have a currentEvent() method). Have you tried this?

Checking the mouseLocation in updateTrackingAreas is senseless.
From the docs of updateTrackingAreas:
Invoked automatically when the view’s geometry changes such that its tracking areas need to be recalculated.
This is usually due to frame change. It will be called as many times as you view changes it's frame or position. I doubt you've implemented live-resizing/moving feature on your view, this is the only way mouse inside view's frame can be the source of your view's geometry change.

Ok, I just reinstall OS X Yosemite and compiled it again with Xcode 7 beta 1. The problem no longer exists.
Might just be a bug of El Capitan. Already reported it to Apple.
Thank you all.

Related

Full width NSCollectionViewFlowLayout with NSCollectionView

I have a NSCollectionViewFlowLayout which contains the following:
- (NSSize) itemSize
{
return CGSizeMake(self.collectionView.bounds.size.width, kItemHeight);
} // End of itemSize
- (CGFloat) minimumLineSpacing
{
return 0;
} // End of minimumLineSpacing
- (CGFloat) minimumInteritemSpacing
{
return 0;
} // End of minimumInteritemSpacing
I've tried to ways to make the layout responsive (set the width whenever resized). I've tried adding the following:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(onWindowDidResize:)
name: NSWindowDidResizeNotification
object: nil];
- (void) onWindowDidResize: (NSNotification*) notification
{
[connectionsListView.collectionViewLayout invalidateLayout];
} // End of windowDidResize:
And this works fine if I expand the collection view (resize larger). But if I attempt to collapse the view (resize smaller), I get the following exceptions:
The behavior of the UICollectionViewFlowLayout is not defined because:
The item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
The relevant UICollectionViewFlowLayout instance is <TestListLayout: 0x106f70f90>, and it is attached to <NSCollectionView: 0x106f76480>.
Any suggestions on how I can resolve this?
NOTE1: This is macOS and not iOS (even though the error message states UICollectionViewFlowLayout).
NOTE2: Even though I receive the warning/error the layout width works, but I would like to figure out the underlying issue.
I had the same problem but in my case I resized view. #Giles solution didn't work for me until I changed invalidation context
class MyCollectionViewFlowLayout: NSCollectionViewFlowLayout {
override func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool {
return true
}
override func invalidationContext(forBoundsChange newBounds: NSRect) -> NSCollectionViewLayoutInvalidationContext {
let context = super.invalidationContext(forBoundsChange: newBounds) as! NSCollectionViewFlowLayoutInvalidationContext
context.invalidateFlowLayoutDelegateMetrics = true
return context
}
}
Hope it helps someone as it took me couple of evenings to find solution
This is because you are using the didResize event, which is too late. Your items are too wide at the moment the window starts to shrink. Try using:
func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool {
return true
}
...when overriding flow layout, to get everything recalculated.
The source code I posted in the question works fine as of macOS 10.14 with no issues. I added the following to my window which displays the collection view.
// Only Mojave and after is resizable. Before that, a full sized collection view caused issues as listed
// at https://stackoverflow.com/questions/48567326/full-width-nscollectionviewflowlayout-with-nscollectionview
if(#available(macOS 10.14, *))
{
self.window.styleMask |= NSWindowStyleMaskResizable;
} // End of macOS 10.14+

AVPlayer.play() in UITableViewCell briefly blocks UI

I’m trying add inline videos to my UITableViewCells a la Instagram, Twitter, Vine…etc. I’m testing out the UI with a local video file using AVPlayerController and a custom cell (see sample code below). I wait for the status of the AVPlayer to be ReadyToPlay and then play the video.
The issue is that when scrolling on the tableView the UI freezes for a fraction of section whenever a video cell is loaded which makes the app seem clunky. This effect is made worse when there are multiple video cells in a row. Any thoughts and help would be greatly appreciated
TableView Code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("videoCell", forIndexPath: indexPath) as! CustomCell
//Set up video player if cell doesn't already have one
if(cell.videoPlayerController == nil){
cell.videoPlayerController = AVPlayerViewController()
cell.videoPlayerController.view.frame.size.width = cell.mediaView.frame.width
cell.videoPlayerController.view.frame.size.height = cell.mediaView.frame.height
cell.videoPlayerController.view.center = cell.mediaView.center
cell.mediaView.addSubview(cell.videoPlayerController.view)
}
//Remove old observers on cell.videoPlayer if they exist
if(cell.videoObserverSet){
cell.videoPlayer.removeObserver(cell, forKeyPath: "status")
}
let localVideoUrl: NSURL = NSBundle.mainBundle().URLForResource("videoTest", withExtension: "m4v")!
cell.videoPlayer = AVPlayer(URL:localVideoUrl)
cell.videoPlayer.addObserver(cell, forKeyPath:"status", options:NSKeyValueObservingOptions.New, context = nil)
cell.videoObserverSet = true
cell.videoPlayerController.player = cell.videoPlayer
return cell
}
Custom Cell Observer Code:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if(keyPath=="status"){
print("status changed on cell video!");
if(self.videoPlayer.status == AVPlayerStatus.ReadyToPlay){
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.videoPlayer.play()
})
}
}
}
I also tried loading an AVAsset version of the video using the loadValuesAsynchronouslyForKeys(["playable"]) but that didn't help with the UI block either.
What can I do to get the silky smooth video playback and scrolling of Instagram?
Another Update
Highly recommend using Facebook's Async Display Kit ASVideoNode as a drop in replacement for AVPlayer. You'll get the silky smooth Instagram tableview performance while loading and playing videos. I think AVPlayer runs a few processes on the main thread and there's no way to get around them cause it's happening at a relatively low level.
Update: Solved
I wasn't able to directly tackle the issue, but I used to some tricks and assumptions and to get the performance I wanted.
The assumption was that there's no way to avoid the video taking a certain amount of time to load on the main thread (unless you're instagram and you possess some AsyncDisplayKit magic, but I didn't want to make that jump), so instead of trying to remove the UI block, try to hide it instead.
I hid the UIBlock by implementing a simple isScrolling check on on my tableView
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if(self.isScrolling){
if(!decelerate){
self.isScrolling = false
self.tableView.reloadData()
}
}
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if(self.isScrolling){
self.isScrolling = false
self.tableView.reloadData()
}
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
self.isScrolling = true
}
And then just made sure not to load the video cells when tableView is scrolling or a video is already playing, and reload the the tableView when the scrolling stops.
if(!isScrolling && cell.videoPlayer == nil){
//Load video cell here
}
But make sure to remove the videoPlayer, and stop listening to replay notifications once the videoCell is not on the screen anymore.
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if(tableView.indexPathsForVisibleRows?.indexOf(indexPath) == nil){
if (cell.videoPlayer != nil && murmur.video_url != nil){
if(cell.videoPlayer.rate != 0){
cell.videoPlayer.removeObserver(cell, forKeyPath: "status")
cell.videoPlayer = nil;
cell.videoPlayerController.view.alpha = 0;
}
}
}
}
With these changes I was able to achieve a smooth UI with multiple (2) video cells on the screen at a time. Hope this helps and let me know if you have any specific questions or if what I described wasn't clear.

Check if NSAnimationContext runAnimationGroup cancelled or succeeded

I'm animating a view (by revealing it) after which I need to post a notification (once the animation completes). However the way the app's designed, there's another notification sent out when the view is hidden (via animation). So essentially I have a 'showView' and a 'hideView' method. Each do something like so:
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
[context setDuration: 0.25];
[[myView animator] setAlphaValue: 0];
} completionHandler:^{
// post notification now
}];
The problem is that in another thread in the background, I have a periodic timer which performs a 'sanity check' to see if the view needs to be hidden if it's not needed. It's an aggressive approach but necessary as many components of the app respond to different events and scenarios. Due to this I at times get a 'race condition' where the user has clicked to 'showView' but at the same time one of our threads feel the view should be immediately hidden.
When both post a notification on the main thread, the app hangs indefinitely in a SpinLock. I could completely avoid this situation if I was able to figure out if the animation block above was 'cancelled' (i.e. another animation block executed on the same view). In such situations I would not post the notification, which would then not force a check.
Long story short: I need to be able to check if the 'completionHandler' was called after animation successfully ended or if it was cancelled. I know in iOS this is possible but I can't find any way to do this in OS X. Please help.
I encountered a similar problem. I solved it by using a currentAnimationUUID instance variable.
NSUUID *animationUUID = [[NSUUID alloc] init];
self.currentAnimationUUID = animationUUID;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
// set properties to animate
} completionHandler:^{
BOOL isCancelled = ![self.currentAnimationUUID isEqual:animationUUID];
if (isCancelled) {
return;
}
self.currentAnimationUUID = nil;
// do stuff
}];
Swift version:
let animationUUID = UUID()
currentAnimationUUID = animationUUID
NSAnimationContext.runAnimationGroup({ context in
// set properties to animate
}, completionHandler: {
let isCancelled = self.currentAnimationUUID != animationUUID
guard !isCancelled else {
return
}
self.currentAnimationUUID = nil
// do stuff
})

MKAnnotationView - hard to drag

I have a MKAnnotationView that the user can drag around the map.
It is very difficult for a user to drag the pin. I've tried increasing the frame size and also using a giant custom image. But nothing seems to actually change the hit area for the drag to be larger than default.
Consequently, I have to attempt to tap/drag about ten times before anything happens.
MKAnnotationView *annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"bluedot"] autorelease];
UIImage *image = [UIImage imageNamed:#"blue_dot.png"];
annView.image = image;
annView.draggable = YES;
annView.selected = YES;
return annView;
What am I missing here?
EDIT:
It turns out the problem is that MKAnnotationView needs to be touched before you can drag it. I was having trouble because there are a lot of pins nearby and my MKAnnotationView is very small.
I didn't realise MKAnnotationView needed to be touched before you can drag it.
To get around this, I created a timer that selected that MKAnnotationView regularly.
NSTimer *selectAnnotationTimer = [[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(selectCenterAnnotation) userInfo:nil repeats:YES] retain];
and the method it calls:
- (void)selectCenterAnnotation {
[mapView selectAnnotation:centerAnnotation animated:NO];
}
Instead of using a timer, you could select the annotation on mouse down. This way you don't mess with the annotation selection, and don't have a timer for each annotation running all the time.
I had the same problem when developing a mac application, and after selecting the annotation on mouse down, the drag works great.
Just came here to say this still helped me today, multiple years later.
In my application, the user can place a bounding area down with annotations and (hopefully) move them around freely. This turned out to be a bit of a nightmare with this "select first and then move" behaviour and just plain made it difficult and infuriating.
To solve this, I default annotation views to selected
func mapView(_ mapView: MKMapView,
viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let view = MKAnnotationView(annotation: annotation, reuseIdentifier: "annotation")
view.isDraggable = true
view.setSelected(true, animated: false)
return view
}
The problem is that there is an annotation manager that resets all annotations back to deselected, so I get around this in this delegate method
func mapView(_ mapView: MKMapView,
annotationView view: MKAnnotationView,
didChange newState: MKAnnotationView.DragState,
fromOldState oldState: MKAnnotationView.DragState) {
if newState == .ending {
mapView.annotations.forEach({
mapview.view(for: $0)?.setSelected(true, animated: false)
})
}
It's a stupid hack for a stupid problem. It does work though.

How do I send key events to a Flash movie in WebKit?

I am trying to create a site-specific browser application. I've created a new Cocoa app, added a WebView to my window, and am loading up the page. The page contains a Flash movie, which can be controlled via the keyboard. I'd like to wire up some menu commands to trigger the actions, presumably by simulating the keystrokes.
I've traversed the view hierarchy and have found the NSView that contains the movie (with class name "WebHostedNetscapePluginView"). But I'm stuck at the point of sending it keys. I tried using CGEventCreateKeyboardEvent() but the input keeps going to the top-level WebView rather than the subview containing the movie.
I tried [[webView window] makeFirstResponder: _myView] to set the target for the input.
Is CGEventCreateKeyboardEvent() the right function here and, if so, how do I get it to my target NSView?
Many thanks in advance.
My original answer would only work if the window was active. I also wanted it to work when the window was hidden, and came up with the code below.
This works for ordinary keys (Enter, Space, arrow keys, etc.). It uses keycodes so it won't work for letters and symbols that might move around based on the user's language and region.
And it doesn't handle modifier keys like Command. If someone can figure that out I will gladly give them credit for the answer to this question.
// For Safari 4.x+
if ([[flashView className] isEqual:#"WebHostedNetscapePluginView"])
{
CGEventRef event = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)keycode, true);
[flashView keyDown:[NSEvent eventWithCGEvent:event]];
CFRelease(event);
}
else
{
EventRecord event;
event.what = keyDown;
event.message = keycode << 8;
event.modifiers = 0;
// For Safari 3.x
if ([flashView respondsToSelector:#selector(sendEvent:)]) {
[(id)flashView sendEvent:(NSEvent*)&event];
event.what = keyUp;
[(id)flashView sendEvent:(NSEvent*)&event];
}
// For Safari 4.x
else if ([(id)flashView respondsToSelector:#selector(sendEvent:isDrawRect:)]) {
[(id)flashView sendEvent:(NSEvent *)&event isDrawRect:NO];
event.what = keyUp;
[(id)flashView sendEvent:(NSEvent *)&event isDrawRect:NO];
}
else
{
NSLog(#"ERROR: unable to locate event selector for Flash plugin");
}
}
You must first locate the Flash widget in the browser; pass your WebView to this.
- (NSView*)_findFlashViewInView:(NSView*)view
{
NSString* className = [view className];
// WebHostedNetscapePluginView showed up in Safari 4.x,
// WebNetscapePluginDocumentView is Safari 3.x.
if ([className isEqual:#"WebHostedNetscapePluginView"] ||
[className isEqual:#"WebNetscapePluginDocumentView"])
{
// Do any checks to make sure you've got the right player
return view;
}
// Okay, this view isn't a plugin, keep going
for (NSView* subview in [view subviews])
{
NSView* result = [self _findFlashViewInView:subview];
if (result) return result;
}
return nil;
}
Did you make sure that the Flash element has (HTML) keyboard focus?
I managed to figure it out. Here's the code:
[[self window] makeFirstResponder:_tunerView];
CGEventRef e1 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)49, true);
CGEventPost(kCGSessionEventTap, e1);
CFRelease(e1);
CGEventRef e2 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)49, false);
CGEventPost(kCGSessionEventTap, e2);
CFRelease(e2);
window is the main application window. _tunerView is the WebHostedNetscapePluginView. I was setting the first responder when the window first opened, that didn't work. I have to set it right before I send the keys.