Don't hard-code '30' as the default button size

This commit is contained in:
Casper Jeukendrup 2022-03-21 15:17:43 +01:00
parent d98cc32281
commit 82c9439f97
No known key found for this signature in database
GPG key ID: 6C571BEF59E722DD
3 changed files with 33 additions and 10 deletions

View file

@ -46,9 +46,9 @@ static const int DEFAULT_RADIUS = 3;
static const int GROUP_BOX_LABEL_SPACING = 2;
//! NOTE In QML, a border is drawn _inside_ a rectangle.
//! In C++, a border would normally be drawn half inside the rectangle, half outside.
//! In this function, we exactly replicate the behaviour from QML.
//! In QML, a border is drawn _inside_ a rectangle.
//! In C++, a border would normally be drawn half inside the rectangle, half outside.
//! In this function, we exactly replicate the behaviour from QML.
static void drawRoundedRect(QPainter* painter, const QRectF& rect, const qreal radius, const QBrush& brush = NO_FILL,
const QPen& pen = NO_BORDER)
{
@ -106,6 +106,7 @@ void UiTheme::init()
initUiFonts();
initIconsFont();
initMusicalFont();
calculateDefaultButtonSize();
setupWidgetTheme();
}
@ -139,6 +140,7 @@ void UiTheme::initThemeValues()
void UiTheme::update()
{
calculateDefaultButtonSize();
initThemeValues();
setupWidgetTheme();
notifyAboutThemeChanged();
@ -269,6 +271,11 @@ QFont UiTheme::defaultFont() const
return m_defaultFont;
}
qreal UiTheme::defaultButtonSize() const
{
return m_defaultButtonSize;
}
qreal UiTheme::borderWidth() const
{
return m_borderWidth;
@ -394,6 +401,18 @@ void UiTheme::setupMusicFont()
m_musicalFont.setPixelSize(configuration()->musicalFontSize());
}
void UiTheme::calculateDefaultButtonSize()
{
constexpr qreal MINIMUM_BUTTON_SIZE = 30.0;
constexpr qreal BUTTON_PADDING = 8.0;
QFontMetricsF bodyFontMetrics(m_bodyFont);
QFontMetricsF iconFontMetrics(m_iconsFont);
qreal requiredSize = std::max(bodyFontMetrics.height(), iconFontMetrics.height()) + BUTTON_PADDING;
m_defaultButtonSize = std::max(requiredSize, MINIMUM_BUTTON_SIZE);
}
void UiTheme::setupWidgetTheme()
{
QColor fontPrimaryColorDisabled = fontPrimaryColor();

View file

@ -81,6 +81,8 @@ class UiTheme : public QProxyStyle, public async::Asyncable
Q_PROPERTY(QFont defaultFont READ defaultFont CONSTANT)
Q_PROPERTY(qreal defaultButtonSize READ defaultButtonSize NOTIFY themeChanged)
Q_PROPERTY(int flickableMaxVelocity READ flickableMaxVelocity CONSTANT)
public:
@ -119,6 +121,7 @@ public:
QFont defaultFont() const;
qreal defaultButtonSize() const;
qreal borderWidth() const;
qreal navCtrlBorderWidth() const;
qreal accentOpacityNormal() const;
@ -168,6 +171,8 @@ private:
void setupIconsFont();
void setupMusicFont();
void calculateDefaultButtonSize();
void setupWidgetTheme();
void notifyAboutThemeChanged();
@ -208,6 +213,7 @@ private:
QColor m_linkColor;
QColor m_focusColor;
qreal m_defaultButtonSize = 0;
qreal m_borderWidth = 0;
qreal m_navCtrlBorderWidth = 0;
qreal m_accentOpacityNormal = 0;

View file

@ -47,8 +47,6 @@ FocusScope {
property color hoverHitColor: accentButton ? accentColor : ui.theme.buttonColor
property color accentColor: ui.theme.accentColor
readonly property real defaultButtonSize: 30
property bool isNarrow: buttonType === FlatButton.Horizontal
property real margins: isNarrow ? 12 : 16
property real minWidth: isNarrow ? 24 : 132
@ -100,7 +98,7 @@ FocusScope {
objectName: root.text
implicitWidth: contentLoader.implicitWidth + 2 * margins
implicitHeight: Math.max(contentLoader.implicitHeight, defaultButtonSize)
implicitHeight: Math.max(contentLoader.implicitHeight, ui.theme.defaultButtonSize)
opacity: root.enabled ? 1.0 : ui.theme.itemOpacityDisabled
@ -234,8 +232,8 @@ FocusScope {
PropertyChanges {
target: root
implicitWidth: root.defaultButtonSize
implicitHeight: root.defaultButtonSize
implicitWidth: ui.theme.defaultButtonSize
implicitHeight: ui.theme.defaultButtonSize
}
},
@ -247,7 +245,7 @@ FocusScope {
target: root
implicitWidth: Math.max(contentLoader.implicitWidth + 2 * root.margins,
root.minWidth)
implicitHeight: root.defaultButtonSize
implicitHeight: ui.theme.defaultButtonSize
}
},
@ -257,7 +255,7 @@ FocusScope {
PropertyChanges {
target: root
implicitHeight: root.defaultButtonSize
implicitHeight: ui.theme.defaultButtonSize
}
},