How can I write an `isTablet()` function in the `class MainWindow` to distinguish between tablet devices and mobile phones on iOS and Android?
I'm already confused. I need some help.
Thank you.
Something like this, maybe?
#include <juce_gui_basics/juce_gui_basics.h>
bool isTablet()
{
// Get the screen's width and height
const auto display = juce::Desktop::getInstance().getDisplays().getPrimaryDisplay();
const auto screenWidth = display.totalArea.getWidth();
const auto screenHeight = display.totalArea.getHeight();
// Define a basic threshold for distinguishing phones from tablets
const int tabletThresholdWidth = 600; // tablets generally have a width of 600px or more
// A simple check based on width (assuming portrait mode for simplicity)
if (screenWidth >= tabletThresholdWidth)
{
return true; // Likely a tablet
}
else
{
return false; // Likely a phone
}
}
void MyComponent::paint (juce::Graphics& g)
{
if (isTablet())
{
g.setColour(juce::Colours::green);
g.fillRect(10, 10, 100, 100);
}
else
{
g.setColour(juce::Colours::blue);
g.fillRect(10, 10, 50, 50);
}
}
Can most devices be detected by their width? Thanks.
Thank you for your help. I’m trying to write the program like this.
Back in my MOAI days (a cross-platform mobile framework that is now defunct) I used to calculate the aspect ratio to determine how to scale/adjust the user interface, and it made the whole point of differentiating between mobile and tablet kind of moot.
Aspect ratio describes the proportion of width to height, and as most tablets have a higher ratio, while most mobile devices are ‘skinny’, it worked out pretty well.
Yes, this method works well,
can I ask another question? After judging the phone and tablet, how can I set Screen Orientaion: Portrait and landscape? This is usually set outside the JUCE tool,
thank you.
