Skip to content

Commit

Permalink
Fix alpha button override issues in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mojganii committed Oct 22, 2024
1 parent 081f9ab commit cf1b66d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
19 changes: 19 additions & 0 deletions ios/MullvadVPN/Extensions/UIImage+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@ extension UIImage {

return resizedImage.withRenderingMode(renderingMode)
}

func withAlpha(_ alpha: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
guard let context = UIGraphicsGetCurrentContext(), let cgImage = self.cgImage else { return nil }

let rect = CGRect(origin: .zero, size: self.size)

context.scaleBy(x: 1.0, y: -1.0) // Flip vertically
context.translateBy(x: 0, y: -rect.size.height)

context.setBlendMode(.normal)
context.setAlpha(alpha) // Set the alpha
context.draw(cgImage, in: rect)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage
}
}
13 changes: 10 additions & 3 deletions ios/MullvadVPN/Views/AppButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,23 @@ class AppButton: CustomButton {
return updatedAttributeContainer
}

let configurationHandler: UIButton.ConfigurationUpdateHandler = { button in
button.alpha = !button.isEnabled ? 0.5 : 1.0
let configurationHandler: UIButton.ConfigurationUpdateHandler = { [weak self] button in
guard let self else { return }
button.configuration?.baseForegroundColor = button.state.customButtonTitleColor
updateButtonBackground()
}
configuration = config
configurationUpdateHandler = configurationHandler
}

/// Set background image based on current style.
private func updateButtonBackground() {
configuration?.background.image = style.backgroundImage
if isEnabled {
// Load the normal image and set it as the background
configuration?.background.image = style.backgroundImage
} else {
// Adjust the image for the disabled state
configuration?.background.image = style.backgroundImage.withAlpha(0.5)
}
}
}
2 changes: 1 addition & 1 deletion ios/MullvadVPN/Views/CustomButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension UIControl.State {
case .normal:
return UIColor.AppButton.normalTitleColor
case .disabled:
return UIColor.AppButton.disabledTitleColor
return UIColor.AppButton.disabledTitleColor.withAlphaComponent(0.5)
case .highlighted:
return UIColor.AppButton.highlightedTitleColor
default:
Expand Down

0 comments on commit cf1b66d

Please sign in to comment.