Window/View Size Constraining

  • The following code is my attempt to constrain a displayView and window
    size such that the displayView maintains the proper aspect ratio when the
    window is resized.  I have two issues:

    1.  It sort of works. However, the displayView size tracking lags by at
    least one time sample and I then have to gently nudge the window size to get
    it right.

    2.  This is a variable size window that is intended to show a miniature
    display of what is in the main window (elsewhere).  I made an abortive
    attempt to scale the coordinates of displayView.  Apparently, I don't
    understand the "scaleUnitSquareToSize" call and the docs don't help.  Any
    suggestions?

    //    Window delegate for size
    - (NSSize)windowWillResize:(NSWindow*)sender
                        toSize:(NSSize)frameSize
    {
        //    displayView/controlView are outlets to top/bottom views in the
    window.
        //    displayView has variable height, controlView fixed height.

        //    First, get the size of main view (elsewhere).
        NSSize fullSize = [[document
    valueForKeyPath:@"course.props.presentaionSize"] sizeValue];
        float frameWidth = frameSize.width;
        //    Maintain aspect ratio of display view.
        float displayHeight = frameWidth * fullSize.height / fullSize.width;
        [displayView setFrameSize:NSMakeSize(frameWidth, displayHeight)];

        float controlViewHeight = [controlView frame].size.height;
        float winHeight = displayHeight + controlViewHeight;

    //    Abortive attempt to scale display coordinates to fit.
    //    float scale = frameWidth / fullSize.width;    //  Tried reciprocal
    also.
    //    [displayView scaleUnitSquareToSize:NSMakeSize(scale, scale)];

      return NSMakeSize (frameSize.width, winHeight);
    }
  • I totally rewrote the "windowWillResize" delegate method to correctly
    compute size by contentRect rather than frameRect.  It works correctly
    except for the same remaining two issues.

    > The following code is my attempt to constrain a displayView and window
    > size such that the displayView maintains the proper aspect ratio when the
    > window is resized.  I have two issues:
    >
    > 1.  The displayView size tracking lags by at
    > least one time sample and I then have to gently nudge the window size to get
    > it right, or at least within one pixel.  I don't understand why the display
    view lags, since I'm computing everything based on the proposed window size, not
    current size.  I could try to clean it up in "windowDidResize", but could end up
    in an infinite loop trying to do that.
    >
    > 2.  This is a variable size window that is intended to show a miniature
    > display of what is in the main window (elsewhere), with a fixed height
    controlView below..  I made an abortive
    > attempt to scale the coordinates of displayView.  Apparently, I don't
    > understand the "scaleUnitSquareToSize" call and the docs don't help much.  Any
    > suggestions?

    - (float)aspectRatio
    {
        NSSize fullSize = [self mainDisplaySize];    //    From original (large)
    window.
        return fullSize.width / fullSize.height;
    }

    - (float)newDisplayHeightFromWidth:(float)width
    {
        //    Computes height to maintain correct aspect ratio.
        return  width / [self aspectRatio];
    }

    - (float)contentHeightFromWidth:(float)width
    {
        float displayViewHeight = [self newDisplayHeightFromWidth:width];
        float controlViewHeight = controlView == nil ? 0 : [controlView
    frame].size.height;
        return displayViewHeight + controlViewHeight;
    }

    //    Window delegate for size
    - (NSSize)windowWillResize:(NSWindow*)sender
                        toSize:(NSSize)frameSize
    {
        NSRect frameRect = [sender frame];    //    To set origin.
        frameRect.size = frameSize;    //    Set to proposed size
        NSRect contentRect = [sender contentRectForFrameRect:frameRect];
        NSSize contentSize = contentRect.size;
        float contentWidth = contentSize.width;

        //    Set displaView size.
        [displayView setFrameSize:NSMakeSize(contentWidth, [self
    newDisplayHeightFromWidth:contentWidth])];

    //    Abortive attempt to scale display to fit.
    //    NSSize fullSize = [self mainDisplaySize];    //    From original
    (large) window.
    //    float scale = fullSize.width / contentWidth;
    //    [displayView scaleUnitSquareToSize:NSMakeSize(scale, scale)];

        contentRect.size = NSMakeSize(contentWidth, [self
    contentHeightFromWidth:contentWidth]);
        frameRect = [sender frameRectForContentRect:contentRect];

        return frameRect.size;
    }
  • In case anybody is interested, I eventually solved both issues.  After I
    found out that "setBoundsSize" also sets the scaling, the scaling works
    right.

        The window/view tracking issue was solved after I figured out that
    "windowDidResize" gets called after each movement, not at the end as I
    erroneously surmised.  I let the "windowWillResize" constrain the window
    size during tracking, then recomputed window/view sizes in
    "windowDidResize".  Now, it all works correctly.

    > I totally rewrote the "windowWillResize" delegate method to correctly
    > compute size by contentRect rather than frameRect.  It works correctly except
    > for the same remaining two issues.
    >
    >> The following code is my attempt to constrain a displayView and window
    >> size such that the displayView maintains the proper aspect ratio when the
    >> window is resized.  I have two issues:
    >>
    >> 1.  The displayView size tracking lags by at
    >> least one time sample and I then have to gently nudge the window size to get
    >> it right, or at least within one pixel.  I don't understand why the display
    >> view lags, since I'm computing everything based on the proposed window size,
    >> not current size.  I could try to clean it up in "windowDidResize", but could
    >> end up in an infinite loop trying to do that.
    >>
    >> 2.  This is a variable size window that is intended to show a miniature
    >> display of what is in the main window (elsewhere), with a fixed height
    >> controlView below..  I made an abortive
    >> attempt to scale the coordinates of displayView.  Apparently, I don't
    >> understand the "scaleUnitSquareToSize" call and the docs don't help much.
    >> Any
    >> suggestions?
    >
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today