Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #322: Adjust height for popup when if it clips off screen #351

Merged
merged 4 commits into from
Oct 25, 2018

Conversation

danielbogomazov
Copy link
Contributor

@danielbogomazov danielbogomazov commented Oct 23, 2018

Fixes issue 322

Opening a popup in landscape mode would clip the popup off the bottom of the screen for smaller devices. This happened because the height of the popup is set based on the contents of the popup without adjusting for the screen's height.

Pull Request Checklist

  • My patch has gone through review and I have addressed review comments

  • My patch has a standard commit message that looks like Fix #123: This fixes the shattered coffee cup!

  • I have updated the Unit Tests to cover new or changed functionality

  • I have updated the UI Tests to cover new or changed functionality

  • I have marked the bug with [needsuplift]

  • I have made sure that localizable strings use NSLocalizableString()

Screenshots

screen shot 2018-10-23 at 12 46 01 am

@@ -467,6 +467,7 @@ class PopupView: UIView, UIGestureRecognizerDelegate {
buttonData.isDefault = true
buttonData.handler = tapped
dialogButtons.append(buttonData)
layoutSubviews()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't call layoutSubviews directly. Instead make this call setNeedsLayout and additionally layoutIfNeeded if layout needs to happen instantly

resizePercentage = desiredHeight / containerViewFrame.height
titleLabel.adjustsFontSizeToFitWidth = true
messageLabel.adjustsFontSizeToFitWidth = true
updateSubviews()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's probably never going to be a problem, but is it possible to get a solution that doesn't involve potential recursion? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up. I've removed the recursion. Let me know what you think :)

Copy link
Contributor

@jhreis jhreis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for submitting a PR! This will be a really nice thing to have, just a few random comments. Please let us know if you have any questions!

: kPopupDialogButtonHeight + paddingHeight

if containerView.frame.height + externalContentHeight > UIScreen.main.bounds.height {
let desiredHeight: CGFloat = UIScreen.main.bounds.height - externalContentHeight
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is actually functionally equivalent to the if conditional logic right above it. Probably cleaner to create this constant outside the if and just use it in the boolean logic:

let desiredHeight: CGFloat = UIScreen.main.bounds.height - externalContentHeight
if containerView.frame.height > desiredHeight {
    // use `desiredHeight`
}


_updateSubviews()

let paddingHeight: CGFloat = padding * 3.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all unneeded manual type setting (e.g. the 3 CGFloat instances)

}
}

fileprivate func _updateSubviews(_ resizePercentage: CGFloat = 1.0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Remove leading _
  • Make resizePercentage a named parameter (no leading _ here either.

let width: CGFloat = dialogWidth

var imageFrame: CGRect = dialogImage?.frame ?? CGRect.zero
if let dialogImage = dialogImage {
imageFrame.size = CGSize(width: dialogImage.image!.size.width * resizePercentage, height: dialogImage.image!.size.height * resizePercentage)
Copy link
Contributor

@jhreis jhreis Oct 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove image forced casting (can unwrap inside the above if statement:

if let dialogImage = dialogImage, let dialogeImageSize = dialogImage.image?.size {

_updateSubviews()

let paddingHeight: CGFloat = padding * 3.0
let externalContentHeight: CGFloat = dialogButtons.count == 0 ? paddingHeight
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed line break, think line is still decent length (< 120 at least), especially after removing the unneeded CGFloat.

@jhreis
Copy link
Contributor

jhreis commented Oct 24, 2018

I've been thinking about this a bit more, and I think we can actually calculate all the necessary variables and setup what is needed before ever calling updateSubviews. Something like:

        let paddingHeight = padding * 3.0
        let externalContentHeight = dialogButtons.count == 0 ? paddingHeight : kPopupDialogButtonHeight + paddingHeight
        let desiredHeight = UIScreen.main.bounds.height - externalContentHeight

        let resizingRequired = containerView.frame.height > desiredHeight

and then use resizingRequired for the boolean related logic, thoughts @danielbogomazov?

@danielbogomazov
Copy link
Contributor Author

Thanks for the review, @jhreis! I've fixed all of the commented blocks.

I'm not quite sure what you mean by

I think we can actually calculate all the necessary variables and setup what is needed before ever calling updateSubviews

The variables required are only relevant after the subviews are updated initially. They then check if the new height can fit on the screen - if not, it will resize them again, this time to fit. This means that desiredHeight can only be calculated after the first set of updates ran by updateSubview - only being relevant if the default sizes clip off screen.

@jhreis
Copy link
Contributor

jhreis commented Oct 24, 2018

Ah, gotcha, I was thinking the height was precalculated, was imaging something like*. But see now why that wasn't possible.

    func updateSubviews() {
        let paddingHeight = padding * 3.0
        let externalContentHeight = dialogButtons.count == 0 ? paddingHeight : kPopupDialogButtonHeight + paddingHeight
        let desiredHeight = UIScreen.main.bounds.height - externalContentHeight

        let resizingRequired = containerView.frame.height > desiredHeight
        let resizePercentage = resizingRequired ? desiredHeight / containerView.frame.height : 1.0
        
        titleLabel.adjustsFontSizeToFitWidth = resizingRequired
        messageLabel.adjustsFontSizeToFitWidth = resizingRequired

        _updateSubviews(resizePercentage)
    }

Copy link
Contributor

@jhreis jhreis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So great! Thank you so much for your help!

@kylehickinson kylehickinson merged commit d3dee8a into brave:development Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duckduckgo popup for iPhone horizontal has wrong size.
3 participants