Improve the rounded background when the text is truncated.

This commit is contained in:
Damiano Galassi committed 2018-06-29 09:52:57 +02:00
1 parent 3da708bd6b
commit 12a2797d8e
3 files changed
+29 -33

No files matched your search

+25 -29
View File
@@ -41,6 +41,8 @@ class ArtworkSelectorViewItemLabel : NSTextField {
needsDisplay = true
}
private static let inset: CGFloat = 12
override func draw(_ dirtyRect: NSRect) {
let windowIsKeyWindow = window?.isKeyWindow ?? false
@@ -57,7 +59,7 @@ class ArtworkSelectorViewItemLabel : NSTextField {
let framesetter = CTFramesetterCreateWithAttributedString(attributedString)
let path = CGMutablePath()
path.addRect(CGRect(x: 0, y: 0, width: bounds.size.width, height: bounds.size.height))
path.addRect(CGRect(x: 0, y: 0, width: bounds.size.width - ArtworkSelectorViewItemLabel.inset, height: bounds.size.height))
let totalFrame = CTFramesetterCreateFrame(framesetter, CFRange(), path, nil)
@@ -69,43 +71,43 @@ class ArtworkSelectorViewItemLabel : NSTextField {
// Highlight the text specified
let lines = CTFrameGetLines(totalFrame) as! [CTLine]
let lineCount = lines.count
var origins = [CGPoint](repeating: CGPoint.zero, count: lineCount)
var origins = [CGPoint](repeating: CGPoint.zero, count: lines.count)
CTFrameGetLineOrigins(totalFrame, CFRange(), &origins)
for index in 0 ..< lineCount {
let line = lines[index]
for (line, origin) in zip(lines, origins) {
let glyphRuns = CTLineGetGlyphRuns(line) as! [CTRun]
let position = CGPoint(x: origin.x + ArtworkSelectorViewItemLabel.inset / 2, y: origin.y)
var offset: CGFloat = 0
let origin = origins[index]
context.textPosition = origin
for i in 0 ..< glyphRuns.count {
let run = glyphRuns[i]
// let attributes = CTRunGetAttributes(run)
// if CFDictionaryGetValue(attributes, "HighlightText") {
if isHighlighted {
if isHighlighted {
for run in glyphRuns {
let range = CTRunGetStringRange(run)
var runBounds = CGRect.zero
var ascent: CGFloat = 0, descent: CGFloat = 0, leading: CGFloat = 0
runBounds.size.width = CGFloat(CTRunGetTypographicBounds(run, CFRange(), &ascent, &descent, &leading))
runBounds.size.height = ascent + descent
runBounds.origin.x = CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, nil) + origin.x
runBounds.origin.y = origin.y - descent
runBounds.origin.x = CTLineGetOffsetForStringIndex(line, range.location, nil) + position.x + offset
runBounds.origin.y = position.y - descent
let highlightCGColor = highlightColor.cgColor
let roundeRect = runBounds.insetBy(dx: -4, dy: 0)
let roundedPath = NSBezierPath(roundedRect: roundeRect.integral, xRadius: cornerRadius, yRadius: cornerRadius)
offset = runBounds.origin.x + runBounds.size.width - position.x
context.setFillColor(windowIsKeyWindow ? highlightCGColor : NSColor.gridColor.cgColor)
let roundedPath = NSBezierPath(roundedRect: runBounds.insetBy(dx: -2, dy: 0).integral,
xRadius: cornerRadius, yRadius: cornerRadius)
let fillColor = windowIsKeyWindow ? highlightColor.cgColor : NSColor.gridColor.cgColor
context.setFillColor(fillColor)
roundedPath.fill()
}
}
context.textPosition = position
for run in glyphRuns {
CTRunDraw(run, context, CFRange())
}
}
// CTFrameDraw(totalFrame, context)
context.restoreGState()
}
}
@@ -145,7 +147,7 @@ class ArtworkSelectorViewItemView: NSView {
backgroundLayer.isOpaque = true
emptyLayer.anchorPoint = CGPoint.zero
emptyLayer.position = CGPoint(x: 10, y: 46)
emptyLayer.position = CGPoint(x: 8, y: 46)
emptyLayer.lineWidth = 3.0
emptyLayer.lineDashPattern = [12,5]
emptyLayer.strokeColor = NSColor.secondarySelectedControlColor.cgColor
@@ -191,6 +193,8 @@ class ArtworkSelectorViewItemView: NSView {
if #available(OSX 10.14, *) {
imageLayer.shadowColor = NSColor.labelColor.cgColor
backgroundLayer.backgroundColor = NSColor.unemphasizedSelectedContentBackgroundColor.cgColor
emptyLayer.strokeColor = NSColor.secondarySelectedControlColor.cgColor
emptyLayer.fillColor = NSColor.windowBackgroundColor.cgColor
} else {
backgroundLayer.backgroundColor = NSColor.controlHighlightColor.cgColor
}
@@ -272,12 +276,4 @@ class ArtworkSelectorViewItem: NSCollectionViewItem {
}
}
override func keyDown(with event: NSEvent) {
if event.keyCode == NSEnterCharacter, let action = doubleAction {
target?.performSelector(onMainThread: action, with: nil, waitUntilDone: true)
} else {
super.keyDown(with: event)
}
}
}
+3 -3
View File
@@ -22,7 +22,7 @@
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" misplaced="YES" textCompletion="NO" id="Wkh-6c-0tU" customClass="ArtworkSelectorViewItemLabel" customModule="Subler" customModuleProvider="target">
<rect key="frame" x="-2" y="17" width="132" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textFieldCell key="cell" allowsUndo="NO" alignment="center" title="Label" drawsBackground="YES" usesSingleLineMode="YES" id="TsL-Ak-xnf">
<textFieldCell key="cell" lineBreakMode="truncatingTail" allowsUndo="NO" alignment="center" title="Label" drawsBackground="YES" usesSingleLineMode="YES" id="TsL-Ak-xnf">
<font key="font" metaFont="cellTitle"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -31,14 +31,14 @@
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" misplaced="YES" textCompletion="NO" id="fGU-Kh-WjD">
<rect key="frame" x="-2" y="4" width="132" height="14"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textFieldCell key="cell" allowsUndo="NO" alignment="center" title="Label" drawsBackground="YES" usesSingleLineMode="YES" id="ggX-Q2-CZ0">
<textFieldCell key="cell" lineBreakMode="truncatingTail" allowsUndo="NO" alignment="center" title="Label" drawsBackground="YES" usesSingleLineMode="YES" id="ggX-Q2-CZ0">
<font key="font" metaFont="system" size="10"/>
<color key="textColor" name="disabledControlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<point key="canvasLocation" x="-425" y="83"/>
<point key="canvasLocation" x="-575" y="163.5"/>
</customView>
</objects>
</document>
+1 -1
View File
@@ -298,7 +298,7 @@ class MetadataSearchController: NSWindowController, NSTableViewDataSource, NSTab
}
private func selectArtwork(artworks: [Artwork], type: MetadataType) {
if #available(OSX 10.11, *) {
if #available(OSX 10.13, *) {
let artworkSelectorController = ArtworkSelectorController(artworks: artworks, size: window?.frame.size,
type: type, delegate: self)
window?.beginSheet(artworkSelectorController.window!, completionHandler: nil)