Skip to content

Commit

Permalink
Pull up window title code to a superclass for reuse.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljungberg committed Aug 15, 2012
1 parent 9f59ac9 commit aa17d8c
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 66 deletions.
71 changes: 5 additions & 66 deletions AppKit/CPWindow/_CPStandardWindowView.j
Expand Up @@ -21,7 +21,7 @@
*/

@import "CPTextField.j"
@import "_CPWindowView.j"
@import "_CPTitleableWindowView.j"


var GRADIENT_HEIGHT = 41.0;
Expand Down Expand Up @@ -107,17 +107,15 @@ var _CPStandardWindowViewBodyBackgroundColor = nil,
_CPStandardWindowViewMinimizeButtonHighlightedImage = nil,
_CPStandardWindowViewThemeValues = nil;

var STANDARD_GRADIENT_HEIGHT = 41.0,
STANDARD_TITLEBAR_HEIGHT = 25.0;
var STANDARD_GRADIENT_HEIGHT = 41.0;

@implementation _CPStandardWindowView : _CPWindowView
@implementation _CPStandardWindowView : _CPTitleableWindowView
{
_CPTexturedWindowHeadView _headView;
CPView _dividerView;
CPView _bodyView;
CPView _toolbarView;

CPTextField _titleField;
CPButton _closeButton;
CPButton _minimizeButton;

Expand Down Expand Up @@ -155,33 +153,6 @@ var STANDARD_GRADIENT_HEIGHT = 41.0,
return _CPStandardWindowViewDividerBackgroundColor;
}

+ (CGRect)contentRectForFrameRect:(CGRect)aFrameRect
{
var contentRect = CGRectMakeCopy(aFrameRect),
titleBarHeight = [self titleBarHeight] + 1.0;

contentRect.origin.y += titleBarHeight;
contentRect.size.height -= titleBarHeight;

return contentRect;
}

+ (CGRect)frameRectForContentRect:(CGRect)aContentRect
{
var frameRect = CGRectMakeCopy(aContentRect),
titleBarHeight = [self titleBarHeight] + 1.0;

frameRect.origin.y -= titleBarHeight;
frameRect.size.height += titleBarHeight;

return frameRect;
}

+ (float)titleBarHeight
{
return STANDARD_TITLEBAR_HEIGHT;
}

- (CGRect)contentRectForFrameRect:(CGRect)aFrameRect
{
var contentRect = [[self class] contentRectForFrameRect:aFrameRect],
Expand Down Expand Up @@ -231,7 +202,7 @@ var STANDARD_GRADIENT_HEIGHT = 41.0,
[_headView setAutoresizingMask:CPViewWidthSizable];;
[_headView setHitTests:NO];

[self addSubview:_headView];
[self addSubview:_headView positioned:CPWindowBelow relativeTo:_titleField];

_dividerView = [[CPView alloc] initWithFrame:CGRectMake(0.0, CGRectGetMaxY([_headView frame]), CGRectGetWidth(bounds), 1.0)];

Expand All @@ -253,19 +224,6 @@ var STANDARD_GRADIENT_HEIGHT = 41.0,

[self setResizeIndicatorOffset:CGSizeMake(2.0, 2.0)];

_titleField = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];

[_titleField setAutoresizingMask:CPViewWidthSizable];

[_titleField setStringValue:@"Untitled"];
[_titleField sizeToFit];
[_titleField setAutoresizingMask:CPViewWidthSizable];
[_titleField setStringValue:@""];

[self setNeedsLayout];

[self addSubview:_titleField];

if (_styleMask & CPClosableWindowMask)
{
if (!_CPStandardWindowViewCloseButtonImage)
Expand Down Expand Up @@ -354,10 +312,7 @@ var STANDARD_GRADIENT_HEIGHT = 41.0,
if (_minimizeButton)
leftOffset += 19.0;

[_titleField sizeToFit];
// The vertical alignment of the title is set by the theme, so just give it all available space. By default
// the title will vertically centre within.
[_titleField setFrame:_CGRectMake(leftOffset, 0, width - leftOffset * 2.0, dividerMinY)];
[_titleField setFrame:_CGRectMake(leftOffset, 0, width - leftOffset * 2.0, [[self class] titleBarHeight])];

var contentRect = _CGRectMake(0.0, dividerMaxY, width, _CGRectGetHeight([_bodyView frame]));

Expand Down Expand Up @@ -412,11 +367,6 @@ var STANDARD_GRADIENT_HEIGHT = 41.0,
[self _updateCloseButton];
}

