Skip to content

Commit bc26ba4

Browse files
committed
🐛 优化ppt使用体验,去除10.15判断
1 parent b04ec5c commit bc26ba4

File tree

7 files changed

+387
-333
lines changed

7 files changed

+387
-333
lines changed

Common/Shared/Extensions/NSColor+.swift

+40-43
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,49 @@
44
//
55

66
import AppKit
7-
import Foundation
87

9-
public extension NSColor {
10-
convenience init(hex: String) {
11-
let trimHex = hex.trimmingCharacters(in: .whitespacesAndNewlines)
12-
let dropHash = String(trimHex.dropFirst()).trimmingCharacters(in: .whitespacesAndNewlines)
13-
let hexString = trimHex.starts(with: "#") ? dropHash : trimHex
14-
let ui64 = UInt64(hexString, radix: 16)
15-
let value = ui64 != nil ? Int(ui64!) : 0
16-
// #RRGGBB
17-
var components = (
18-
R: CGFloat((value >> 16) & 0xff) / 255,
19-
G: CGFloat((value >> 08) & 0xff) / 255,
20-
B: CGFloat((value >> 00) & 0xff) / 255,
21-
a: CGFloat(1)
22-
)
23-
if String(hexString).count == 8 {
24-
// #RRGGBBAA
25-
components = (
26-
R: CGFloat((value >> 24) & 0xff) / 255,
27-
G: CGFloat((value >> 16) & 0xff) / 255,
28-
B: CGFloat((value >> 08) & 0xff) / 255,
29-
a: CGFloat((value >> 00) & 0xff) / 255
30-
)
31-
}
32-
self.init(red: components.R, green: components.G, blue: components.B, alpha: components.a)
33-
}
34-
35-
func toHex(alpha: Bool = false) -> String? {
36-
guard let components = cgColor.components, components.count >= 3 else {
37-
return nil
38-
}
39-
40-
let r = Float(components[0])
41-
let g = Float(components[1])
42-
let b = Float(components[2])
43-
var a = Float(1.0)
8+
extension NSColor {
9+
private static let cssColorNames: [String: String] = [
10+
"black": "#000000",
11+
"silver": "#C0C0C0",
12+
"gray": "#808080",
13+
"white": "#FFFFFF",
14+
"maroon": "#800000",
15+
"red": "#FF0000",
16+
"purple": "#800080",
17+
"fuchsia": "#FF00FF",
18+
"green": "#008000",
19+
"lime": "#00FF00",
20+
"olive": "#808000",
21+
"yellow": "#FFFF00",
22+
"navy": "#000080",
23+
"blue": "#0000FF",
24+
"teal": "#008080",
25+
"aqua": "#00FFFF"
26+
]
4427

45-
if components.count >= 4 {
46-
a = Float(components[3])
28+
convenience init?(css: String) {
29+
var colorString = css.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
30+
if let hexValue = NSColor.cssColorNames[colorString] {
31+
colorString = hexValue
4732
}
48-
49-
if alpha {
50-
return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
51-
} else {
52-
return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
33+
let r, g, b, a: CGFloat
34+
if colorString.hasPrefix("#") {
35+
let start = colorString.index(colorString.startIndex, offsetBy: 1)
36+
let hexColor = String(colorString[start...])
37+
if hexColor.count == 6 {
38+
let scanner = Scanner(string: hexColor)
39+
var hexNumber: UInt64 = 0
40+
if scanner.scanHexInt64(&hexNumber) {
41+
r = CGFloat((hexNumber & 0xFF0000) >> 16) / 255
42+
g = CGFloat((hexNumber & 0x00FF00) >> 8) / 255
43+
b = CGFloat(hexNumber & 0x0000FF) / 255
44+
a = 1.0
45+
self.init(red: r, green: g, blue: b, alpha: a)
46+
return
47+
}
48+
}
5349
}
50+
return nil
5451
}
5552
}

Mac/Extensions/NSAppearance+.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ extension NSAppearance {
44
var isDark: Bool {
55
if name == .vibrantDark { return true }
66

7-
guard #available(macOS 10.14, *) else { return false }
8-
97
switch name {
108
case .accessibilityHighContrastDarkAqua,
11-
.darkAqua,
12-
.accessibilityHighContrastVibrantDark:
9+
.accessibilityHighContrastVibrantDark,
10+
.darkAqua:
1311
return true
1412
default:
1513
return false

Mac/Extensions/NSColor+.swift

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// NSColor+.swift
3+
// MiaoYan
4+
//
5+
// Created by Tw93 on 2023/3/26.
6+
// Copyright © 2023 MiaoYan App. All rights reserved.
7+
//
8+
9+
import AppKit
10+
11+
extension NSColor {
12+
private static let cssColorNames: [String: String] = [
13+
"black": "#000000",
14+
"silver": "#C0C0C0",
15+
"gray": "#808080",
16+
"white": "#FFFFFF",
17+
"maroon": "#800000",
18+
"red": "#FF0000",
19+
"purple": "#800080",
20+
"fuchsia": "#FF00FF",
21+
"green": "#008000",
22+
"lime": "#00FF00",
23+
"olive": "#808000",
24+
"yellow": "#FFFF00",
25+
"navy": "#000080",
26+
"blue": "#0000FF",
27+
"teal": "#008080",
28+
"aqua": "#00FFFF"
29+
]
30+
31+
convenience init?(css: String) {
32+
var colorString = css.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
33+
34+
if let hexValue = NSColor.cssColorNames[colorString] {
35+
colorString = hexValue
36+
}
37+
38+
let r, g, b, a: CGFloat
39+
40+
if colorString.hasPrefix("#") {
41+
let start = colorString.index(colorString.startIndex, offsetBy: 1)
42+
let hexColor = String(colorString[start...])
43+
44+
if hexColor.count == 6 {
45+
let scanner = Scanner(string: hexColor)
46+
var hexNumber: UInt64 = 0
47+
48+
if scanner.scanHexInt64(&hexNumber) {
49+
r = CGFloat((hexNumber & 0xFF0000) >> 16) / 255
50+
g = CGFloat((hexNumber & 0x00FF00) >> 8) / 255
51+
b = CGFloat(hexNumber & 0x0000FF) / 255
52+
a = 1.0
53+
54+
self.init(red: r, green: g, blue: b, alpha: a)
55+
return
56+
}
57+
}
58+
}
59+
60+
return nil
61+
}
62+
}

Mac/View/EditorScrollView.swift

+5-8
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ class EditorScrollView: NSScrollView {
55

66
override var isFindBarVisible: Bool {
77
set {
8-
// macOS 10.14 margin hack
9-
if #available(OSX 10.14, *) {
10-
if let clip = subviews.first as? NSClipView {
11-
guard let currentHeight = findBarView?.frame.height else { return }
8+
if let clip = subviews.first as? NSClipView {
9+
guard let currentHeight = findBarView?.frame.height else { return }
1210

13-
clip.contentInsets.top = newValue ? CGFloat(currentHeight) : 0
14-
if newValue, let documentView = documentView {
15-
documentView.scroll(NSPoint(x: 0, y: CGFloat(-currentHeight)))
16-
}
11+
clip.contentInsets.top = newValue ? CGFloat(currentHeight) : 0
12+
if newValue, let documentView = documentView {
13+
documentView.scroll(NSPoint(x: 0, y: CGFloat(-currentHeight)))
1714
}
1815
}
1916

0 commit comments

Comments
 (0)