89 lines
2.8 KiB
Swift
89 lines
2.8 KiB
Swift
//
|
|
// StatusBarController.swift
|
|
// LiquipediaMenu
|
|
//
|
|
// Created by Felix Förtsch on 08.10.18.
|
|
// Copyright © 2018 Felix Förtsch. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
class StatusBarController: NSObject, NSMenuItemValidation {
|
|
|
|
@IBOutlet weak var matchView: MatchView!
|
|
|
|
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
|
|
return true
|
|
}
|
|
|
|
let statusBar = NSMenu()
|
|
let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
|
let matchesAPI = LiquipediaMatchesAPI()
|
|
var matches = [Match]()
|
|
|
|
override func awakeFromNib() {
|
|
// Set the icon of the statusbar item and put it into the statusbar.
|
|
let icon = NSImage(named: "statusBarIcon")
|
|
icon?.isTemplate = true
|
|
statusBarItem.image = icon
|
|
statusBarItem.menu = statusBar
|
|
|
|
// Add the starting menu items.
|
|
statusBar.addItem(NSMenuItem.init(title: "Refresh", action: #selector(refreshClicked), keyEquivalent: ""))
|
|
statusBar.addItem(NSMenuItem.init(title: "Quit", action: #selector(quitClicked), keyEquivalent: ""))
|
|
|
|
// Set the target to self, so the selectors know where to select.
|
|
for item in statusBar.items {
|
|
item.target = self
|
|
}
|
|
|
|
//--
|
|
statusBar.addItem(NSMenuItem.separator())
|
|
//--
|
|
|
|
// TODO: Refresh on Startup
|
|
}
|
|
|
|
@objc func refreshClicked(_ sender: NSMenuItem) {
|
|
|
|
// TODO: Do refresh async
|
|
// DispatchQueue.global().async {
|
|
// TODO: Add Spinner to indicate that loading is going on.
|
|
|
|
for item in self.statusBar.items {
|
|
if item.tag == 1 {
|
|
self.statusBar.removeItem(item)
|
|
}
|
|
}
|
|
|
|
|
|
if let matches = self.matchesAPI.fetchMatches(for: "dota2") {
|
|
for match in matches {
|
|
let newItem = MatchMenuItem.init(for: match, action: #selector(self.openStreamLink))
|
|
newItem.tag = 1
|
|
newItem.target = self
|
|
newItem.view = matchView
|
|
self.statusBar.addItem(newItem)
|
|
}
|
|
} else {
|
|
let newItem = NSMenuItem.init(title: "There was an error receiving ongoing matches.", action: nil, keyEquivalent: "")
|
|
newItem.tag = 1
|
|
newItem.target = self
|
|
self.statusBar.addItem(newItem)
|
|
}
|
|
// }
|
|
|
|
}
|
|
|
|
@objc func quitClicked() {
|
|
NSApplication.shared.terminate(self)
|
|
}
|
|
|
|
@objc func openStreamLink(_ sender: MatchMenuItem) {
|
|
if let url = URL(string: sender.streamLink) {
|
|
NSWorkspace.shared.open(url)
|
|
}
|
|
}
|
|
|
|
}
|