A MacOS menu bar applet that displays the current lunar phase.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.9 KiB

//
// ViewController.swift
// LunaMac
//
// Created by Claire Davis on 9/22/22.
// Copyright © 2022 A Better Geek. All rights reserved.
//
import Cocoa
class ViewController: NSViewController, NSWindowDelegate, NSTextViewDelegate {
let appDelegate = NSApplication.shared.delegate as! AppDelegate
@IBOutlet var textView: NSTextView!
@IBOutlet var label: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
toggleWindow(boolShow: true)
}
override func viewDidAppear() {
self.view.window?.delegate = self
let title = self.view.window?.title ?? ""
// use this to load the changelog, maybe?
if (title.contains("Changelog")) {
// get log file path
let logPath = Bundle.main.path(forResource: "changelog.txt", ofType: nil)!
// get contents of log file
let logText = (try? String(contentsOfFile: logPath, encoding: String.Encoding.utf8)) ?? "Changelog couldn't be read."
// create an object to hold the text and its formatting
let textStorage = NSTextStorage(string: logText)
textStorage.font = NSFont(name: "Monaco", size: 11)
textStorage.foregroundColor = NSColor.textColor
// replace textView's textStorage contents
self.textView?.layoutManager?.replaceTextStorage(textStorage)
}
else if (title.contains("About")) {
// set label with version number
if (label.tag == 100) {
label.stringValue = ("Using LunaMac " + Bundle.main.appBuild)
}
}
}
// triggers when window is closed
override func viewWillDisappear() {
toggleWindow(boolShow: false)
}
func toggleWindow(boolShow: Bool = false) {
// get the window title to set the right bool
// window title is in self.view.window.title
let winTitle = self.view.window?.title
if (winTitle?.contains("About") ?? false) {
appDelegate.infoOpen = boolShow
}
else if (winTitle?.contains("Changelog") ?? false) {
appDelegate.logOpen = boolShow
}
}
}
extension Bundle {
public var appName: String { return getInfo("CFBundleName") }
public var displayName: String { return getInfo("CFBundleDisplayName")}
public var language: String { return getInfo("CFBundleDevelopmentRegion")}
public var identifier: String { return getInfo("CFBundleIdentifier")}
public var copyright: String { return getInfo("NSHumanReadableCopyright").replacingOccurrences(of: "\\\\n", with: "\n") }
public var appBuild: String { return getInfo("CFBundleVersion") }
public var appVersionLong: String { return getInfo("CFBundleShortVersionString") }
fileprivate func getInfo(_ str: String) -> String { return infoDictionary?[str] as? String ?? "" }
}