123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- //
- // KMMergeTitleBar.swift
- // PDF Master
- //
- // Created by tangchao on 2022/11/23.
- //
- import Cocoa
- enum KMMergeTitleBarButtonID: Int {
- case add = 0
- case delete = 1
- case zoomOut = 2
- case zoomIn = 3
- case merge = 4
- case cancel = 5
- }
- typealias KMMergeTitleBarItemViewClick = (Int)->()
- class KMMergeTitleBarItem: NSView {
- lazy var leftButton = NSButton()
- lazy var rightButton = NSButton()
-
- var itemClick: KMMergeTitleBarItemViewClick!
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- addSubview(leftButton)
- addSubview(rightButton)
-
- leftButton.isBordered = false
- rightButton.isBordered = false
-
- leftButton.title = ""
- leftButton.target = self
- leftButton.action = #selector(leftButtonAction)
- rightButton.target = self
- rightButton.action = #selector(rightButtonAction)
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- override func layout() {
- super.layout()
-
- let width = NSWidth(self.bounds)
- let height = NSHeight(self.bounds)
-
- let leftSize: CGFloat = 18
- leftButton.frame = NSMakeRect(0, (height-leftSize)*0.5, leftSize, leftSize)
- rightButton.frame = NSMakeRect(leftSize, 0, width-leftSize, height)
- }
-
- @objc func leftButtonAction() {
- guard let callback = itemClick else {
- return
- }
- callback(1)
- }
-
- @objc func rightButtonAction() {
- guard let callback = itemClick else {
- return
- }
- callback(2)
- }
- }
- protocol KMMergeTitleBarDelegate : NSObjectProtocol {
- func titleBar(titleBar: KMMergeTitleBar, itemDidClick: KMMergeTitleBarButtonID)
- }
- typealias KMMergeTitleBarItemClick = (KMMergeTitleBarButtonID)->()
- typealias KMMergeTitleBarPageRangeClick = (_ index: Int, _ pageString: String)->()
- class KMMergeTitleBar: NSView {
- lazy var addItem = KMMergeTitleBarItem()
- private lazy var deleteItem = KMMergeTitleBarItem()
- private lazy var vLine = NSView()
- private lazy var zoomOutButton = NSButton()
- private lazy var zoomInButton = NSButton()
-
- private lazy var pagePageComboBox = NSComboBox()
-
- private lazy var mergeButton = NSButton()
- private lazy var cancelButton = NSButton()
- private lazy var bottomLine = NSView()
-
- weak open var delegate: KMMergeTitleBarDelegate?
- var itemClick: KMMergeTitleBarItemClick!
- var pageRangeClick: KMMergeTitleBarPageRangeClick!
-
- private var myModel: KMMergeFileModel!
- var model: KMMergeFileModel {
- get {
- myModel
- }
- set {
- myModel = newValue
-
- if newValue.pageRange == .custom {
- if !newValue.pageRangeString.isEmpty {
- pagePageComboBox.stringValue = newValue.pageRangeString
- }
-
- pagePageComboBox.isEditable = true
- } else if newValue.pageRange == .all {
- pagePageComboBox.stringValue = NSLocalizedString("全部页面", comment: "")
- } else if newValue.pageRange == .oddPages {
- pagePageComboBox.stringValue = NSLocalizedString("奇数页面", comment: "")
- } else if newValue.pageRange == .evenPages {
- pagePageComboBox.stringValue = NSLocalizedString("偶数页面", comment: "")
- }
- }
- }
-
- deinit {
- /// 释放
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- initDefaultValue()
- initSubViews()
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- override var isFlipped: Bool {
- return true
- }
-
- func initDefaultValue() {
- addItem.wantsLayer = true
- addItem.rightButton.title = NSLocalizedString("新增文件", comment: "")
-
- addItem.itemClick = {
- [weak weakSelf = self] (index: Int) -> () in
- guard let itemClickAction = weakSelf?.itemClick else {
- return
- }
-
- itemClickAction(KMMergeTitleBarButtonID.add)
- }
-
- deleteItem.wantsLayer = true
- deleteItem.rightButton.title = NSLocalizedString("删除", comment: "")
- deleteItem.itemClick = {
- [weak weakSelf = self] (index: Int) -> () in
- guard let itemClickAction = weakSelf?.itemClick else {
- return
- }
-
- itemClickAction(KMMergeTitleBarButtonID.delete)
- }
-
- vLine.wantsLayer = true
- vLine.layer?.backgroundColor = NSColor.lightGray.cgColor
-
- zoomOutButton.wantsLayer = true
- zoomOutButton.isBordered = false
- zoomOutButton.title = "+"
- zoomOutButton.layer?.borderWidth = 1;
- zoomOutButton.layer?.borderColor = NSColor.gray.cgColor;
- zoomOutButton.layer?.cornerRadius = 12
- zoomOutButton.target = self
- zoomOutButton.action = #selector(zoomOutButtonAction)
-
- zoomInButton.wantsLayer = true
- zoomInButton.isBordered = false
- zoomInButton.title = "-"
- zoomInButton.layer?.borderWidth = 1;
- zoomInButton.layer?.borderColor = NSColor.gray.cgColor;
- zoomInButton.layer?.cornerRadius = 12
- zoomInButton.target = self
- zoomInButton.action = #selector(zoomInButtonAction)
-
- pagePageComboBox.stringValue = "全部页面"
- pagePageComboBox.placeholderString = NSLocalizedString("如1,3-5,10", comment: "")
- pagePageComboBox.delegate = self
- pagePageComboBox.isEditable = false
- pagePageComboBox.addItems(withObjectValues: [NSLocalizedString("全部页面", comment: ""),
- NSLocalizedString("奇数页面", comment: ""),
- NSLocalizedString("偶数页面", comment: ""),
- NSLocalizedString("自定义", comment: "")])
-
- mergeButton.wantsLayer = true
- mergeButton.isBordered = false
- mergeButton.title = NSLocalizedString("合并", comment: "")
- mergeButton.attributedTitle = NSMutableAttributedString(string: mergeButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
- mergeButton.layer?.backgroundColor = NSColor.black.cgColor
- mergeButton.layer?.cornerRadius = 5
- mergeButton.target = self
- mergeButton.action = #selector(mergeButtonAction)
-
- cancelButton.wantsLayer = true
- cancelButton.isBordered = false
- cancelButton.title = NSLocalizedString("关闭", comment: "")
- cancelButton.layer?.backgroundColor = NSColor.white.cgColor
- cancelButton.layer?.cornerRadius = 5
- cancelButton.layer?.borderWidth = 1
- cancelButton.layer?.borderColor = NSColor.black.cgColor
- cancelButton.target = self
- cancelButton.action = #selector(cancelButtonAction)
-
- bottomLine.wantsLayer = true
- bottomLine.layer?.backgroundColor = NSColor.lightGray.cgColor
- }
-
- func initSubViews() {
- addSubview(addItem)
- addSubview(deleteItem)
- addSubview(vLine)
- addSubview(zoomOutButton)
- addSubview(zoomInButton)
- addSubview(pagePageComboBox)
-
- addSubview(mergeButton)
- addSubview(cancelButton)
- addSubview(bottomLine)
- }
-
- override func layout() {
- super.layout()
-
- let width = NSWidth(self.bounds)
- let height = NSHeight(self.bounds)
- let space: CGFloat = 12
-
- let centerX: CGFloat = width * 0.5
- let lineH: CGFloat = 18
- vLine.frame = NSMakeRect(centerX, (height-lineH)*0.5, 1, lineH);
-
- let itemH: CGFloat = 28
- let itemY: CGFloat = (height-itemH) * 0.5
- let deleteItemWidth: CGFloat = 60
- let deleteItemX = centerX-deleteItemWidth-space
- deleteItem.frame = NSMakeRect(deleteItemX, itemY, deleteItemWidth, itemH)
-
- let addItemWidth: CGFloat = 80
- let addItemX = deleteItemX-addItemWidth-space
- addItem.frame = NSMakeRect(addItemX, itemY, addItemWidth, itemH)
-
- let zoomButtonSize: CGFloat = 24
- let zoomOutButtonX = centerX + space
- let zoomButtonY = (height-zoomButtonSize) * 0.5
- zoomOutButton.frame = NSMakeRect(zoomOutButtonX, zoomButtonY, zoomButtonSize, zoomButtonSize)
-
- zoomInButton.frame = NSMakeRect(zoomOutButtonX+zoomButtonSize+space, zoomButtonY, zoomButtonSize, zoomButtonSize)
-
- let pagePageH: CGFloat = 24
- let pagePageX: CGFloat = zoomInButton.frame.maxX + 30
- pagePageComboBox.frame = NSMakeRect(pagePageX, (height-pagePageH) * 0.5, 160, pagePageH)
-
- let rightButtonH: CGFloat = 30
- let rightButtonW: CGFloat = 60
- let rightButtonY: CGFloat = (height-rightButtonH)*0.5
- let rightMargin: CGFloat = 28
- let cancelButtonX = width-rightMargin-rightButtonW
- cancelButton.frame = NSMakeRect(cancelButtonX, rightButtonY, rightButtonW, rightButtonH)
- mergeButton.frame = NSMakeRect(cancelButtonX-rightButtonW-12, rightButtonY, rightButtonW, rightButtonH)
-
- bottomLine.frame = NSMakeRect(0, height-1, width, 1)
- }
-
- /**
- * MARK: - User Action
- */
-
- @objc func addItemAction() {
- guard let itemClickAction = itemClick else {
- return
- }
-
- itemClickAction(KMMergeTitleBarButtonID.add)
- }
-
- @objc func deleteItemAction() {
- guard let itemClickAction = itemClick else {
- return
- }
-
- itemClickAction(KMMergeTitleBarButtonID.delete)
- }
-
- @objc func zoomOutButtonAction() {
- guard let itemClickAction = itemClick else {
- return
- }
-
- itemClickAction(KMMergeTitleBarButtonID.zoomOut)
- }
-
- @objc func zoomInButtonAction() {
- guard let itemClickAction = itemClick else {
- return
- }
-
- itemClickAction(KMMergeTitleBarButtonID.zoomIn)
- }
-
- @objc func mergeButtonAction() {
- guard let itemClickAction = itemClick else {
- return
- }
-
- itemClickAction(KMMergeTitleBarButtonID.merge)
- }
-
- @objc func cancelButtonAction() {
- guard let itemClickAction = itemClick else {
- return
- }
-
- itemClickAction(KMMergeTitleBarButtonID.cancel)
- }
-
- func setDeleteItemEnable(enable: Bool) {
- deleteItem.leftButton.isEnabled = enable
- deleteItem.rightButton.isEnabled = enable
- }
-
- func setZoomItemEnable(zoomIn: Bool, enable: Bool) {
- if zoomIn {
- zoomInButton.isEnabled = enable
- } else {
- zoomOutButton.isEnabled = enable
- }
- }
-
- func setPageRangeComboBoxHidden(isHidden: Bool) {
- pagePageComboBox.isHidden = isHidden
- }
-
- func setPagePageComboBoxString(string: String) {
- pagePageComboBox.stringValue = string
- }
- }
- extension KMMergeTitleBar: NSComboBoxDelegate {
- func comboBoxSelectionDidChange(_ notification: Notification) {
- if notification.object as! NSComboBox != pagePageComboBox {
- return
- }
-
- let box: NSComboBox = notification.object as! NSComboBox
- let index = box.indexOfSelectedItem
- if index < 0 {
- return
- }
-
- if index == 3 {
- pagePageComboBox.isEditable = true
- pagePageComboBox.stringValue = "如1,3-5,10"
- self.window?.makeFirstResponder(pagePageComboBox)
- } else {
- pagePageComboBox.isEditable = false
- guard let callback = pageRangeClick else {
- return
- }
-
- callback(index, "")
- }
- }
-
- func controlTextDidEndEditing(_ obj: Notification) {
- if obj.object as! NSComboBox != pagePageComboBox {
- return
- }
-
- let box: NSComboBox = obj.object as! NSComboBox
-
- pagePageComboBox.isEditable = false
- guard let callback = pageRangeClick else {
- return
- }
-
- callback(3, pagePageComboBox.stringValue)
- }
- }
|