// // KMHeaderFooterContentInfoView.swift // PDF Reader Pro // // Created by tangchao on 2022/12/28. // import Cocoa typealias KMHeaderFooterContentInfoView_selectAreaViewItemClick = (_ index: Int) -> () private class KMHeaderFooterContentInfoView_selectAreaView: NSView { private var hLine = NSView() private var vLine = NSView() private var vLine2 = NSView() private var buttons: Array = [] var itemClick: KMHeaderFooterContentInfoView_selectAreaViewItemClick! override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.initSubViews() } required init?(coder: NSCoder) { super.init(coder: coder) self.initSubViews() } override var isFlipped: Bool { return true } private func initSubViews() { self.addSubview(self.hLine) self.addSubview(self.vLine) self.addSubview(self.vLine2) self.wantsLayer = true self.layer?.borderWidth = 1 self.layer?.borderColor = NSColor.black.cgColor self.layer?.cornerRadius = 2 for line in [self.hLine, self.vLine, self.vLine2] { line.wantsLayer = true line.layer?.backgroundColor = NSColor.black.cgColor } self.buttons.removeAll() for i in 0 ... 1 { for j in 0 ... 2 { let button = NSButton() button.isBordered = false button.title = "" self.addSubview(button) self.buttons.append(button) button.tag = i * 3 + j + 1 button.target = self button.action = #selector(buttonAction) } } } override func layout() { super.layout() let width: CGFloat = NSWidth(self.bounds) let height: CGFloat = NSHeight(self.bounds) let itemSize: NSSize = NSMakeSize(width/3, height*0.5) self.hLine.frame = NSMakeRect(0, itemSize.height, width, 1) self.vLine.frame = NSMakeRect(itemSize.width, 0, 1, height) self.vLine2.frame = NSMakeRect(itemSize.width*2, 0, 1, height) for i in 0 ... 1 { for j in 0 ... 2 { let button = self.buttons[i*3+j] button.frame = NSMakeRect(CGFloat(j)*itemSize.width, CGFloat(i)*itemSize.height, itemSize.width, itemSize.height) } } } func selectIndex(index: Int) { for button in self.buttons { button.wantsLayer = true if (button.tag == index) { button.layer?.backgroundColor = NSColor.black.cgColor } else { button.layer?.backgroundColor = NSColor.clear.cgColor } } } @objc private func buttonAction(sender: NSButton) { guard let callback = self.itemClick else { return } self.selectIndex(index: sender.tag) callback(sender.tag) } } typealias KMHeaderFooterContentInfoViewAddAction = (_ index: Int, _ sender: NSButton) -> () class KMHeaderFooterContentInfoView: KMHeaderFooterAdjectiveInfoBaseView { var addButton = NSButton() private var selectAreaView = KMHeaderFooterContentInfoView_selectAreaView() var textViewBox = NSView() var scrollView = NSScrollView() var textView = NSTextView() var addAction: KMHeaderFooterContentInfoViewAddAction! private var selectedIndex: Int = 1 var indexForSelectedItem: Int { get { return self.selectedIndex } } override func initSubviews() { super.initSubviews() self.addSubview(self.addButton) self.addSubview(self.selectAreaView) self.addSubview(self.textViewBox) self.addSubview(self.scrollView) self.scrollView.documentView = self.textView self.titleLabel.stringValue = NSLocalizedString("Layout & Content", comment: "") self.addButton.isBordered = false self.addButton.title = "+" self.addButton.target = self self.addButton.action = #selector(addButtonAction) self.textViewBox.wantsLayer = true self.textViewBox.layer?.borderWidth = 1 self.textViewBox.layer?.borderColor = NSColor.black.cgColor self.textViewBox.layer?.cornerRadius = 4 self.scrollView.borderType = .noBorder self.scrollView.wantsLayer = true self.scrollView.layer?.borderWidth = 1 self.scrollView.layer?.borderColor = NSColor.black.cgColor self.scrollView.layer?.cornerRadius = 2 self.scrollView.layer?.masksToBounds = true self.textView.textContainerInset = NSMakeSize(6, 8) self.textView.isRichText = false self.textView.drawsBackground = false self.scrollView.drawsBackground = false self.textView.delegate = self self.selectAreaView.itemClick = { (index: Int) in self.selectedIndex = index if (index == 1) { self.textView.string = self.model.topLeftString } else if (index == 2) { self.textView.string = self.model.topCenterString } else if (index == 3) { self.textView.string = self.model.topRightString } else if (index == 4) { self.textView.string = self.model.bottomLeftString } else if (index == 5) { self.textView.string = self.model.bottomCenterString } else if (index == 6) { self.textView.string = self.model.bottomRightString } } } override func layout() { super.layout() let leftMargin = self.contentInset.left let width: CGFloat = NSWidth(self.bounds) let contentWidth: CGFloat = width-leftMargin-self.contentInset.right let buttonSize: CGFloat = 22 let buttonX: CGFloat = width-self.contentInset.right-buttonSize self.addButton.frame = NSMakeRect(buttonX, 0, buttonSize, buttonSize) self.selectAreaView.frame = NSMakeRect(leftMargin, self.titleLabel.frame.maxY+10, contentWidth, 40) self.textViewBox.frame = NSMakeRect(leftMargin, self.selectAreaView.frame.maxY+10, contentWidth, 80) self.scrollView.frame = NSMakeRect(leftMargin+1, self.selectAreaView.frame.maxY+11, contentWidth-2, 78) self.textView.frame = NSMakeRect(1, 1, contentWidth-2, 78) } override var model: KMHeaderFooterAdjectiveModel { get { return super.model } set { super.model = newValue self.selectAreaView.selectIndex(index: 1) self.selectedIndex = 1 self.textView.string = model.topLeftString } } @objc private func addButtonAction() { guard let callback = self.addAction else { return } callback(self.selectedIndex, self.addButton) } } extension KMHeaderFooterContentInfoView: NSTextViewDelegate { func textDidChange(_ notification: Notification) { guard let callback = self.itemClick else { return } callback(self.selectedIndex, self.textView.string) } }