Skip to content

Instantly share code, notes, and snippets.

@kballenegger
Last active December 31, 2015 16:38
Show Gist options
  • Select an option

  • Save kballenegger/8014488 to your computer and use it in GitHub Desktop.

Select an option

Save kballenegger/8014488 to your computer and use it in GitHub Desktop.
Custom window problem
{
NSView *emailFieldContainer = [[NSView alloc] initWithFrame:NSMakeRect(80.0/2, 150.0/2, 650.0/2, 60.0/2)];
self->emailField = [[NSTextField alloc] initWithFrame:NSMakeRect(10.0/2, 10.0/2, 670.0/2, 40.0/2)];
self->emailField.stringValue = @"[email protected]";
[self->emailField setFont:boldFont];
[self->emailField setEditable:YES];
self->emailField.drawsBackground = NO;
[self->emailField setBezeled:NO];
self->emailField.textColor = [NSColor colorWithCalibratedRed:0.872 green:0.197 blue:0.228 alpha:1.000];
[self->emailField setWantsLayer:YES];
[emailFieldContainer addSubview:self->emailField];
[emailFieldContainer setWantsLayer:YES];
emailFieldContainer.layer.cornerRadius = kMacchiatoEmailSubscriptionWindowButtonRadius;
emailFieldContainer.layer.borderWidth = 1.0;
emailFieldContainer.layer.borderColor = blueColor.CGColor;
}
- (void)show {
[NSApp runModalForWindow:self.window];
[self.window makeKeyAndOrderFront:nil];
[self.window makeFirstResponder:self->emailField];
}
//
// NewsletterWindow.m
// Macchiato
//
// Created by Kenneth Ballenegger on 4/7/13.
// Copyright (c) 2013 Azure Talon. All rights reserved.
//
const NSSize kNewsletterWindowSize = (NSSize){.width = 800/2, .height = 508/2};
#import "NewsletterWindow.h"
@implementation NewsletterWindow {
@private
NSView *childContentView;
}
- (id)initWithContentRect:(__unused NSRect)rect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation {
NSSize screenSize = [[NSScreen mainScreen] visibleFrame].size;
NSPoint origin = (NSPoint){
.x = (screenSize.width / 2 - kNewsletterWindowSize.width),
.y = (screenSize.height / 2 - kNewsletterWindowSize.height)
};
NSRect contentRect = (NSRect){.origin = origin, .size = kNewsletterWindowSize};
self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:bufferingType
defer:deferCreation];
if (self) {
[super setOpaque:NO];
[super setBackgroundColor:[NSColor clearColor]];
}
return self;
}
- (NSRect)contentRectForFrameRect:(NSRect)frameRect {
return frameRect;
}
+ (NSRect)frameRectForContentRect:(NSRect)cRect styleMask:(NSUInteger)_ {
return cRect;
}
#pragma mark Override Super
- (void)setContentView:(NSView *)aView {
if ([self->childContentView isEqualTo:aView]) {
return;
}
NSRect bounds = self.frame;
bounds.origin = NSZeroPoint;
NSImageView *backgroundView = [super contentView];
if (!backgroundView) {
backgroundView = [[NSImageView alloc] initWithFrame:bounds];
backgroundView.image = [NSImage imageNamed:@"NewsletterWindow"];
[super setContentView:backgroundView];
}
if (self->childContentView) {
[self->childContentView removeFromSuperview];
}
self->childContentView = aView;
[self->childContentView setFrame:[self contentRectForFrameRect:bounds]];
[self->childContentView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[backgroundView addSubview:self->childContentView];
}
- (NSView *)contentView {
return self->childContentView;
}
//- (void)setFrame:(NSRect)frameRect display:(BOOL)flag {
// [self setFrame:frameRect display:flag animate:NO];
//}
//
//- (void)setFrame:(NSRect)frameRect display:(BOOL)displayFlag animate:(BOOL)animateFlag {
// frameRect.size = kNewsletterWindowSize;
// [super setFrame:frameRect display:displayFlag animate:animateFlag];
//}
- (void)setOpaque:(BOOL)isOpaque {
[super setOpaque:NO];
}
- (void)setBackgroundColor:(NSColor *)color {
[super setBackgroundColor:[NSColor clearColor]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment