@@ -10,7 +10,7 @@ import SafariServices
10
10
11
11
class SafariExtensionViewController : SFSafariExtensionViewController {
12
12
13
- var items : [ CleanedParameter ] = [ ] {
13
+ var items : [ Event ] = [ ] {
14
14
didSet {
15
15
DispatchQueue . main. async {
16
16
self . collectionView. reloadData ( )
@@ -32,7 +32,7 @@ class SafariExtensionViewController: SFSafariExtensionViewController {
32
32
collectionView. backgroundColors = [ . clear]
33
33
collectionView. dataSource = self
34
34
collectionView. delegate = self
35
- collectionView. register ( CleanedLinkItem . self, forItemWithIdentifier: NSUserInterfaceItemIdentifier ( rawValue: " CleanedLinkItem " )
35
+ collectionView. register ( CleanedLinkItem . self, forItemWithIdentifier: NSUserInterfaceItemIdentifier ( rawValue: " Event " )
36
36
)
37
37
38
38
}
@@ -63,30 +63,52 @@ extension NSMutableAttributedString {
63
63
self . append ( NSAttributedString ( string: value, attributes: attributes) )
64
64
return self
65
65
}
66
+ }
66
67
67
- func underlined( _ value: String ) -> NSMutableAttributedString {
68
-
69
- let attributes : [ NSAttributedString . Key : Any ] = [
70
- . font : normalFont,
71
- . underlineStyle : NSUnderlineStyle . single. rawValue
68
+ extension SafariExtensionViewController : NSCollectionViewDelegateFlowLayout , NSCollectionViewDataSource {
69
+ private func getLabel( item: Event ) -> NSMutableAttributedString {
70
+ switch item. type {
71
+ case EventType . linkTracker:
72
+ return NSMutableAttributedString ( )
73
+ . normal ( " Prevented redirect from " )
74
+ . bold ( " \( item. domain) " )
75
+ . normal ( " to " )
76
+ . bold ( " \( item. value) " )
77
+ default :
78
+ return NSMutableAttributedString ( )
79
+ . normal ( " Removed " )
80
+ . bold ( " \( item. value) " )
81
+ . normal ( " from " )
82
+ . bold ( " \( item. domain) " )
83
+ }
84
+ }
85
+
86
+ private func estimateFrameForText( text: String ) -> CGRect {
87
+ let height : CGFloat = 30
72
88
73
- ]
89
+ let size = CGSize ( width: collectionView. frame. size. width, height: height)
90
+ let options = NSString . DrawingOptions. usesFontLeading. union ( . usesLineFragmentOrigin)
91
+ let attributes = [ NSAttributedString . Key. font: NSFont . boldSystemFont ( ofSize: 14 ) ]
74
92
75
- self . append ( NSAttributedString ( string: value, attributes: attributes) )
76
- return self
93
+ return NSString ( string: text) . boundingRect ( with: size, options: options, attributes: attributes, context: nil )
77
94
}
78
- }
79
-
80
- extension SafariExtensionViewController : NSCollectionViewDelegateFlowLayout , NSCollectionViewDataSource {
95
+
81
96
func collectionView( _ collectionView: NSCollectionView , itemForRepresentedObjectAt indexPath: IndexPath ) -> NSCollectionViewItem {
82
97
83
- let item = collectionView. makeItem ( withIdentifier: NSUserInterfaceItemIdentifier ( rawValue: " CleanedLinkItem " ) , for: indexPath) as! CleanedLinkItem
98
+ let item = collectionView. makeItem ( withIdentifier: NSUserInterfaceItemIdentifier ( rawValue: " Event " ) , for: indexPath) as! CleanedLinkItem
84
99
85
- item. label. attributedStringValue = NSMutableAttributedString ( )
86
- . normal ( " Removed " )
87
- . bold ( " \( items [ indexPath. item] . name) " )
88
- . normal ( " from " )
89
- . bold ( " \( items [ indexPath. item] . path) " )
100
+ var image : NSImage ? = nil
101
+ let label : NSMutableAttributedString = getLabel ( item: items [ indexPath. item] )
102
+
103
+ switch items [ indexPath. item] . type {
104
+ case EventType . linkTracker:
105
+ image = NSImage ( named: NSImage . Name ( " Link " ) )
106
+ default :
107
+ image = NSImage ( named: NSImage . Name ( " ShieldCheck " ) )
108
+ }
109
+
110
+ item. image. image = image
111
+ item. label. attributedStringValue = label
90
112
91
113
return item
92
114
}
@@ -96,10 +118,14 @@ extension SafariExtensionViewController: NSCollectionViewDelegateFlowLayout, NSC
96
118
}
97
119
98
120
func collectionView( _ collectionView: NSCollectionView , layout collectionViewLayout: NSCollectionViewLayout , sizeForItemAt indexPath: IndexPath ) -> NSSize {
121
+ //we are just measuring height so we add a padding constant to give the label some room to breathe!
122
+ let padding : CGFloat = 5
123
+
124
+ //estimate each cell's height
125
+ let text = getLabel ( item: items [ indexPath. item] )
126
+ let options = NSString . DrawingOptions. usesFontLeading. union ( . usesLineFragmentOrigin)
127
+ let height = text. boundingRect ( with: collectionView. frame. size, options: options)
99
128
100
- return NSSize (
101
- width: collectionView. frame. size. width,
102
- height: 30
103
- )
129
+ return CGSize ( width: collectionView. frame. size. width, height: height. size. height + padding)
104
130
}
105
131
}
0 commit comments