+void GraphicalUi::setMainWidget(QWidget* widget)
+{
+ _mainWidget = widget;
+}
+
+void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider* provider)
+{
+ _contextMenuActionProvider = provider;
+}
+
+void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider* provider)
+{
+ _toolBarActionProvider = provider;
+}
+
+void GraphicalUi::setUiStyle(UiStyle* style)
+{
+ _uiStyle = style;
+}
+
+void GraphicalUi::disconnectedFromCore()
+{
+ _contextMenuActionProvider->disconnectedFromCore();
+ _toolBarActionProvider->disconnectedFromCore();
+ AbstractUi::disconnectedFromCore();
+}
+
+bool GraphicalUi::eventFilter(QObject* obj, QEvent* event)
+{
+#ifdef Q_OS_WIN
+ if (obj == mainWidget() && event->type() == QEvent::ActivationChange) {
+ _dwTickCount = GetTickCount();
+ }
+#endif
+ return AbstractUi::eventFilter(obj, event);
+}
+
+// NOTE: Window activation stuff seems to work just fine in Plasma 5 without requiring X11 hacks.
+// TODO: Evaluate cleaning all this up once we can get rid of Qt4/KDE4
+
+// Code taken from KStatusNotifierItem for handling minimize/restore
+
+bool GraphicalUi::checkMainWidgetVisibility(bool perform)
+{
+ bool needsActivation{true};
+
+#ifdef Q_OS_WIN
+ // the problem is that we lose focus when the systray icon is activated
+ // and we don't know the former active window
+ // therefore we watch for activation event and use our stopwatch :)
+ if (GetTickCount() - _dwTickCount < 300) {
+ // we were active in the last 300ms -> hide it
+ needsActivation = false;
+ }
+#else
+ if (mainWidget()->isVisible() && !mainWidget()->isMinimized() && mainWidget()->isActiveWindow()) {
+ needsActivation = false;
+ }
+#endif
+
+ if (perform) {
+ minimizeRestore(needsActivation);
+ }
+ return needsActivation;
+}
+
+bool GraphicalUi::isMainWidgetVisible()
+{
+ return !instance()->checkMainWidgetVisibility(false);
+}
+
+void GraphicalUi::minimizeRestore(bool show)
+{
+ if (show)
+ activateMainWidget();
+ else
+ hideMainWidget();
+}
+
+void GraphicalUi::activateMainWidget()
+{
+#ifdef Q_WS_X11
+ // Bypass focus stealing prevention
+ QX11Info::setAppUserTime(QX11Info::appTime());
+#endif
+
+ if (mainWidget()->windowState() & Qt::WindowMinimized) {
+ // restore
+ mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
+ }
+
+ // this does not actually work on all platforms... and causes more evil than good
+ // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
+#ifdef Q_OS_MAC
+ SetFrontProcess(&instance()->_procNum);
+#else
+ mainWidget()->show();
+ mainWidget()->raise();
+ mainWidget()->activateWindow();
+#endif
+}
+
+void GraphicalUi::hideMainWidget()
+{
+ if (instance()->isHidingMainWidgetAllowed())
+#ifdef Q_OS_MAC
+ ShowHideProcess(&instance()->_procNum, false);
+#else
+ mainWidget()->hide();
+#endif
+}
+
+void GraphicalUi::toggleMainWidget()
+{
+ instance()->checkMainWidgetVisibility(true);