make notification come to front without stealing the focus, currently only on windows

This commit is contained in:
Patrick von Reth 2015-04-16 14:29:32 +02:00
parent c717150236
commit c7f0e42a47
4 changed files with 52 additions and 20 deletions

View File

@ -27,23 +27,6 @@
using namespace Snore;
void Utils::bringWindowToFront(qlonglong _wid, bool focus)
{
snoreDebug(SNORE_DEBUG) << _wid;
#ifdef Q_OS_WIN
HWND wid = (HWND)_wid;
int idActive = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
bool attetched = AttachThreadInput(GetCurrentThreadId(), idActive, TRUE);
SetForegroundWindow(wid);
if (focus) {
SetFocus(wid);
}
if (attetched) {
AttachThreadInput(GetCurrentThreadId(), idActive, FALSE);
}
#endif
}
Utils::Utils(QObject *parent):
QObject(parent)
{
@ -55,4 +38,42 @@ Utils::~Utils()
}
void Utils::bringWindowToFront(qlonglong _wid, bool focus)
{
snoreDebug(SNORE_DEBUG) << _wid;
#ifdef Q_OS_WIN
HWND wid = (HWND)_wid;
int active = attatchToActiveProcess();
SetForegroundWindow(wid);
if (focus) {
SetFocus(wid);
}
detatchActiveProcess(active);
#endif
}
void Utils::raiseWindowToFront(qlonglong wid)
{
snoreDebug(SNORE_DEBUG) << wid;
#ifdef Q_OS_WIN
int active = attatchToActiveProcess();
SetWindowPos((HWND)wid, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
detatchActiveProcess(active);
#endif
}
#ifdef Q_OS_WIN
int Utils::attatchToActiveProcess()
{
int idActive = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
return AttachThreadInput(GetCurrentThreadId(), idActive, TRUE) ? idActive : -1;
}
void Utils::detatchActiveProcess(int idActive)
{
if (idActive!= -1) {
AttachThreadInput(GetCurrentThreadId(), idActive, FALSE);
}
}
#endif

View File

@ -33,13 +33,18 @@ public:
~Utils();
/**
* Raise a window to the front.
* Raise a window to the front and activates it.
* @param wid the Id of the window to raise.
* @param focus whether the window should request focus.
*/
//TODO: make Wid usable with the meta system and change signature.
Q_INVOKABLE static void bringWindowToFront(qlonglong wid, bool focus);
/**
* Raised the Window to front and don't make it active or steal focus.
*/
Q_INVOKABLE static void raiseWindowToFront(qlonglong wid);
/**
*
* @param string A string to decode if needed.
@ -76,6 +81,12 @@ public:
return QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex();
}
private:
#ifdef Q_OS_WIN
static int attatchToActiveProcess();
static void detatchActiveProcess(int idActive);
#endif
};
}

View File

@ -45,7 +45,7 @@ Rectangle {
animation.start()
window.visible = true
utils.bringWindowToFront(window.wid, false)
utils.raiseWindowToFront(window.wid)
}
}

View File

@ -91,5 +91,5 @@ void Toasty::slotNotify(Notification notification)
PluginSettingsWidget *Toasty::settingsWidget()
{
return new NotifyMyAndroidSettings(this);
return new ToastySettings(this);
}