1/*
2    PPScreencastPopupPanelController.m
3
4    Copyright 2013-2018 Josh Freeman
5    http://www.twilightedge.com
6
7    This file is part of PikoPixel for Mac OS X and GNUstep.
8    PikoPixel is a graphical application for drawing & editing pixel-art images.
9
10    PikoPixel is free software: you can redistribute it and/or modify it under
11    the terms of the GNU Affero General Public License as published by the
12    Free Software Foundation, either version 3 of the License, or (at your
13    option) any later version approved for PikoPixel by its copyright holder (or
14    an authorized proxy).
15
16    PikoPixel is distributed in the hope that it will be useful, but WITHOUT ANY
17    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18    FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19    details.
20
21    You should have received a copy of the GNU Affero General Public License
22    along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#import "PPOptional.h"
26#if PP_OPTIONAL__BUILD_WITH_SCREENCASTING
27
28#import "PPScreencastPopupPanelController.h"
29
30#import "PPUIColors_Panels.h"
31
32
33#define kScreencastPopupPanelNibName        @"ScreencastPopupPanel"
34
35#define kScreencastPopupPanelBottomMargin   17.0f
36
37
38@interface PPScreencastPopupPanelController (PrivateMethods)
39
40- (void) setupPanelPosition;
41- (void) increasePanelWindowLevel;
42
43@end
44
45@implementation PPScreencastPopupPanelController
46
47- initWithWindowNibName: (NSString *) windowNibName
48{
49    self = [super initWithWindowNibName: windowNibName];
50
51    if (!self)
52        goto ERROR;
53
54    if (!PP_RUNTIME_CHECK_OPTIONAL__RUNTIME_SUPPORTS_SCREENCASTING)
55    {
56        goto ERROR;
57    }
58
59    return self;
60
61ERROR:
62    [self release];
63
64    return nil;
65}
66
67- (void) setStateString: (NSString *) stateString
68{
69    // method may be called before window's loaded, so check _panelDidLoad
70
71    if (stateString)
72    {
73        if (!_panelDidLoad)
74        {
75            [self window];  // force load
76        }
77
78        [_stateTextField setStringValue: stateString];
79
80        [[self window] orderFront: self];
81    }
82    else
83    {
84        if (!_panelDidLoad)
85        {
86            return;
87        }
88
89        [[self window] orderOut: self];
90
91        [_stateTextField setStringValue: @""];
92    }
93}
94
95#pragma mark NSWindowController overrides
96
97- (void) windowDidLoad
98{
99    [super windowDidLoad];
100
101    [self setupPanelPosition];
102
103    [self increasePanelWindowLevel];    // make sure popup stays in front of other popups
104}
105
106#pragma mark PPPopupPanelController overrides
107
108+ (NSString *) panelNibName
109{
110    return kScreencastPopupPanelNibName;
111}
112
113- (NSColor *) backgroundColorForPopupPanel
114{
115    return kUIColor_ScreencastPopupPanel_Background;
116}
117
118#pragma mark Private methods
119
120- (void) setupPanelPosition
121{
122    NSScreen *mainScreen;
123    NSRect screenFrame, screenVisibleFrame, panelFrame;
124    NSWindow *panel;
125    NSPoint panelOrigin;
126
127    mainScreen = [NSScreen mainScreen];
128
129    screenFrame = [mainScreen frame];
130    screenVisibleFrame = [mainScreen visibleFrame];
131
132    panel = [self window];
133
134    panelFrame = [panel frame];
135
136    panelOrigin.x =
137        roundf(screenFrame.origin.x + ((screenFrame.size.width - panelFrame.size.width) / 2.0f));
138
139    panelOrigin.y =
140        roundf(screenVisibleFrame.origin.y + kScreencastPopupPanelBottomMargin);
141
142    [panel setFrameOrigin: panelOrigin];
143}
144
145- (void) increasePanelWindowLevel
146{
147    NSWindow *panel = [self window];
148
149    [panel setLevel: [panel level] + 1];
150}
151
152@end
153
154#endif  // PP_OPTIONAL__BUILD_WITH_SCREENCASTING
155