summaryrefslogtreecommitdiff
path: root/Sources/Shell/OwlWindow.m
blob: 78ccc5614e611d556cd588b748b49176b6866a25 (plain)
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
/* This file is part of Owl.
 *
 * Copyright © 2019-2021 Sergey Bugaev <bugaevc@gmail.com>
 *
 * Owl is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * Owl 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 General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Owl.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "OwlWindow.h"
#import <Cocoa/Cocoa.h>

@implementation OwlWindow

+ (NSUInteger) styleMaskWhenDisplayingSSD: (BOOL) displaySSD {
    if (!displaySSD) {
        return NSBorderlessWindowMask;
    }
    return NSTitledWindowMask | NSClosableWindowMask |
        NSMiniaturizableWindowMask | NSResizableWindowMask;
}

- (id) initWithSize: (NSSize) size displaySSD: (BOOL) displaySSD {
    NSRect contentRect;
    contentRect.origin = NSMakePoint(500, 500);
    contentRect.size = size;

    NSUInteger styleMask = [OwlWindow styleMaskWhenDisplayingSSD: displaySSD];
    self = [super initWithContentRect: contentRect
                            styleMask: styleMask
                              backing: NSBackingStoreBuffered
                                defer: NO];

    [self setOpaque: NO];
    [self setBackgroundColor: [NSColor clearColor]];
    [self setReleasedWhenClosed: NO];
    [self setAcceptsMouseMovedEvents: YES];

    // Does not automatically happen for borderless windows.
    [NSApp addWindowsItem: self title: @"Window" filename: NO];

    return self;
}

- (void) setTitle: (NSString *) newTitle {
    newTitle = [newTitle retain];
    [_title release];
    _title = newTitle;
    [super setTitle: newTitle];
    [NSApp changeWindowsItem: self title: newTitle filename: NO];
}

- (void) dealloc {
    [_title release];
    [super dealloc];
}

- (BOOL) displaySSD {
    return ([self styleMask] & NSTitledWindowMask) != 0;
}

- (void) setDisplaySSD: (BOOL) displaySSD {
    [self setStyleMask: [OwlWindow styleMaskWhenDisplayingSSD: displaySSD]];
    if (_title != nil) {
        [self setTitle: _title];
    }
}

- (IBAction) toggleDisplaySSD: (NSMenuItem *) sender {
    BOOL displaySSD = ![self displaySSD];
    [self setDisplaySSD: displaySSD];
    [sender setState: displaySSD];
}

- (BOOL) canBecomeKeyWindow {
    return YES;
}

- (BOOL) canBecomeMainWindow {
    return YES;
}

- (void) runInteractiveMove {
    NSPoint originalMouseLocation = [NSEvent mouseLocation];
    NSPoint originalOrigin = [self frame].origin;
    NSEventMask mask = NSLeftMouseUpMask | NSMouseMovedMask | NSLeftMouseDraggedMask;

    while (YES) {
        NSEvent *event = [NSApp nextEventMatchingMask: mask
                                            untilDate: [NSDate distantFuture]
                                               inMode: NSEventTrackingRunLoopMode
                                              dequeue: YES];

        if ([event type] == NSLeftMouseUp) {
            break;
        }

        NSPoint mouseLocation = [NSEvent mouseLocation];

        NSPoint origin = originalOrigin;
        origin.x += mouseLocation.x - originalMouseLocation.x;
        origin.y += mouseLocation.y - originalMouseLocation.y;
        [self setFrameOrigin: origin];
    }
}

@end