2017-01-02 17:05:30 -05:00
/*
* Copyright 2016 Smith AR < audoban @ openmailbox . org >
* Michail Vourlakos < mvourlakos @ gmail . com >
*
* This file is part of Latte - Dock
*
* Latte - Dock is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation ; either version 2 of
* the License , or ( at your option ) any later version .
*
* Latte - Dock is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2016-12-25 09:25:27 +02:00
# include "visibilitymanager.h"
2016-12-29 00:40:35 -05:00
# include "visibilitymanager_p.h"
2016-12-30 02:17:38 -05:00
# include "windowinfowrap.h"
2016-12-30 16:27:39 -05:00
# include "dockview.h"
2016-12-30 02:17:38 -05:00
# include "../liblattedock/extras.h"
2016-12-25 09:25:27 +02:00
2017-01-02 01:04:10 -05:00
# include <QDebug>
2016-12-30 19:26:48 -05:00
2016-12-29 00:40:35 -05:00
namespace Latte {
2016-12-25 09:25:27 +02:00
2016-12-29 00:40:35 -05:00
//! BEGIN: VisiblityManagerPrivate implementation
VisibilityManagerPrivate : : VisibilityManagerPrivate ( PlasmaQuick : : ContainmentView * view , VisibilityManager * q )
2016-12-30 16:27:39 -05:00
: QObject ( q ) , q ( q ) , view ( view ) , wm ( AbstractWindowInterface : : getInstance ( view , nullptr ) )
2016-12-29 00:40:35 -05:00
{
2016-12-30 16:27:39 -05:00
DockView * dockView = qobject_cast < DockView * > ( view ) ;
2016-12-30 16:32:48 -05:00
2016-12-30 13:46:56 +02:00
if ( dockView ) {
2016-12-30 16:27:39 -05:00
connect ( dockView , & DockView : : eventTriggered , this , & VisibilityManagerPrivate : : event ) ;
2016-12-30 13:46:56 +02:00
}
2016-12-30 16:32:48 -05:00
2016-12-30 02:17:38 -05:00
timerCheckWindows . setInterval ( 350 ) ;
timerCheckWindows . setSingleShot ( true ) ;
timerShow . setSingleShot ( true ) ;
timerHide . setSingleShot ( true ) ;
connect ( & timerCheckWindows , & QTimer : : timeout , this , & VisibilityManagerPrivate : : checkAllWindows ) ;
2017-01-02 01:04:10 -05:00
connect ( & timerShow , & QTimer : : timeout , this , [ this ] ( ) {
if ( isHidden ) {
qDebug ( ) < < " must be shown " ;
emit this - > q - > mustBeShown ( ) ;
}
} ) ;
connect ( & timerHide , & QTimer : : timeout , this , [ this ] ( ) {
2017-01-03 12:36:40 -05:00
if ( ! blockHiding & & ! isHidden & & ! dragEnter ) {
2017-01-02 01:04:10 -05:00
qDebug ( ) < < " must be hide " ;
emit this - > q - > mustBeHide ( ) ;
}
} ) ;
2016-12-30 02:45:38 -05:00
wm - > setDockDefaultFlags ( ) ;
2016-12-29 00:40:35 -05:00
}
2016-12-25 09:25:27 +02:00
2016-12-29 00:40:35 -05:00
VisibilityManagerPrivate : : ~ VisibilityManagerPrivate ( )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
wm - > removeDockStruts ( ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
inline void VisibilityManagerPrivate : : setMode ( Dock : : Visibility mode )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
if ( this - > mode = = mode )
return ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:17:38 -05:00
// clear mode
if ( this - > mode = = Dock : : AlwaysVisible )
wm - > removeDockStruts ( ) ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:17:38 -05:00
for ( auto & c : connections ) {
disconnect ( c ) ;
}
timerShow . stop ( ) ;
timerHide . stop ( ) ;
timerCheckWindows . stop ( ) ;
this - > mode = mode ;
switch ( this - > mode ) {
2016-12-30 02:24:04 -05:00
case Dock : : AlwaysVisible : {
wm - > setDockStruts ( dockRect , view - > location ( ) ) ;
raiseDock ( true ) ;
}
2016-12-30 02:17:38 -05:00
break ;
2016-12-30 19:26:48 -05:00
2016-12-30 02:24:04 -05:00
case Dock : : AutoHide : {
2017-01-02 01:04:10 -05:00
raiseDock ( containsMouse ) ;
2016-12-30 02:24:04 -05:00
}
2016-12-30 02:17:38 -05:00
break ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:24:04 -05:00
case Dock : : DodgeActive : {
connections [ 0 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : activeWindowChanged
, this , & VisibilityManagerPrivate : : dodgeActive ) ;
connections [ 1 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : windowChanged
, this , & VisibilityManagerPrivate : : dodgeActive ) ;
2017-01-02 01:04:10 -05:00
connections [ 2 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : currentDesktopChanged
, this , [ & ] ( int ) {
dodgeActive ( wm - > activeWindow ( ) ) ;
} ) ;
2016-12-30 02:24:04 -05:00
dodgeActive ( wm - > activeWindow ( ) ) ;
2016-12-30 02:17:38 -05:00
}
2016-12-30 02:24:04 -05:00
break ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:24:04 -05:00
case Dock : : DodgeMaximized : {
2017-01-02 01:04:10 -05:00
connections [ 0 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : activeWindowChanged
, this , & VisibilityManagerPrivate : : dodgeMaximized ) ;
connections [ 1 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : windowChanged
2016-12-30 02:24:04 -05:00
, this , & VisibilityManagerPrivate : : dodgeMaximized ) ;
2017-01-02 01:04:10 -05:00
connections [ 2 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : currentDesktopChanged
, this , [ & ] ( int ) {
dodgeMaximized ( wm - > activeWindow ( ) ) ;
} ) ;
dodgeMaximized ( wm - > activeWindow ( ) ) ;
2016-12-30 02:24:04 -05:00
}
break ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:24:04 -05:00
case Dock : : DodgeAllWindows : {
for ( const auto & wid : wm - > windows ( ) ) {
windows . insert ( { wid , wm - > requestInfo ( wid ) } ) ;
}
2016-12-30 16:32:48 -05:00
2016-12-30 02:24:04 -05:00
connections [ 0 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : windowChanged
, this , & VisibilityManagerPrivate : : dodgeWindows ) ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:24:04 -05:00
connections [ 1 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : windowRemoved
, this , [ & ] ( WId wid ) {
2016-12-30 02:17:38 -05:00
windows . erase ( wid ) ;
timerCheckWindows . start ( ) ;
2016-12-30 02:24:04 -05:00
} ) ;
connections [ 2 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : windowAdded
, this , [ & ] ( WId wid ) {
2016-12-30 02:17:38 -05:00
windows . insert ( { wid , wm - > requestInfo ( wid ) } ) ;
timerCheckWindows . start ( ) ;
2016-12-30 02:24:04 -05:00
} ) ;
2017-01-02 01:04:10 -05:00
connections [ 3 ] = connect ( wm . get ( ) , & AbstractWindowInterface : : currentDesktopChanged
, this , [ & ] ( int ) {
timerCheckWindows . start ( ) ;
} ) ;
timerCheckWindows . start ( ) ;
2016-12-30 02:24:04 -05:00
}
2016-12-30 02:17:38 -05:00
}
saveConfig ( ) ;
2016-12-30 16:32:48 -05:00
2016-12-30 14:03:46 -05:00
emit q - > modeChanged ( ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
inline void VisibilityManagerPrivate : : setIsHidden ( bool isHidden )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
if ( this - > isHidden = = isHidden )
return ;
2016-12-30 16:32:48 -05:00
2017-01-02 03:06:47 -05:00
if ( blockHiding ) {
qWarning ( ) < < " isHidden property is blocked, ignoring update " ;
return ;
}
2016-12-30 02:17:38 -05:00
this - > isHidden = isHidden ;
emit q - > isHiddenChanged ( ) ;
2016-12-29 00:40:35 -05:00
}
2016-12-25 09:25:27 +02:00
2017-01-02 03:06:47 -05:00
void VisibilityManagerPrivate : : setBlockHiding ( bool blockHiding )
{
if ( this - > blockHiding = = blockHiding )
return ;
this - > blockHiding = blockHiding ;
if ( this - > blockHiding ) {
timerHide . stop ( ) ;
if ( isHidden )
isHidden = false ;
} else {
updateHiddenState ( ) ;
}
emit q - > blockHidingChanged ( ) ;
}
2016-12-30 02:17:38 -05:00
inline void VisibilityManagerPrivate : : setTimerShow ( int msec )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
timerShow . setInterval ( msec ) ;
saveConfig ( ) ;
emit q - > timerShowChanged ( ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
inline void VisibilityManagerPrivate : : setTimerHide ( int msec )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
timerHide . setInterval ( msec ) ;
saveConfig ( ) ;
emit q - > timerHideChanged ( ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
inline void VisibilityManagerPrivate : : raiseDock ( bool raise )
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
if ( raise ) {
timerHide . stop ( ) ;
2016-12-30 20:24:21 +02:00
if ( ! timerShow . isActive ( ) ) {
2016-12-29 00:40:35 -05:00
timerShow . start ( ) ;
2016-12-30 13:46:56 +02:00
}
2017-01-03 12:36:40 -05:00
} else if ( ! blockHiding & & ! dragEnter ) {
2016-12-29 00:40:35 -05:00
timerShow . stop ( ) ;
2016-12-30 16:32:48 -05:00
2017-01-02 01:04:10 -05:00
if ( ! timerHide . isActive ( ) )
2016-12-29 00:40:35 -05:00
timerHide . start ( ) ;
}
2016-12-25 09:25:27 +02:00
}
2017-01-02 03:06:47 -05:00
void VisibilityManagerPrivate : : updateHiddenState ( )
{
2017-01-03 12:36:40 -05:00
if ( dragEnter )
return ;
2017-01-02 03:06:47 -05:00
switch ( mode ) {
case Dock : : AutoHide :
2017-01-03 12:36:40 -05:00
raiseDock ( containsMouse ) ;
2017-01-02 03:06:47 -05:00
break ;
case Dock : : DodgeActive :
dodgeActive ( wm - > activeWindow ( ) ) ;
break ;
case Dock : : DodgeMaximized :
dodgeMaximized ( wm - > activeWindow ( ) ) ;
break ;
case Dock : : DodgeAllWindows :
dodgeWindows ( wm - > activeWindow ( ) ) ;
break ;
}
}
2016-12-30 02:17:38 -05:00
inline void VisibilityManagerPrivate : : setDockRect ( const QRect & dockRect )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:24:04 -05:00
if ( ! view - > containment ( ) | | this - > dockRect = = dockRect )
2016-12-30 02:17:38 -05:00
return ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:17:38 -05:00
this - > dockRect = dockRect ;
if ( mode = = Dock : : AlwaysVisible ) {
wm - > setDockStruts ( this - > dockRect , view - > containment ( ) - > location ( ) ) ;
}
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
void VisibilityManagerPrivate : : dodgeActive ( WId wid )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
auto winfo = wm - > requestInfo ( wid ) ;
2017-01-02 01:04:10 -05:00
if ( ! winfo . isValid ( ) )
2016-12-30 02:17:38 -05:00
return ;
2016-12-30 19:26:48 -05:00
2017-01-02 01:04:10 -05:00
if ( ! winfo . isActive ( ) ) {
if ( winfo . isPlasmaDesktop ( ) )
raiseDock ( true ) ;
return ;
}
raiseDock ( wm - > isOnCurrentDesktop ( wid ) & & ! intersects ( winfo ) ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
void VisibilityManagerPrivate : : dodgeMaximized ( WId wid )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
auto winfo = wm - > requestInfo ( wid ) ;
2017-01-02 01:04:10 -05:00
if ( ! winfo . isValid ( ) )
2016-12-30 02:17:38 -05:00
return ;
2016-12-30 19:26:48 -05:00
2017-01-02 01:04:10 -05:00
if ( ! winfo . isActive ( ) ) {
if ( winfo . isPlasmaDesktop ( ) )
raiseDock ( true ) ;
return ;
}
raiseDock ( wm - > isOnCurrentDesktop ( wid ) & & ! winfo . isMaximized ( ) ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
void VisibilityManagerPrivate : : dodgeWindows ( WId wid )
2016-12-25 09:25:27 +02:00
{
2017-01-02 01:04:10 -05:00
if ( windows . find ( wid ) = = std : : end ( windows ) )
return ;
2016-12-30 02:17:38 -05:00
auto winfo = wm - > requestInfo ( wid ) ;
2017-01-02 01:04:10 -05:00
windows [ wid ] = winfo ;
2016-12-30 02:17:38 -05:00
2017-01-02 01:04:10 -05:00
if ( ! winfo . isValid ( ) | | ! wm - > isOnCurrentDesktop ( wid ) )
2016-12-30 02:17:38 -05:00
return ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:17:38 -05:00
if ( intersects ( winfo ) )
raiseDock ( false ) ;
else
timerCheckWindows . start ( ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
void VisibilityManagerPrivate : : checkAllWindows ( )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
bool raise { true } ;
for ( const auto & winfo : windows ) {
//! std::pair<WId, WindowInfoWrap>
2017-01-02 01:04:10 -05:00
if ( ! std : : get < 1 > ( winfo ) . isValid ( ) | | ! wm - > isOnCurrentDesktop ( std : : get < 0 > ( winfo ) ) )
2016-12-30 02:17:38 -05:00
continue ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:17:38 -05:00
if ( std : : get < 1 > ( winfo ) . isFullscreen ( ) ) {
raise = false ;
break ;
} else if ( intersects ( std : : get < 1 > ( winfo ) ) ) {
raise = false ;
break ;
}
}
raiseDock ( raise ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
inline bool VisibilityManagerPrivate : : intersects ( const WindowInfoWrap & winfo )
2016-12-25 09:25:27 +02:00
{
2017-01-02 01:04:10 -05:00
return ! winfo . isMinimized ( ) & & winfo . geometry ( ) . intersects ( dockRect ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
inline void VisibilityManagerPrivate : : saveConfig ( )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
if ( ! view - > containment ( ) )
return ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:17:38 -05:00
auto config = view - > containment ( ) - > config ( ) ;
config . writeEntry ( " visibility " , static_cast < int > ( mode ) ) ;
config . writeEntry ( " timerShow " , timerShow . interval ( ) ) ;
config . writeEntry ( " timerHide " , timerHide . interval ( ) ) ;
view - > containment ( ) - > configNeedsSaving ( ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
inline void VisibilityManagerPrivate : : restoreConfig ( )
2016-12-25 09:25:27 +02:00
{
2016-12-30 02:17:38 -05:00
if ( ! view - > containment ( ) )
2016-12-30 02:24:04 -05:00
return ;
2016-12-30 16:32:48 -05:00
2016-12-30 02:24:04 -05:00
auto config = view - > containment ( ) - > config ( ) ;
timerShow . setInterval ( config . readEntry ( " timerShow " , 0 ) ) ;
timerHide . setInterval ( config . readEntry ( " timerHide " , 0 ) ) ;
2016-12-30 14:33:42 -05:00
2017-01-03 12:01:29 -05:00
auto mode = static_cast < Dock : : Visibility > ( config . readEntry ( " visibility " , static_cast < int > ( Dock : : DodgeActive ) ) ) ;
2016-12-30 14:33:42 -05:00
emit q - > timerShowChanged ( ) ;
emit q - > timerHideChanged ( ) ;
2017-01-03 12:01:29 -05:00
setMode ( mode ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
bool VisibilityManagerPrivate : : event ( QEvent * ev )
2016-12-25 09:25:27 +02:00
{
2017-01-03 12:36:40 -05:00
switch ( ev - > type ( ) ) {
case QEvent : : Enter :
if ( containsMouse )
break ;
2016-12-29 00:40:35 -05:00
containsMouse = true ;
emit q - > containsMouseChanged ( ) ;
2017-01-02 01:04:10 -05:00
if ( mode ! = Dock : : AlwaysVisible )
2016-12-30 19:26:48 -05:00
raiseDock ( true ) ;
2017-01-03 12:36:40 -05:00
break ;
case QEvent : : Leave :
if ( ! containsMouse )
break ;
2016-12-29 00:40:35 -05:00
containsMouse = false ;
emit q - > containsMouseChanged ( ) ;
2017-01-03 12:36:40 -05:00
updateHiddenState ( ) ;
2016-12-30 19:26:48 -05:00
2017-01-03 12:36:40 -05:00
break ;
case QEvent : : DragEnter :
dragEnter = true ;
emit q - > mustBeShown ( ) ;
break ;
case QEvent : : DragLeave :
case QEvent : : Drop :
dragEnter = false ;
2017-01-02 03:06:47 -05:00
updateHiddenState ( ) ;
2017-01-03 12:36:40 -05:00
break ;
case QEvent : : Show :
2016-12-30 02:45:38 -05:00
wm - > setDockDefaultFlags ( ) ;
2017-01-03 12:01:29 -05:00
restoreConfig ( ) ;
2017-01-03 12:36:40 -05:00
break ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
return QObject : : event ( ev ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
//! END: VisibilityManager implementation
//! BEGIN: VisiblityManager implementation
VisibilityManager : : VisibilityManager ( PlasmaQuick : : ContainmentView * view )
: d ( new VisibilityManagerPrivate ( view , this ) )
2016-12-25 09:25:27 +02:00
{
}
2016-12-29 00:40:35 -05:00
VisibilityManager : : ~ VisibilityManager ( )
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
Dock : : Visibility VisibilityManager : : mode ( ) const
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
return d - > mode ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
void VisibilityManager : : setMode ( Dock : : Visibility mode )
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
d - > setMode ( mode ) ;
}
2016-12-25 09:25:27 +02:00
2016-12-29 00:40:35 -05:00
bool VisibilityManager : : isHidden ( ) const
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
return d - > isHidden ;
2016-12-25 09:25:27 +02:00
}
2016-12-30 02:17:38 -05:00
void VisibilityManager : : setIsHidden ( bool isHidden )
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
d - > setIsHidden ( isHidden ) ;
2016-12-25 09:25:27 +02:00
}
2017-01-02 03:06:47 -05:00
bool VisibilityManager : : blockHiding ( ) const
{
return d - > blockHiding ;
}
void VisibilityManager : : setBlockHiding ( bool blockHiding )
{
d - > setBlockHiding ( blockHiding ) ;
}
2016-12-29 00:40:35 -05:00
bool VisibilityManager : : containsMouse ( ) const
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
return d - > containsMouse ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
int VisibilityManager : : timerShow ( ) const
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
return d - > timerShow . interval ( ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
void VisibilityManager : : setTimerShow ( int msec )
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
d - > setTimerShow ( msec ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
int VisibilityManager : : timerHide ( ) const
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
return d - > timerHide . interval ( ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
void VisibilityManager : : setTimerHide ( int msec )
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
d - > setTimerHide ( msec ) ;
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00
void VisibilityManager : : updateDockGeometry ( const QRect & geometry )
2016-12-25 09:25:27 +02:00
{
2016-12-29 00:40:35 -05:00
d - > setDockRect ( geometry ) ;
}
//! END: VisibilityManager implementation
2016-12-25 09:25:27 +02:00
}
2016-12-29 00:40:35 -05:00