|
@@ -1253,6 +1253,77 @@ extension KMMainViewController {
|
|
|
@IBAction func menuItemAction_currentWindowName(_ sender: Any) {
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ @IBAction func performFit(_ sender: Any?) {
|
|
|
+ if self.interactionMode != .normal {
|
|
|
+ NSSound.beep()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let displayMode = self.listView.fetchDisplayViewMode()
|
|
|
+ let scaleFactor = self.listView.scaleFactor
|
|
|
+ let autoScales = self.listView.autoScales
|
|
|
+ var isSingleRow = false
|
|
|
+ if displayMode == .singlePage || displayMode == .twoUp {
|
|
|
+ isSingleRow = true
|
|
|
+ } else if displayMode == .singlePageContinuous || self.listView.displaysAsBook {
|
|
|
+ isSingleRow = (self.listView?.document?.pageCount ?? 0) <= 1
|
|
|
+ } else {
|
|
|
+ isSingleRow = (self.listView?.document?.pageCount ?? 0) <= 2
|
|
|
+ }
|
|
|
+
|
|
|
+ var frame = self.view.window?.frame ?? .zero
|
|
|
+ var size: NSSize = .zero
|
|
|
+ let oldSize = self.listView.frame.size
|
|
|
+ let documentView = self.listView.documentView()
|
|
|
+ let documentRect = documentView?.convert(documentView?.bounds ?? .zero, to: nil) ?? .zero
|
|
|
+ let page = self.listView.currentPage()
|
|
|
+ let box = self.listView.displayBox
|
|
|
+ let PAGE_BREAK_MARGIN: CGFloat = 8.0
|
|
|
+ let margin = self.listView.displaysPageBreaks ? PAGE_BREAK_MARGIN : 0.0
|
|
|
+
|
|
|
+ // Calculate the new size for the pdfView
|
|
|
+ size.width = NSWidth(documentRect)
|
|
|
+ if (autoScales) {
|
|
|
+ size.width /= scaleFactor
|
|
|
+ }
|
|
|
+ if (isSingleRow) {
|
|
|
+ size.height = NSHeight(documentRect)
|
|
|
+ } else {
|
|
|
+ let rect = self.listView.convert(page?.bounds(for: box) ?? .zero, from: page)
|
|
|
+ size.height = NSHeight(rect) + margin * scaleFactor
|
|
|
+ // if ([NSScroller respondsToSelector:@selector(preferredScrollerStyle)] == NO || [NSScroller preferredScrollerStyle] == NSScrollerStyleLegacy)
|
|
|
+ // size.width += [NSScroller scrollerWidth];
|
|
|
+ if NSScroller.responds(to: NSSelectorFromString("preferredScrollerStyle")) == false {
|
|
|
+ let w = NSScroller.scrollerWidth(for: .regular, scrollerStyle: .legacy)
|
|
|
+ size.width += w
|
|
|
+ } else {
|
|
|
+ if NSScroller.preferredScrollerStyle == .legacy {
|
|
|
+ let w = NSScroller.scrollerWidth(for: .regular, scrollerStyle: .legacy)
|
|
|
+ size.width += w
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (autoScales) {
|
|
|
+ size.height /= scaleFactor
|
|
|
+ }
|
|
|
+
|
|
|
+ // Calculate the new size for the window
|
|
|
+ size.width = ceil(NSWidth(frame) + size.width - oldSize.width)
|
|
|
+ size.height = ceil(NSHeight(frame) + size.height - oldSize.height)
|
|
|
+ // Align the window frame from the old topleft point and constrain to the screen
|
|
|
+ frame.origin.y = NSMaxY(frame) - size.height
|
|
|
+ frame.size = size
|
|
|
+ if let screen = self.view.window?.screen ?? NSScreen.main {
|
|
|
+ frame = self.view.window?.constrainFrameRect(frame, to: screen) ?? .zero
|
|
|
+ }
|
|
|
+
|
|
|
+ self.view.window?.setFrame(frame, display: self.view.window?.isVisible ?? false)
|
|
|
+
|
|
|
+ if displayMode == .singlePageContinuous || displayMode == .twoUpContinuous {
|
|
|
+ self.listView.go(to: NSInsetRect(page?.bounds(for: box) ?? .zero, -0.5 * margin, -0.5 * margin), on: page)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
extension KMMainViewController: NSMenuItemValidation, NSMenuDelegate {
|