• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to make NSWindow redraw vacated rects "ifNeeded"?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to make NSWindow redraw vacated rects "ifNeeded"?


  • Subject: Re: How to make NSWindow redraw vacated rects "ifNeeded"?
  • From: Jerry Krinock <email@hidden>
  • Date: Sun, 8 Apr 2007 19:43:51 -0700


On 2007 Apr, 08, at 15:46, Adam R. Maxwell wrote:

Did you try [[window contentView] setNeedsDisplay:YES]? You need to tell the box's superview to redisplay, or else the displayIfNeeded won't have any dirty rects to redraw (as far as I understand).

I just tried that now and indeed, it works. Furthermore, as Andrew suggested, the -setNeedsDisplay messages are unnecessary when your suggestion is used.


However, I don't think that your suggested idiom:

    [[window contentView] setNeedsDisplay:YES] ;
    [window displayIfNeeded] ;

is any more efficient than simply:

    [window display] ;

because the documentation for -setNeedsDisplay says "If YES, marks the receiver’s entire bounds as needing display". Think about it...if you mark a "whole bounds" as needing display (whether it needs it or not), and then display the parts that you marked, well, that's the same as just displaying the whole damned window, right!

However, your idea does suggest a "more efficient" idiom for moving a subview:

    [[window contentView] setNeedsDisplayInRect:[subview frame]] ;
    [subview setFrame:newRect] ;
    [subview setNeedsDisplay:YES] ;

Now, you see the window is only displaying the two rects (subview's old and new) that really "need" display. So, this in fact may be the correct way to do it, although three lines of code to do a simple thing like move a subview seems kind of un-Cocoa-like.

In my actual project, I recalculate the whole window layout and have lots of subviews to consider, so if this is the best answer, I believe I'll put coding efficiency over computing efficiency, and just send [window display] at the end, that is, "display the whole damned window whether it needs it or not". Maybe someday Apple will do some optimization under the hood to figure out what "really" needs to be displayed, or maybe they already have, since these methods date back to Mac OS 10.0.

For the record though, here's my demo project again, revised to use this "more efficient" idiom:


/* How to Create a window with a subview, and then later, move the subview */


@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)noti {
    // Create window
    NSWindow* window = [[NSWindow alloc]
      initWithContentRect:NSMakeRect(0,0,300,100)
                styleMask:NSTitledWindowMask
                  backing:NSBackingStoreBuffered
                    defer:NO] ;
    [window center] ;

    // Create a box and add to window
    NSBox* box ;
    box = [[NSBox alloc]
      initWithFrame:NSMakeRect(20,10,260,40)] ;
    [[window contentView] addSubview:box] ;

    [window orderFront:self] ;

    // Move the subview
    [[window contentView] setNeedsDisplayInRect:[box frame]] ;
    [box setFrame:NSMakeRect(20,50,260,40)] ;
    [box setNeedsDisplay:YES] ;

    // Update the window
    [window displayIfNeeded] ;
}

@end

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: How to make NSWindow redraw vacated rects "ifNeeded"?
      • From: Andrew Farmer <email@hidden>
    • Re: How to make NSWindow redraw vacated rects "ifNeeded"?
      • From: "Adam R. Maxwell" <email@hidden>
References: 
 >How to make NSWindow redraw vacated rects "ifNeeded"? (From: Jerry Krinock <email@hidden>)
 >Re: How to make NSWindow redraw vacated rects "ifNeeded"? (From: Andrew Farmer <email@hidden>)
 >Re: How to make NSWindow redraw vacated rects "ifNeeded"? (From: Jerry Krinock <email@hidden>)
 >Re: How to make NSWindow redraw vacated rects "ifNeeded"? (From: "Adam R. Maxwell" <email@hidden>)

  • Prev by Date: Re: Strange behavior with nib loading
  • Next by Date: Re: Multiple developers and nib files
  • Previous by thread: Re: How to make NSWindow redraw vacated rects "ifNeeded"?
  • Next by thread: Re: How to make NSWindow redraw vacated rects "ifNeeded"?
  • Index(es):
    • Date
    • Thread