|
| 1 | +import Foundation |
| 2 | +import Highlightr |
| 3 | +import Preferences |
| 4 | + |
| 5 | +public class HighlightrThemeManager: ThemeManager { |
| 6 | + let defaultManager: ThemeManager |
| 7 | + |
| 8 | + weak var controller: XcodeThemeController? |
| 9 | + |
| 10 | + public init(defaultManager: ThemeManager, controller: XcodeThemeController) { |
| 11 | + self.defaultManager = defaultManager |
| 12 | + self.controller = controller |
| 13 | + } |
| 14 | + |
| 15 | + public func theme(for name: String) -> Theme? { |
| 16 | + let syncSuggestionTheme = UserDefaults.shared.value(for: \.syncSuggestionHighlightTheme) |
| 17 | + let syncPromptToCodeTheme = UserDefaults.shared.value(for: \.syncPromptToCodeHighlightTheme) |
| 18 | + let syncChatTheme = UserDefaults.shared.value(for: \.syncChatCodeHighlightTheme) |
| 19 | + |
| 20 | + lazy var defaultLight = Theme(themeString: defaultLightTheme) |
| 21 | + lazy var defaultDark = Theme(themeString: defaultDarkTheme) |
| 22 | + |
| 23 | + switch name { |
| 24 | + case "suggestion-light": |
| 25 | + guard syncSuggestionTheme, let theme = theme(lightMode: true) else { |
| 26 | + return defaultLight |
| 27 | + } |
| 28 | + return theme |
| 29 | + case "suggestion-dark": |
| 30 | + guard syncSuggestionTheme, let theme = theme(lightMode: false) else { |
| 31 | + return defaultDark |
| 32 | + } |
| 33 | + return theme |
| 34 | + case "promptToCode-light": |
| 35 | + guard syncPromptToCodeTheme, let theme = theme(lightMode: true) else { |
| 36 | + return defaultLight |
| 37 | + } |
| 38 | + return theme |
| 39 | + case "promptToCode-dark": |
| 40 | + guard syncPromptToCodeTheme, let theme = theme(lightMode: false) else { |
| 41 | + return defaultDark |
| 42 | + } |
| 43 | + return theme |
| 44 | + case "chat-light": |
| 45 | + guard syncChatTheme, let theme = theme(lightMode: true) else { |
| 46 | + return defaultLight |
| 47 | + } |
| 48 | + return theme |
| 49 | + case "chat-dark": |
| 50 | + guard syncChatTheme, let theme = theme(lightMode: false) else { |
| 51 | + return defaultDark |
| 52 | + } |
| 53 | + return theme |
| 54 | + case "light": |
| 55 | + return defaultLight |
| 56 | + case "dark": |
| 57 | + return defaultDark |
| 58 | + default: |
| 59 | + return defaultLight |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + func theme(lightMode: Bool) -> Theme? { |
| 64 | + guard let controller else { return nil } |
| 65 | + guard let directories = controller.createSupportDirectoriesIfNeeded() else { return nil } |
| 66 | + |
| 67 | + let themeURL: URL = if lightMode { |
| 68 | + directories.themeDirectory.appendingPathComponent("highlightjs-light") |
| 69 | + } else { |
| 70 | + directories.themeDirectory.appendingPathComponent("highlightjs-dark") |
| 71 | + } |
| 72 | + |
| 73 | + if let themeString = try? String(contentsOf: themeURL) { |
| 74 | + return Theme(themeString: themeString) |
| 75 | + } |
| 76 | + |
| 77 | + controller.syncXcodeThemeIfNeeded() |
| 78 | + |
| 79 | + if let themeString = try? String(contentsOf: themeURL) { |
| 80 | + return Theme(themeString: themeString) |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +let defaultLightTheme = ".hljs{display:block;overflow-x:auto;padding:0.5em;background:#FFFFFFFF;color:#000000D8}.xml .hljs-meta{color:#495460FF}.hljs-comment,.hljs-quote{color:#5D6B79FF}.hljs-tag,.hljs-keyword,.hljs-selector-tag,.hljs-literal,.hljs-name{color:#9A2393FF}.hljs-attribute{color:#805E03FF}.hljs-variable,.hljs-template-variable{color:#6B36A9FF}.hljs-code,.hljs-string,.hljs-meta-string{color:#C31A15FF}.hljs-regexp{color:#000000D8}.hljs-link{color:#0E0EFFFF}.hljs-title{color:#000000FF}.hljs-symbol,.hljs-bullet{color:#805E03FF}.hljs-number{color:#1C00CFFF}.hljs-section{color:#495460FF}.hljs-meta{color:#9A2393FF}.hljs-type,.hljs-built_in,.hljs-builtin-name{color:#3900A0FF}.hljs-class .hljs-title,.hljs-title .class_{color:#0B4F79FF}.hljs-function .hljs-title,.hljs-title .function_{color:#0E67A0FF}.hljs-params{color:#0E67A0FF}.hljs-attr{color:#805E03FF}.hljs-subst{color:#000000D8}.hljs-formula{background-color:#A3CCFEFF;font-style:italic}.hljs-addition{background-color:#baeeba}.hljs-deletion{background-color:#ffc8bd}.hljs-selector-id,.hljs-selector-class{color:#000000D8}.hljs-doctag,.hljs-strong{font-weight:bold}.hljs-emphasis{font-style:italic}" |
| 88 | + |
| 89 | +let defaultDarkTheme = ".hljs{display:block;overflow-x:auto;padding:0.5em;background:#1F1F23FF;color:#FFFFFFD8}.xml .hljs-meta{color:#91A1B1FF}.hljs-comment,.hljs-quote{color:#6B7985FF}.hljs-tag,.hljs-keyword,.hljs-selector-tag,.hljs-literal,.hljs-name{color:#FC5FA2FF}.hljs-attribute{color:#BF8554FF}.hljs-variable,.hljs-template-variable{color:#A166E5FF}.hljs-code,.hljs-string,.hljs-meta-string{color:#FC695DFF}.hljs-regexp{color:#FFFFFFD8}.hljs-link{color:#5482FEFF}.hljs-title{color:#FFFFFFFF}.hljs-symbol,.hljs-bullet{color:#BF8554FF}.hljs-number{color:#CFBF69FF}.hljs-section{color:#91A1B1FF}.hljs-meta{color:#FC5FA2FF}.hljs-type,.hljs-built_in,.hljs-builtin-name{color:#D0A7FEFF}.hljs-class .hljs-title,.hljs-title .class_{color:#5CD7FEFF}.hljs-function .hljs-title,.hljs-title .function_{color:#41A1BFFF}.hljs-params{color:#41A1BFFF}.hljs-attr{color:#BF8554FF}.hljs-subst{color:#FFFFFFD8}.hljs-formula{background-color:#505A6FFF;font-style:italic}.hljs-addition{background-color:#baeeba}.hljs-deletion{background-color:#ffc8bd}.hljs-selector-id,.hljs-selector-class{color:#FFFFFFD8}.hljs-doctag,.hljs-strong{font-weight:bold}.hljs-emphasis{font-style:italic}" |
0 commit comments