Skip to content

Commit

Permalink
fix(#47): frameless window for qt 6.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
ffiirree committed Mar 18, 2024
1 parent 6873111 commit c9c9223
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

int main(int argc, char *argv[])
{
qputenv("QT_ENABLE_HIGHDPI_SCALING", "0");

QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);

#ifdef _WIN32
Expand Down
2 changes: 1 addition & 1 deletion src/player/control-widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ ControlWidget::ControlWidget(FramelessWindow *parent)

bool ControlWidget::hideable() const
{
return !time_slider_->isSliderDown() && !volume_slider_->isSliderDown() &&
return !control_bar_->geometry().contains(mapFromGlobal(QCursor::pos())) &&
!speed_box_->view()->isVisible();
}

Expand Down
4 changes: 2 additions & 2 deletions src/player/video-player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ using AudioOutput = PulseAudioRenderer;
#endif

VideoPlayer::VideoPlayer(QWidget *parent)
: FramelessWindow(parent, Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint |
Qt::WindowFullscreenButtonHint | Qt::WindowStaysOnTopHint)
: FramelessWindow(parent, Qt::WindowMinMaxButtonsHint | Qt::WindowFullscreenButtonHint |
Qt::WindowStaysOnTopHint)
{
setAttribute(Qt::WA_DeleteOnClose);

Expand Down
2 changes: 1 addition & 1 deletion src/preview/image-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <QVBoxLayout>

ImageWindow::ImageWindow(const std::shared_ptr<QMimeData>& data, QWidget *parent)
: FramelessWindow(parent, Qt::Tool | Qt::WindowStaysOnTopHint | Qt::WindowTitleHint)
: FramelessWindow(parent, Qt::Tool | Qt::WindowStaysOnTopHint)
{
setWindowTitle("Image Window");

Expand Down
2 changes: 1 addition & 1 deletion src/setting/settingdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static const std::vector<std::pair<std::underlying_type_t<Qt::PenStyle>, QString
};

SettingWindow::SettingWindow(QWidget *parent)
: FramelessWindow(parent, Qt::WindowMinMaxButtonsHint | Qt::WindowTitleHint)
: FramelessWindow(parent, Qt::WindowMinMaxButtonsHint)
{
setMinimumSize(1080, 760);
setContentsMargins({});
Expand Down
2 changes: 1 addition & 1 deletion src/snipping/menu/recording-menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#endif

RecordingMenu::RecordingMenu(bool mm, bool sm, uint8_t buttons, QWidget *parent)
: FramelessWindow(parent, Qt::Tool | Qt::WindowStaysOnTopHint | Qt::WindowTitleHint)
: FramelessWindow(parent, Qt::Tool | Qt::WindowStaysOnTopHint)
{
setAttribute(Qt::WA_ShowWithoutActivating);

Expand Down
53 changes: 23 additions & 30 deletions src/widgets/framelesswindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

// [microsoft/terminal](https://github.com/microsoft/terminal/blob/main/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp)
FramelessWindow::FramelessWindow(QWidget *parent, const Qt::WindowFlags flags)
: QWidget(parent, Qt::Window | Qt::FramelessWindowHint | Qt::WindowCloseButtonHint | flags)
: QWidget(parent, Qt::Window | Qt::WindowCloseButtonHint | flags)
{
#ifdef Q_OS_WIN
setAttribute(Qt::WA_DontCreateNativeAncestors);
Expand All @@ -32,16 +32,15 @@ FramelessWindow::FramelessWindow(QWidget *parent, const Qt::WindowFlags flags)
::DwmExtendFrameIntoClientArea(hwnd, &margins);

// Window Styles: https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles
auto style = ::GetWindowLong(hwnd, GWL_STYLE);
if (windowFlags() & Qt::WindowTitleHint) style |= WS_CAPTION | WS_THICKFRAME;
::SetWindowLong(hwnd, GWL_STYLE, style & ~WS_SYSMENU);
::SetWindowLong(hwnd, GWL_STYLE, ::GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);

::SetWindowPos(hwnd, nullptr, 0, 0, 0, 0,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER |
SWP_FRAMECHANGED);
#endif

#ifdef Q_OS_LINUX
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setAttribute(Qt::WA_Hover, true);
#endif
}
Expand Down Expand Up @@ -279,37 +278,31 @@ bool FramelessWindow::nativeEvent(const QByteArray& eventType, void *message, Q_
}

case WM_NCHITTEST: {
if (const LRESULT res = ::DefWindowProcW(hwnd, WM_NCHITTEST, 0, wmsg->lParam); res != HTCLIENT) {

if (IsFullscreen(hwnd) || IsMaximized(hwnd) || isSizeFixed()) {
switch (res) {
case HTBOTTOMRIGHT:
case HTRIGHT:
case HTTOPRIGHT:
case HTTOP:
case HTTOPLEFT:
case HTLEFT:
case HTBOTTOMLEFT:
case HTBOTTOM: *result = HTCLIENT; return true;
default: break;
}
LRESULT res = ::DefWindowProcW(hwnd, WM_NCHITTEST, 0, wmsg->lParam);
if (res == HTCLIENT) {
RECT rect{};
if (::GetWindowRect(hwnd, &rect) &&
GET_Y_LPARAM(wmsg->lParam) < rect.top + ResizeHandleHeight(hwnd)) {
res = HTTOP;
}

*result = static_cast<long>(res);
return true;
}

// TOP
RECT rect{};
if (!::GetWindowRect(hwnd, &rect)) return false;

if (!IsMaximized(hwnd) && !IsFullscreen(hwnd) &&
(GET_Y_LPARAM(wmsg->lParam) < rect.top + ResizeHandleHeight(hwnd))) {
*result = isSizeFixed() ? HTCLIENT : HTTOP;
return true;
if (IsFullscreen(hwnd) || IsMaximized(hwnd) || isSizeFixed()) {
switch (res) {
case HTTOP:
case HTRIGHT:
case HTLEFT:
case HTBOTTOM:
case HTTOPLEFT:
case HTTOPRIGHT:
case HTBOTTOMLEFT:
case HTBOTTOMRIGHT: res = HTCLIENT; break;
default: break;
}
}

break;
*result = static_cast<long>(res);
return true;
}

default: break;
Expand Down

0 comments on commit c9c9223

Please sign in to comment.