menu: Support hovered state

This commit is contained in:
Peter Hartmann 2023-06-30 15:28:20 +02:00 committed by uwerat
parent 0f6a1206e6
commit f126a9007d
5 changed files with 54 additions and 4 deletions

View File

@ -724,6 +724,8 @@ void Editor::setupMenuColors(
setGradient( Q::Panel, pal.background.flyout.defaultColor );
setShadowColor( Q::Panel, theme.shadow.flyout.color );
setGradient( Q::Segment | Q::Hovered, pal.fillColor.subtle.secondary );
setGradient( Q::Segment | Q::Selected, pal.fillColor.subtle.secondary );
/*
@ -734,7 +736,7 @@ void Editor::setupMenuColors(
const auto c1 = pal.fillColor.subtle.secondary;
const auto c2 = pal.fillColor.accent.defaultColor;
setBoxBorderColors( Q::Segment | Q::Selected,
setBoxBorderColors( Q::Segment | Q::Selected,
QskGradient( { { 0.25, c1 }, { 0.25, c2 }, { 0.75, c2 }, { 0.75, c1 } } ) );
setColor( Q::Text, pal.fillColor.text.primary );

View File

@ -340,8 +340,8 @@ void Editor::setupMenu()
// The color here is primary with an opacity of 8% - we blend that
// with the background, because we don't want the menu to have transparency:
const auto panel = flattenedColor( m_pal.primary, m_pal.background, 0.08 );
setGradient( Q::Panel, panel );
const auto panelColor = flattenedColor( m_pal.primary, m_pal.background, 0.08 );
setGradient( Q::Panel, panelColor );
setShadowMetrics( Q::Panel, m_pal.elevation2 );
setShadowColor( Q::Panel, m_pal.shadow );
@ -355,7 +355,12 @@ void Editor::setupMenu()
setSpacing( Q::Segment, 5_dp );
setGradient( Q::Segment, Qt::transparent );
setGradient( Q::Cursor, m_pal.primary12 );
const auto hoverColor = flattenedColor( m_pal.onSurface, panelColor, m_pal.hoverOpacity );
setGradient( Q::Segment | Q::Hovered, hoverColor );
setGradient( Q::Segment | Q::Selected, m_pal.primary12 );
const auto hoverSelectedColor = flattenedColor( m_pal.onSurface, m_pal.primary12, m_pal.hoverOpacity );
setGradient( Q::Segment | Q::Selected | Q::Hovered, hoverSelectedColor );
setPadding( Q::Icon, 7_dp );
setStrutSize( Q::Icon, 24_dp, 24_dp );

View File

@ -72,6 +72,8 @@ QskMenu::QskMenu( QQuickItem* parent )
connect( this, &QskMenu::opened, this,
[this]() { m_data->triggeredIndex = -1; } );
setAcceptHoverEvents( true );
}
QskMenu::~QskMenu()
@ -319,6 +321,30 @@ void QskMenu::keyReleaseEvent( QKeyEvent* )
}
}
void QskMenu::hoverEnterEvent( QHoverEvent* event )
{
using A = QskAspect;
setSkinHint( Segment | Hovered | A::Metric | A::Position, qskHoverPosition( event ) );
update();
}
void QskMenu::hoverMoveEvent( QHoverEvent* event )
{
using A = QskAspect;
setSkinHint( Segment | Hovered | A::Metric | A::Position, qskHoverPosition( event ) );
update();
}
void QskMenu::hoverLeaveEvent( QHoverEvent* )
{
using A = QskAspect;
setSkinHint( Segment | Hovered | A::Metric | A::Position, QPointF() );
update();
}
#ifndef QT_NO_WHEELEVENT
void QskMenu::wheelEvent( QWheelEvent* event )

View File

@ -68,6 +68,7 @@ class QSK_EXPORT QskMenu : public QskPopup
QVector< int > actions() const;
int currentIndex() const;
QString currentText() const;
int triggeredIndex() const;
@ -97,6 +98,10 @@ class QSK_EXPORT QskMenu : public QskPopup
void keyPressEvent( QKeyEvent* ) override;
void keyReleaseEvent( QKeyEvent* ) override;
void hoverEnterEvent( QHoverEvent* ) override;
void hoverMoveEvent( QHoverEvent* ) override;
void hoverLeaveEvent( QHoverEvent* ) override;
#ifndef QT_NO_WHEELEVENT
void wheelEvent( QWheelEvent* ) override;
#endif

View File

@ -385,6 +385,7 @@ QskAspect::States QskMenuSkinlet::sampleStates(
const QskSkinnable* skinnable, QskAspect::Subcontrol subControl, int index ) const
{
using Q = QskMenu;
using A = QskAspect;
auto states = Inherited::sampleStates( skinnable, subControl, index );
@ -394,6 +395,17 @@ QskAspect::States QskMenuSkinlet::sampleStates(
if ( menu->currentIndex() == menu->actions()[ index ] )
states |= QskMenu::Selected;
const auto cursorPos = menu->effectiveSkinHint( Q::Segment | Q::Hovered | A::Metric | A::Position ).toPointF();
if( !cursorPos.isNull() && menu->indexAtPosition( cursorPos ) == index )
{
states |= Q::Hovered;
}
else
{
states &= ~Q::Hovered;
}
}
return states;