Repository navigation
Expand file tree
/
Copy pathNoteCellView.swift
More file actions
389 lines (310 loc) · 12.7 KB
/
Copy pathNoteCellView.swift
File metadata and controls
389 lines (310 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//
// NoteCellView.swift
// FSNotes
//
// Created by Oleksandr Glushchenko on 7/31/17.
// Copyright © 2017 Oleksandr Glushchenko. All rights reserved.
//
import Cocoa
class NoteCellView: NSTableCellView {
@IBOutlet var name: NSTextField!
@IBOutlet var preview: PreviewTextField!
@IBOutlet var date: NSTextField!
@IBOutlet var pin: NSImageView!
@IBOutlet weak var titleConstraint: NSLayoutConstraint!
@IBOutlet weak var imagePreview: NSImageView!
@IBOutlet weak var imagePreviewSecond: NSImageView!
@IBOutlet weak var imagePreviewThird: NSImageView!
public var note: Note?
public var contentLength: Int = 0
public var timestamp: Int64?
private var previewMaximumLineHeight: CGFloat = 12
private let previewLineSpacing: CGFloat = 3
public var imageKeys = [String]()
public var tableView: NotesTableView? {
get {
guard let vc = ViewController.shared() else { return nil }
return vc.notesTableView
}
}
override func viewWillDraw() {
if let originY = UserDefaultsManagement.cellViewFrameOriginY {
adjustTopMargin(margin: originY)
}
super.viewWillDraw()
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
renderPin()
name.layer?.zPosition = 1000
if let descriptor = date.font?.fontDescriptor {
date.font = NSFont.init(descriptor: descriptor, size: 11)
}
date.layer?.cornerRadius = 5
date.layer?.zPosition = 1001
date.isHidden = UserDefaultsManagement.hideDate
titleConstraint.constant = UserDefaultsManagement.hideDate ? 0 : 5
if (UserDefaultsManagement.horizontalOrientation) {
preview.isHidden = true
} else {
preview.isHidden = false
}
if UserDefaultsManagement.hidePreviewImages || UserDefaultsManagement.horizontalOrientation {
imagePreview.isHidden = true
imagePreviewSecond.isHidden = true
imagePreviewThird.isHidden = true
}
applyPreviewStyle()
applyTextColors()
var margin = 0
if !UserDefaultsManagement.horizontalOrientation && !UserDefaultsManagement.hidePreviewImages{
self.note?.loadPreviewInfo()
margin = self.note?.imageUrl?.count ?? 0 > 0 ? 58 : 0
}
pin.frame.origin.y = CGFloat(-4) + CGFloat(UserDefaultsManagement.cellSpacing) + CGFloat(margin)
}
public func configure(note: Note) {
self.note = note
}
func applyPreviewStyle() {
let additionalHeight = CGFloat(UserDefaultsManagement.cellSpacing)
guard additionalHeight >= 0 else {
applyPreviewAttributes()
return
}
let fontName = UserDefaultsManagement.noteFont.fontName
let previewFontSzie = CGFloat(UserDefaultsManagement.previewFontSize)
guard let font = NSFont(name: fontName, size: previewFontSzie) else { return }
self.previewMaximumLineHeight = font.lineHeightCustom
// vertically align
var numberOfLines = 0
var frameY = 0
if !UserDefaultsManagement.horizontalOrientation && !UserDefaultsManagement.hidePreview {
var size = CGFloat(0)
var i = -1
while true {
if size > additionalHeight - 8 {
break
}
i += 1
if i == 1 {
size += previewMaximumLineHeight
} else {
size += previewLineSpacing + previewMaximumLineHeight
}
}
numberOfLines = i
}
if numberOfLines > 1 {
frameY = Int(
(additionalHeight - previewMaximumLineHeight * CGFloat(numberOfLines) - previewLineSpacing * CGFloat(numberOfLines - 1)) / 2
)
} else {
let lines = numberOfLines > 0 ? numberOfLines : 0
frameY = Int(
(additionalHeight - previewMaximumLineHeight * CGFloat(lines)) / 2
)
}
// save margin
if frameY >= 0 {
let y = CGFloat(Int(frameY))
adjustTopMargin(margin: y)
UserDefaultsManagement.cellViewFrameOriginY = y
}
// apply font and max lines numbers
applyPreviewAttributes(numberOfLines)
}
func applyPreviewAttributes(_ maximumNumberOfLines: Int = 1) {
let string = preview.stringValue
let fontName = UserDefaultsManagement.noteFont.fontName
let previewFontSize = CGFloat(UserDefaultsManagement.previewFontSize)
guard let font = NSFont(name: fontName, size: previewFontSize) else { return }
let textParagraph = NSMutableParagraphStyle()
textParagraph.lineSpacing = previewLineSpacing
textParagraph.maximumLineHeight = previewMaximumLineHeight
let attribs = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.paragraphStyle: textParagraph
]
if maximumNumberOfLines > 0 {
preview.attributedStringValue = NSAttributedString.init(string: string, attributes: attribs)
preview.maximumNumberOfLines = maximumNumberOfLines
} else {
preview.attributedStringValue = NSAttributedString()
preview.maximumNumberOfLines = -1
}
}
override var backgroundStyle: NSView.BackgroundStyle {
set {
applyTextColors()
}
get {
return super.backgroundStyle;
}
}
public func applyTextColors() {
if let rowView = self.superview as? NSTableRowView, rowView.isSelected {
// first responder
if window?.firstResponder == superview?.superview {
name.textColor = NSColor.white
date.textColor = NSColor.white
preview.textColor = NSColor.white
// no first responder
} else {
let color = NSColor(named: "color_selected_not_fr")!
name.textColor = color
date.textColor = color
preview.textColor = color
}
return
}
// reset to not selected
let color = NSColor(deviceRed: 0.6, green: 0.6, blue: 0.6, alpha: 1)
date.textColor = color
if UserDefaultsManagement.appearanceType == AppearanceType.Custom {
name.textColor = NSColor.black
} else {
name.textColor = NSColor(named: "color_not_selected")!
}
preview.textColor = color
}
func renderPin() {
if let value = objectValue, let note = value as? Note {
if note.isPublished() {
pin.image = NSImage(named: "web")
pin.isHidden = false
pin.image?.size = NSSize(width: 14, height: 14)
} else if note.isEncrypted() {
let name = note.isUnlocked() ? "lock-open" : "lock-closed"
pin.image = NSImage(named: name)
pin.isHidden = false
pin.image?.size = NSSize(width: 14, height: 14)
} else {
pin.image = NSImage(named: "pin")
pin.isHidden = !note.isPinned
}
}
adjustPinPosition()
}
public func styleImageView(imageView: ImageView) {
imageView.isHidden = false
imageView.layer?.borderWidth = 1
imageView.layer?.borderColor = Color.darkGray.cgColor
imageView.layer?.cornerRadius = 4
}
public func getPreviewImage(imageUrl: URL, note: Note) -> Image? {
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("MainNotesList")
if !FileManager.default.fileExists(atPath: tempURL.path) {
try? FileManager.default.createDirectory(at: tempURL, withIntermediateDirectories: false, attributes: nil)
}
if let cacheName = imageUrl.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)?.md5 {
let file = tempURL.appendingPathComponent(cacheName)
if FileManager.default.fileExists(atPath: file.path) {
if let data = try? Data(contentsOf: file), let image = NSImage(data: data) {
return image
}
}
do {
let data = try Data(contentsOf: imageUrl)
if let image = NSImage(data: data) {
let size = CGSize(width: 70, height: 70)
if let resized = image.crop(to: size) {
let jpegImageData = resized.jpgData
try? jpegImageData?.write(to: file, options: .atomic)
return resized
}
}
} catch {
print(error.localizedDescription)
}
}
return nil
}
public func adjustPinPosition() {
for constraint in self.constraints {
if constraint.secondAttribute == .leading, let im = constraint.firstItem as? NSImageView {
if im.identifier?.rawValue == "pin" {
if let note = objectValue as? Note, !note.showIconInList() {
constraint.constant = -25
} else {
constraint.constant = 3
}
}
}
}
}
private func adjustTopMargin(margin: CGFloat) {
for constraint in self.constraints {
if constraint.secondAttribute == .top, let item = constraint.firstItem {
if let firstItem = item as? NSImageView, firstItem.identifier?.rawValue == "pin" {
constraint.constant = margin
continue
}
if item.isKind(of: NameTextField.self) {
constraint.constant = margin + 1.5
continue
}
if let item = item as? NSTextField, item.identifier?.rawValue == "cellDate" {
constraint.constant = margin + 3.5
}
}
}
}
public func fixTopConstraint(position: Int?, note: Note) {
guard let tableView = tableView else { return }
for constraint in self.constraints {
if ["firstImageTop", "secondImageTop", "thirdImageTop"].contains(constraint.identifier) {
let ident = constraint.identifier
let height = position != nil ? tableView.tableView(tableView, heightOfRow: position!) : self.frame.height
self.removeConstraint(constraint)
var con = CGFloat(0)
if note.getTitle() != nil {
con += self.name.frame.height
}
let isPreviewExist = note.preview.trim().count > 0
if isPreviewExist {
con += 3 + self.preview.frame.height
}
var diff = (height - con - 48) / 2
diff += con
var imageLink: NSImageView?
switch constraint.identifier {
case "firstImageTop":
imageLink = self.imagePreview
case "secondImageTop":
imageLink = self.imagePreviewSecond
case "thirdImageTop":
imageLink = self.imagePreviewThird
default:
imageLink = self.imagePreview
}
guard let firstItem = imageLink else { continue }
let secondItem = isPreviewExist ? self.preview : self
let secondAttribute: NSLayoutConstraint.Attribute = isPreviewExist ? .bottom : .top
let constant = isPreviewExist ? 6 : diff
let constr = NSLayoutConstraint(item: firstItem, attribute: .top, relatedBy: .equal, toItem: secondItem, attribute: secondAttribute, multiplier: 1, constant: constant)
constr.identifier = ident
self.addConstraint(constr)
}
}
}
public func attachHeaders(note: Note) {
if let title = note.getTitle() {
self.name.stringValue = title
self.preview.stringValue = note.preview
} else {
self.name.stringValue = ""
self.preview.stringValue = ""
}
if let viewController = ViewController.shared(),
let sidebarItem = viewController.getSidebarItem(),
let sort = sidebarItem.project?.sortBy,
sort == .creationDate,
let date = note.getCreationDateForLabel() {
self.date.stringValue = date
} else {
self.date.stringValue = note.getDateForLabel()
}
self.applyTextColors()
}
}