<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import Foundation
import UIKit
import Snappy

protocol BrowserToolbarDelegate {
    func didBeginEditing()
    func didClickBack()
    func didClickForward()
    func didClickAddTab()
    func didLongPressBack()
    func didLongPressForward()
    func didClickReaderMode()
    func didClickStop()
    func didClickReload()
}

class BrowserToolbar: UIView, UITextFieldDelegate, BrowserLocationViewDelegate {
    var browserToolbarDelegate: BrowserToolbarDelegate?

    private var forwardButton: UIButton!
    private var backButton: UIButton!
    private var locationView: BrowserLocationView!
    private var tabsButton: UIButton!
    private var progressBar: UIProgressView!

    private var longPressGestureBackButton: UILongPressGestureRecognizer!
    private var longPressGestureForwardButton: UILongPressGestureRecognizer!

    override init() {
        super.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        viewDidInit()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        viewDidInit()
    }

    private func viewDidInit() {
        self.backgroundColor = UIColor(white: 0.80, alpha: 1.0)

        backButton = UIButton()
        backButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        backButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)
        backButton.setTitle("<", forState: UIControlState.Normal)
        backButton.accessibilityLabel = NSLocalizedString("Back", comment: "")
        backButton.addTarget(self, action: "SELdidClickBack", forControlEvents: UIControlEvents.TouchUpInside)
        longPressGestureBackButton = UILongPressGestureRecognizer(target: self, action: "SELdidLongPressBack")
        backButton.accessibilityHint = NSLocalizedString("Double tap and hold to open history", comment: "")
        backButton.addGestureRecognizer(longPressGestureBackButton)
        self.addSubview(backButton)

