Skip to content

Commit

Permalink
Added main screen option; Added Helvetica Font features; Tweaks and C…
Browse files Browse the repository at this point in the history
…omments
  • Loading branch information
Kamik423 committed Feb 10, 2022
1 parent 8db7b76 commit a84250c
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 133 deletions.
8 changes: 6 additions & 2 deletions Padbury Clock Revived.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = VW549R3EEQ;
INFOPLIST_FILE = "Padbury Clock Revived/Info.plist";
INSTALL_PATH = "$(HOME)/Library/Screen Savers";
Expand All @@ -284,7 +285,8 @@
"@executable_path/../Frameworks",
"@loader_path/../Frameworks",
);
MARKETING_VERSION = 1.1.0;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.1.1;
PRODUCT_BUNDLE_IDENTIFIER = "com.hans.Padbury-Clock-Revived";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand All @@ -300,6 +302,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = VW549R3EEQ;
INFOPLIST_FILE = "Padbury Clock Revived/Info.plist";
INSTALL_PATH = "$(HOME)/Library/Screen Savers";
Expand All @@ -308,7 +311,8 @@
"@executable_path/../Frameworks",
"@loader_path/../Frameworks",
);
MARKETING_VERSION = 1.1.0;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.1.1;
PRODUCT_BUNDLE_IDENTIFIER = "com.hans.Padbury-Clock-Revived";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1320"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3AA5BF5925C98A31005B39FE"
BuildableName = "Padbury Clock Revived.saver"
BlueprintName = "Padbury Clock Revived"
ReferencedContainer = "container:Padbury Clock Revived.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3AA5BF5925C98A31005B39FE"
BuildableName = "Padbury Clock Revived.saver"
BlueprintName = "Padbury Clock Revived"
ReferencedContainer = "container:Padbury Clock Revived.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>3AA5BF5925C98A31005B39FE</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
146 changes: 92 additions & 54 deletions Padbury Clock Revived/ClockView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ final class ClockView: ScreenSaverView {
static var shared: ClockView? = nil

override init?(frame: NSRect, isPreview: Bool) {
// Setup Window
super.init(frame: frame, isPreview: isPreview)
ClockView.shared = self
if isPreview { ClockView.shared = self }
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
ClockView.shared = self
// ClockView.shared = self
}

// MARK: - Configuration
Expand All @@ -32,85 +33,122 @@ final class ClockView: ScreenSaverView {
var vOffset: CGFloat = 0

var backgroundColor: NSColor = .blue
var foregroundColor: NSColor = .blue
var foregroundColor: NSColor = .green

var dateFormatter: DateFormatter = DateFormatter()
var attributes: [NSAttributedString.Key: Any] = [:]

var lastUsedNightTimeMode: Bool = false

func setup(force: Bool = false) {
if force || !hasSetup {
hasSetup = true
let currentHour = Calendar.current.component(.hour, from: Date())
let useNightTimeMode = preferences.nightTimeMode && (currentHour >= (10 + 12) || currentHour < 6)
if preferences.darkTheme {
backgroundColor = .black
foregroundColor = useNightTimeMode ? NSColor(red: 2.0 / 3.0, green: 0, blue: 0, alpha: 1) : .white
} else {
backgroundColor = .white
foregroundColor = useNightTimeMode ? .red : .black
}

let separator = preferences.showTimeSeparators ? ":" : " "
let hour = preferences.useAmPm ? "h" : "HH"
let minute = "\(separator)mm"
let second = preferences.showSeconds ? "\(separator)ss" : ""
let suffix = preferences.useAmPm ? " a" : ""
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "\(hour)\(minute)\(second)\(suffix)"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let fontSizeScaleFactor : CGFloat
switch preferences.font {
case .sanFrancisco, .helveticaNeue:
fontSizeScaleFactor = 0.20
case .sanFranciscoMono:
fontSizeScaleFactor = 0.15
case .newYork:
fontSizeScaleFactor = 0.17
}
fontSize = fontSizeScaleFactor * bounds.width
// TODO: dynamically determine value. I don't know how though
// 0.5: align center of target rect with center of screen
// 0.15: offset so font is visually centered
vOffset = fontSize * 0.5 - 0.15 * fontSize
let font = preferences.nsFont(ofSize: fontSize)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
attributes = [
.font : font,
.foregroundColor: foregroundColor,
.paragraphStyle: paragraphStyle,
]
// Get the current hour to determine conditions for night mode
let currentHour = Calendar.current.component(.hour, from: Date())
let useNightTimeMode = preferences.nightTimeMode && (currentHour >= (10 + 12) || currentHour < 6)
let nightTimeModeChanged = lastUsedNightTimeMode != useNightTimeMode

// Don't run the setup for every frame, only for the first time or when explicitly asked to
// or when the night time mode changed
if hasSetup && !force && !nightTimeModeChanged { return }
hasSetup = true

// Set the background and foreground color variables to use later
if useNightTimeMode {
backgroundColor = .black
// Red night time mode color, picked from the original
foregroundColor = NSColor(red: 255 / 255, green: 59 / 255, blue: 48 / 255, alpha: 1)
} else if preferences.darkTheme {
backgroundColor = .black
foregroundColor = .white
} else { // light theme
backgroundColor = .white
foregroundColor = .black
}

// Use a space or a colon as separator according to the settings
let separator = preferences.showTimeSeparators ? ":" : " "

// Time format setup according to the seconds
let hour = preferences.useAmPm ? "h" : "HH"
let minute = "\(separator)mm"
let second = preferences.showSeconds ? "\(separator)ss" : ""
let suffix = preferences.useAmPm ? " a" : ""
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "\(hour)\(minute)\(second)\(suffix)"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"

// The font gets a font size relative to the screen width as not to clip.
// I don't know how to determine this correctly, these are ugly hard coded
// numbers and they MIGHT fail for long strings (bold + AM/PM). If you know
// of a better system please make a pull request or write me an email!
// >>> [email protected] <<<
let fontSizeScaleFactor : CGFloat
switch preferences.font {
case .sanFrancisco, .neueHelvetica:
fontSizeScaleFactor = 0.20
case .sanFranciscoMono:
fontSizeScaleFactor = 0.13
case .newYork:
fontSizeScaleFactor = 0.17
}
fontSize = fontSizeScaleFactor * bounds.width

// Vertical offset of the font. This is manually determined.
// The 0.15 is to move it down a bit since the center of the font rect is
// lower than the center of the numbers as the rect accounts for lower case
// letter that might extend below the baseline.
//
// 0.5: align center of target rect with center of screen
// 0.15: offset so font is visually centered
vOffset = fontSize * 0.5 - 0.15 * fontSize

// Load the correct font
let font = preferences.nsFont(ofSize: fontSize)
// Set the paragraph style with the correct font, centering and color.
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
attributes = [
.font : font,
.foregroundColor: foregroundColor,
.paragraphStyle: paragraphStyle,
]
}

// MARK: - Drawing

override func animateOneFrame() {
super.animateOneFrame()
// Update the screen. This could potentially be improved by only updating
// the text bit.
setNeedsDisplay(bounds)
}

override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)

// Reload variables if needed
setup()

// Fill the screen in with the background color in case it was not painted
// yet and to overwrite the last font printed
backgroundColor.setFill()
bounds.fill()

// If the setting is set to only draw on the main screen and this is not
// the main screen stop here.
let isMainScreen = self.window?.screen == .main
if preferences.mainScreenOnly && !isMainScreen { return }

// The font rect. vOffset has been described above
let targetRect = NSRect(x: 0, y: bounds.height * 0.5 - vOffset, width: bounds.width, height: fontSize)

// NSColor.red.setFill()
// targetRect.fill()
///// For debugging the font rect can be drawn now.
// NSColor.blue.setFill()
// targetRect.fill()

// Get the time string
let time = NSString(string: dateFormatter.string(from: Date()))
// Draw the time
time.draw(in: targetRect, withAttributes: attributes)
}

Expand Down
Loading

0 comments on commit a84250c

Please sign in to comment.