<commit_before>//
//  MainWindowController.swift
//  Train12306
//
//  Created by fancymax on 15/7/30.
//  Copyright (c) 2015年 fancy. All rights reserved.
//

import Cocoa

class MainWindowController: NSWindowController{
    @IBOutlet weak var loginButton: LoginButton!
    @IBOutlet var LoginMenu: NSMenu!
    @IBOutlet var IPLabel: NSTextField!
    
    var orderQueryViewController:OrderViewController?
    var ticketQueryViewController:TicketQueryViewController?
    
    var loginWindowController:LoginWindowController!
    
    lazy var preferencesWindowController:NSWindowController = {
        let generalViewController = GeneralPreferenceViewController()
        let advanceViewController = AdvancedPreferenceViewController()
        let controllers = [generalViewController,advanceViewController]
        
        return MASPreferencesWindowController(viewControllers:controllers,title: nil)
    }()
    
    
    let TrainBook = "车票预订"
    let TrainOrder = "订单查询"
    
    override func windowDidLoad()
    {
        super.windowDidLoad()
        
        let aWindow = self.window as! INAppStoreWindow
        aWindow.titleBarHeight = 38.0
    	aWindow.centerTrafficLightButtons = true;
        
        aWindow.titleBarDrawingBlock = {[unowned self] drawsAsMainWindow, drawingRect, edge, clippingPath  in
            NSNotificationCenter.defaultCenter().postNotificationName(NSWindowDidMoveNotification, object:self.window)
        }
        
        
        let titleView = aWindow.titleBarView
        let buttonPoint = NSMakePoint(85, 5)
        self.loginButton.setFrameOrigin(buttonPoint)
        titleView.addSubview(self.loginButton)
        
        let segmentSize = NSMakeSize(120, 25)
        let segmentFrame = NSMakeRect(
            NSMidX(titleView.bounds) - (segmentSize.width / 2.0),
            NSMidY(titleView.bounds) - (segmentSize.height / 2.0),
            segmentSize.width, segmentSize.height)
        let segment = NSSegmentedControl(frame: segmentFrame)
        segment.segmentCount = 2
        segment.setLabel(TrainBook, forSegment: 0)
        segment.setLabel(TrainOrder, forSegment: 1)
        segment.selectedSegment = 0
        segment.segmentStyle = NSSegmentStyle.TexturedSquare
        segment.target = self
        segment.action = #selector(MainWindowController.segmentTab(_:))
        
        titleView.addSubview(segment)
        
        segment.translatesAutoresizingMaskIntoConstraints = false
        
        let segmentConstraint1 = NSLayoutConstraint(item: segment, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
        let segmentConstraint2 = NSLayoutConstraint(item: segment, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
        NSLayoutConstraint.activateConstraints([segmentConstraint1,segmentConstraint2])
        
        selectModule(TrainBook)
        
        self.window?.recalculateKeyViewLoop()
        
        //login notification
        let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(self, selector: #selector(MainWindowController.receiveDidSendLoginMessageNotification(_:)), name: DidSendLoginMessageNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(MainWindowController.receiveAutoLoginMessageNotification(_:)), name: DidSendAutoLoginMessageNotification, object: nil)
    }
    
    func segmentTab(sender: NSSegmentedControl){
        selectModule(sender.labelForSegment(sender.selectedSegment)!)
    }
    
    func selectModule(moduleName:String){
        if(moduleName == TrainOrder){
            if orderQueryViewController == nil{
                orderQueryViewController = OrderViewController()
            }
            self.window?.contentView = orderQueryViewController!.view
        }
        else if(moduleName == TrainBook){
            if ticketQueryViewController == nil{
                ticketQueryViewController = TicketQueryViewController()
            }
            self.window?.contentView = ticketQueryViewController!.view
        }
    }
    
    func receiveDidSendLoginMessageNotification(note: NSNotification){
        print("receiveDidSendLoginMessageNotification")
        loginOut()
        login(isAutoLogin: false)<commit_msg>处理快速切换 车票预订 和 订单查询 会异常的Bug<commit_after>//
//  MainWindowController.swift
//  Train12306
//
//  Created by fancymax on 15/7/30.
//  Copyright (c) 2015年 fancy. All rights reserved.
//

import Cocoa

class MainWindowController: NSWindowController{
    @IBOutlet weak var loginButton: LoginButton!
    @IBOutlet var LoginMenu: NSMenu!
    @IBOutlet var IPLabel: NSTextField!
    
    var orderQueryViewController = OrderViewController()
    var ticketQueryViewController = TicketQueryViewController()
    
    var loginWindowController:LoginWindowController!
    
    lazy var preferencesWindowController:NSWindowController = {
        let generalViewController = GeneralPreferenceViewController()
        let advanceViewController = AdvancedPreferenceViewController()
        let controllers = [generalViewController,advanceViewController]
        
        return MASPreferencesWindowController(viewControllers:controllers,title: nil)
    }()
    
    
    let TrainBook = "车票预订"
    let TrainOrder = "订单查询"
    
    override func windowDidLoad()
    {
        super.windowDidLoad()
        
        let aWindow = self.window as! INAppStoreWindow
        aWindow.titleBarHeight = 38.0
    	aWindow.centerTrafficLightButtons = true;
        
        aWindow.titleBarDrawingBlock = {[unowned self] drawsAsMainWindow, drawingRect, edge, clippingPath  in
            NSNotificationCenter.defaultCenter().postNotificationName(NSWindowDidMoveNotification, object:self.window)
        }
        
        
        let titleView = aWindow.titleBarView
        let buttonPoint = NSMakePoint(85, 5)
        self.loginButton.setFrameOrigin(buttonPoint)
        titleView.addSubview(self.loginButton)
        
        let segmentSize = NSMakeSize(120, 25)
        let segmentFrame = NSMakeRect(
            NSMidX(titleView.bounds) - (segmentSize.width / 2.0),
            NSMidY(titleView.bounds) - (segmentSize.height / 2.0),
            segmentSize.width, segmentSize.height)
        let segment = NSSegmentedControl(frame: segmentFrame)
        segment.segmentCount = 2
        segment.setLabel(TrainBook, forSegment: 0)
        segment.setLabel(TrainOrder, forSegment: 1)
        segment.selectedSegment = 0
        segment.segmentStyle = NSSegmentStyle.TexturedSquare
        segment.target = self
        segment.action = #selector(MainWindowController.segmentTab(_:))
        
        titleView.addSubview(segment)
        
        segment.translatesAutoresizingMaskIntoConstraints = false
        
        let segmentConstraint1 = NSLayoutConstraint(item: segment, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
        let segmentConstraint2 = NSLayoutConstraint(item: segment, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
        NSLayoutConstraint.activateConstraints([segmentConstraint1,segmentConstraint2])
        
        selectModule(TrainBook)
        
        self.window?.recalculateKeyViewLoop()
        
        //login notification
        let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(self, selector: #selector(MainWindowController.receiveDidSendLoginMessageNotification(_:)), name: DidSendLoginMessageNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(MainWindowController.receiveAutoLoginMessageNotification(_:)), name: DidSendAutoLoginMessageNotification, object: nil)
    }
    
    func segmentTab(sender: NSSegmentedControl){
        selectModule(sender.labelForSegment(sender.selectedSegment)!)
    }
    
    func selectModule(moduleName:String){
        if(moduleName == TrainOrder){
            self.window?.contentView = orderQueryViewController.view
        }
        else if(moduleName == TrainBook){
            self.window?.contentView = ticketQueryViewController.view
        }
    }
    
    func receiveDidSendLoginMessageNotification(note: NSNotification){
        print("receiveDidSendLoginMessageNotification")
        loginOut()
        login(isAutoLogin: false)
{'fraction_non_alphanumeric': 0.07742827514690633, 'fraction_numerical': 0.009908975688443369, 'mean_word_length': 3.2033898305084745, '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': '4108663', 'n_tokens_mistral': 2333, 'n_tokens_neox': 2234, 'n_words': 427}