123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- //
- // KMThumbnailTableView.swift
- // PDF Master
- //
- // Created by tangchao on 2023/11/16.
- //
- import Cocoa
- @objc protocol KMThumbnailTableViewDelegate: KMBotaTableViewDelegate {
- @objc optional func tableView(_ tableView: NSTableView, highlightLevelForRow row: Int) -> UInt
- @objc optional func tableView(_ tableView: NSTableView, commandSelectRow rowIndex: Int) -> Bool
- @objc optional func tableView(_ tableView: NSTableView, shiftSelectRow rowIndex: Int) -> Bool
- }
- class KMThumbnailTableView: KMBotaTableView {
-
- weak var thumbDelegate: KMThumbnailTableViewDelegate?
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
- deinit {
- KMPrint("KMThumbnailTableView deinit.")
-
- NotificationCenter.default.removeObserver(self)
- }
-
- /*
- - (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect {
- // if ([[self delegate] respondsToSelector:@selector(tableView:highlightLevelForRow:)] &&
- // [self isRowSelected:row] == NO) {
- //
- // NSUInteger level = [[self delegate] tableView:self highlightLevelForRow:row];
- //
- // if (level < MAX_HIGHLIGHTS) {
- //
- // NSColor *color = [NSColor sourceListHighlightColorForView:self];
- // if (color) {
- // NSRect rect = [self rectOfRow:row];
- // if (NSIntersectsRect(rect, clipRect)) {
- // [NSGraphicsContext saveGraphicsState];
- // [[color colorWithAlphaComponent:0.1 * (MAX_HIGHLIGHTS - level)] setFill];
- // [NSBezierPath fillRect:rect];
- // [NSGraphicsContext restoreGraphicsState];
- // }
- // }
- // }
- // }
-
- [super drawRow:row clipRect:clipRect];
- }
- */
-
- override func highlightSelection(inClipRect clipRect: NSRect) {
- if self.selectionHighlightStyle == .regular {
- NSGraphicsContext.saveGraphicsState()
- var color = NSColor(red: 227.0/255.0, green: 238.0/255.0, blue: 250.0/255.0, alpha: 1)
- if KMAppearance.isSupportNewColor() {
- if KMAppearance.isDarkMode() {
- color = NSColor(red: 50.0/255.0, green: 68/255.0, blue: 92.0/255.0, alpha: 1)
- }
- }
- color.setFill()
- for row in self.selectedRowIndexes {
- __NSRectFill(self.rect(ofRow: row))
- }
- NSGraphicsContext.restoreGraphicsState()
- } else {
- super.highlightSelection(inClipRect: clipRect)
- }
- }
-
- override func preparedCell(atColumn column: Int, row: Int) -> NSCell? {
- let cell = super.preparedCell(atColumn: column, row: row)
- if self.selectionHighlightStyle == .regular && self.isRowSelected(row) && cell?.type == .textCellType {
- var attrString = cell?.attributedStringValue.mutableCopy() as? NSMutableAttributedString
- attrString?.addAttribute(.foregroundColor, value: NSColor.black, range: NSMakeRange(0, attrString?.length ?? 0))
- if attrString != nil {
- cell?.attributedStringValue = attrString!
- }
- }
- return cell
- }
-
- override func selectRowIndexes(_ indexes: IndexSet, byExtendingSelection extend: Bool) {
- super.selectRowIndexes(indexes, byExtendingSelection: extend)
-
- self._handleHighlightsChanged()
- }
-
- override func deselectRow(_ row: Int) {
- super.deselectRow(row)
-
- self._handleHighlightsChanged()
- }
- override func becomeFirstResponder() -> Bool {
- if super.becomeFirstResponder() {
- self._handleHighlightsChanged()
- return true
- }
- return false
- }
-
- override func resignFirstResponder() -> Bool {
- if super.resignFirstResponder() {
- self._handleHighlightsChanged()
- return true
- }
- return false
- }
-
- override func viewWillMove(toWindow newWindow: NSWindow?) {
- let oldWindow = self.window
- let names = [NSWindow.didBecomeMainNotification, NSWindow.didResignMainNotification, NSWindow.didBecomeKeyNotification, NSWindow.didResignKeyNotification]
- if (oldWindow != nil) {
- for name in names {
- NotificationCenter.default.removeObserver(self, name: name, object: oldWindow)
- }
- }
- if (newWindow != nil) {
- for name in names {
- NotificationCenter.default.addObserver(self, selector: #selector(_handleKeyOrMainStateChanged), name: name, object: newWindow)
- }
- }
-
- super.viewWillMove(toWindow: newWindow)
- }
- override func viewDidMoveToWindow() {
- super.viewDidMoveToWindow()
-
- if (self.window != nil) {
- self._handleKeyOrMainStateChanged(nil)
- }
- }
-
- override func mouseDown(with event: NSEvent) {
- if event.modifierFlags.contains(.command) {
- let row = self.row(at: event.locationInView(self))
- if row != -1 {
- if let data = self.thumbDelegate?.tableView?(self, commandSelectRow: row), data {
- return
- }
- }
- } else if event.modifierFlags.contains(.shift) {
- let row = self.row(at: event.locationInView(self))
- if row != -1 {
- if let data = self.thumbDelegate?.tableView?(self, shiftSelectRow: row), data {
- return
- }
- }
- }
- super.mouseDown(with: event)
- }
-
- }
- // MARK: - Private
- extension KMThumbnailTableView {
- @objc private func _handleKeyOrMainStateChanged(_ note: NSNotification?) {
- if self.selectionHighlightStyle == .sourceList {
- self._handleHighlightsChanged()
- } else {
- if let win = self.window, win.isMainWindow || win.isKeyWindow {
- self.backgroundColor = KMAppearance.viewBackgroundColor()
- } else {
- self.backgroundColor = .controlColor
- }
- self.needsDisplay = true
- }
- }
-
- @objc private func _handleHighlightsChanged() {
- if let _ = self.thumbDelegate?.tableView?(self, highlightLevelForRow: -1) {
- self.needsDisplay = true
- }
- }
- }
|