KMNoteOutlineView.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // KMNoteOutlineView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/17.
  6. //
  7. import Cocoa
  8. protocol KMNoteOutlineViewDelegate: KMCustomOutlineViewDelegate {
  9. func outlineView(_ anOutlineView: NSOutlineView, canResizeRowByItem item: AnyObject?) -> Bool?
  10. func outlineView(_ anOutlineView: NSOutlineView, setHeight newHeight: CGFloat, ofRowByItem item: AnyObject?)
  11. func outlineView(_ anOutlineView: NSOutlineView, didChangeHiddenOfTableColumn aTableColumn: NSTableColumn)
  12. func outlineViewCommandKeyPressedDuringNavigation(_ anOutlineView: NSOutlineView)
  13. }
  14. class KMNoteOutlineView: KMCustomOutlineView {
  15. weak var noteDelegate: KMNoteOutlineViewDelegate?
  16. private let NUMBER_OF_TYPES = 9
  17. private let PAGE_COLUMNID = "page"
  18. private let NOTE_COLUMNID = "note"
  19. private let TYPE_COLUMNID = "type"
  20. private let COLOR_COLUMNID = "color"
  21. private let AUTHOR_COLUMNID = "author"
  22. private let DATE_COLUMNID = "date"
  23. private let SMALL_COLUMN_WIDTH = 32.0
  24. private let RESIZE_EDGE_HEIGHT = 5.0
  25. override func draw(_ dirtyRect: NSRect) {
  26. super.draw(dirtyRect)
  27. // Drawing code here.
  28. }
  29. required init?(coder: NSCoder) {
  30. super.init(coder: coder)
  31. var menu = NSMenu()
  32. for tc in self.tableColumns {
  33. let identifier = tc.identifier.rawValue
  34. let title = self._titleForTableColumnIdentifier(identifier)
  35. let menuItem = menu.addItem(title: title ?? "", action: #selector(toggleTableColumn), target: self)
  36. menuItem?.representedObject = identifier
  37. if tc.maxWidth >= SMALL_COLUMN_WIDTH {
  38. tc.headerCell.title = title ?? ""
  39. }
  40. }
  41. self.headerView?.menu = menu
  42. }
  43. override func drawRow(_ row: Int, clipRect: NSRect) {
  44. super.drawRow(row, clipRect: clipRect)
  45. if let can = self.noteDelegate?.outlineView(self, canResizeRowByItem: self.item(atRow: row) as? AnyObject), can {
  46. var isHighlighted = false
  47. if let win = self.window, win.isKeyWindow && self.isEqual(to: win.firstResponder) && self.isRowSelected(row) {
  48. isHighlighted = true
  49. }
  50. let rect = self.rect(ofRow: row)
  51. var x = ceil(NSMidX(rect))
  52. var y = NSMaxY(rect) - 1.5
  53. NSGraphicsContext.saveGraphicsState()
  54. NSBezierPath.defaultLineWidth = 1.0
  55. NSColor(calibratedWhite: isHighlighted ? 1.0 : 0.5, alpha: 0.7).setStroke()
  56. NSBezierPath.strokeLine(from: NSMakePoint(x - 1.0, y), to: NSMakePoint(x + 1.0, y))
  57. y -= 2.0
  58. NSBezierPath.strokeLine(from: NSMakePoint(x - 3.0, y), to: NSMakePoint(x + 3.0, y))
  59. NSGraphicsContext.restoreGraphicsState()
  60. }
  61. }
  62. override func expandItem(_ item: Any?, expandChildren: Bool) {
  63. super.expandItem(expandChildren ? nil : item, expandChildren: expandChildren)
  64. self.window?.invalidateCursorRects(for: self)
  65. }
  66. override func collapseItem(_ item: Any?, collapseChildren: Bool) {
  67. super.collapseItem(collapseChildren ? nil : item, collapseChildren: collapseChildren)
  68. self.window?.invalidateCursorRects(for: self)
  69. }
  70. /*
  71. - (void)mouseDown:(NSEvent *)theEvent {
  72. if ([theEvent clickCount] == 1 && [[self delegate] respondsToSelector:@selector(outlineView:canResizeRowByItem:)] && [[self delegate] respondsToSelector:@selector(outlineView:setHeight:ofRowByItem:)]) {
  73. NSPoint mouseLoc = [theEvent locationInView:self];
  74. NSInteger row = [self rowAtPoint:mouseLoc];
  75. id item = row != -1 ? [self itemAtRow:row] : nil;
  76. if (item && [[self delegate] outlineView:self canResizeRowByItem:item]) {
  77. NSRect rect = SKSliceRect([self rectOfRow:row], RESIZE_EDGE_HEIGHT, [self isFlipped] ? NSMaxYEdge : NSMinYEdge);
  78. if (NSMouseInRect(mouseLoc, rect, [self isFlipped]) && [NSApp willDragMouse]) {
  79. CGFloat startHeight = [[self delegate] outlineView:self heightOfRowByItem:item];
  80. [[NSCursor resizeUpDownCursor] push];
  81. while ([theEvent type] != NSLeftMouseUp) {
  82. theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | NSLeftMouseDraggedMask];
  83. if ([theEvent type] == NSLeftMouseDragged) {
  84. CGFloat currentHeight = fmax([self rowHeight], startHeight + [theEvent locationInView:self].y - mouseLoc.y);
  85. [[self delegate] outlineView:self setHeight:currentHeight ofRowByItem:item];
  86. [self noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndex:row]];
  87. }
  88. }
  89. [NSCursor pop];
  90. return;
  91. }
  92. }
  93. }
  94. [super mouseDown:theEvent];
  95. }
  96. - (void)keyDown:(NSEvent *)theEvent {
  97. unichar eventChar = [theEvent firstCharacter];
  98. NSUInteger modifiers = [theEvent standardModifierFlags];
  99. [super keyDown:theEvent];
  100. if ((eventChar == NSDownArrowFunctionKey || eventChar == NSUpArrowFunctionKey) && modifiers == NSCommandKeyMask &&
  101. [[self delegate] respondsToSelector:@selector(outlineViewCommandKeyPressedDuringNavigation:)]) {
  102. [[self delegate] outlineViewCommandKeyPressedDuringNavigation:self];
  103. }
  104. }
  105. */
  106. override func resetCursorRects() {
  107. if let _ = self.noteDelegate?.outlineView(self, canResizeRowByItem: nil) {
  108. self.discardCursorRects()
  109. super.resetCursorRects()
  110. let visibleRows = self.rows(in: self.visibleRect)
  111. // NSUInteger row;
  112. if (visibleRows.length == 0) {
  113. return
  114. }
  115. for row in visibleRows.location ..< NSMaxRange(visibleRows) {
  116. var item = self.item(atRow: row)
  117. if let can = self.noteDelegate?.outlineView(self, canResizeRowByItem: item as? AnyObject), can == false {
  118. continue
  119. }
  120. let rect = KMSliceRect(rect: self.rect(ofRow: row), amount: RESIZE_EDGE_HEIGHT, edge: self.isFlipped ? .maxY : .minY)
  121. self.addCursorRect(rect, cursor: .resizeUpDown)
  122. }
  123. } else {
  124. super.resetCursorRects()
  125. }
  126. }
  127. @objc func toggleTableColumn(_ sender: AnyObject?) {
  128. // NSTableColumn *tc = [self tableColumnWithIdentifier:[sender representedObject]];
  129. // [tc setHidden:[tc isHidden] == NO];
  130. // if ([self outlineTableColumn] == tc && [tc isHidden])
  131. // [self collapseItem:nil collapseChildren:YES];
  132. // if ([[self delegate] respondsToSelector:@selector(outlineView:didChangeHiddenOfTableColumn:)])
  133. // [[self delegate] outlineView:self didChangeHiddenOfTableColumn:tc];
  134. }
  135. override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  136. if (menuItem.action == #selector(toggleTableColumn)) {
  137. if let column = self.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: (menuItem.representedObject as? String) ?? "")), column.isHidden {
  138. menuItem.state = .off
  139. } else {
  140. menuItem.state = .on
  141. }
  142. return true
  143. }
  144. // else if ([[SKNoteOutlineView superclass] instancesRespondToSelector:_cmd]) {
  145. // return [super validateMenuItem:menuItem];
  146. // }
  147. return super.validateMenuItem(menuItem)
  148. }
  149. }
  150. // MARK: - Private Methods
  151. extension KMNoteOutlineView {
  152. private func _titleForTableColumnIdentifier(_ identifier: String) -> String? {
  153. if (identifier == NOTE_COLUMNID) {
  154. return KMLocalizedString("Note", comment: "Table header title")
  155. } else if (identifier == TYPE_COLUMNID) {
  156. return KMLocalizedString("Type", comment: "Table header title")
  157. } else if (identifier == COLOR_COLUMNID) {
  158. return KMLocalizedString("Color", comment: "Table header title")
  159. } else if (identifier == PAGE_COLUMNID) {
  160. return KMLocalizedString("Page", comment: "Table header title")
  161. } else if (identifier == AUTHOR_COLUMNID) {
  162. return KMLocalizedString("Author", comment: "Table header title")
  163. } else if (identifier == DATE_COLUMNID) {
  164. return KMLocalizedString("Date", comment: "Table header title")
  165. }
  166. return nil
  167. }
  168. }