-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: text color of the last event description was incorrect.
- Loading branch information
Showing
5 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,5 @@ extension ThemeService { | |
return nil | ||
} | ||
return ThemeIdentifier(rawValue: themeId) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// Copyright 2023 New Vector Ltd | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Utility struct to convert a color to its equivalent in a given theme | ||
struct ThemeColorConverter { | ||
private static var conversionTable: [String: UIColor] = [:] | ||
private static let conversionTableQueue = DispatchQueue(label: "io.element.ThemeColorConverter.queue", qos: .userInteractive) | ||
|
||
/// Tries to convert a color to its equivalent in the given theme | ||
/// - Parameters: | ||
/// - color: the color to convert | ||
/// - targetTheme: destination theme | ||
/// - Returns: the converted color or the original one if no match is found | ||
static func convertColor(_ color: UIColor, to targetTheme: Theme) -> UIColor { | ||
let conversionTableKey = computeColorConversionTableKey(color: color, theme: targetTheme) | ||
if let cachedColor = conversionTable[conversionTableKey] { | ||
return cachedColor | ||
} | ||
|
||
var resultColor = color | ||
|
||
defer { | ||
// Store the resulting color in the conversion table | ||
conversionTableQueue.sync { | ||
conversionTable[conversionTableKey] = resultColor | ||
} | ||
} | ||
|
||
for themeIdentifier in ThemeIdentifier.allCases { | ||
if themeIdentifier.rawValue == targetTheme.identifier { | ||
continue | ||
} | ||
if let convertedColor = convertColor(color, from: ThemeService.shared().theme(withThemeId: themeIdentifier.rawValue), to: targetTheme) { | ||
resultColor = convertedColor | ||
break | ||
} | ||
} | ||
|
||
return resultColor | ||
} | ||
|
||
/// Attempts to convert a color from one theme to another | ||
/// - Parameters: | ||
/// - color: the color to convert | ||
/// - source: source Theme | ||
/// - destination: destination Theme | ||
/// - Returns: corresponding color or nil if not found | ||
private static func convertColor(_ color: UIColor, from source: Theme, to destination: Theme) -> UIColor? { | ||
let mirror = Mirror(reflecting: source.colors) | ||
// If the source theme contains the same color value for several colors, the first one will be used. | ||
if let colorName = mirror.children.first(where: {($0.value as? UIColor) == color})?.label { | ||
return destination.colors.value(forKey: colorName) as? UIColor | ||
} | ||
|
||
return nil | ||
} | ||
|
||
private static func computeColorConversionTableKey(color: UIColor, theme: Theme) -> String { | ||
var r: CGFloat = 0 | ||
var g: CGFloat = 0 | ||
var b: CGFloat = 0 | ||
var a: CGFloat = 0 | ||
color.getRed(&r, green: &g, blue: &b, alpha: &a) | ||
let hexValue = Int(r * 255) << 24 | Int(g * 255) << 16 | Int(b * 255) << 8 | Int(a * 255) << 0 | ||
return "\(theme.identifier)-\(String(format: "#%08x", hexValue))" | ||
} | ||
} | ||
|
||
extension UIColor { | ||
@objc func convertToCurrentTheme() -> UIColor { | ||
return ThemeColorConverter.convertColor(self, to: ThemeService.shared().theme) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix: The last event description text color now matches the active theme. |