<commit_before>//
//  AppDelegate.swift
//  FuturesStatus
//
//  Created by Andrew(Zhiyong) Yang on 5/7/16.
//  Copyright © 2016 FoolDragon. All rights reserved.
//

import Cocoa
import Alamofire
import ReactiveCocoa
import BlocksKit

let UP_CHAR="↓"
let DOWN_CHAR="↑"

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    
    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
    let popover = NSPopover()
    
    let sinaSource = SinaFuturesSource();
    
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
//        let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
        
        if let button = statusItem.button {
//            button.image = NSImage(named: "StatusBarButtonImage")
            button.title = "Waiting...";
            button.action = #selector(AppDelegate.showSettings(_:))
            
        }
        
        sinaSource.newItemSignal.observeNext { (item: InfoItemProtocol) in
            if let button = self.statusItem.button {
                //            button.image = NSImage(named: "StatusBarButtonImage")
                button.image = item.info();
                
            }
        }
        
        sinaSource.itemId = Settings.sharedInstance().itemName.value;
        
        sinaSource.start();
        
        Settings.sharedInstance().itemName.signal.observeNext { (value:String) in
            self.sinaSource.itemId = value;
        }
        
        popover.contentViewController = SettingsViewController(nibName: "SettingsViewController", bundle: nil)
        
    }
    
    func showPopover(sender: AnyObject?) {
        if let button = statusItem.button {
            popover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSRectEdge.MinY)
        }
    }
    
    func closePopover(sender: AnyObject?) {
        popover.performClose(sender)
    }
    func togglePopover(sender: AnyObject?) {
        if popover.shown {
            closePopover(sender)
        } else {
            showPopover(sender)
        }
    }
    func showSettings(sender:AnyObject?){
        togglePopover(sender);<commit_msg>Hide popover while popover window lost focus.<commit_after>//
//  AppDelegate.swift
//  FuturesStatus
//
//  Created by Andrew(Zhiyong) Yang on 5/7/16.
//  Copyright © 2016 FoolDragon. All rights reserved.
//

import Cocoa
import Alamofire
import ReactiveCocoa
import BlocksKit

let UP_CHAR="↓"
let DOWN_CHAR="↑"

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    
    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
    let popover = NSPopover()
    
    let sinaSource = SinaFuturesSource();
    
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
//        let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
        
        if let button = statusItem.button {
//            button.image = NSImage(named: "StatusBarButtonImage")
            button.title = "Waiting...";
            button.action = #selector(AppDelegate.showSettings(_:))
            
        }
        
        sinaSource.newItemSignal.observeNext { (item: InfoItemProtocol) in
            if let button = self.statusItem.button {
                //            button.image = NSImage(named: "StatusBarButtonImage")
                button.image = item.info();
                
            }
        }
        
        sinaSource.itemId = Settings.sharedInstance().itemName.value;
        
        sinaSource.start();
        
        Settings.sharedInstance().itemName.signal.observeNext { (value:String) in
            self.sinaSource.itemId = value;
        }
        
        popover.contentViewController = SettingsViewController(nibName: "SettingsViewController", bundle: nil)
        popover.behavior = NSPopoverBehavior.Transient;
    }
    
    func showPopover(sender: AnyObject?) {
        if let button = statusItem.button {
            popover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSRectEdge.MinY)
        }
    }
    
    func closePopover(sender: AnyObject?) {
        popover.performClose(sender)
    }
    func togglePopover(sender: AnyObject?) {
        if popover.shown {
            closePopover(sender)
        } else {
            showPopover(sender)
        }
    }
    func showSettings(sender:AnyObject?){
        togglePopover(sender);
{'fraction_non_alphanumeric': 0.09495998269521956, 'fraction_numerical': 0.004326195111399524, 'mean_word_length': 2.2290502793296088, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5207522', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1284, 'n_words': 255}