View sizing/positioning problem

12 views
Skip to first unread message

Thomas Balthazar

unread,
Nov 26, 2009, 10:17:06 AM11/26/09
to objec...@googlegroups.com
Hi,

I'm trying to create 3 view that behave like the Genres/Artists/Albums
views at the top of the iTunes window : I want those 3 views to take
the whole width of the window and to grow and shrink with the window
(each view should always take 1/3 of the window width).

I've created a simple project to try to do that, you can download it here :
http://dl.dropbox.com/u/40466/tmp/cappuccino/TestMask.zip

If you run the test project, you'll see that the size of the views
doesn't remain the same when I resize the window.
If you have a look at the views real size with the inspector, you'll
see that, in fact, the views overlap each other.

See the screencast here :
http://dl.dropbox.com/u/40466/tmp/cappuccino/cp-mask.mov

Any idea of what's wrong?

Thanks,
Thomas.

Stefan Wallström

unread,
Nov 29, 2009, 5:40:21 AM11/29/09
to Cappuccino & Objective-J
I'll repeat my answer from the Atlas forums:
This is strange. Cocoa and Cappuccino doesn't seem to use the
autoresizing masks the same way, but Cappuccino seems to do what the
Cocoa docs says:

The Cocoa docs for setAutoresizingMask: says:

“Where more than one option along an axis is set,
resizeWithOldSuperviewSize: by default distributes the size difference
as evenly as possible among the flexible portions. For example, if
NSViewWidthSizable and NSViewMaxXMargin are set and the superview’s
width has increased by 10.0 units, the receiver’s frame and right
margin are each widened by 5.0 units.”

That means that if the window is made 10 pixels wider, your leftmost
view should get 5 pixels wider and it’s right margin should get 5
pixels wider. And this is exactly what happens in Cappuccino. And I
checked the source of NSView in GNUStep and it behaves exactly like
capp too.
This is however not how how it works in Cocoa. It seems to distribute
the extra space proportionally among the flexible parts. If the width
is half the size of the right margin, then if you resize the superview
the width gets 1/3 of the extra space and the right margin 2/3 of
that.

This is a patch that makes you example work like Cocoa (but it's not
tested on so many other cases).

diff --git a/AppKit/CPView.j b/AppKit/CPView.j
index c53892f..b718342 100644
--- a/AppKit/CPView.j
+++ b/AppKit/CPView.j
@@ -911,20 +911,22 @@ var CPViewFlags = { },

var frame = _superview._frame,
newFrame = _CGRectMakeCopy(_frame),
- dX = (_CGRectGetWidth(frame) - aSize.width) /
- (((mask & CPViewMinXMargin) ? 1 : 0) + (mask &
CPViewWidthSizable ? 1 : 0) + (mask & CPViewMaxXMargin ? 1 : 0)),
- dY = (_CGRectGetHeight(frame) - aSize.height) /
- ((mask & CPViewMinYMargin ? 1 : 0) + (mask &
CPViewHeightSizable ? 1 : 0) + (mask & CPViewMaxYMargin ? 1 : 0));
+
+ dX = (_CGRectGetWidth(frame) - aSize.width);
+ flexibleX = (((mask & CPViewMinXMargin) ?
_frame.origin.x : 0) + (mask & CPViewWidthSizable ?
_frame.size.width : 0) + (mask & CPViewMaxXMargin ? aSize.width -
_frame.size.width - _frame.origin.x : 0));
+ dY = (_CGRectGetHeight(frame) - aSize.height);
+ flexibleY = (((mask & CPViewMinYMargin) ?
_frame.origin.y : 0) + (mask & CPViewHeightSizable ?
_frame.size.height : 0) + (mask & CPViewMaxYMargin ? aSize.height -
_frame.size.height - _frame.origin.y : 0));
+

if (mask & CPViewMinXMargin)
- newFrame.origin.x += dX;
+ newFrame.origin.x += dX * (_frame.origin.x / flexibleX);
if (mask & CPViewWidthSizable)
- newFrame.size.width += dX;
+ newFrame.size.width += dX * (_frame.size.width / flexibleX);

if (mask & CPViewMinYMargin)
- newFrame.origin.y += dY;
+ newFrame.origin.y += dY * (_frame.origin.y / flexibleY);
if (mask & CPViewHeightSizable)
- newFrame.size.height += dY;
+ newFrame.size.height += dY * (_frame.size.height /
flexibleY);

[self setFrame:newFrame];
Reply all
Reply to author
Forward
0 new messages