Skip to content

Commit

Permalink
QskSwitchButton M3 fixes, using shadows instead of an additonal
Browse files Browse the repository at this point in the history
subcontrol
  • Loading branch information
uwerat committed Oct 25, 2024
1 parent 635cd3c commit 99b4bdd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 77 deletions.
77 changes: 54 additions & 23 deletions designsystems/material3/QskMaterial3Skin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,6 @@ void Editor::setupRadioBox()

setStrutSize( Q::CheckIndicatorPanel, 20_dp, 20_dp );

for ( const auto state1 : { Q::Hovered, Q::Focused, Q::Pressed } )
{
for ( const auto state2 : { A::NoState, Q::Selected } )
{
const auto aspect = Q::CheckIndicatorPanel | state1 | state2;
setShadowMetrics( aspect, { 10_dp, 0 } );
}
}

for ( auto subControl : { Q::CheckIndicatorPanel, Q::CheckIndicator } )
setBoxShape( subControl, 100, Qt::RelativeSize ); // circular

Expand All @@ -508,6 +499,8 @@ void Editor::setupRadioBox()
{
const auto aspect = Q::CheckIndicatorPanel | state1 | state2;

setShadowMetrics( aspect, { 10_dp, 0 } );

QRgb rgb;

if ( state1 == Q::Hovered )
Expand Down Expand Up @@ -1030,25 +1023,52 @@ void Editor::setupSwitchButton()
setBoxBorderMetrics( Q::Groove | Q::Checked, 0 );

setBoxShape( Q::Handle, 100, Qt::RelativeSize );
setStrutSize( Q::Handle, 16_dp, 16_dp );
setStrutSize( Q::Handle | Q::Checked, 24_dp, 24_dp,
{ QskStateCombination::CombinationNoState, Q::Disabled } );
setStrutSize( Q::Handle, 30_dp, 30_dp );
setMargin( Q::Handle, 7_dp );
setShadowMetrics( Q::Handle, { 17_dp, 0 } );
setShadowColor( Q::Handle, QskRgb::Transparent );

setGradient( Q::Handle, m_pal.outline );
setGradient( Q::Handle | Q::Checked, m_pal.primaryContainer );
setGradient( Q::Handle | Q::Checked, m_pal.onPrimary );

setGradient( Q::Handle | Q::Disabled, m_pal.onSurface38 );
setGradient( Q::Handle | Q::Disabled | Q::Checked, m_pal.surface );
for ( auto state1 : { A::NoState, Q::Hovered, Q::Focused, Q::Pressed } )
{
const qreal opacity = m_pal.stateOpacity( state1 );

// just to keep the strut size the same at all times:
setStrutSize( Q::Halo, 40_dp, 40_dp );
setGradient( Q::Halo, Qt::transparent );
for ( const auto state2 : { A::NoState, Q::Checked } )
{
const auto aspect = Q::Handle | state1 | state2;

setStrutSize( Q::Halo | Q::Hovered, 40_dp, 40_dp );
setBoxShape( Q::Halo, 100, Qt::RelativeSize );
setGradient( Q::Halo | Q::Hovered, stateLayerColor( m_pal.onSurface, m_pal.focusOpacity ) );
setGradient( Q::Halo | Q::Hovered | Q::Checked,
stateLayerColor( m_pal.primary, m_pal.focusOpacity ) );
if ( state1 == Q::Pressed )
{
setShadowMetrics( aspect, { 10_dp, 0 } );
setMargin( aspect, 0.0 );
}
else if ( state2 == Q::Checked )
{
setShadowMetrics( aspect, { 13_dp, 0 } );
setMargin( aspect, 3_dp );
}

if ( state1 )
{
if ( state2 == Q::Checked )
{
setGradient( aspect, m_pal.primaryContainer );
setShadowColor( aspect, stateLayerColor( m_pal.primary, opacity ) );
}
else
{
setGradient( aspect, m_pal.onSurfaceVariant );
setShadowColor( aspect, stateLayerColor( m_pal.onSurface, opacity ) );
}
}
}
}


setGradient( Q::Handle | Q::Disabled, m_pal.onSurface38 );
setGradient( Q::Handle | Q::Disabled | Q::Checked, m_pal.surface );

setBoxBorderColors( Q::Handle, m_pal.outline );
setBoxBorderColors( Q::Handle | Q::Checked, m_pal.primary );
Expand Down Expand Up @@ -1490,6 +1510,17 @@ QskMaterial3Theme::QskMaterial3Theme( QskSkin::ColorScheme colorScheme,
shapeExtraSmallTop = QskBoxShapeMetrics( 4_dp, 4_dp, 0, 0 );
}

qreal QskMaterial3Theme::stateOpacity( int state ) const
{
if ( state == QskControl::Hovered )
return hoverOpacity;

if ( state == QskControl::Focused )
return focusOpacity;

return state ? pressedOpacity : 0.0;
}

QskMaterial3Skin::QskMaterial3Skin( QObject* parent )
: Inherited( parent )
{
Expand Down
2 changes: 2 additions & 0 deletions designsystems/material3/QskMaterial3Skin.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class QSK_MATERIAL3_EXPORT QskMaterial3Theme
const qreal pressedOpacity = 0.12;
const qreal draggedOpacity = 0.16;

qreal stateOpacity( int state ) const;

QskBoxShapeMetrics shapeExtraSmallTop;
};

Expand Down
1 change: 0 additions & 1 deletion src/controls/QskSwitchButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

QSK_SUBCONTROL( QskSwitchButton, Handle )
QSK_SUBCONTROL( QskSwitchButton, Groove )
QSK_SUBCONTROL( QskSwitchButton, Halo )

struct QskSwitchButton::PrivateData
{
Expand Down
2 changes: 1 addition & 1 deletion src/controls/QskSwitchButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class QSK_EXPORT QskSwitchButton : public QskAbstractButton
WRITE setInverted NOTIFY invertedChanged FINAL )

public:
QSK_SUBCONTROLS( Groove, Handle, Halo )
QSK_SUBCONTROLS( Groove, Handle )

QskSwitchButton( Qt::Orientation, QQuickItem* parent = nullptr );
QskSwitchButton( QQuickItem* parent = nullptr );
Expand Down
51 changes: 1 addition & 50 deletions src/controls/QskSwitchButtonSkinlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static inline qreal qskEffectivePosition( const QskSwitchButton* switchButton )
QskSwitchButtonSkinlet::QskSwitchButtonSkinlet( QskSkin* skin )
: Inherited( skin )
{
setNodeRoles( { GrooveRole, HandleRole, HaloRole } );
setNodeRoles( { GrooveRole, HandleRole } );
}

QskSwitchButtonSkinlet::~QskSwitchButtonSkinlet()
Expand All @@ -48,12 +48,6 @@ QRectF QskSwitchButtonSkinlet::subControlRect( const QskSkinnable* skinnable,
return grooveRect( skinnable, contentsRect );
}

if ( subControl == Q::Halo )
{
return haloRect( skinnable, contentsRect );
}


return Inherited::subControlRect( skinnable, contentsRect, subControl );
}

