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 21, 2024
1 parent 081f9ab commit 9591071
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class OutOfTimeContentView: UIView {
}

func enableDisconnectButton(_ enabled: Bool, animated: Bool) {
disconnectButton.isEnabled = enabled
UIView.animate(withDuration: animated ? 0.25 : 0) {
self.disconnectButton.alpha = enabled ? 1 : 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class OutOfTimeViewController: UIViewController, RootContainment {
.isSecured
contentView.restoreButton.isEnabled = isInteractionEnabled

contentView.enableDisconnectButton(tunnelState.isSecured, animated: true)
contentView.updateDisconnectButton(tunnelState.isSecured, animated: true)

Check failure on line 152 in ios/MullvadVPN/View controllers/OutOfTime/OutOfTimeViewController.swift

View workflow job for this annotation

GitHub Actions / Unit tests

value of type 'OutOfTimeContentView' has no member 'updateDisconnectButton'

if tunnelState.isSecured {
contentView.setBodyLabelText(
Expand Down Expand Up @@ -265,7 +265,7 @@ class OutOfTimeViewController: UIViewController, RootContainment {
}

@objc private func handleDisconnect(_ sender: Any) {
contentView.disconnectButton.isEnabled = false
contentView.updateDisconnectButton(false, animated: false)

Check failure on line 268 in ios/MullvadVPN/View controllers/OutOfTime/OutOfTimeViewController.swift

View workflow job for this annotation

GitHub Actions / Unit tests

value of type 'OutOfTimeContentView' has no member 'updateDisconnectButton'
interactor.stopTunnel()
}
}
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 9591071

Please sign in to comment.