mirror-chatterino2/src/widgets/attachedwindow.cpp

119 lines
2.9 KiB
C++
Raw Normal View History

#include "attachedwindow.hpp"
#include "application.hpp"
#include <QTimer>
#include <QVBoxLayout>
#include "widgets/split.hpp"
2018-04-26 20:58:32 +02:00
#ifdef USEWINSDK
#include "Windows.h"
#pragma comment(lib, "Dwmapi.lib")
2018-04-26 20:58:32 +02:00
#endif
namespace chatterino {
namespace widgets {
AttachedWindow::AttachedWindow(void *_target, int _yOffset)
2018-05-06 14:16:41 +02:00
: QWidget(nullptr, Qt::FramelessWindowHint | Qt::Window)
, target(_target)
, yOffset(_yOffset)
{
QLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
this->setLayout(layout);
auto *split = new Split(this);
this->ui.split = split;
split->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
layout->addWidget(split);
}
AttachedWindow::~AttachedWindow()
{
for (auto it = items.begin(); it != items.end(); it++) {
if (it->window == this) {
items.erase(it);
break;
}
}
}
AttachedWindow *AttachedWindow::get(void *target, const QString &winId, int yOffset)
{
for (Item &item : items) {
if (item.hwnd == target) {
return item.window;
}
}
auto *window = new AttachedWindow(target, yOffset);
items.push_back(Item{target, window, winId});
return window;
}
void AttachedWindow::detach(const QString &winId)
{
for (Item &item : items) {
if (item.winId == winId) {
item.window->deleteLater();
}
}
}
void AttachedWindow::setChannel(ChannelPtr channel)
{
this->ui.split->setChannel(channel);
}
void AttachedWindow::showEvent(QShowEvent *)
{
attachToHwnd(this->target);
}
void AttachedWindow::attachToHwnd(void *_hwnd)
{
2018-04-26 20:58:32 +02:00
#ifdef USEWINSDK
QTimer *timer = new QTimer(this);
timer->setInterval(1);
HWND hwnd = (HWND)this->winId();
HWND attached = (HWND)_hwnd;
QObject::connect(timer, &QTimer::timeout, [this, hwnd, attached, timer] {
::SetLastError(0);
RECT xD;
::GetWindowRect(attached, &xD);
if (::GetLastError() != 0) {
timer->stop();
timer->deleteLater();
this->deleteLater();
}
HWND next = ::GetNextWindow(attached, GW_HWNDPREV);
::SetWindowPos(hwnd, next ? next : HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
::MoveWindow(hwnd, xD.right - 360, xD.top + this->yOffset - 8, 360 - 8,
xD.bottom - xD.top - this->yOffset, false);
// ::MoveWindow(hwnd, xD.right - 360, xD.top + 82, 360 - 8, xD.bottom - xD.top - 82 -
// 8,
// false);
});
timer->start();
2018-04-26 20:58:32 +02:00
#endif
}
// void AttachedWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
//{
// MSG *msg = reinterpret_cast
// case WM_NCCALCSIZE: {
// }
//}
std::vector<AttachedWindow::Item> AttachedWindow::items;
} // namespace widgets
} // namespace chatterino