Subversion Repositories Mobile Apps.GyroMouse

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
//
2
//  FirstBootWindowController.swift
3
//  GyroServer
4
//
5
//  Created by Matteo Riva on 07/09/15.
6
//  Copyright © 2015 Matteo Riva. All rights reserved.
7
//
8
 
9
import Cocoa
10
 
11
class FirstBootWindowController: NSWindowController {
12
 
13
    @IBOutlet weak var activeScreenListButton: NSPopUpButton!
14
    @IBOutlet weak var doneButton: NSButton!
15
 
16
    var completion: (() -> Void)?
17
 
18
    override func awakeFromNib() {
19
        super.awakeFromNib()
20
        if #available(OSX 10.10, *) {
21
            self.window?.styleMask = [self.window!.styleMask, .fullSizeContentView]
22
            //window.window?.appearance = NSAppearance(appearanceNamed: NSAppearanceNameVibrantLight, bundle: nil)
23
            self.window?.titleVisibility = .hidden
24
            self.window?.titlebarAppearsTransparent = true
25
        }
26
 
27
        self.window?.backgroundColor = NSColor.white
28
        updateWindowTitlebar()
29
    }
30
 
31
    override func windowDidLoad() {
32
        super.windowDidLoad()
33
 
34
        activeScreenListButton.removeAllItems()
35
        for (i,_) in NSScreen.screens.enumerated() {
36
            activeScreenListButton.addItem(withTitle: "\(1 + i)")
37
        }
38
    }
39
 
40
    @IBAction func setActiveScreenAction(_ sender: NSPopUpButton) {
41
        let i = sender.indexOfSelectedItem
42
        UserDefaults.standard.set(i, forKey: "activeScreen")
43
        NotificationCenter.default.post(name: ActiveScreenDidChangeNotification, object: nil)
44
    }
45
 
46
    @IBAction func doneAction(_ sender: AnyObject) {
47
        UserDefaults.standard.set(true, forKey: "firstBoot")
48
        self.close()
49
        completion?()
50
    }
51
 
52
    func updateWindowTitlebar() {
53
        let kTitlebarHeight: CGFloat = 50
54
        let kFullScreenButtonYOrigin: CGFloat = 3
55
        let windowFrame = self.window!.frame
56
        let fullScreen = (self.window!.styleMask.rawValue & NSWindow.StyleMask.fullScreen.rawValue) == NSWindow.StyleMask.fullScreen.rawValue
57
 
58
        // Set size of titlebar container
59
        let titlebarContainerView = self.window!.standardWindowButton(.closeButton)!.superview!.superview!
60
        var titlebarContainerFrame = titlebarContainerView.frame
61
        titlebarContainerFrame.origin.y = windowFrame.size.height - kTitlebarHeight
62
        titlebarContainerFrame.size.height = kTitlebarHeight
63
        titlebarContainerView.frame = titlebarContainerFrame
64
 
65
        // Set position of window buttons
66
        var x: CGFloat = 12 // initial LHS margin, matching Safari 8.0 on OS X 10.10.
67
        let updateButton = {(buttonView: NSView) in
68
            var buttonFrame = buttonView.frame
69
 
70
            // in fullscreen, the titlebar frame is not governed by kTitlebarHeight but rather appears to be fixed by the system.
71
            // thus, we set a constant Y origin for the buttons when in fullscreen.
72
            buttonFrame.origin.y = fullScreen ?
73
                kFullScreenButtonYOrigin :
74
                round((kTitlebarHeight - buttonFrame.size.height) / 2.0);
75
 
76
            buttonFrame.origin.x = x;
77
 
78
            // spacing for next button, matching Safari 8.0 on OS X 10.10.
79
            x += buttonFrame.size.width + 6;
80
 
81
            buttonView.setFrameOrigin(buttonFrame.origin)
82
        }
83
 
84
        updateButton(self.window!.standardWindowButton(.closeButton)!)
85
        updateButton(self.window!.standardWindowButton(.miniaturizeButton)!)
86
        updateButton(self.window!.standardWindowButton(.zoomButton)!)
87
    }
88
 
89
}