From 13ebda767e69da63e56a4c51808246d4f9a1a99b Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Wed, 18 Nov 2020 21:25:58 +0100 Subject: [PATCH] GH-812 Allow all fonts, but warn when variable width font is selected --- NvimView/Sources/NvimView/NvimView.swift | 4 +-- VimR/VimR/AppDelegate.swift | 2 ++ VimR/VimR/MainWindow.swift | 1 + VimR/VimR/PrefMiddleware.swift | 32 +++++++++++++++++++++--- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/NvimView/Sources/NvimView/NvimView.swift b/NvimView/Sources/NvimView/NvimView.swift index 21f7093c..e0a126a4 100644 --- a/NvimView/Sources/NvimView/NvimView.swift +++ b/NvimView/Sources/NvimView/NvimView.swift @@ -91,8 +91,8 @@ public class NvimView: NSView, } set { - guard newValue.isFixedPitch else { - return + if !newValue.fontDescriptor.symbolicTraits.contains(.monoSpace) { + self.log.info("\(newValue) is not monospaced.") } let size = newValue.pointSize diff --git a/VimR/VimR/AppDelegate.swift b/VimR/VimR/AppDelegate.swift index 92f3a7f9..7c70640a 100644 --- a/VimR/VimR/AppDelegate.swift +++ b/VimR/VimR/AppDelegate.swift @@ -60,6 +60,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele self.useSnapshot = initialAppState.useSnapshotUpdate super.init() + + NSUserNotificationCenter.default.delegate = self } override func awakeFromNib() { diff --git a/VimR/VimR/MainWindow.swift b/VimR/VimR/MainWindow.swift index 4c6cdd54..30e02795 100644 --- a/VimR/VimR/MainWindow.swift +++ b/VimR/VimR/MainWindow.swift @@ -503,6 +503,7 @@ class MainWindow: NSObject, private func showInitError() { let notification = NSUserNotification() + notification.identifier = UUID().uuidString notification.title = "Error during initialization" notification.informativeText = """ diff --git a/VimR/VimR/PrefMiddleware.swift b/VimR/VimR/PrefMiddleware.swift index 9aa325a9..3c8ca43f 100644 --- a/VimR/VimR/PrefMiddleware.swift +++ b/VimR/VimR/PrefMiddleware.swift @@ -3,7 +3,7 @@ * See LICENSE */ -import Foundation +import Cocoa import DictionaryCoding import os @@ -30,10 +30,33 @@ class PrefMiddleware: MiddlewareType { return { tuple in let result = reduce(tuple) - guard tuple.modified else { + guard result.modified else { return result } + let newFont = result.state.mainWindowTemplate.appearance.font + let traits = newFont.fontDescriptor.symbolicTraits + + if newFont != self.currentFont { + self.currentFont = newFont + + let newFontNameText: String + if let newFontName = newFont.displayName { + newFontNameText = ", \(newFontName)," + } else { + newFontNameText = "" + } + + if !traits.contains(.monoSpace) { + let notification = NSUserNotification() + notification.identifier = UUID().uuidString + notification.title = "No monospaced font" + notification.informativeText = "The font you selected\(newFontNameText) does not seem " + + "to be a monospaced font. The rendering will most likely be broken." + NSUserNotificationCenter.default.deliver(notification) + } + } + do { let dictionary: [String: Any] = try dictEncoder.encode(result.state) defaults.set(dictionary, forKey: PrefMiddleware.compatibleVersion) @@ -45,8 +68,9 @@ class PrefMiddleware: MiddlewareType { } } - private let log = OSLog(subsystem: Defs.loggerSubsystem, - category: Defs.LoggerCategory.middleware) + private let log = OSLog(subsystem: Defs.loggerSubsystem, category: Defs.LoggerCategory.middleware) + + private var currentFont = NSFont.userFixedPitchFont(ofSize: 13)! class MainWindowMiddleware: MiddlewareType {