PCB // Renovate the UI theme.

This commit is contained in:
ShikiSuen 2024-01-09 12:38:29 +08:00
parent 68d61f2311
commit b3ae482c70
3 changed files with 73 additions and 22 deletions

View File

@ -99,17 +99,7 @@ public extension SessionCtl {
name: PrefMgr.shared.candidateTextFontName, size: PrefMgr.shared.candidateListTextSize name: PrefMgr.shared.candidateTextFontName, size: PrefMgr.shared.candidateListTextSize
) )
candidateUI?.locale = { candidateUI?.locale = localeForFontFallbacks
switch inputMode {
case .imeModeCHS: return "zh-Hans"
case .imeModeCHT:
if !PrefMgr.shared.shiftJISShinjitaiOutputEnabled, !PrefMgr.shared.chineseConversionEnabled {
return "zh-Hant"
}
return "ja"
default: return ""
}
}()
if let ctlCandidateCurrent = candidateUI as? CtlCandidateTDK { if let ctlCandidateCurrent = candidateUI as? CtlCandidateTDK {
ctlCandidateCurrent.useMouseScrolling = PrefMgr.shared.enableMouseScrollingForTDKCandidatesCocoa ctlCandidateCurrent.useMouseScrolling = PrefMgr.shared.enableMouseScrollingForTDKCandidatesCocoa
@ -139,6 +129,18 @@ public extension SessionCtl {
} }
} }
var localeForFontFallbacks: String {
switch inputMode {
case .imeModeCHS: return "zh-Hans"
case .imeModeCHT:
if !PrefMgr.shared.shiftJISShinjitaiOutputEnabled, !PrefMgr.shared.chineseConversionEnabled {
return "zh-Hant"
}
return "ja"
default: return ""
}
}
/// FB10978412: Since macOS 11 Big Sur, CTFontCreateUIFontForLanguage cannot /// FB10978412: Since macOS 11 Big Sur, CTFontCreateUIFontForLanguage cannot
/// distinguish zh-Hans and zh-Hant with correct adoptation of proper PingFang SC/TC variants. /// distinguish zh-Hans and zh-Hant with correct adoptation of proper PingFang SC/TC variants.
/// Update: This has been fixed in macOS 13. /// Update: This has been fixed in macOS 13.

View File

