|
| 1 | +import Cocoa |
| 2 | + |
| 3 | +class CollectionViewItemView: NSView { |
| 4 | + var window_: Window? |
| 5 | + var thumbnail = NSImageView() |
| 6 | + var appIcon = NSImageView() |
| 7 | + var label = CellTitle(Preferences.fontHeight) |
| 8 | + var minimizedIcon = FontIcon(FontIcon.sfSymbolCircledMinusSign, Preferences.fontIconSize, .white) |
| 9 | + var hiddenIcon = FontIcon(FontIcon.sfSymbolCircledDotSign, Preferences.fontIconSize, .white) |
| 10 | + var spaceIcon = FontIcon(FontIcon.sfSymbolCircledNumber0, Preferences.fontIconSize, .white) |
| 11 | + var mouseDownCallback: MouseDownCallback! |
| 12 | + var mouseMovedCallback: MouseMovedCallback! |
| 13 | + var dragAndDropTimer: Timer? |
| 14 | + |
| 15 | + convenience init() { |
| 16 | + self.init(frame: .zero) |
| 17 | + let hStackView = makeHStackView() |
| 18 | + let vStackView = makeVStackView(hStackView) |
| 19 | + let shadow = CollectionViewItemView.makeShadow(.gray) |
| 20 | + thumbnail.shadow = shadow |
| 21 | + appIcon.shadow = shadow |
| 22 | + observeDragAndDrop() |
| 23 | + subviews.append(vStackView) |
| 24 | + } |
| 25 | + |
| 26 | + private func observeDragAndDrop() { |
| 27 | + // NSImageView instances are registered to drag-and-drop by default |
| 28 | + thumbnail.unregisterDraggedTypes() |
| 29 | + appIcon.unregisterDraggedTypes() |
| 30 | + // we only handle URLs (i.e. not text, image, or other draggable things) |
| 31 | + registerForDraggedTypes([NSPasteboard.PasteboardType(kUTTypeURL as String)]) |
| 32 | + } |
| 33 | + |
| 34 | + override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation { |
| 35 | + mouseMovedCallback() |
| 36 | + return .link |
| 37 | + } |
| 38 | + |
| 39 | + override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation { |
| 40 | + dragAndDropTimer = Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { _ in |
| 41 | + self.mouseDownCallback() |
| 42 | + }) |
| 43 | + return .link |
| 44 | + } |
| 45 | + |
| 46 | + override func draggingExited(_ sender: NSDraggingInfo?) { |
| 47 | + dragAndDropTimer?.invalidate() |
| 48 | + dragAndDropTimer = nil |
| 49 | + } |
| 50 | + |
| 51 | + override func performDragOperation(_ sender: NSDraggingInfo) -> Bool { |
| 52 | + let urls = sender.draggingPasteboard.readObjects(forClasses: [NSURL.self]) as! [URL] |
| 53 | + let appUrl = window_!.application.runningApplication.bundleURL! |
| 54 | + let open = try? NSWorkspace.shared.open(urls, withApplicationAt: appUrl, options: [], configuration: [:]) |
| 55 | + (App.shared as! App).hideUi() |
| 56 | + return open != nil |
| 57 | + } |
| 58 | + |
| 59 | + override func mouseMoved(with event: NSEvent) { |
| 60 | + mouseMovedCallback() |
| 61 | + } |
| 62 | + |
| 63 | + override func mouseDown(with theEvent: NSEvent) { |
| 64 | + mouseDownCallback() |
| 65 | + } |
| 66 | + |
| 67 | + func updateRecycledCellWithNewContent(_ element: Window, _ mouseDownCallback: @escaping MouseDownCallback, _ mouseMovedCallback: @escaping MouseMovedCallback, _ screen: NSScreen) { |
| 68 | + window_ = element |
| 69 | + thumbnail.image = element.thumbnail |
| 70 | + let (thumbnailWidth, thumbnailHeight) = CollectionViewItemView.thumbnailSize(element.thumbnail, screen) |
| 71 | + let thumbnailSize = NSSize(width: thumbnailWidth.rounded(), height: thumbnailHeight.rounded()) |
| 72 | + thumbnail.image?.size = thumbnailSize |
| 73 | + thumbnail.frame.size = thumbnailSize |
| 74 | + appIcon.image = element.icon |
| 75 | + let appIconSize = NSSize(width: Preferences.iconSize, height: Preferences.iconSize) |
| 76 | + appIcon.image?.size = appIconSize |
| 77 | + appIcon.frame.size = appIconSize |
| 78 | + label.string = element.title |
| 79 | + // workaround: setting string on NSTextView change the font (most likely a Cocoa bug) |
| 80 | + label.font = Preferences.font |
| 81 | + hiddenIcon.isHidden = !window_!.isHidden |
| 82 | + minimizedIcon.isHidden = !window_!.isMinimized |
| 83 | + spaceIcon.isHidden = element.spaceIndex == nil || Spaces.isSingleSpace || Preferences.hideSpaceNumberLabels |
| 84 | + if !spaceIcon.isHidden { |
| 85 | + if element.isOnAllSpaces { |
| 86 | + spaceIcon.setStar() |
| 87 | + } else { |
| 88 | + spaceIcon.setNumber(UInt32(element.spaceIndex!)) |
| 89 | + } |
| 90 | + } |
| 91 | + let fontIconWidth = CGFloat([minimizedIcon, hiddenIcon, spaceIcon].filter { !$0.isHidden }.count) * (Preferences.fontIconSize + Preferences.intraCellPadding) |
| 92 | + label.textContainer!.size.width = frame.width - Preferences.iconSize - Preferences.intraCellPadding * 3 - fontIconWidth |
| 93 | + subviews.first!.frame.size = frame.size |
| 94 | + self.mouseDownCallback = mouseDownCallback |
| 95 | + self.mouseMovedCallback = mouseMovedCallback |
| 96 | + if trackingAreas.count > 0 { |
| 97 | + removeTrackingArea(trackingAreas[0]) |
| 98 | + } |
| 99 | + addTrackingArea(NSTrackingArea(rect: bounds, options: [.mouseMoved, .activeAlways], owner: self, userInfo: nil)) |
| 100 | + } |
| 101 | + |
| 102 | + static func makeShadow(_ color: NSColor) -> NSShadow { |
| 103 | + let shadow = NSShadow() |
| 104 | + shadow.shadowColor = color |
| 105 | + shadow.shadowOffset = .zero |
| 106 | + shadow.shadowBlurRadius = 1 |
| 107 | + return shadow |
| 108 | + } |
| 109 | + |
| 110 | + static func downscaleFactor() -> CGFloat { |
| 111 | + let nCellsBeforePotentialOverflow = Preferences.minRows * Preferences.minCellsPerRow |
| 112 | + guard CGFloat(Windows.list.count) > nCellsBeforePotentialOverflow else { return 1 } |
| 113 | + // TODO: replace this buggy heuristic with a correct implementation of downscaling |
| 114 | + return nCellsBeforePotentialOverflow / (nCellsBeforePotentialOverflow + (sqrt(CGFloat(Windows.list.count) - nCellsBeforePotentialOverflow) * 2)) |
| 115 | + } |
| 116 | + |
| 117 | + static func widthMax(_ screen: NSScreen) -> CGFloat { |
| 118 | + return (ThumbnailsPanel.widthMax(screen) / Preferences.minCellsPerRow - Preferences.interCellPadding) * CollectionViewItemView.downscaleFactor() |
| 119 | + } |
| 120 | + |
| 121 | + static func widthMin(_ screen: NSScreen) -> CGFloat { |
| 122 | + return (ThumbnailsPanel.widthMax(screen) / Preferences.maxCellsPerRow - Preferences.interCellPadding) * CollectionViewItemView.downscaleFactor() |
| 123 | + } |
| 124 | + |
| 125 | + static func height(_ screen: NSScreen) -> CGFloat { |
| 126 | + return (ThumbnailsPanel.heightMax(screen) / Preferences.minRows - Preferences.interCellPadding) * CollectionViewItemView.downscaleFactor() |
| 127 | + } |
| 128 | + |
| 129 | + static func width(_ image: NSImage?, _ screen: NSScreen) -> CGFloat { |
| 130 | + return max(thumbnailSize(image, screen).0 + Preferences.intraCellPadding * 2, CollectionViewItemView.widthMin(screen)) |
| 131 | + } |
| 132 | + |
| 133 | + static func thumbnailSize(_ image: NSImage?, _ screen: NSScreen) -> (CGFloat, CGFloat) { |
| 134 | + guard let image = image else { return (0, 0) } |
| 135 | + let thumbnailHeightMax = CollectionViewItemView.height(screen) - Preferences.intraCellPadding * 3 - Preferences.iconSize |
| 136 | + let thumbnailWidthMax = CollectionViewItemView.widthMax(screen) - Preferences.intraCellPadding * 2 |
| 137 | + let thumbnailHeight = min(image.size.height, thumbnailHeightMax) |
| 138 | + let thumbnailWidth = min(image.size.width, thumbnailWidthMax) |
| 139 | + let imageRatio = image.size.width / image.size.height |
| 140 | + let thumbnailRatio = thumbnailWidth / thumbnailHeight |
| 141 | + if thumbnailRatio > imageRatio { |
| 142 | + return (image.size.width * thumbnailHeight / image.size.height, thumbnailHeight) |
| 143 | + } |
| 144 | + return (thumbnailWidth, image.size.height * thumbnailWidth / image.size.width) |
| 145 | + } |
| 146 | + |
| 147 | + private func makeHStackView() -> NSStackView { |
| 148 | + let hStackView = NSStackView() |
| 149 | + hStackView.spacing = Preferences.intraCellPadding |
| 150 | + hStackView.setViews([appIcon, label, hiddenIcon, minimizedIcon, spaceIcon], in: .leading) |
| 151 | + return hStackView |
| 152 | + } |
| 153 | + |
| 154 | + private func makeVStackView(_ hStackView: NSStackView) -> NSStackView { |
| 155 | + let vStackView = NSStackView() |
| 156 | + vStackView.wantsLayer = true |
| 157 | + vStackView.layer!.backgroundColor = .clear |
| 158 | + vStackView.layer!.cornerRadius = Preferences.cellCornerRadius |
| 159 | + vStackView.layer!.borderWidth = Preferences.cellBorderWidth |
| 160 | + vStackView.layer!.borderColor = .clear |
| 161 | + vStackView.edgeInsets = NSEdgeInsets(top: Preferences.intraCellPadding, left: Preferences.intraCellPadding, bottom: Preferences.intraCellPadding, right: Preferences.intraCellPadding) |
| 162 | + vStackView.orientation = .vertical |
| 163 | + vStackView.spacing = Preferences.intraCellPadding |
| 164 | + vStackView.setViews([hStackView, thumbnail], in: .leading) |
| 165 | + return vStackView |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +typealias MouseDownCallback = () -> Void |
| 170 | +typealias MouseMovedCallback = () -> Void |
0 commit comments