Skip to content

Commit

Permalink
Fix clock bug
Browse files Browse the repository at this point in the history
  • Loading branch information
glouel committed Oct 27, 2018
1 parent e7a1c74 commit 766d4b8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Aerial/Resources/PreferencesWindow.xib
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ is disabled
</view>
</tabViewItem>
<tabViewItem label="Text" identifier="" id="bPY-uP-DGa">
<view key="view" id="M1m-ud-C95">
<view key="view" ambiguous="YES" id="M1m-ud-C95">
<rect key="frame" x="10" y="33" width="614" height="348"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand Down Expand Up @@ -1163,7 +1163,7 @@ Shift, but macOS 10.12.4 or above and a compatible Mac are required) </string>
</view>
</tabViewItem>
<tabViewItem label="Brightness" identifier="" id="qTo-kf-FbK">
<view key="view" ambiguous="YES" id="dIK-5r-Sr4">
<view key="view" id="dIK-5r-Sr4">
<rect key="frame" x="10" y="33" width="614" height="348"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand Down
3 changes: 2 additions & 1 deletion Aerial/Source/Controllers/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,8 @@ NSOutlineViewDelegate {
if (event != nil) {
if (event!.type != .leftMouseUp && event!.type != .leftMouseDown && event!.type != .leftMouseDragged)
{
warnLog("Unexepected event type \(event!.type)")
//warnLog("Unexepected event type \(event!.type)")
return
}
if event!.type == .leftMouseUp {
if savedBrightness != nil {
Expand Down
50 changes: 40 additions & 10 deletions Aerial/Source/Views/AerialView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ class AerialView: ScreenSaverView {
}
}
self.textLayer.add(fadeAnimation, forKey: "textfade")
setupTextLayer(string: str, duration : fadeAnimation.duration, isInitial: false, totalDuration: video.duration)
setupTextLayer(string: str, duration : fadeAnimation.duration, isInitial: true, totalDuration: video.duration)
}
}
}
Expand Down Expand Up @@ -758,6 +758,44 @@ class AerialView: ScreenSaverView {
setupAndRepositionExtra(position: preferences.descriptionCorner!, duration: duration, isInitial: isInitial, totalDuration: totalDuration)
}
}
private func reRectClock() {
let preferences = Preferences.sharedInstance

let dateFormatter = DateFormatter()
if (preferences.withSeconds) {
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "j:mm:ss", options: 0, locale: Locale.current)
} else {
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "j:mm", options: 0, locale: Locale.current)
}

let dateString = dateFormatter.string(from: Date())
self.clockLayer.string = dateString
// We override font size on previews
var fontSize = CGFloat(preferences.extraFontSize!)
if (layer!.bounds.height < 200) {
fontSize = 12
}

// Get font with a fallback in case
var font = NSFont(name: "Monaco", size: 28)
if let tryFont = NSFont(name: preferences.extraFontName!,size: fontSize) {
font = tryFont
}

// Make sure we change the layer font/size
self.clockLayer.font = font
self.clockLayer.fontSize = fontSize

let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : font as Any]

// Calculate bounding box
let s = NSAttributedString(string: dateString, attributes: attributes)
let rect = s.boundingRect(with: layer!.visibleRect.size, options: NSString.DrawingOptions.usesLineFragmentOrigin)

// Rebind frame
let oldRect = self.clockLayer.frame
self.clockLayer.frame = CGRect(x: oldRect.minX, y: oldRect.minY, width: rect.maxX, height: rect.maxY)
}

private func setupAndRepositionExtra(position: Int, duration: CFTimeInterval, isInitial: Bool, totalDuration: Double)
{
Expand All @@ -769,15 +807,7 @@ class AerialView: ScreenSaverView {
{
if #available(OSX 10.12, *) {
clockTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (Timer) in
let dateFormatter = DateFormatter()
if (preferences.withSeconds) {
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "j:mm:ss", options: 0, locale: Locale.current)
} else {
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "j:mm", options: 0, locale: Locale.current)
}

let dateString = dateFormatter.string(from: Date())
self.clockLayer.string = dateString
self.reRectClock()
})
}

Expand Down

0 comments on commit 766d4b8

Please sign in to comment.