From 96c4e8702d15fb38e7642fc8592064499de299b1 Mon Sep 17 00:00:00 2001 From: Ximi1970 Date: Sun, 21 Jun 2020 23:08:33 +0200 Subject: [PATCH] Update for multi win --- app/SysTray-X/windowctrl-win.cpp | 108 +++++++++++++++++++++++-------- app/SysTray-X/windowctrl-win.h | 53 +++++++++++++-- app/SysTray-X/windowctrl.cpp | 5 ++ 3 files changed, 134 insertions(+), 32 deletions(-) diff --git a/app/SysTray-X/windowctrl-win.cpp b/app/SysTray-X/windowctrl-win.cpp index 6dcf46c..bbcfee2 100644 --- a/app/SysTray-X/windowctrl-win.cpp +++ b/app/SysTray-X/windowctrl-win.cpp @@ -36,9 +36,11 @@ WindowCtrlWin::WindowCtrlWin( QObject *parent) : QObject( parent ) * Initialize */ m_tb_window = 0; - m_tb_windows = QList< quint64 >(); m_window_state = Preferences::STATE_UNKNOWN; + m_tb_windows = QList< quint64 >(); + m_tb_window_states = QList< Preferences::WindowState >(); + /* * Setup the minimize intercept */ @@ -59,6 +61,26 @@ WindowCtrlWin::~WindowCtrlWin() } +/* + * Set the window state. + */ +void WindowCtrlWin::setWindowState( int state ) +{ + m_window_state = state; +} + + +/** + * @brief getWindowState. Get the window state. + * + * @return The state. + */ +int WindowCtrlWin::getWindowState() const +{ + return m_window_state; +} + + /* * Set the minimize type */ @@ -170,45 +192,72 @@ BOOL CALLBACK WindowCtrlWin::enumWindowsTitleProc( HWND hwnd, LPARAM lParam ) /* * Find the window by pid */ -bool WindowCtrlWin::findWindows( qint64 pid ) +void WindowCtrlWin::findWindows( qint64 pid ) { - EnumWindowsPidProcData data; - data.pid = pid; - data.hwnd = nullptr; + m_tb_windows = QList< quint64 >(); + m_tb_window_states = QList< Preferences::WindowState >(); + + EnumWindowsPidProcData data{ *this, pid }; EnumWindows( &enumWindowsPidProc, reinterpret_cast(&data) ); - if( data.hwnd == nullptr ) + emit signalConsole( QString( "Number of windows found: %1" ).arg( m_tb_windows.length() ) ); + + for( int i = 0 ; i< m_tb_windows.length() ; ++i ) { - return false; + emit signalConsole( QString( "WinID %1, State %2" ).arg( m_tb_windows.at( i ) ).arg( m_tb_window_states.at( i ) ) ); } - - /* - * Store it - */ - m_tb_window = (quint64)data.hwnd; - - return true; } /* * Callback for the window enumaration pid find. */ -BOOL CALLBACK WindowCtrlWin::enumWindowsPidProc( HWND hwnd, LPARAM lParam ) +BOOL CALLBACK WindowCtrlWin::enumWindowsPidProc( HWND hWnd, LPARAM lParam ) { auto& data = *reinterpret_cast(lParam); unsigned long pid = 0; - GetWindowThreadProcessId( hwnd, &pid ); + GetWindowThreadProcessId( hWnd, &pid ); - if( data.pid != pid || !isMainWindow( hwnd ) ) + if( data.pid != pid || !isMainWindow( hWnd ) ) { return TRUE; } - data.hwnd = hwnd; + /* + * Store the window id + */ + data.window_ctrl.m_tb_windows.append( (quint64)hWnd ); - return FALSE; + /* + * Get the window state + */ + WINDOWPLACEMENT wp; + wp.length = sizeof( WINDOWPLACEMENT ); + GetWindowPlacement( hWnd, &wp ); + + if( SW_SHOWMINIMIZED == wp.showCmd ) + { + /* + * Window is minimized + */ + data.window_ctrl.m_tb_window_states.append( Preferences::STATE_MINIMIZED ); + } + else + { + data.window_ctrl.m_tb_window_states.append( Preferences::STATE_NORMAL ); + } + + return TRUE; +} + + +/* + * Get the states of the TB windows. + */ +const QList< Preferences::WindowState >& WindowCtrlWin::getWindowStates() const +{ + return m_tb_window_states; } @@ -217,7 +266,7 @@ BOOL CALLBACK WindowCtrlWin::enumWindowsPidProc( HWND hwnd, LPARAM lParam ) */ BOOL WindowCtrlWin::isMainWindow( HWND hwnd ) { - return GetWindow( hwnd, GW_OWNER ) == nullptr && IsWindowVisible( hwnd ); + return GetWindow( hwnd, GW_OWNER ) == nullptr && ( IsWindowVisible( hwnd ) || IsIconic( hwnd ) ); } @@ -244,6 +293,15 @@ void WindowCtrlWin::displayWindowElements( quint64 window ) } +/* + * Get the Thunderbird window ID + */ +quint64 WindowCtrlWin::getWinId() +{ + return m_tb_window; +} + + /* * Get the Thunderbird window IDs */ @@ -302,7 +360,7 @@ void CALLBACK WindowCtrlWin::handleWinEvent( HWINEVENTHOOK hook, DWORD event, */ if( m_ctrl_parent ) { - m_ctrl_parent->hookAction(); + m_ctrl_parent->hookAction( hWnd ); } } } @@ -312,16 +370,14 @@ void CALLBACK WindowCtrlWin::handleWinEvent( HWINEVENTHOOK hook, DWORD event, /* * Non-static method to use by the hook callback */ -void WindowCtrlWin::hookAction() +void WindowCtrlWin::hookAction( HWND hWnd ) { - if( ( getWindowState() != Preferences::STATE_MINIMIZED ) && ( getMinimizeType() > 0 ) ) + if( getMinimizeType() > 0 ) { - setWindowState( Preferences::STATE_MINIMIZED ); - /* * Hide to tray */ - hideWindow( (HWND)getWinId() ); + hideWindow( hWnd ); } } diff --git a/app/SysTray-X/windowctrl-win.h b/app/SysTray-X/windowctrl-win.h index d0df51c..4ebf1d6 100644 --- a/app/SysTray-X/windowctrl-win.h +++ b/app/SysTray-X/windowctrl-win.h @@ -40,8 +40,8 @@ class WindowCtrlWin : public QObject struct EnumWindowsPidProcData { - unsigned long pid; - HWND hwnd; + WindowCtrlWin& window_ctrl; + long long pid; }; struct EnumWindowsTitleProcData @@ -64,6 +64,20 @@ class WindowCtrlWin : public QObject */ ~WindowCtrlWin(); + /** + * @brief setWindowState. Set the window state. + * + * @param state The state. + */ + void setWindowState( int state ); + + /** + * @brief getWindowState. Get the window state. + * + * @return The state. + */ + int getWindowState() const; + /** * @brief setMinimizeType * @@ -116,10 +130,15 @@ class WindowCtrlWin : public QObject * @brief findWindow. Find window by pid. * * @param pid The pid to find. - * - * @return State of the find. */ - bool findWindows( qint64 pid ); + void findWindows( qint64 pid ); + + /** + * @brief getWindowStates. Get the states of the TB windows. + * + * @return The list of window states. + */ + const QList< Preferences::WindowState >& getWindowStates() const; /** * @brief displayWindowElements. Display window elements. @@ -135,6 +154,13 @@ class WindowCtrlWin : public QObject */ void displayWindowElements( quint64 window ); + /** + * @brief getWinId. Get the Thunderbird window ID. + * + * @return The TB window ID. + */ + quint64 getWinId(); + /** * @brief getWinIds. Get the Thunderbird window IDs. * @@ -238,7 +264,7 @@ class WindowCtrlWin : public QObject /** * @brief hookAction. Non-static function to be used by the hook callback. */ - void hookAction(); + void hookAction( HWND hWnd ); signals: @@ -275,16 +301,31 @@ class WindowCtrlWin : public QObject private: + /** + * @brief m_tb_window. The Thunderbird window. + */ + quint64 m_tb_window; + /** * @brief m_tb_window. The Thunderbird windows. */ QList< quint64 > m_tb_windows; + /** + * @brief m_tb_window_states. The Thunderbird window states. + */ + QList< Preferences::WindowState > m_tb_window_states; + /** * @brief m_minimize_type. Minimize type. */ Preferences::MinimizeType m_minimize_type; + /** + * @brief m_window_state. State of the TB window. + */ + int m_window_state; + /** * @brief m_hook */ diff --git a/app/SysTray-X/windowctrl.cpp b/app/SysTray-X/windowctrl.cpp index fdaae2a..e2c861c 100644 --- a/app/SysTray-X/windowctrl.cpp +++ b/app/SysTray-X/windowctrl.cpp @@ -169,6 +169,11 @@ void WindowCtrl::slotWindowState( Preferences::WindowState state ) emit signalConsole( QString( "State change to: %1" ).arg( state ) ); + /* + * Update the TB windows and states + */ + findWindows( m_ppid ); + #ifdef Q_OS_UNIX /*