- (void)setTitle:(CPString)aTitle
{
[_titleField setStringValue:aTitle];
}

- (void)mouseDown:(CPEvent)anEvent
{
if (![_headView isHidden])
Expand Down Expand Up @@ -459,15 +409,4 @@ var STANDARD_GRADIENT_HEIGHT = 41.0,
[theWindow setFrame:frame display:NO animate:NO];
}

- (void)layoutSubviews
{
[_titleField setTextColor:[self currentValueForThemeAttribute:@"title-text-color"]];
[_titleField setFont:[self currentValueForThemeAttribute:@"title-font"]];
[_titleField setAlignment:[self currentValueForThemeAttribute:@"title-alignment"]];
[_titleField setVerticalAlignment:[self currentValueForThemeAttribute:@"title-vertical-alignment"]];
[_titleField setLineBreakMode:[self currentValueForThemeAttribute:@"title-line-break-mode"]];
[_titleField setTextShadowColor:[self currentValueForThemeAttribute:@"title-text-shadow-color"]];
[_titleField setTextShadowOffset:[self currentValueForThemeAttribute:@"title-text-shadow-offset"]];
}

@end
114 changes: 114 additions & 0 deletions AppKit/CPWindow/_CPTitleableWindowView.j
@@ -0,0 +1,114 @@
/*
* _CPTitleableWindowView.j
* AppKit
*
* Created by Alexander Ljungberg.
* Copyright 2012, SlevenBits Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

@import "_CPWindowView.j"

var STANDARD_TITLEBAR_HEIGHT = 25.0;

@implementation _CPTitleableWindowView : _CPWindowView
{
CPTextField _titleField;
}

+ (int)titleBarHeight
{
return STANDARD_TITLEBAR_HEIGHT;
}

+ (CGRect)contentRectForFrameRect:(CGRect)aFrameRect
{
var contentRect = CGRectMakeCopy(aFrameRect),
titleBarHeight = [[self class] titleBarHeight];

contentRect.origin.y += titleBarHeight;
contentRect.size.height -= titleBarHeight;

return contentRect;
}

+ (CGRect)frameRectForContentRect:(CGRect)aContentRect
{
var frameRect = CGRectMakeCopy(aContentRect),
titleBarHeight = [[self class] titleBarHeight];

frameRect.origin.y -= titleBarHeight;
frameRect.size.height += titleBarHeight;

return frameRect;
}

- (id)initWithFrame:(CPRect)aFrame styleMask:(unsigned)aStyleMask
{
self = [super initWithFrame:aFrame styleMask:aStyleMask];

if (self)
{
_titleField = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];

[_titleField setHitTests:NO];
[_titleField setStringValue:@"Untitled"];
[_titleField sizeToFit];
[_titleField setAutoresizingMask:CPViewWidthSizable];
[_titleField setStringValue:@""];

[_titleField setFrame:CGRectMake(20.0, 3.0, CGRectGetWidth([self bounds]) - 40.0, CGRectGetHeight([_titleField frame]))];

[self addSubview:_titleField];

[self setNeedsLayout];
}

return self;
}

- (void)setTitle:(CPString)aTitle
{
[_titleField setStringValue:aTitle];
}

- (void)tile
{
[super tile];

var theWindow = [self window],
bounds = [self bounds],
width = CGRectGetWidth(bounds);

// The vertical alignment of the title is set by the theme, so just give it all available space. By default
// the title will vertically centre within.
[_titleField setFrame:_CGRectMake(20.0, 0, width - 40.0, [[self class] titleBarHeight])];
}

- (void)layoutSubviews
{
[super layoutSubviews];

[_titleField setTextColor:[self currentValueForThemeAttribute:@"title-text-color"]];
[_titleField setFont:[self currentValueForThemeAttribute:@"title-font"]];
[_titleField setAlignment:[self currentValueForThemeAttribute:@"title-alignment"]];
[_titleField setVerticalAlignment:[self currentValueForThemeAttribute:@"title-vertical-alignment"]];
[_titleField setLineBreakMode:[self currentValueForThemeAttribute:@"title-line-break-mode"]];
[_titleField setTextShadowColor:[self currentValueForThemeAttribute:@"title-text-shadow-color"]];
[_titleField setTextShadowOffset:[self currentValueForThemeAttribute:@"title-text-shadow-offset"]];
}

@end

0 comments on commit aa17d8c

Please sign in to comment.