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.

62 lines
1.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 {
let appDelegate = NSApplication.shared.delegate as! AppDelegate
@IBOutlet var textView: NSTextView!
override func viewDidLoad() {
super.viewDidLoad()
toggleWindow(boolShow: true)
}
override func viewDidAppear() {
self.view.window?.delegate = self
// use this to load the changelog, maybe?
if (self.view.window?.title.contains("Changelog") ?? false) {
// 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)
}
}
// 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
}
}
}