<commit_before>//
//  MainWindowController.swift
//  yata
//
//  Created by <NAME> on 2017. 6. 20..
//  Copyright © 2017년 <NAME>. All rights reserved.
//


import AppKit
import SnapKit

class MainWindowController: WindowController {

    let splitViewController = MainSplitViewController()

    private func setupWindow() {
        window?.styleMask.insert(.miniaturizable)
        window?.styleMask.insert(.resizable)
        window?.styleMask.insert(.fullSizeContentView)
        
        window?.contentMinSize = NSSize(width: 400, height: 300)
    }
    
    private func setupToolbar() {
        let toolbar = NSToolbar(identifier: "toolbar")
        
        toolbar.delegate = self
        toolbar.allowsUserCustomization = true
        toolbar.displayMode = .iconOnly
        
        window?.toolbar = toolbar

        // Customize popup menu of toolbar
        // from https://stackoverflow.com/questions/39622468/display-only-customize-toolbar-in-nstoolbars-context-menu-in-swift
        if let contextMenu = window?.contentView?.superview?.menu {
            contextMenu.items.forEach({ (item) in
                if let action = item.action,
                    NSStringFromSelector(action) != "runToolbarCustomizationPalette:" {
                    contextMenu.removeItem(item)
                }
            })
        }
    }
    
    init() {
        super.init(windowSize: CGSize(width: 800, height: 600))

        self.contentView.addSubview(splitViewController.view)
        
        splitViewController.view.snp.makeConstraints { maker in
            maker.top.equalTo(self.contentView)
            maker.bottom.equalTo(self.contentView)
            maker.leading.equalTo(self.contentView)
            maker.trailing.equalTo(self.contentView)
        }
        
        setupWindow()
        setupToolbar()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: NSWindowDelegate
// from https://stackoverflow.com/questions/39622468/display-only-customize-toolbar-in-nstoolbars-context-menu-in-swift
extension MainWindowController {

    func window(_ window: NSWindow, willPositionSheet sheet: NSWindow, using rect: NSRect) -> NSRect {
        if sheet.className == "NSToolbarConfigPanel" {
            removeSizeAndDisplayMode(in: sheet)
        }

        return rect
    }
    
    private func removeSizeAndDisplayMode(in sheet: NSWindow) {

        guard let views = sheet.contentView?.subviews else { return }
        
        // Hide Small Size Option
        views.lazy
            .flatMap { $0 as? NSButton }
            .filter { button -> Bool in<commit_msg>Add to restore previous window location<commit_after>//
//  MainWindowController.swift
//  yata
//
//  Created by <NAME> on 2017. 6. 20..
//  Copyright © 2017년 <NAME>. All rights reserved.
//


import AppKit
import SnapKit

class MainWindowController: WindowController {

    let splitViewController = MainSplitViewController()

    private func setupWindow() {
        window?.styleMask.insert(.miniaturizable)
        window?.styleMask.insert(.resizable)
        window?.styleMask.insert(.fullSizeContentView)
        
        // window?.titleVisibility = .hidden
        
        // For restoring window position
        window?.identifier = "MainWindowController"
        window?.isRestorable = true
        window?.restorationClass = MainWindowController.self

        window?.contentMinSize = NSSize(width: 400, height: 300)
    }
    
    private func setupToolbar() {
        let toolbar = NSToolbar(identifier: "toolbar")
        
        toolbar.delegate = self
        toolbar.allowsUserCustomization = true
        toolbar.displayMode = .iconOnly
        
        window?.toolbar = toolbar

        // Customize popup menu of toolbar
        // from https://stackoverflow.com/questions/39622468/display-only-customize-toolbar-in-nstoolbars-context-menu-in-swift
        if let contextMenu = window?.contentView?.superview?.menu {
            contextMenu.items.forEach({ (item) in
                if let action = item.action,
                    NSStringFromSelector(action) != "runToolbarCustomizationPalette:" {
                    contextMenu.removeItem(item)
                }
            })
        }
    }
    
    init() {
        super.init(windowSize: CGSize(width: 800, height: 600))

        self.contentView.addSubview(splitViewController.view)
        
        splitViewController.view.snp.makeConstraints { maker in
            maker.top.equalTo(self.contentView)
            maker.bottom.equalTo(self.contentView)
            maker.leading.equalTo(self.contentView)
            maker.trailing.equalTo(self.contentView)
        }
        
        setupWindow()
        setupToolbar()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: NSWindowRestoration
extension MainWindowController: NSWindowRestoration {
    static func restoreWindow(withIdentifier identifier: String, state: NSCoder, completionHandler: @escaping (NSWindow?, Error?) -> Void) {
    
        if identifier == "MainWindowController" {
            if let appDelegate = NSApp.delegate as? AppDelegate {
                let myWindow = appDelegate.mainWindow.window
                completionHandler(myWindow ,nil)
            }
        }
    }
}

// MARK: NSWindowDelegate
// from https://stackoverflow.com/questions/39622468/display-only-customize-toolbar-in-nstoolbars-context-menu-in-swift
extension MainWindowController {

    func window(_ window: NSWindow, willPositionSheet sheet: NSWindow, using rect: NSRect) -> NSRect {
        if sheet.className == "NSToolbarConfigPanel" {
            removeSizeAndDisplayMode(in: sheet)
        }

        return rect
    }
    
    private func removeSizeAndDisplayMode(in sheet: NSWindow) {

        guard let views = sheet.contentView?.subviews else { return }
        
        // Hide Small Size Option
        views.lazy
            .flatMap { $0 as? NSButton }
            .filter { button -> Bool in
{'fraction_non_alphanumeric': 0.09606843230794539, 'fraction_numerical': 0.01316005922026649, 'mean_word_length': 2.2237539766702015, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 4, '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': '2011578', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1611, 'n_words': 339}