Skip to content

Conversation

takahirom
Copy link
Owner

No description provided.

val currentHdp = (dm.heightPixels / dm.density).toInt()
val w = if (widthDp > 0) widthDp else currentWdp
val h = if (heightDp > 0) heightDp else currentHdp
qualifiers.add(if (w > h) "land" else "port")
Copy link
Owner Author

Choose a reason for hiding this comment

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

Hi @sergio-sastre!

I've implemented a workaround in this PR that automatically adds -land/-port qualifiers based on dimensions. This should handle the 2000x1000 case you mentioned.

Could you take a look and let me know if it addresses your use cases?

Thanks! 🙏
#711 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

@takahirom
Sure! I’m just thinking it could intercept with the device property of the previews, where one can also set the orientation… I’ll create an extra preview for that and see how it behaves 🧐

Copy link
Contributor

@sergio-sastre sergio-sastre Jun 30, 2025

Choose a reason for hiding this comment

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

@takahirom
So I've tested it and when combined with device, it does not work.
I've added the following previews:

@Preview(
  name = "Preview width and height large pixel5",
  device = "spec:parent=pixel_5",
  widthDp = 2000,
  heightDp = 1000,
)
@Composable
fun PreviewWithWidthAndHeightPixel5() {
  Card(
    Modifier.fillMaxSize()
  ) {
    Text(
      modifier = Modifier.padding(8.dp),
      text = "Hello, World in portrait pixel 5"
    )
  }
}

@Preview(
  name = "Preview width and height large in portrait",
  device = "spec:parent=pixel_5,orientation=portrait",
  widthDp = 2000,
  heightDp = 1000,
)
@Composable
fun PreviewWithWidthAndHeightInportrait() {
  Card(
    Modifier.fillMaxSize()
  ) {
    Text(
      modifier = Modifier.padding(8.dp),
      text = "Hello, World in portrait"
    )
  }
}

@Preview(
  name = "Preview width and height large in landscape",
  device = "spec:parent=pixel_5,orientation=landscape",
  widthDp = 2000,
  heightDp = 1000,
)
@Composable
fun PreviewWithWidthAndHeightInLandscape() {
  Card(
    Modifier.fillMaxSize()
  ) {
    Text(
      modifier = Modifier.padding(8.dp),
      text = "Hello, World in Landscape!"
    )
  }
}

All these 3 Previews in Android studio differ by a lot from the screenshots generated by Roborazzi. Moreover, the image sizes are 1078dp x 2337dp. There is no factor that makes 1000 -> 1078 and 2000-> 2337...

So it's like sth looks fishy... but I'm unsure if it has to do with width and height or with device...

Copy link
Contributor

@sergio-sastre sergio-sastre Jun 30, 2025

Choose a reason for hiding this comment

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

So, I also put these previews in the 1.45.1 branch and they look fine, exactly like in the Previews in Android Studio. This means, this approach with the setQualifiers does not fully work...
I am starting to think that the shadowDisplay approach is the only valid one...

I'd gladly add these Previews to confirm we do not have regressions in the future once we fix this.

Copy link
Owner Author

Choose a reason for hiding this comment

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

First, let's add the previews you provided. I can commit them directly, or if you prefer, you can open a PR against this branch.

Copy link
Contributor

Choose a reason for hiding this comment

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

Feel free to do it. I think our best bet is to add them, record the screenshots in version 1.45.1 since they look right, and then whenever we make changes for the size option, check what breaks.

I can help you from Wednesday evening on, today I’ll not be able to find some time for this 🙏

Let’s make this work reliably 😉

Copy link
Owner Author

@takahirom takahirom Jul 2, 2025

Choose a reason for hiding this comment

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

Thanks. I've introduced them. I've changed the order of the Qualifiers. While it's not ideal because it relies on Robolectric's implementation, it does the job.
606a1cc

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome! I've also tested it locally, I think this is ready!! :)
Thanks for fixing it!

@takahirom
Copy link
Owner Author

Current diff
image

@takahirom takahirom marked this pull request as ready for review July 5, 2025 03:54
@takahirom takahirom merged commit ad485a6 into main Jul 5, 2025
8 checks passed
@takahirom takahirom deleted the tm/add-automatic-land-port-qualifier-when-both-widthdp-and-heightdp-are-set/2025-06-30 branch July 5, 2025 03:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants