//
// AppDelegate.swift
// Window
//
// Created by iDevFans on 16/6/24.
// Copyright © 2016年 http://www.macdev.io. All rights reserved.
//
import Cocoa
import AppKit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var modalWindow: NSWindow!
var myWindow: NSWindow!
var sessionCode : NSApplication.ModalSession?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application 添加一个消息观察通知
NotificationCenter.default.addObserver(self, selector:#selector(self.windowClose(_:)), name: NSWindow.willCloseNotification, object: nil)
//设置窗口标题图片
self.setWindowTitleImage()
//self.setWindowBKColor()
//添加按钮到标题栏
self.addButtonToTitleBar()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application当应用程序终止时,移除消息观察服务
NotificationCenter.default.removeObserver(self)
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
@IBAction func showModalWindow(_ sender: NSButton) {
//显示模式窗口
NSApplication.shared.runModal(for: self.modalWindow)
// 模式窗口中心显示
self.modalWindow.center()
}
@IBAction func showSessionsWindow(_ sender: NSButton) {
//开始模态模式
sessionCode = NSApplication.shared.beginModalSession(for: self.modalWindow)
}
@objc func windowClose(_ aNotification: Notification){
//判断模式是否开启
if let sessionCode = sessionCode {
NSApplication.shared.endModalSession(sessionCode) //结束模式
self.sessionCode = nil //清空变量
}
//检查通知对象
if let window = aNotification.object as? NSWindow {
if self.modalWindow == window {
NSApplication.shared.stopModal() //停止模式窗口
}
//判断是否终止程序
if window == self.window! {
NSApp.terminate(self)
}
}
}
@IBAction func createWindowAction(_ sender: NSButton) {
self.createWindow()
}
// MARK: 创建窗口
func createWindow() {
//定义窗口大小 let frame = CGRect(0,0,200,200)
let frame = CGRect(x: 0, y: 0, width: 400, height: 280)
//定义窗口样式 let style : NSWindow.StyleMask = [NSWindow.StyleMask.titled,NSWindow.StyleMask.closable,NSWindow.StyleMask.resizable]
let style : NSWindow.StyleMask = [NSWindow.StyleMask.titled,NSWindow.StyleMask.closable,NSWindow.StyleMask.resizable]
//创建window
myWindow = NSWindow(contentRect:frame, styleMask:style, backing:.buffered, defer:false)
myWindow.title = "New Create Window" //设置标题
//显示window
myWindow.makeKeyAndOrderFront(self);//显示窗口
//居中
myWindow.center() //居中心显示
}
//显示图片
func setWindowTitleImage(){
self.window.representedURL = URL(string:"WindowTitle")//显示窗口的
self.window.title = "My Window" //窗口标题 self.window.title
let image = NSImage(named: NSImage.Name(rawValue: "AppIcon.png")) //加载图片 let image = NSTimage(named:NSImage.Name(rawValue:"AppIcon.png"))
self.window.standardWindowButton(.documentIconButton)?.image = image //设置标准窗口按钮的图片图像
}
func setWindowBKColor(){
self.window.backgroundColor = NSColor.green //设置窗口的的背景颜色
}
//设置按钮的标题
func addButtonToTitleBar(){
let titleView = self.window.standardWindowButton(.closeButton)?.superview //这只 标准窗口的 let titleView = self.window.standardWindowButton(.closeButton).superview
let button = NSButton() //定义按钮 let button = NSButton()
let x = (self.window.contentView?.frame.size.width)! - 100 //获取窗口的视图的宽度
let frame = CGRect(x: x, y: 0, width: 80, height: 24) //设置按钮宽度
button.frame = frame
button.title = "Register"
button.bezelStyle = .rounded //按钮的边角类型 button.bezeStyle = .rounded
titleView?.addSubview(button) //titleView?.addSubview(button) 添加到标题视图
}
}