@ -109,6 +109,7 @@ public extension SessionCtl {
if state.hasComposition, clientMitigationLevel >= 2 { if state.hasComposition, clientMitigationLevel >= 2 {
updateVerticalTypingStatus() updateVerticalTypingStatus()
popupCompositionBuffer.isTypingDirectionVertical = isVerticalTyping popupCompositionBuffer.isTypingDirectionVertical = isVerticalTyping
popupCompositionBuffer.sync(accent: clientAccentColor, locale: localeForFontFallbacks)
popupCompositionBuffer.show( popupCompositionBuffer.show(
state: state, at: lineHeightRect(zeroCursor: true).origin state: state, at: lineHeightRect(zeroCursor: true).origin
) )

View File

@ -32,6 +32,32 @@ public class PopupCompositionBuffer: NSWindowController {
} }
} }
public func sync(accent: NSColor?, locale: String) {
self.locale = locale
self.accent = accent ?? themeColorCocoa
window?.backgroundColor = adjustedThemeColor
messageTextField.backgroundColor = .clear
messageTextField.textColor = textColor
}
private var accent: NSColor = .accentColor
private var locale: String = ""
var themeColorCocoa: NSColor {
switch locale {
case "zh-Hans": return .init(red: 255 / 255, green: 64 / 255, blue: 53 / 255, alpha: 0.85)
case "zh-Hant": return .init(red: 5 / 255, green: 127 / 255, blue: 255 / 255, alpha: 0.85)
case "ja": return .init(red: 167 / 255, green: 137 / 255, blue: 99 / 255, alpha: 0.85)
default: return .init(red: 5 / 255, green: 127 / 255, blue: 255 / 255, alpha: 0.85)
}
}
private func bufferFont(size: CGFloat = 18) -> NSFont {
let defaultResult: CTFont? = CTFontCreateUIFontForLanguage(.system, size, locale as CFString)
return defaultResult ?? NSFont.systemFont(ofSize: size)
}
public init() { public init() {
let contentRect = NSRect(x: 128.0, y: 128.0, width: 300.0, height: 20.0) let contentRect = NSRect(x: 128.0, y: 128.0, width: 300.0, height: 20.0)
let styleMask: NSWindow.StyleMask = [.borderless, .nonactivatingPanel] let styleMask: NSWindow.StyleMask = [.borderless, .nonactivatingPanel]
@ -41,16 +67,16 @@ public class PopupCompositionBuffer: NSWindowController {
panel.level = NSWindow.Level(Int(max(CGShieldingWindowLevel(), kCGPopUpMenuWindowLevel)) + 1) panel.level = NSWindow.Level(Int(max(CGShieldingWindowLevel(), kCGPopUpMenuWindowLevel)) + 1)
panel.hasShadow = true panel.hasShadow = true
panel.backgroundColor = NSColor.controlBackgroundColor panel.backgroundColor = NSColor.controlBackgroundColor
panel.styleMask = .fullSizeContentView panel.styleMask = .utilityWindow
panel.isMovable = false panel.isMovable = false
messageTextField = NSTextField() messageTextField = NSTextField()
messageTextField.isEditable = false messageTextField.isEditable = false
messageTextField.isSelectable = false messageTextField.isSelectable = false
messageTextField.isBezeled = false messageTextField.isBezeled = false
messageTextField.textColor = NSColor.textColor messageTextField.textColor = NSColor.selectedMenuItemTextColor
messageTextField.drawsBackground = true messageTextField.drawsBackground = true
messageTextField.backgroundColor = NSColor.clear messageTextField.backgroundColor = NSColor.clear
messageTextField.font = .systemFont(ofSize: 18) messageTextField.font = .systemFont(ofSize: 18) //
panel.contentView?.addSubview(messageTextField) panel.contentView?.addSubview(messageTextField)
panel.contentView?.wantsLayer = true panel.contentView?.wantsLayer = true
Self.currentWindow = panel Self.currentWindow = panel
@ -72,6 +98,7 @@ public class PopupCompositionBuffer: NSWindowController {
let attrPCBHeader: NSMutableAttributedString = .init(string: " ") let attrPCBHeader: NSMutableAttributedString = .init(string: " ")
let verticalAttributes: [NSAttributedString.Key: Any] = [ let verticalAttributes: [NSAttributedString.Key: Any] = [
.kern: 0, .kern: 0,
.font: bufferFont(),
.verticalGlyphForm: true, .verticalGlyphForm: true,
.paragraphStyle: { .paragraphStyle: {
let newStyle = NSMutableParagraphStyle() let newStyle = NSMutableParagraphStyle()
@ -84,7 +111,10 @@ public class PopupCompositionBuffer: NSWindowController {
return newStyle return newStyle
}(), }(),
] ]
let horizontalAttributes: [NSAttributedString.Key: Any] = [.kern: 0] let horizontalAttributes: [NSAttributedString.Key: Any] = [
.font: bufferFont(),
.kern: 0,
]
if isTypingDirectionVertical { if isTypingDirectionVertical {
attrPCBHeader.setAttributes( attrPCBHeader.setAttributes(
@ -102,10 +132,12 @@ public class PopupCompositionBuffer: NSWindowController {
) )
} }
let markerAttributes: [NSAttributedString.Key: Any] = { var markerAttributes: [NSAttributedString.Key: Any] {
var result: [NSAttributedString.Key: Any] = [ var result: [NSAttributedString.Key: Any] = [
.kern: 0, .kern: 0,
.backgroundColor: NSApplication.isDarkMode ? NSColor.systemRed : NSColor.systemYellow, .font: bufferFont(),
.backgroundColor: markerColor,
.foregroundColor: markerTextColor,
.markedClauseSegment: 0, .markedClauseSegment: 0,
] ]
if isTypingDirectionVertical { if isTypingDirectionVertical {
@ -113,7 +145,7 @@ public class PopupCompositionBuffer: NSWindowController {
result[.verticalGlyphForm] = true result[.verticalGlyphForm] = true
} }
return result return result
}() }
// //
attrString.setAttributes( attrString.setAttributes(
@ -124,10 +156,11 @@ public class PopupCompositionBuffer: NSWindowController {
) )
) )
let cursorAttributes: [NSAttributedString.Key: Any] = { var cursorAttributes: [NSAttributedString.Key: Any] {
var result: [NSAttributedString.Key: Any] = [ var result: [NSAttributedString.Key: Any] = [
.kern: -18, .kern: -18,
.foregroundColor: NSColor.textColor, .font: bufferFont(),
.foregroundColor: textColor,
] ]
if isTypingDirectionVertical { if isTypingDirectionVertical {
result[.paragraphStyle] = verticalAttributes[.paragraphStyle] result[.paragraphStyle] = verticalAttributes[.paragraphStyle]
@ -141,7 +174,7 @@ public class PopupCompositionBuffer: NSWindowController {
result[.baselineOffset] = 0 result[.baselineOffset] = 0
} }
return result return result
}() }
let attrCursor: NSAttributedString = let attrCursor: NSAttributedString =
isTypingDirectionVertical isTypingDirectionVertical
@ -153,7 +186,6 @@ public class PopupCompositionBuffer: NSWindowController {
attrString.insert(attrPCBHeader, at: attrString.length) attrString.insert(attrPCBHeader, at: attrString.length)
textShown = attrString textShown = attrString
messageTextField.maximumNumberOfLines = 1
if let editor = messageTextField.currentEditor() { if let editor = messageTextField.currentEditor() {
editor.selectedRange = NSRange(state.u16MarkedRange) editor.selectedRange = NSRange(state.u16MarkedRange)
} }
@ -205,4 +237,20 @@ public class PopupCompositionBuffer: NSWindowController {
messageTextField.frame = rect messageTextField.frame = rect
window?.setFrame(bigRect, display: true) window?.setFrame(bigRect, display: true)
} }
private var markerColor: NSColor {
.selectedMenuItemTextColor.withAlphaComponent(0.9)
}
private var markerTextColor: NSColor {
adjustedThemeColor
}
private var textColor: NSColor {
.selectedMenuItemTextColor
}
private var adjustedThemeColor: NSColor {
accent.blended(withFraction: NSApplication.isDarkMode ? 0.75 : 0.25, of: .black) ?? accent
}
} }