This commit is contained in:
relikd
2023-03-19 13:19:25 +01:00
commit 65ab9c3115
11 changed files with 251 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
.DS_Store
/*.app
/*.tar.gz
*.xcodeproj

7
LICENSE Normal file
View File

@@ -0,0 +1,7 @@
Copyright 2023 relikd
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

43
Makefile Normal file
View File

@@ -0,0 +1,43 @@
# usage: make [CONFIG=debug|release]
ifeq ($(CONFIG), debug)
CFLAGS=-Onone -g
else
CFLAGS=-O
endif
PLIST=$(shell grep -A1 $(1) src/Info.plist | tail -1 | cut -d'>' -f2 | cut -d'<' -f1)
Darker.app: SDK_PATH=$(shell xcrun --show-sdk-path --sdk macosx)
Darker.app: src/*
@mkdir -p Darker.app/Contents/MacOS/
# compile x64
swiftc ${CFLAGS} src/main.swift -target x86_64-apple-macos10.10 -emit-executable -sdk ${SDK_PATH} -o bin_x64
# compile arm64
swiftc ${CFLAGS} src/main.swift -target arm64-apple-macos10.10 -emit-executable -sdk ${SDK_PATH} -o bin_arm64
# make universal bundle
lipo -create bin_x64 bin_arm64 -o Darker.app/Contents/MacOS/Darker
@rm bin_x64 bin_arm64
@echo 'APPL????' > Darker.app/Contents/PkgInfo
@mkdir -p Darker.app/Contents/Resources/
@cp src/AppIcon.icns Darker.app/Contents/Resources/AppIcon.icns
@cp src/Info.plist Darker.app/Contents/Info.plist
@touch Darker.app
.PHONY: sign
sign: Darker.app
codesign -v -s 'Apple Development' --options=runtime --timestamp Darker.app
@echo
@echo 'Verify Signature...'
@echo
codesign -dvv Darker.app
@echo
codesign -vvv --deep --strict Darker.app
@echo
spctl -vvv --assess --type exec Darker.app
.PHONY: release
release: VERSION=$(call PLIST,CFBundleShortVersionString)
release: Darker.app
tar -czf "Darker_v${VERSION}.tar.gz" Darker.app

41
README.md Normal file
View File

@@ -0,0 +1,41 @@
[![macOS 10.10+](https://img.shields.io/badge/macOS-10.10+-888)](#install)
[![Current release](https://img.shields.io/github/release/relikd/Darker)](https://github.com/relikd/Darker/releases)
[![All downloads](https://img.shields.io/github/downloads/relikd/Darker/total)](https://github.com/relikd/Darker/releases)
# Darker
Darker allows you to dim your screen beyond the last screen brightness beam.
![menu bar icons](img/screen.png)
## Install
You will need macOS 10.10 or newer.
Download and unzip the tar.gz from [latest release](https://github.com/relikd/Darker/releases/latest).
Alternatively, you can compile Darker from source by running `make`.
Or call the script directly (`swift src/main.swift`) without building an app bundle.
## FAQ
### Why?
There are a number of other tools in the AppStore that do exactly the same.
So why bother in creating a new solution?
Well, previously I used QuickShade and was happy with it.
But I moved away from the AppStore and this was my last application that was only available via AppStore.
I asked the developer if the source code is available but got no reply.
So, thats why you have this open source project now ;-)
The app is just a wrapper around a single code line (`CGSetDisplayTransferByTable`) and was finished in just half a day.
Enjoy.
### Develop
You can either run the `main.swift` file directly with `swift main.swift`, via Terminal `./main.swift` (`chmod 755 main.swift`), or create a new Xcode project.
In Xcode, select the Command-Line template and replace the template provided `main.swift` with this one.

8
img/icon-16-alt.svg Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
<clipPath id="c"><circle cx="512" cy="512" r="512"/></clipPath>
<circle cx="512" cy="512" r="512" fill="#740"/>
<path clip-path="url(#c)" fill="#C90" d="
M-999,-999H9999V9999H-999z
M250,412 a450,450,1,0,0,900,0 a450,450,1,0,0,-900,0z"/>
</svg>

After

Width:  |  Height:  |  Size: 356 B

9
img/icon-16.svg Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
<clipPath id="c"><circle cx="512" cy="512" r="512"/></clipPath>
<g clip-path="url(#c)"><rect width="1024" height="1024" fill="#740"/></g>
<g clip-path="url(#c)" transform="translate(512,512)scale(0.75)translate(-512,-512)">
<path fill="#FFF" d="M-999,-999H9999V9999H-999z
M250,412a450,450,1,0,0,900,0a450,450,1,0,0,-900,0z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 446 B

15
img/icon-512.svg Normal file
View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
<clipPath id="c"><circle cx="512" cy="512" r="512"/></clipPath>
<g clip-path="url(#c)">
<rect width="1024" height="1024" fill="#960"/>
<rect y="0" width="1024" height="256" style="opacity:0.2"/>
<rect y="256" width="1024" height="256" style="opacity:0.4"/>
<rect y="512" width="1024" height="256" style="opacity:0.6"/>
<rect y="768" width="1024" height="256" style="opacity:0.8"/>
</g>
<g clip-path="url(#c)" transform="translate(512,512)scale(0.75)translate(-512,-512)">
<path fill="#FFF" d="M-999,-999H9999V9999H-999z
M250,412a450,450,1,0,0,900,0a450,450,1,0,0,-900,0z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 706 B

BIN
img/screen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
src/AppIcon.icns Normal file

Binary file not shown.

36
src/Info.plist Normal file
View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>de.relikd.Darker</string>
<key>CFBundleExecutable</key>
<string>Darker</string>
<key>CFBundleName</key>
<string>Darker</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>42</string>
<key>LSMinimumSystemVersion</key>
<string>10.10</string>
<key>LSBackgroundOnly</key>
<true/>
<key>LSUIElement</key>
<true/>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSSupportsSuddenTermination</key>
<true/>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2023 relikd.</string>
</dict>
</plist>

88
src/main.swift Normal file
View File

@@ -0,0 +1,88 @@
#!/usr/bin/env swift
import ApplicationServices
import Cocoa
import AppKit
class AppDelegate: NSObject, NSApplicationDelegate {
private var statusItem: NSStatusItem!
private var value: Float = 1.0
func applicationDidFinishLaunching(_ aNotification: Notification) {
UserDefaults.standard.register(defaults: ["value": 1.0])
value = UserDefaults.standard.float(forKey: "value")
updateGamma()
// create status menu icon
self.statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
self.statusItem.button?.image = NSImage.statusIcon
self.statusItem.menu = NSMenu(title: "")
self.statusItem.menu!.addItem(sliderMenuItem())
self.statusItem.menu!.addItem(NSMenuItem.separator())
self.statusItem.menu!.addItem(withTitle: "Quit", action: #selector(NSApp.terminate), keyEquivalent: "q")
}
private func sliderMenuItem() -> NSMenuItem {
let slider = NSSlider(frame: NSRect(x: 10, y: 5, width: 150, height: 0))
slider.frame.size.height = slider.fittingSize.height
slider.target = self
slider.action = #selector(sliderCallback)
slider.floatValue = value
slider.numberOfTickMarks = 9
slider.allowsTickMarkValuesOnly = true
let item = NSMenuItem()
item.view = NSView(frame: slider.frame.insetBy(dx: -10, dy: -5)) // add padding
item.view!.addSubview(slider)
return item
}
@objc
private func sliderCallback(sender: NSSlider) {
value = max(sender.floatValue, 0.1) // safety measure, never get venta black
UserDefaults.standard.setValue(value, forKey: "value")
updateGamma()
}
private func updateGamma() {
let table: [CGGammaValue] = [0, value]
CGSetDisplayTransferByTable(CGMainDisplayID(), 2, table, table, table)
}
}
// MARK: - Menu Bar Icon -
extension NSImage {
static var statusIcon: NSImage {
let img = NSImage(size: .init(width: 16, height: 16), flipped: true) { rect in
let icon = UserDefaults.standard.integer(forKey: "icon")
let w = min(rect.width, rect.height)
let ctx = NSGraphicsContext.current!.cgContext
// clip to circle
ctx.addEllipse(in: rect)
ctx.clip()
// draw moon
if icon == 0 || icon == 1 {
ctx.addRect(rect)
ctx.addEllipse(in: .init(x: 0.3 * w, y: -0.1 * w, width: 0.84 * w, height: 0.84 * w))
ctx.fillPath(using: .evenOdd)
}
// draw stripes background
if icon == 0 || icon == 2 {
ctx.setFillColor(gray: 1.0, alpha: 0.2)
ctx.addRect(rect); ctx.fillPath()
ctx.addRect(rect.offsetBy(dx: 0, dy: 0.2 * rect.height)); ctx.fillPath()
ctx.addRect(rect.offsetBy(dx: 0, dy: 0.45 * rect.height)); ctx.fillPath()
ctx.addRect(rect.offsetBy(dx: 0, dy: 0.7 * rect.height)); ctx.fillPath()
}
return true
}
img.isTemplate = true
return img
}
}
// MARK: - Main Entry
let delegate = AppDelegate()
NSApplication.shared.delegate = delegate
NSApplication.shared.run()
// _ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)