Fixed sizeToFit drawing when there is no image, setNeedsDisplay is no longer needed after sizeToFit.

This commit is contained in:
Aparajita Fishman committed 2010-11-06 17:12:09 -04:00
1 parent e3b6cbcee9
commit 5cd00d71c7
9 files changed
+547 -6

No files matched your search

+3
View File
@@ -493,6 +493,9 @@ CPButtonImageOffset = 3.0;
size.height = MIN(size.height, maxSize.height);
[self setFrameSize:size];
if (contentView)
[self layoutSubviews];
}
- (CGRect)rectForEphemeralSubviewNamed:(CPString)aName
+6 -6
View File
@@ -714,7 +714,12 @@ var _CPimageAndTextViewFrameSizeChangedFlag = 1 << 0,
if (!_textSize)
_textSize = [_text sizeWithFont:_font ? _font : [CPFont systemFontOfSize:12.0]];
if (_imagePosition === CPImageLeft || _imagePosition === CPImageRight)
if (!_image || _imagePosition === CPImageOverlaps)
{
size.width = MAX(size.width, _textSize.width);
size.height = MAX(size.height, _textSize.height);
}
else if (_imagePosition === CPImageLeft || _imagePosition === CPImageRight)
{
size.width += _textSize.width + _imageOffset;
size.height = MAX(size.height, _textSize.height);
@@ -724,11 +729,6 @@ var _CPimageAndTextViewFrameSizeChangedFlag = 1 << 0,
size.width = MAX(size.width, _textSize.width);
size.height += _textSize.height + _imageOffset;
}
else // if (_imagePosition == CPImageOverlaps)
{
size.width = MAX(size.width, _textSize.width);
size.height = MAX(size.height, _textSize.height);
}
}
[self setFrameSize:size];
+258
View File
@@ -0,0 +1,258 @@
/*
* AppController.j
* ButtonLayoutIssue
*
* Created by You on October 19, 2010.
* Copyright 2010, Your Company All rights reserved.
*/
@import <Foundation/CPObject.j>
var ReadMeText = @"Click one of the Add Button buttons to create a button, \n" +
"then click \"Call sizeToFit\" twice with the index of the button.";
@implementation AppController : CPObject
{
CPView contentView;
CPView _navBar;
CPTextField _indexField;
CPArray _contentStack;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
contentView = [theWindow contentView];
var addOldButton = [[CPButton alloc] initWithFrame:CGRectMakeZero()];
_contentStack = [CPArray array];
_navBar = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([contentView frame]), 30)];
[_navBar setBackgroundColor:[CPColor redColor]];
[contentView addSubview:_navBar];
[addOldButton setTitle:@"Add Old Button"];
[addOldButton sizeToFit];
[addOldButton setTarget:self];
[addOldButton setAction:@selector(addOldButton:)];
[addOldButton setFrameOrigin:CGPointMake(10, CGRectGetMaxY([_navBar frame]) + 10)];
[contentView addSubview:addOldButton];
var addNewButton = [[CPButton alloc] initWithFrame:CGRectMakeZero()];
[addNewButton setTitle:@"Add New Button"];
[addNewButton sizeToFit];
[addNewButton setTarget:self];
[addNewButton setAction:@selector(addNewButton:)];
[addNewButton setFrameOrigin:CGPointMake(CGRectGetMaxX([addOldButton frame]) + 10, CGRectGetMaxY([_navBar frame]) + 10)];
[contentView addSubview:addNewButton];
var sizeToFitButton = [CPButton buttonWithTitle:@"Call sizeToFit on button on button at index:"];
[sizeToFitButton setFrameOrigin:CGPointMake(10, CGRectGetMaxY([addOldButton frame]) + 10)];
[contentView addSubview:sizeToFitButton];
[sizeToFitButton setTarget:self];
[sizeToFitButton setAction:@selector(sizeButtonToFit:)];
_indexField = [CPTextField textFieldWithStringValue:@"0" placeholder:@"" width:100];
[_indexField setFrameOrigin:CGPointMake(
CGRectGetMaxX([sizeToFitButton frame]) + 10,
CGRectGetMinY([sizeToFitButton frame]) + (CGRectGetHeight([sizeToFitButton frame]) - CGRectGetHeight([_indexField frame])) / 2.0
)];
[contentView addSubview:_indexField];
var readme = [CPTextField labelWithTitle:ReadMeText];
[readme setFrameOrigin:CGPointMake(CGRectGetMinX([sizeToFitButton frame]), CGRectGetMaxY([sizeToFitButton frame]) + 10)];
[contentView addSubview:readme];
[theWindow orderFront:self];
// Uncomment the following line to turn on the standard menu bar.
//[CPMenu setMenuBarVisible:YES];
}
- (void)_rebuildNavBar
{
var buttons = [_navBar subviews],
xMargin = 15,
xOffset = xMargin;
for (var i = 0, count = [_contentStack count]; i < count; i++)
{
var navButton,
title = [self titleForContentView:_contentStack[i] inViewWithToolbar:self];
if (i < [buttons count])
navButton = [buttons objectAtIndex:i];
else
{
navButton = [[(title === "Broken" ? OldButton : CPButton) alloc] initWithFrame:CGRectMakeZero()];
[buttons addObject:navButton];
navButton._MMViewWithToolbar_index = i;
[navButton setTarget:self];
[navButton setAction:@selector(_navBaraddButton:)];
}
[navButton setTitle:title];
[navButton sizeToFit];
[navButton setFrameOrigin:CGPointMake(
xOffset,
(CGRectGetHeight([_navBar frame]) - CGRectGetHeight([navButton frame])) / 2.0
)];
xOffset = CGRectGetMaxX([navButton frame]) + xMargin;
[navButton setHidden:NO];
}
for (var i = [_contentStack count]; i < [buttons count]; i++)
[buttons[i] setHidden:YES];
[_navBar setSubviews:buttons];
}
- (void)addOldButton:(id)sender
{
[_contentStack addObject:"Broken"];
[_indexField setStringValue:[CPString stringWithFormat:@"%d", [_contentStack count] - 1]];
[self _rebuildNavBar];
}
- (void)addNewButton:(id)sender
{
[_contentStack addObject:"Fixed"];
[_indexField setStringValue:[CPString stringWithFormat:@"%d", [_contentStack count] - 1]];
[self _rebuildNavBar];
}
- (void)sizeButtonToFit:(id)sender
{
[[[_navBar subviews] objectAtIndex:[_indexField intValue]] sizeToFit];
}
- (CPString)titleForContentView:(id)aView inViewWithToolbar:(id)viewWithToolbar
{
return [aView description];
}
@end
@implementation OldButton : CPButton
- (void)sizeToFit
{
[self layoutSubviews];
var size,
contentView = [self ephemeralSubviewNamed:@"content-view"];
if (contentView)
{
[contentView sizeToFit];
size = [contentView frameSize];
}
else
{
size = [([self title] || " ") sizeWithFont:[self currentValueForThemeAttribute:@"font"]];
}
var contentInset = [self currentValueForThemeAttribute:@"content-inset"],
minSize = [self currentValueForThemeAttribute:@"min-size"],
maxSize = [self currentValueForThemeAttribute:@"max-size"];
size.width = MAX(size.width + contentInset.left + contentInset.right, minSize.width);
size.height = MAX(size.height + contentInset.top + contentInset.bottom, minSize.height);
if (maxSize.width >= 0.0)
size.width = MIN(size.width, maxSize.width);
if (maxSize.height >= 0.0)
size.height = MIN(size.height, maxSize.height);
[self setFrameSize:size];
// This bit is in the new version. Without it the layout is not displayed correctly.
// if (contentView)
// [self layoutSubviews];
}
@end
@implementation _CPImageAndTextView (sizeToFitFix)
- (void)sizeToFit
{
var size = CGSizeMakeZero();
if ((_imagePosition !== CPNoImage) && _image)
{
var imageSize = [_image size];
size.width += imageSize.width;
size.height += imageSize.height;
}
if ((_imagePosition !== CPImageOnly) && [_text length] > 0)
{
if (!_textSize)
_textSize = [_text sizeWithFont:_font ? _font : [CPFont systemFontOfSize:12.0]];
if (_text === "Fixed")
{
// In the new code we make sure not to add the image offset if there is not image.
if (!_image || _imagePosition === CPImageOverlaps)
{
size.width = MAX(size.width, _textSize.width);
size.height = MAX(size.height, _textSize.height);
}
else if (_imagePosition === CPImageLeft || _imagePosition === CPImageRight)
{
size.width += _textSize.width + _imageOffset;
size.height = MAX(size.height, _textSize.height);
}
else if (_imagePosition === CPImageAbove || _imagePosition === CPImageBelow)
{
size.width = MAX(size.width, _textSize.width);
size.height += _textSize.height + _imageOffset;
}
}
else
{
// In the old code, if there was no image and an image position other than CPImageOverlaps,
// the image offset would mistakenly get added to the width.
if (_imagePosition === CPImageLeft || _imagePosition === CPImageRight)
{
size.width += _textSize.width + _imageOffset;
size.height = MAX(size.height, _textSize.height);
}
else if (_imagePosition === CPImageAbove || _imagePosition === CPImageBelow)
{
size.width = MAX(size.width, _textSize.width);
size.height += _textSize.height + _imageOffset;
}
else // if (_imagePosition == CPImageOverlaps)
{
size.width = MAX(size.width, _textSize.width);
size.height = MAX(size.height, _textSize.height);
}
}
}
[self setFrameSize:size];
}
@end
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CPApplicationDelegateClass</key>
<string>AppController</string>
<key>CPBundleName</key>
<string>ButtonLayoutIssue</string>
<key>CPPrincipalClass</key>
<string>CPApplication</string>
</dict>
</plist>
+93
View File
@@ -0,0 +1,93 @@
/*
* Jakefile
* sizeToFit
*
* Created by aparajita on November 6, 2010.
* Copyright 2010, Victory-Heart Productions All rights reserved.
*/
var ENV = require("system").env,
FILE = require("file"),
JAKE = require("jake"),
task = JAKE.task,
FileList = JAKE.FileList,
app = require("cappuccino/jake").app,
configuration = ENV["CONFIG"] || ENV["CONFIGURATION"] || ENV["c"] || "Debug",
OS = require("os");
app ("sizeToFit", function(task)
{
task.setBuildIntermediatesPath(FILE.join("Build", "sizeToFit.build", configuration));
task.setBuildPath(FILE.join("Build", configuration));
task.setProductName("sizeToFit");
task.setIdentifier("com.aparajitaworld.sizeToFit");
task.setVersion("1.0");
task.setAuthor("Victory-Heart Productions");
task.setEmail("feedback @nospam@ yourcompany.com");
task.setSummary("sizeToFit");
task.setSources((new FileList("**/*.j")).exclude(FILE.join("Build", "**")));
task.setResources(new FileList("Resources/**"));
task.setIndexFilePath("index.html");
task.setInfoPlistPath("Info.plist");
if (configuration === "Debug")
task.setCompilerFlags("-DDEBUG -g");
else
task.setCompilerFlags("-O");
});
task ("default", ["sizeToFit"], function()
{
printResults(configuration);
});
task ("build", ["default"]);
task ("debug", function()
{
ENV["CONFIGURATION"] = "Debug";
JAKE.subjake(["."], "build", ENV);
});
task ("release", function()
{
ENV["CONFIGURATION"] = "Release";
JAKE.subjake(["."], "build", ENV);
});
task ("run", ["debug"], function()
{
OS.system(["open", FILE.join("Build", "Debug", "sizeToFit", "index.html")]);
});
task ("run-release", ["release"], function()
{
OS.system(["open", FILE.join("Build", "Release", "sizeToFit", "index.html")]);
});
task ("deploy", ["release"], function()
{
FILE.mkdirs(FILE.join("Build", "Deployment", "sizeToFit"));
OS.system(["press", "-f", FILE.join("Build", "Release", "sizeToFit"), FILE.join("Build", "Deployment", "sizeToFit")]);
printResults("Deployment")
});
task ("desktop", ["release"], function()
{
FILE.mkdirs(FILE.join("Build", "Desktop", "sizeToFit"));
require("cappuccino/nativehost").buildNativeHost(FILE.join("Build", "Release", "sizeToFit"), FILE.join("Build", "Desktop", "sizeToFit", "sizeToFit.app"));
printResults("Desktop")
});
task ("run-desktop", ["desktop"], function()
{
OS.system([FILE.join("Build", "Desktop", "sizeToFit", "sizeToFit.app", "Contents", "MacOS", "NativeHost"), "-i"]);
});
function printResults(configuration)
{
print("----------------------------");
print(configuration+" app built at path: "+FILE.join("Build", configuration, "sizeToFit"));
print("----------------------------");
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

@@ -0,0 +1,79 @@
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!--
index.html
ButtonLayoutIssue
Created by You on October 19, 2010.
Copyright 2010, Your Company All rights reserved.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="apple-touch-icon" href="Resources/icon.png" />
<link rel="apple-touch-startup-image" href="Resources/default.png" />
<title>ButtonLayoutIssue</title>
<script type="text/javascript">
OBJJ_MAIN_FILE = "main.j";
OBJJ_INCLUDE_PATHS = ["Frameworks/Debug", "Frameworks", "SomethingElse"];
</script>
<script src="Frameworks/Debug/Objective-J/Objective-J.js" type="text/javascript" charset="UTF-8"></script>
<style type="text/css">
body{margin:0; padding:0;}
#container {position: absolute; top:50%; left:50%;}
#content {width:800px; text-align:center; margin-left: -400px; height:50px; margin-top:-25px; line-height: 50px;}
#content {font-family: "Helvetica", "Arial", sans-serif; font-size: 18px; color: black; text-shadow: 0px 1px 0px white; }
#loadgraphic {margin-right: 0.2em; margin-bottom:-2px;}
</style>
<!--[if lt IE 7]>
<STYLE type="text/css">
#container { position: relative; top: 50%; }
#content { position: relative;}
</STYLE>
<![endif]-->
</head>
<body style="">
<div id="cappuccino-body">
<div id="loadingcontainer" style="background-color: #eeeeee; overflow:hidden; width:100%; height:100%; position: absolute; top: 0; left: 0;">
<script type="text/javascript">
document.write("<div id='container'><p id='content'>" +
"<img id='loadgraphic' width='16' height='16' src='Resources/spinner.gif' /> " +
"Loading ButtonLayoutIssue...</p></div>");
</script>
<noscript>
<div id="container">
<div style="width: 440px; padding: 10px 25px 20px 25px; font-family: sans-serif; background-color: #ffffff; position: relative; left: -245px; top: -120px; text-align: center; -moz-border-radius: 20px; -webkit-border-radius: 20px; color: #555555">
<p style="line-height: 1.4em;">JavaScript is required for this site to work correctly but is either disabled or not supported by your browser.</p>
<p style="font-size:120%; padding:10px;"><a href="http://cappuccino.org/noscript">Show me how to enable JavaScript</a></p>
<p style="font-size:80%;">You may want to upgrade to a newer browser while you're at it:</p>
<ul style="margin:0;padding:0; text-align: center; font-size:80%;" >
<li style="display: inline;"><a href="http://www.apple.com/safari/download/">Safari</a></li>
<li style="display: inline;"><a href="http://www.mozilla.com/en-US/firefox/">Firefox</a></li>
<li style="display: inline;"><a href="http://www.google.com/chrome/">Chrome</a></li>
<li style="display: inline;"><a href="http://www.opera.com/download/">Opera</a></li>
<li style="display: inline;"><a href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">Internet Explorer</a></li>
</ul>
</div>
</div>
</noscript>
</div>
</div>
</body>
</html>
+78
View File
@@ -0,0 +1,78 @@
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!--
index.html
sizeToFit
Created by aparajita on November 6, 2010.
Copyright 2010, Victory-Heart Productions All rights reserved.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="apple-touch-icon" href="Resources/icon.png" />
<link rel="apple-touch-startup-image" href="Resources/default.png" />
<title>sizeToFit</title>
<script type="text/javascript">
OBJJ_MAIN_FILE = "main.j";
</script>
<script type="text/javascript" src="Frameworks/Objective-J/Objective-J.js" charset="UTF-8"></script>
<style type="text/css">
body{margin:0; padding:0;}
#container {position: absolute; top:50%; left:50%;}
#content {width:800px; text-align:center; margin-left: -400px; height:50px; margin-top:-25px; line-height: 50px;}
#content {font-family: "Helvetica", "Arial", sans-serif; font-size: 18px; color: black; text-shadow: 0px 1px 0px white; }
#loadgraphic {margin-right: 0.2em; margin-bottom:-2px;}
</style>
<!--[if lt IE 7]>
<STYLE type="text/css">
#container { position: relative; top: 50%; }
#content { position: relative;}
</STYLE>
<![endif]-->
</head>
<body style="">
<div id="cappuccino-body">
<div id="loadingcontainer" style="background-color: #eeeeee; overflow:hidden; width:100%; height:100%; position: absolute; top: 0; left: 0;">
<script type="text/javascript">
document.write("<div id='container'><p id='content'>" +
"<img id='loadgraphic' width='16' height='16' src='Resources/spinner.gif' /> " +
"Loading sizeToFit...</p></div>");
</script>
<noscript>
<div id="container">
<div style="width: 440px; padding: 10px 25px 20px 25px; font-family: sans-serif; background-color: #ffffff; position: relative; left: -245px; top: -120px; text-align: center; -moz-border-radius: 20px; -webkit-border-radius: 20px; color: #555555">
<p style="line-height: 1.4em;">JavaScript is required for this site to work correctly but is either disabled or not supported by your browser.</p>
<p style="font-size:120%; padding:10px;"><a href="http://cappuccino.org/noscript">Show me how to enable JavaScript</a></p>
<p style="font-size:80%;">You may want to upgrade to a newer browser while you're at it:</p>
<ul style="margin:0;padding:0; text-align: center; font-size:80%;" >
<li style="display: inline;"><a href="http://www.apple.com/safari/download/">Safari</a></li>
<li style="display: inline;"><a href="http://www.mozilla.com/en-US/firefox/">Firefox</a></li>
<li style="display: inline;"><a href="http://www.google.com/chrome/">Chrome</a></li>
<li style="display: inline;"><a href="http://www.opera.com/download/">Opera</a></li>
<li style="display: inline;"><a href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">Internet Explorer</a></li>
</ul>
</div>
</div>
</noscript>
</div>
</div>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
/*
* AppController.j
* sizeToFit
*
* Created by aparajita on November 6, 2010.
* Copyright 2010, Victory-Heart Productions All rights reserved.
*/
@import <Foundation/Foundation.j>
@import <AppKit/AppKit.j>
@import "AppController.j"
function main(args, namedArgs)
{
CPApplicationMain(args, namedArgs);
}