From 298f3ea5fa007a8ef162803d0b495bfbaa43cda4 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Thu, 21 Apr 2022 17:53:24 +0200 Subject: [PATCH] support macOS fixes #2 --- autoload/lumen/platforms.vim | 2 ++ autoload/lumen/platforms/macos.vim | 17 ++++++++++ autoload/lumen/platforms/macos/watcher.swift | 35 ++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 autoload/lumen/platforms/macos.vim create mode 100755 autoload/lumen/platforms/macos/watcher.swift diff --git a/autoload/lumen/platforms.vim b/autoload/lumen/platforms.vim index 20c086b..8a64dc3 100644 --- a/autoload/lumen/platforms.vim +++ b/autoload/lumen/platforms.vim @@ -1,6 +1,8 @@ let s:platform = '' if has('linux') let s:platform = 'linux' +elseif has('osx') + let s:platform = 'macos' endif func lumen#platforms#call(func, ...) diff --git a/autoload/lumen/platforms/macos.vim b/autoload/lumen/platforms/macos.vim new file mode 100644 index 0000000..959e695 --- /dev/null +++ b/autoload/lumen/platforms/macos.vim @@ -0,0 +1,17 @@ +func lumen#platforms#macos#watch_cmd() + let path = fnamemodify(resolve(expand(':p')), ':h') + return [path . "/autoload/lumen/platforms/macos/watcher.swift"] +endfunc + +func lumen#platforms#macos#parse_line(line) + if a:line == "Dark" + call lumen#dark_hook() + else + call lumen#light_hook() + endif +endfunc + +func lumen#platforms#macos#oneshot() + let out = system(join(lumen#platforms#macos#watch_cmd() + ["get"])) + call lumen#platforms#macos#parse_line(out) +endfunc diff --git a/autoload/lumen/platforms/macos/watcher.swift b/autoload/lumen/platforms/macos/watcher.swift new file mode 100755 index 0000000..da56b33 --- /dev/null +++ b/autoload/lumen/platforms/macos/watcher.swift @@ -0,0 +1,35 @@ +#!/usr/bin/env swift + +// Based on https://github.com/bouk/dark-mode-notify/blob/main/dark-mode-notify.swift (MIT license) +// Which itself is based on https://github.com/mnewt/dotemacs/blob/master/bin/dark-mode-notifier.swift (Unlicense license) + +import Cocoa +import Darwin + +var isAppearanceDark: Bool { + if #available(macOS 11.0, *) { + return NSAppearance.currentDrawing().bestMatch(from: [.darkAqua, .aqua]) == .darkAqua + } else { + return NSAppearance.current.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua + } +} + +func printAppearance() { + let string = isAppearanceDark ? "Dark" : "Light" + try! FileHandle.standardOutput.write(contentsOf: Data((string + "\n").utf8)) + // print(string) // doesn't work for some strange reason?! +} + +if CommandLine.arguments.dropFirst(1).first == "get" { + printAppearance() + exit(0) +} + +DistributedNotificationCenter.default.addObserver( + forName: Notification.Name("AppleInterfaceThemeChangedNotification"), + object: nil, + queue: nil) { _ in + printAppearance() +} + +NSApplication.shared.run()