|
@@ -7,18 +7,15 @@
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
-protocol SKFindControllerDelegate: AnyObject {
|
|
|
- func findString(_ searchString: String, forward: Bool) -> Bool
|
|
|
- func showFindView(_ sender: AnyObject)
|
|
|
-}
|
|
|
+typealias KMSearchFindViewDoneAction = (_ view: KMSearchFindView) -> Void
|
|
|
+typealias KMSearchFindViewSearchAction = (_ view: KMSearchFindView, _ searchString: String, _ forward: Bool) -> Bool
|
|
|
+typealias KMSearchFindViewShowAllAction = (_ view: KMSearchFindView) -> Void
|
|
|
|
|
|
class KMSearchFindView: KMBaseXibView {
|
|
|
-
|
|
|
- weak var delegate: SKFindControllerDelegate?
|
|
|
- @IBOutlet var findField: NSTextField!
|
|
|
+ @IBOutlet var findField: NSSearchField!
|
|
|
@IBOutlet var messageField: NSTextField!
|
|
|
@IBOutlet var doneButton: NSButton!
|
|
|
- @IBOutlet var navigationButton: NSButton!
|
|
|
+ @IBOutlet var navigationButton: NSSegmentedControl!
|
|
|
@IBOutlet var showAllButton: NSButton!
|
|
|
|
|
|
var ownerController: AnyObject?
|
|
@@ -27,6 +24,10 @@ class KMSearchFindView: KMBaseXibView {
|
|
|
private var lastChangeCount: Int = 0
|
|
|
private var didChange: Bool = false
|
|
|
private var animating: Bool = false
|
|
|
+
|
|
|
+ var showAllAction: KMSearchFindViewShowAllAction?
|
|
|
+ var doneAction: KMSearchFindViewDoneAction?
|
|
|
+ var searchAction: KMSearchFindViewSearchAction?
|
|
|
|
|
|
override func setup() {
|
|
|
refreshSearchBarMenu()
|
|
@@ -100,87 +101,6 @@ class KMSearchFindView: KMBaseXibView {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- func toggleAboveView(_ view: NSView?, animate: Bool) {
|
|
|
- if animating {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- if UserDefaults.standard.bool(forKey: SKDisableAnimationsKey) {
|
|
|
- animating = false
|
|
|
- }
|
|
|
-
|
|
|
- let findBar = self
|
|
|
-
|
|
|
- var viewToAttachTo: NSView?
|
|
|
- if view == nil {
|
|
|
- let subviews = findBar.superview?.subviews ?? []
|
|
|
- for v in subviews {
|
|
|
- if v != findBar && (fabs(NSMinY(v.frame) - NSMaxY(findBar.frame)) <= 0.0 ||
|
|
|
- fabs(NSMaxY(v.frame) - NSMinY(findBar.frame)) <= 0.0) {
|
|
|
- viewToAttachTo = v
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- viewToAttachTo = view
|
|
|
- }
|
|
|
-
|
|
|
- guard let attachView = viewToAttachTo else { return }
|
|
|
-
|
|
|
- let viewFrame = attachView.frame
|
|
|
- guard let contentView = attachView.superview else { return }
|
|
|
- var barRect = attachView.frame
|
|
|
- var barHeight = NSHeight(findBar.frame)
|
|
|
- var visible = findBar.superview != nil
|
|
|
-
|
|
|
- barRect.size.height = barHeight
|
|
|
-
|
|
|
- if visible {
|
|
|
- if contentView.isFlipped {
|
|
|
- barRect.origin.y -= barHeight
|
|
|
- } else {
|
|
|
- barRect.origin.y = NSMaxY(contentView.bounds)
|
|
|
- }
|
|
|
-
|
|
|
- findBar.frame = barRect
|
|
|
- contentView.addSubview(findBar, positioned: .below, relativeTo: nil)
|
|
|
- barHeight = -barHeight
|
|
|
- } else {
|
|
|
- if contentView.isFlipped {
|
|
|
- barRect.origin.y -= barHeight
|
|
|
- } else {
|
|
|
- barRect.origin.y = NSMaxY(contentView.bounds) - barHeight
|
|
|
- }
|
|
|
- contentView.addSubview(findBar, positioned: .below, relativeTo: nil)
|
|
|
- }
|
|
|
-
|
|
|
- if animate {
|
|
|
- animating = true
|
|
|
- NSAnimationContext.runAnimationGroup({ context in
|
|
|
- context.duration = 0.5 * context.duration
|
|
|
- view?.animator().frame = viewFrame
|
|
|
- findBar.animator().frame = barRect
|
|
|
- }, completionHandler: {
|
|
|
- [weak self] in
|
|
|
- guard let self = self else { return }
|
|
|
- let window = self.window
|
|
|
- if !visible {
|
|
|
- self.removeFromSuperview()
|
|
|
- }
|
|
|
- window?.recalculateKeyViewLoop()
|
|
|
- self.animating = false
|
|
|
- })
|
|
|
- } else {
|
|
|
- view?.frame = viewFrame
|
|
|
- findBar.frame = barRect
|
|
|
- let window = contentView.window
|
|
|
- if !visible {
|
|
|
- findBar.removeFromSuperview()
|
|
|
- }
|
|
|
- window?.recalculateKeyViewLoop()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
func setFindString(_ newFindString: String?) {
|
|
|
if findString != newFindString {
|
|
|
findString = newFindString
|
|
@@ -191,7 +111,8 @@ class KMSearchFindView: KMBaseXibView {
|
|
|
func findForward(_ forward: Bool) {
|
|
|
var found = true
|
|
|
if let findStringLength = findString?.count, findStringLength > 0 {
|
|
|
- found = delegate?.findString(findString!, forward: forward) ?? false
|
|
|
+ guard let searchAction = searchAction else { return }
|
|
|
+ found = searchAction(self,findString!,forward)
|
|
|
updateFindPboard()
|
|
|
|
|
|
if var words = UserDefaults.standard.object(forKey: "kmDocumentSearchWordArrays") as? [String] {
|
|
@@ -212,16 +133,16 @@ class KMSearchFindView: KMBaseXibView {
|
|
|
messageField.isHidden = found
|
|
|
}
|
|
|
|
|
|
- @IBAction func find(_ sender: NSButton) {
|
|
|
- findForward(sender.tag == 1)
|
|
|
+ @IBAction func find(_ sender: Any) {
|
|
|
+ findForward((sender as AnyObject).selectedTag() == 1)
|
|
|
}
|
|
|
|
|
|
@IBAction func buttonItemClick_ShowAll(_ sender: NSButton) {
|
|
|
- delegate?.showFindView(self)
|
|
|
+ showAllAction?(self)
|
|
|
}
|
|
|
|
|
|
@IBAction func remove(_ sender: Any) {
|
|
|
- toggleAboveView(nil, animate: true)
|
|
|
+ doneAction?(self)
|
|
|
}
|
|
|
|
|
|
@IBAction func toggleCaseInsensitiveFind(_ sender: Any) {
|