Expand All @@ -65,10 +59,8 @@ QSizeF QskSwitchButtonSkinlet::sizeHint( const QskSkinnable* skinnable,

const auto grooveHint = skinnable->strutSizeHint( QskSwitchButton::Groove );
const auto handleHint = skinnable->strutSizeHint( QskSwitchButton::Handle );
const auto haloHint = skinnable->strutSizeHint( QskSwitchButton::Halo );

auto hint = grooveHint;
hint = hint.expandedTo( haloHint );
hint = hint.expandedTo( handleHint );

return hint;
Expand All @@ -81,9 +73,6 @@ QSGNode* QskSwitchButtonSkinlet::updateSubNode( const QskSkinnable* skinnable,

switch ( nodeRole )
{
case HaloRole:
return updateBoxNode( skinnable, node, Q::Halo );

case HandleRole:
return updateBoxNode( skinnable, node, Q::Handle );

Expand Down Expand Up @@ -166,42 +155,4 @@ QRectF QskSwitchButtonSkinlet::handleRect(
return r;
}

QRectF QskSwitchButtonSkinlet::haloRect(
const QskSkinnable* skinnable, const QRectF& contentsRect ) const
{
using Q = QskSwitchButton;

const auto switchButton = static_cast< const Q* >( skinnable );

const auto grooveRect = subControlRect( skinnable, contentsRect, Q::Groove );
const auto pos = qskEffectivePosition( switchButton );
const auto sizeHandle = skinnable->strutSizeHint( Q::Handle );
const auto sizeHalo = skinnable->strutSizeHint( Q::Halo );

qreal cx, cy;

if( switchButton->orientation() == Qt::Vertical )
{
const qreal y0 = grooveRect.y() + 0.5 * sizeHandle.height();
const qreal h = grooveRect.height() - sizeHandle.height();

cx = grooveRect.x() + 0.5 * grooveRect.width();
cy = y0 + pos * h;
}
else
{
const qreal x0 = grooveRect.x() + 0.5 * sizeHandle.width();
const qreal w = grooveRect.width() - sizeHandle.width();

cx = x0 + pos * w;
cy = grooveRect.y() + 0.5 * grooveRect.height();
}

QRectF r;
r.setSize( sizeHalo );
r.moveCenter( QPointF( cx, cy ) );

return r;
}

#include "moc_QskSwitchButtonSkinlet.cpp"
2 changes: 0 additions & 2 deletions src/controls/QskSwitchButtonSkinlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class QSK_EXPORT QskSwitchButtonSkinlet : public QskSkinlet
{
GrooveRole,
HandleRole,
HaloRole,

RoleCount
};
Expand All @@ -40,7 +39,6 @@ class QSK_EXPORT QskSwitchButtonSkinlet : public QskSkinlet
private:
QRectF grooveRect( const QskSkinnable*, const QRectF& ) const;
QRectF handleRect( const QskSkinnable*, const QRectF& ) const;
QRectF haloRect( const QskSkinnable*, const QRectF& ) const;
};

#endif

0 comments on commit 99b4bdd

Please sign in to comment.