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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import QtQuick 2.12
|
|
|
|
import QtQuick.Window 2.12
|
|
|
|
import QtGraphicalEffects 1.0
|
|
|
|
|
|
|
|
Window {
|
|
|
|
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-10 02:25:21 -06:00
|
|
|
readonly property int handleSize: radius > 0 ? radius + 10 + shadowMargin : 0
|
2021-11-09 22:16:36 -06:00
|
|
|
readonly property int radius: ((root.visibility === 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-11 23:51:03 -06:00
|
|
|
readonly property int customFlags: Qt.Window |
|
|
|
|
Qt.CustomizeWindowHint |
|
2021-11-09 14:55:26 -06:00
|
|
|
Qt.FramelessWindowHint |
|
2021-11-11 23:51:03 -06:00
|
|
|
Qt.WindowSystemMenuHint |
|
|
|
|
Qt.NoDropShadowWindowHint |
|
|
|
|
Qt.WindowMinMaxButtonsHint
|
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
|
|
|
|
//
|
|
|
|
property int shadowMargin: root.radius > 0 ? 20 : 0
|
|
|
|
|
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
|
|
|
|
|
|
|
//
|
|
|
|
// Shadow implementation
|
|
|
|
//
|
|
|
|
RectangularGlow {
|
2021-11-09 14:55:26 -06:00
|
|
|
spread: 0.2
|
2021-11-09 14:30:59 -06:00
|
|
|
anchors.fill: bg
|
2021-11-09 14:55:26 -06:00
|
|
|
color: Qt.rgba(0,0,0,0.5)
|
2021-11-09 15:01:30 -06:00
|
|
|
glowRadius: root.shadowMargin / 2
|
2021-11-09 14:55:26 -06:00
|
|
|
cornerRadius: bg.radius + glowRadius
|
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 {
|
|
|
|
z: 1000
|
|
|
|
opacity: 0.8
|
|
|
|
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-09 17:18:32 -08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Resize handler
|
|
|
|
//
|
|
|
|
ResizeHandles {
|
|
|
|
window: root
|
|
|
|
anchors.fill: parent
|
|
|
|
handleSize: root.handleSize
|
|
|
|
}
|
2021-11-09 14:30:59 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Titlebar control
|
|
|
|
//
|
2021-11-09 22:52:52 -06:00
|
|
|
Titlebar {
|
2021-11-12 02:24:31 -06:00
|
|
|
id: _title
|
2021-11-09 14:30:59 -06:00
|
|
|
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
|
2021-11-09 15:01:30 -06:00
|
|
|
margins: root.shadowMargin
|
2021-11-09 14:30:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Maximize window fixes
|
|
|
|
//
|
|
|
|
onVisibilityChanged: {
|
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-09 14:30:59 -06:00
|
|
|
if (visibility === 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-09 14:30:59 -06:00
|
|
|
else if (visibility === 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-09 14:30:59 -06:00
|
|
|
else if (visibility === 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-09 14:30:59 -06:00
|
|
|
else if (visibility !== 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
|
|
|
|
root.x = (Screen.desktopAvailableWidth - root.width) / 2
|
|
|
|
root.y = (Screen.desktopAvailableHeight - root.height) / 2
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|