2021-11-09 14:30:59 -06:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2021 Alex Spataru <https://github.com/alex-spataru>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-12-18 03:52:07 -05:00
|
|
|
import QtQuick 2.12
|
|
|
|
import QtQuick.Window 2.12 as QtWindow
|
2021-11-25 02:07:07 -06:00
|
|
|
|
|
|
|
import "../Widgets" as Widgets
|
2021-11-09 14:30:59 -06:00
|
|
|
|
2021-11-28 22:07:36 -06:00
|
|
|
QtWindow.Window {
|
2021-11-09 14:30:59 -06:00
|
|
|
id: root
|
|
|
|
color: "transparent"
|
2021-11-09 20:18:51 -06:00
|
|
|
flags: root.customFlags | root.extraFlags
|
2021-11-09 14:30:59 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Custom signals
|
|
|
|
//
|
|
|
|
signal closed()
|
|
|
|
signal minimized()
|
|
|
|
signal maximized()
|
|
|
|
signal unmaximized()
|
|
|
|
|
|
|
|
//
|
|
|
|
// Window radius control
|
|
|
|
//
|
2021-11-09 16:59:53 -06:00
|
|
|
property int borderWidth: 2
|
2021-11-28 22:04:18 -06:00
|
|
|
readonly property int handleSize: radius > 0 ? radius + shadowMargin + 10 : 0
|
2021-11-28 22:07:36 -06:00
|
|
|
readonly property int radius: ((root.visibility === QtWindow.Window.Maximized && maximizeEnabled) || isFullscreen) ? 0 : 10
|
2021-11-09 14:30:59 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Visibility properties
|
|
|
|
//
|
2021-11-09 20:18:51 -06:00
|
|
|
property int extraFlags: 0
|
2021-11-09 14:30:59 -06:00
|
|
|
property bool firstChange: true
|
2021-11-09 22:16:36 -06:00
|
|
|
property bool isMaximized: false
|
|
|
|
property bool isMinimized: false
|
2021-11-14 19:52:41 -06:00
|
|
|
property alias showIcon: _title.showIcon
|
2021-11-12 02:24:31 -06:00
|
|
|
property alias isFullscreen: _title.isFullscreen
|
2021-11-25 02:31:44 -06:00
|
|
|
readonly property int customFlags: {
|
|
|
|
// Setup frameless window flags
|
|
|
|
var flags = Qt.Window |
|
|
|
|
Qt.CustomizeWindowHint |
|
|
|
|
Qt.FramelessWindowHint |
|
|
|
|
Qt.WindowSystemMenuHint |
|
|
|
|
Qt.WindowMinMaxButtonsHint
|
|
|
|
|
|
|
|
//
|
|
|
|
// The macOS window manager is able to generate shadows for Qt frameless
|
|
|
|
// windows. Other operating systems have varied mileage, so we draw the
|
|
|
|
// shadow ourselves to avoid ugly surprises.
|
|
|
|
//
|
|
|
|
// Also, disabling the custom shadow on macOS is a good idea so that users can
|
|
|
|
// move the window freely over all the desktop without being blocked by the
|
|
|
|
// menubar or dock due to the custom shadow border.
|
|
|
|
//
|
|
|
|
if (!Cpp_IsMac)
|
|
|
|
flags |= Qt.NoDropShadowWindowHint
|
|
|
|
|
|
|
|
// Return "calculated" window flags
|
|
|
|
return flags
|
|
|
|
}
|
2021-11-09 14:30:59 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Toggle fullscreen state
|
|
|
|
//
|
|
|
|
function toggleFullscreen() {
|
2021-11-09 22:16:36 -06:00
|
|
|
root.isFullscreen = !root.isFullscreen
|
|
|
|
if (root.isFullscreen)
|
2021-11-09 14:30:59 -06:00
|
|
|
root.showFullScreen()
|
|
|
|
else
|
|
|
|
root.showNormal()
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Alias to the titlebar
|
|
|
|
//
|
2021-11-12 02:24:31 -06:00
|
|
|
property alias titlebar: _title
|
2021-11-09 14:30:59 -06:00
|
|
|
|
2021-11-09 15:01:30 -06:00
|
|
|
//
|
|
|
|
// Size of the shadow object
|
|
|
|
//
|
2021-12-21 17:06:08 -05:00
|
|
|
property int shadowMargin: (Cpp_IsMac | !Cpp_ThemeManager.shadowsEnabled) ?
|
|
|
|
0 : (root.radius > 0 ? 20 : 0)
|
2021-11-09 15:01:30 -06:00
|
|
|
|
2021-11-09 20:18:51 -06:00
|
|
|
//
|
|
|
|
// Titlebar left/right margins for custom controls
|
|
|
|
//
|
2021-11-12 02:24:31 -06:00
|
|
|
property alias leftTitlebarMargin: _title.leftMargin
|
|
|
|
property alias rightTitlebarMargin: _title.rightMargin
|
|
|
|
property alias showMacControls: _title.showMacControls
|
2021-11-09 20:18:51 -06:00
|
|
|
|
2021-11-09 14:30:59 -06:00
|
|
|
//
|
|
|
|
// Background color of the window & the titlebar
|
|
|
|
//
|
|
|
|
property color borderColor: Cpp_ThemeManager.highlight
|
|
|
|
property color backgroundColor: Cpp_ThemeManager.window
|
|
|
|
property color titlebarText: Cpp_ThemeManager.brightText
|
|
|
|
property color titlebarColor: Cpp_ThemeManager.toolbarGradient2
|
|
|
|
|
|
|
|
//
|
|
|
|
// Window controls
|
|
|
|
//
|
2021-11-12 02:24:31 -06:00
|
|
|
property alias closeEnabled: _title.closeEnabled
|
|
|
|
property alias minimizeEnabled: _title.minimizeEnabled
|
|
|
|
property alias maximizeEnabled: _title.maximizeEnabled
|
2021-11-09 14:30:59 -06:00
|
|
|
|
2021-11-18 23:43:12 -06:00
|
|
|
//
|
|
|
|
// Ensure that window size & position is valid upon showing
|
|
|
|
//
|
|
|
|
Component.onCompleted: {
|
|
|
|
// Reset window size for whatever reason
|
|
|
|
if (root.width <= 0 || root.height <= 0) {
|
|
|
|
root.width = root.minimumWidth
|
|
|
|
root.height = root.minimumHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
// Startup verifications to ensure that app is displayed inside the screen
|
2021-11-28 22:07:36 -06:00
|
|
|
if (root.x < 0 || root.x >= QtWindow.Screen.desktopAvailableWidth)
|
2021-11-18 23:43:12 -06:00
|
|
|
root.x = 100
|
2021-11-28 22:07:36 -06:00
|
|
|
if (root.y < 0 || root.y >= QtWindow.Screen.desktopAvailableHeight)
|
2021-11-18 23:43:12 -06:00
|
|
|
root.y = 100
|
|
|
|
|
|
|
|
// Startup verifications to ensure that app fits in current screen
|
2021-11-28 22:07:36 -06:00
|
|
|
if (root.width > QtWindow.Screen.desktopAvailableWidth) {
|
2021-11-18 23:43:12 -06:00
|
|
|
root.x = 100
|
2021-11-28 22:07:36 -06:00
|
|
|
root.width = QtWindow.Screen.desktopAvailableWidth - root.x
|
2021-11-18 23:43:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Startup verifications to ensure that app fits in current screen
|
2021-11-28 22:07:36 -06:00
|
|
|
if (root.height > QtWindow.Screen.desktopAvailableHeight) {
|
2021-11-18 23:43:12 -06:00
|
|
|
root.y = 100
|
2021-11-28 22:07:36 -06:00
|
|
|
root.height = QtWindow.Screen.desktopAvailableHeight - root.y
|
2021-11-18 23:43:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:30:59 -06:00
|
|
|
//
|
2021-11-25 02:31:44 -06:00
|
|
|
// Custom shadow implementation for non-macOS systems. The shadow shall be disabled
|
|
|
|
// when the window is maximized.
|
2021-11-09 14:30:59 -06:00
|
|
|
//
|
2021-11-25 02:07:07 -06:00
|
|
|
Widgets.Shadow {
|
2021-11-26 01:04:38 -06:00
|
|
|
border.top: 50
|
|
|
|
border.left: 50
|
|
|
|
border.right: 50
|
|
|
|
border.bottom: 50
|
2021-11-09 14:30:59 -06:00
|
|
|
anchors.fill: bg
|
2021-11-26 01:04:38 -06:00
|
|
|
shadowOpacity: 1.0
|
2021-11-25 02:07:07 -06:00
|
|
|
visible: root.shadowMargin > 0
|
|
|
|
topMargin: -1 * root.shadowMargin
|
|
|
|
leftMargin: -1 * root.shadowMargin
|
|
|
|
rightMargin: -1 * root.shadowMargin
|
|
|
|
bottomMargin: -1 * root.shadowMargin
|
2021-11-26 01:04:38 -06:00
|
|
|
source: "qrc:/images/window-shadow.png"
|
2021-11-09 14:30:59 -06:00
|
|
|
} Rectangle {
|
|
|
|
id: bg
|
|
|
|
color: "transparent"
|
|
|
|
radius: root.radius
|
|
|
|
anchors.fill: parent
|
2021-11-09 15:01:30 -06:00
|
|
|
anchors.margins: root.shadowMargin
|
2021-11-09 14:30:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Window border
|
|
|
|
//
|
|
|
|
Rectangle {
|
|
|
|
opacity: 0.8
|
2021-11-28 21:06:11 -06:00
|
|
|
z: titlebar.z + 1
|
2021-11-09 14:30:59 -06:00
|
|
|
radius: root.radius
|
|
|
|
color: "transparent"
|
|
|
|
anchors.fill: parent
|
|
|
|
border.color: root.borderColor
|
2021-11-09 16:59:53 -06:00
|
|
|
border.width: root.borderWidth
|
|
|
|
anchors.margins: root.shadowMargin
|
2021-11-09 14:30:59 -06:00
|
|
|
}
|
|
|
|
|
2021-11-25 05:42:40 -06:00
|
|
|
//
|
|
|
|
// Handle for resizing the window
|
|
|
|
// Note: this does not work on macOS, see the following article for more information
|
|
|
|
// https://www.qt.io/blog/custom-window-decorations
|
|
|
|
//
|
|
|
|
DragHandler {
|
|
|
|
id: resizeHandler
|
|
|
|
target: null
|
|
|
|
enabled: !Cpp_IsMac
|
|
|
|
grabPermissions: TapHandler.TakeOverForbidden
|
|
|
|
onActiveChanged: {
|
|
|
|
if (active) {
|
|
|
|
const p = resizeHandler.centroid.position
|
|
|
|
const b = root.handleSize
|
|
|
|
let e = 0;
|
|
|
|
|
|
|
|
if (p.x < b)
|
|
|
|
e |= Qt.LeftEdge
|
|
|
|
|
|
|
|
if (p.x >= width - b)
|
|
|
|
e |= Qt.RightEdge
|
|
|
|
|
|
|
|
if (p.y < b)
|
|
|
|
e |= Qt.TopEdge
|
|
|
|
|
|
|
|
if (p.y >= height - b)
|
|
|
|
e |= Qt.BottomEdge
|
|
|
|
|
2021-11-28 21:06:11 -06:00
|
|
|
root.startSystemResize(e)
|
2021-11-25 05:42:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 21:06:11 -06:00
|
|
|
//
|
|
|
|
// Global mouse area to set cursor shape while resizing
|
|
|
|
//
|
|
|
|
MouseArea {
|
|
|
|
z: titlebar.z - 1
|
|
|
|
hoverEnabled: true
|
|
|
|
anchors.fill: parent
|
|
|
|
acceptedButtons: Qt.NoButton
|
|
|
|
cursorShape: {
|
|
|
|
const p = Qt.point(mouseX, mouseY)
|
2021-11-28 22:04:18 -06:00
|
|
|
const b = root.handleSize / 2
|
2021-11-28 21:06:11 -06:00
|
|
|
|
|
|
|
if (p.x < b && p.y < b)
|
|
|
|
return Qt.SizeFDiagCursor
|
|
|
|
|
|
|
|
if (p.x >= width - b && p.y >= height - b)
|
|
|
|
return Qt.SizeFDiagCursor
|
|
|
|
|
|
|
|
if (p.x >= width - b && p.y < b)
|
|
|
|
return Qt.SizeBDiagCursor
|
|
|
|
|
|
|
|
if (p.x < b && p.y >= height - b)
|
|
|
|
return Qt.SizeBDiagCursor
|
|
|
|
|
|
|
|
if (p.x < b || p.x >= width - b)
|
|
|
|
return Qt.SizeHorCursor
|
|
|
|
|
|
|
|
if (p.y < b || p.y >= height - b)
|
|
|
|
return Qt.SizeVerCursor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Titlebar control
|
|
|
|
//
|
|
|
|
Titlebar {
|
|
|
|
z: 2000
|
|
|
|
id: _title
|
|
|
|
window: root
|
|
|
|
radius: root.radius
|
|
|
|
color: root.titlebarColor
|
|
|
|
textColor: root.titlebarText
|
|
|
|
|
|
|
|
onClosed: root.closed()
|
|
|
|
onMinimized: root.minimized()
|
|
|
|
onMaximized: root.maximized()
|
|
|
|
onUnmaximized: root.unmaximized()
|
|
|
|
|
|
|
|
anchors {
|
|
|
|
top: parent.top
|
|
|
|
left: parent.left
|
|
|
|
right: parent.right
|
|
|
|
margins: root.shadowMargin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:30:59 -06:00
|
|
|
//
|
|
|
|
// Maximize window fixes
|
|
|
|
//
|
2021-12-16 13:43:57 -05:00
|
|
|
onVisibilityChanged: (visibility) => {
|
2021-11-09 20:18:51 -06:00
|
|
|
// Hard-reset window flags on macOS to fix most glitches
|
|
|
|
if (Cpp_IsMac)
|
|
|
|
root.flags = Qt.Window
|
|
|
|
|
|
|
|
// Ensure that correct window flags are still used
|
|
|
|
root.flags = root.customFlags | root.extraFlags
|
|
|
|
|
|
|
|
// Window has been just maximized, update internal variables
|
2021-11-28 22:07:36 -06:00
|
|
|
if (visibility === QtWindow.Window.Maximized) {
|
2021-11-09 22:16:36 -06:00
|
|
|
if (!root.isMaximized)
|
2021-11-09 14:30:59 -06:00
|
|
|
root.firstChange = false
|
|
|
|
|
2021-11-09 22:16:36 -06:00
|
|
|
root.isMaximized = true
|
|
|
|
root.isFullscreen = false
|
2021-11-09 14:30:59 -06:00
|
|
|
}
|
|
|
|
|
2021-11-09 20:18:51 -06:00
|
|
|
// Window has been just minimized, update internal variables
|
2021-11-28 22:07:36 -06:00
|
|
|
else if (visibility === QtWindow.Window.Minimized) {
|
2021-11-09 22:16:36 -06:00
|
|
|
root.isFullscreen = false
|
|
|
|
root.isMaximized = false
|
2021-11-09 14:30:59 -06:00
|
|
|
}
|
|
|
|
|
2021-11-09 20:18:51 -06:00
|
|
|
// Window has been just switched to full-screen, update internal variables
|
2021-11-28 22:07:36 -06:00
|
|
|
else if (visibility === QtWindow.Window.FullScreen) {
|
2021-11-09 22:16:36 -06:00
|
|
|
if (!root.isFullscreen)
|
2021-11-09 14:30:59 -06:00
|
|
|
root.firstChange = false
|
|
|
|
|
2021-11-09 22:16:36 -06:00
|
|
|
root.isFullscreen = true
|
|
|
|
root.isMaximized = false
|
2021-11-09 14:30:59 -06:00
|
|
|
}
|
|
|
|
|
2021-11-09 20:18:51 -06:00
|
|
|
// Window was just restored to "normal" mode, recover previous geometry
|
2021-11-28 22:07:36 -06:00
|
|
|
else if (visibility !== QtWindow.Window.Hidden) {
|
2021-11-09 22:16:36 -06:00
|
|
|
if (isMaximized || isFullscreen && firstChange) {
|
2021-11-09 14:30:59 -06:00
|
|
|
root.width = root.minimumWidth
|
|
|
|
root.height = root.minimumHeight
|
2021-11-28 22:07:36 -06:00
|
|
|
root.x = (QtWindow.Screen.desktopAvailableWidth - root.width) / 2
|
|
|
|
root.y = (QtWindow.Screen.desktopAvailableHeight - root.height) / 2
|
2021-11-09 14:30:59 -06:00
|
|
|
}
|
|
|
|
|
2021-11-09 22:16:36 -06:00
|
|
|
root.isMaximized = false
|
|
|
|
root.isFullscreen = false
|
|
|
|
|
|
|
|
// Force active focus
|
|
|
|
root.requestActivate()
|
|
|
|
root.requestUpdate()
|
2021-11-09 14:30:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|