        forwardButton = UIButton()
        forwardButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        forwardButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)
        forwardButton.setTitle(">", forState: UIControlState.Normal)
        forwardButton.accessibilityLabel = NSLocalizedString("Forward", comment: "")
        forwardButton.addTarget(self, action: "SELdidClickForward", forControlEvents: UIControlEvents.TouchUpInside)
        longPressGestureForwardButton = UILongPressGestureRecognizer(target: self, action: "SELdidLongPressForward")
        forwardButton.accessibilityHint = NSLocalizedString("Double tap and hold to open history", comment: "")
        forwardButton.addGestureRecognizer(longPressGestureForwardButton)
        self.addSubview(forwardButton)

        locationView = BrowserLocationView(frame: CGRectZero)
        locationView.readerModeState = ReaderModeState.Unavailable
        locationView.delegate = self
        addSubview(locationView)

        progressBar = UIProgressView()
        self.progressBar.trackTintColor = self.backgroundColor
        self.addSubview(progressBar)

        tabsButton = UIButton()
        tabsButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        tabsButton.titleLabel?.layer.borderColor = UIColor.blackColor().CGColor
        tabsButton.titleLabel?.layer.cornerRadius = 4
        tabsButton.titleLabel?.layer.borderWidth = 1
        tabsButton.titleLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 12)
        tabsButton.titleLabel?.textAlignment = NSTextAlignment.Center
        tabsButton.titleLabel?.snp_makeConstraints { make in
            make.size.equalTo(24)
            return
        }
        tabsButton.addTarget(self, action: "SELdidClickAddTab", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(tabsButton)

        self.backButton.snp_remakeConstraints { make in
            make.left.equalTo(self)
            make.centerY.equalTo(self).offset(10)
            make.width.height.equalTo(44)
        }

        self.forwardButton.snp_remakeConstraints { make in
            make.left.equalTo(self.backButton.snp_right)
            make.centerY.equalTo(self).offset(10)
            make.width.height.equalTo(44)
        }

        self.locationView.snp_remakeConstraints { make in
            make.left.equalTo(self.forwardButton.snp_right)
            make.centerY.equalTo(self).offset(10)
        }

        self.tabsButton.snp_remakeConstraints { make in
            make.left.equalTo(self.locationView.snp_right)
            make.centerY.equalTo(self).offset(10)
            make.width.height.equalTo(44)
            make.right.equalTo(self).offset(-8)
        }

        self.progressBar.snp_remakeConstraints { make in
            make.centerY.equalTo(self.snp_bottom)
            make.width.equalTo(self)
        }
    }

    func updateURL(url: NSURL?) {
        if let url = url {
            locationView.url = url
        }
    }

    func updateTabCount(count: Int) {
        tabsButton.setTitle(count.description, forState: UIControlState.Normal)
        tabsButton.accessibilityValue = count.description
        tabsButton.accessibilityLabel = NSLocalizedString("Show Tabs", comment: "")
    }

    func updateBackStatus(canGoBack: Bool) {
        backButton.enabled = canGoBack
    }

    func updateFowardStatus(canGoForward: Bool) {
        forwardButton.enabled = canGoForward
    }

    func updateLoading(loading: Bool) {
        locationView.loading = loading
    }

    func SELdidClickBack() {
        browserToolbarDelegate?.didClickBack()
    }

    func SELdidLongPressBack() {
        browserToolbarDelegate?.didLongPressBack()
    }

    func SELdidClickForward() {
        browserToolbarDelegate?.didClickForward()
    }

    func SELdidLongPressForward() {
        browserToolbarDelegate?.didLongPressForward()<commit_msg>Check for UIGestureRecognizerState.Began to prevent multiple callbacks<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import Foundation
import UIKit
import Snappy

protocol BrowserToolbarDelegate {
    func didBeginEditing()
    func didClickBack()
    func didClickForward()
    func didClickAddTab()
    func didLongPressBack()
    func didLongPressForward()
    func didClickReaderMode()
    func didClickStop()
    func didClickReload()
}

class BrowserToolbar: UIView, UITextFieldDelegate, BrowserLocationViewDelegate {
    var browserToolbarDelegate: BrowserToolbarDelegate?

    private var forwardButton: UIButton!
    private var backButton: UIButton!
    private var locationView: BrowserLocationView!
    private var tabsButton: UIButton!
    private var progressBar: UIProgressView!

    private var longPressGestureBackButton: UILongPressGestureRecognizer!
    private var longPressGestureForwardButton: UILongPressGestureRecognizer!

    override init() {
        super.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        viewDidInit()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        viewDidInit()
    }

    private func viewDidInit() {
        self.backgroundColor = UIColor(white: 0.80, alpha: 1.0)

        backButton = UIButton()
        backButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        backButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)
        backButton.setTitle("<", forState: UIControlState.Normal)
        backButton.accessibilityLabel = NSLocalizedString("Back", comment: "")
        backButton.addTarget(self, action: "SELdidClickBack", forControlEvents: UIControlEvents.TouchUpInside)
        longPressGestureBackButton = UILongPressGestureRecognizer(target: self, action: "SELdidLongPressBack:")
        backButton.accessibilityHint = NSLocalizedString("Double tap and hold to open history", comment: "")
        backButton.addGestureRecognizer(longPressGestureBackButton)
        self.addSubview(backButton)

        forwardButton = UIButton()
        forwardButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        forwardButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)
        forwardButton.setTitle(">", forState: UIControlState.Normal)
        forwardButton.accessibilityLabel = NSLocalizedString("Forward", comment: "")
        forwardButton.addTarget(self, action: "SELdidClickForward", forControlEvents: UIControlEvents.TouchUpInside)
        longPressGestureForwardButton = UILongPressGestureRecognizer(target: self, action: "SELdidLongPressForward:")
        forwardButton.accessibilityHint = NSLocalizedString("Double tap and hold to open history", comment: "")
        forwardButton.addGestureRecognizer(longPressGestureForwardButton)
        self.addSubview(forwardButton)

        locationView = BrowserLocationView(frame: CGRectZero)
        locationView.readerModeState = ReaderModeState.Unavailable
        locationView.delegate = self
        addSubview(locationView)

        progressBar = UIProgressView()
        self.progressBar.trackTintColor = self.backgroundColor
        self.addSubview(progressBar)

        tabsButton = UIButton()
        tabsButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        tabsButton.titleLabel?.layer.borderColor = UIColor.blackColor().CGColor
        tabsButton.titleLabel?.layer.cornerRadius = 4
        tabsButton.titleLabel?.layer.borderWidth = 1
        tabsButton.titleLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 12)
        tabsButton.titleLabel?.textAlignment = NSTextAlignment.Center
        tabsButton.titleLabel?.snp_makeConstraints { make in
            make.size.equalTo(24)
            return
        }
        tabsButton.addTarget(self, action: "SELdidClickAddTab", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(tabsButton)

        self.backButton.snp_remakeConstraints { make in
            make.left.equalTo(self)
            make.centerY.equalTo(self).offset(10)
            make.width.height.equalTo(44)
        }

        self.forwardButton.snp_remakeConstraints { make in
            make.left.equalTo(self.backButton.snp_right)
            make.centerY.equalTo(self).offset(10)
            make.width.height.equalTo(44)
        }

        self.locationView.snp_remakeConstraints { make in
            make.left.equalTo(self.forwardButton.snp_right)
            make.centerY.equalTo(self).offset(10)
        }

        self.tabsButton.snp_remakeConstraints { make in
            make.left.equalTo(self.locationView.snp_right)
            make.centerY.equalTo(self).offset(10)
            make.width.height.equalTo(44)
            make.right.equalTo(self).offset(-8)
        }

        self.progressBar.snp_remakeConstraints { make in
            make.centerY.equalTo(self.snp_bottom)
            make.width.equalTo(self)
        }
    }

    func updateURL(url: NSURL?) {
        if let url = url {
            locationView.url = url
        }
    }

    func updateTabCount(count: Int) {
        tabsButton.setTitle(count.description, forState: UIControlState.Normal)
        tabsButton.accessibilityValue = count.description
        tabsButton.accessibilityLabel = NSLocalizedString("Show Tabs", comment: "")
    }

    func updateBackStatus(canGoBack: Bool) {
        backButton.enabled = canGoBack
    }

    func updateFowardStatus(canGoForward: Bool) {
        forwardButton.enabled = canGoForward
    }

    func updateLoading(loading: Bool) {
        locationView.loading = loading
    }

    func SELdidClickBack() {
        browserToolbarDelegate?.didClickBack()
    }

    func SELdidLongPressBack(recognizer: UILongPressGestureRecognizer) {
        if recognizer.state == UIGestureRecognizerState.Began {
            browserToolbarDelegate?.didLongPressBack()
        }
    }

    func SELdidClickForward() {
        browserToolbarDelegate?.didClickForward()
    }

    func SELdidLongPressForward(recognizer: UILongPressGestureRecognizer) {
        if recognizer.state == UIGestureRecognizerState.Began {
            browserToolbarDelegate?.didLongPressForward()
        }
{'fraction_non_alphanumeric': 0.08620004766822913, 'fraction_numerical': 0.0047668229125287995, 'mean_word_length': 3.2016021361815756, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3390269', 'n_tokens_mistral': 3635, 'n_tokens_neox': 3482, 'n_words': 633}