-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWLFullScreenController.m
190 lines (169 loc) · 5.47 KB
/
WLFullScreenController.m
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// LLFullScreenController.m
// Welly
//
// Created by gtCarrera @ 9# on 08-8-11.
// Copyright 2008. All rights reserved.
//
#import "WLFullScreenController.h"
#import "WLFullScreenProcessor.h"
#import "WLTerminalView.h"
#import "WLGlobalConfig.h"
#import <Carbon/Carbon.h>
#import <Quartz/Quartz.h>
@interface WLFullScreenWindow : NSWindow
@end
@implementation WLFullScreenWindow
- (BOOL)canBecomeKeyWindow {
return YES;
}
@end
@implementation WLFullScreenController
@synthesize isInFullScreen = _isInFullScreen;
#pragma mark -
#pragma mark Init
// Initiallize the controller with a given processor
- (id)initWithProcessor:(NSObject <WLFullScreenProcessor>*)pro
targetView:(NSView *)tview
superView:(NSView *)sview
originalWindow:(NSWindow *)owin {
if (self = [super init]) {
_targetView = [tview retain];
_superView = [sview retain];
_originalWindow = [owin retain];
_isInFullScreen = NO;
_screenRatio = 0.0f;
}
return self;
}
// Initiallize the controller with non-processor
// This function ONLY makes the target view showed in full
// screen but cannot resize it
- (id)initWithTargetView:(NSView*)tview
superView:(NSView*)sview
originalWindow:(NSWindow*)owin {
if (self = [super init]) {
_targetView = [tview retain];
_superView = [sview retain];
_originalWindow = [owin retain];
_isInFullScreen = NO;
}
return self;
}
#pragma mark -
#pragma mark Dealloc
- (void)dealloc {
[_fullScreenWindow release];
[super dealloc];
}
#pragma mark -
#pragma mark Handle functions - Control Logic
// The main control function of this object
- (void)handleFullScreen {
if (!_isInFullScreen) {
// Set current state
_isInFullScreen = YES;
// Init the window and show
NSRect screenRect = [[NSScreen mainScreen] frame];
_fullScreenWindow = [[WLFullScreenWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[_fullScreenWindow setAlphaValue:0];
[_fullScreenWindow setBackgroundColor:[NSColor blackColor]];
[_fullScreenWindow setAcceptsMouseMovedEvents:YES];
// Order front now
[_fullScreenWindow makeKeyAndOrderFront:nil];
// Initiallize the animation
CAAnimation * anim = [CABasicAnimation animation];
[anim setDelegate:self];
[anim setDuration:0.8];
// Set the animation to full screen window
[_fullScreenWindow setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"alphaValue"]];
[_fullScreenWindow.animator setAlphaValue:1.0];
// Change UI mode by carbon
SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar);
// Then, let the delegate function do it...
} else {
[self releaseFullScreen];
}
}
// Make the view out of the full screen state
- (void)releaseFullScreen {
if(_isInFullScreen) {
// Change the state
_isInFullScreen = NO;
// Set the super view back
[_superView addSubview:_targetView];
// Pre-process if necessary
// Do not move it to else where!
[self processBeforeExit];
[_fullScreenWindow.animator setAlphaValue:0];
// Change UI mode by carbon
SetSystemUIMode(kUIModeNormal, 0);
// Now, the delegate function will close the window
// So simply do nothing here.
}
}
#pragma mark -
#pragma mark Delegate function
- (void)animationDidStop:(CAAnimation *)animation
finished:(BOOL)flag {
if(!_isInFullScreen) {
// Close the window!
[_fullScreenWindow close];
// Show the main window
[_originalWindow setAlphaValue:100.0f];
} else { // Set the window when the animation is over
// Hide the main window
[_originalWindow setAlphaValue:0.0f];
// Pre-process if necessary
[self processBeforeEnter];
// Record new origin
NSRect screenRect = [[NSScreen mainScreen] frame];
NSPoint newOP = {0, (screenRect.size.height - [_targetView frame].size.height) / 2};
// Set the window style
[_fullScreenWindow setOpaque:NO];
[_fullScreenWindow setBackgroundColor:[[WLGlobalConfig sharedInstance] colorBG]];
// Set the view to the full screen window
[_fullScreenWindow setContentView:_targetView];
// Move the origin point
[[_fullScreenWindow contentView] setFrameOrigin:newOP];
// Focus on the view
[_fullScreenWindow makeFirstResponder:_targetView];
}
}
#pragma mark -
#pragma mark For TerminalView
// Set and reset font size
- (void)setFont:(BOOL)isEnteringFullScreen {
// In case of some stupid uses...
if(_screenRatio == 0.0f)
return;
// Decide whether to set or to reset the font size
CGFloat currRatio = (isEnteringFullScreen ? _screenRatio : (1.0f / _screenRatio));
// And do it..
[[WLGlobalConfig sharedInstance] setEnglishFontSize:
[[WLGlobalConfig sharedInstance] englishFontSize] * currRatio];
[[WLGlobalConfig sharedInstance] setChineseFontSize:
[[WLGlobalConfig sharedInstance] chineseFontSize] * currRatio];
[[WLGlobalConfig sharedInstance] setCellWidth:
[[WLGlobalConfig sharedInstance] cellWidth] * currRatio];
[[WLGlobalConfig sharedInstance] setCellHeight:
[[WLGlobalConfig sharedInstance] cellHeight] * currRatio];
}
// Overrided functions
- (void)processBeforeEnter {
// Get the fittest ratio for the expansion
NSRect screenRect = [[NSScreen mainScreen] frame];
CGFloat ratioH = screenRect.size.height / [_targetView frame].size.height;
CGFloat ratioW = screenRect.size.width / [_targetView frame].size.width;
_screenRatio = (ratioH > ratioW) ? ratioW : ratioH;
// Then, do the expansion
[self setFont:YES];
}
- (void)processBeforeExit {
// And reset the font...
[self setFont:NO];
}
@end