Unity 8
WindowDecoration.qml
1 /*
2  * Copyright (C) 2014-2016 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 import Unity.Application 0.1 // For Mir singleton
19 import Ubuntu.Components 1.3
20 import "../Components"
21 import "../Components/PanelState"
22 
23 MouseArea {
24  id: root
25  clip: true
26 
27  property Item target
28  property alias title: titleLabel.text
29  property alias maximizeButtonShown: buttons.maximizeButtonShown
30  property bool active: false
31  acceptedButtons: Qt.AllButtons // prevent leaking unhandled mouse events
32  property alias overlayShown: buttons.overlayShown
33 
34  signal closeClicked()
35  signal minimizeClicked()
36  signal maximizeClicked()
37  signal maximizeHorizontallyClicked()
38  signal maximizeVerticallyClicked()
39 
40  onDoubleClicked: {
41  if (maximizeButtonShown && mouse.button == Qt.LeftButton) {
42  root.maximizeClicked();
43  }
44  }
45 
46  QtObject {
47  id: priv
48  property real distanceX
49  property real distanceY
50  property bool dragging
51  }
52 
53  onPressedChanged: {
54  if (pressed && pressedButtons == Qt.LeftButton) {
55  var pos = mapToItem(root.target, mouseX, mouseY);
56  priv.distanceX = pos.x;
57  priv.distanceY = pos.y;
58  priv.dragging = true;
59  } else {
60  priv.dragging = false;
61  Mir.cursorName = "";
62  }
63  }
64 
65  onPositionChanged: {
66  if (priv.dragging) {
67  Mir.cursorName = "grabbing";
68  var pos = mapToItem(root.target.parent, mouseX, mouseY);
69  // Use integer coordinate values to ensure that target is left in a pixel-aligned
70  // position. Mouse movement could have subpixel precision, yielding a fractional
71  // mouse position.
72  root.target.requestedX = Math.round(pos.x - priv.distanceX);
73  root.target.requestedY = Math.round(Math.max(pos.y - priv.distanceY, PanelState.panelHeight));
74  }
75  }
76 
77  // do not let unhandled wheel event pass thru the decoration
78  onWheel: wheel.accepted = true;
79 
80  Rectangle {
81  anchors.fill: parent
82  anchors.bottomMargin: -radius
83  radius: units.gu(.5)
84  color: theme.palette.normal.background
85  }
86 
87  Row {
88  anchors {
89  fill: parent
90  leftMargin: overlayShown ? units.gu(5) : units.gu(1)
91  rightMargin: units.gu(1)
92  topMargin: units.gu(0.5)
93  bottomMargin: units.gu(0.5)
94  }
95  Behavior on anchors.leftMargin {
96  UbuntuNumberAnimation {}
97  }
98 
99  spacing: units.gu(3)
100 
101  WindowControlButtons {
102  id: buttons
103  height: parent.height
104  active: root.active
105  onCloseClicked: root.closeClicked();
106  onMinimizeClicked: root.minimizeClicked();
107  onMaximizeClicked: root.maximizeClicked();
108  onMaximizeHorizontallyClicked: root.maximizeHorizontallyClicked();
109  onMaximizeVerticallyClicked: root.maximizeVerticallyClicked();
110  closeButtonShown: root.target.application.appId !== "unity8-dash"
111  target: root.target
112  }
113 
114  Label {
115  id: titleLabel
116  objectName: "windowDecorationTitle"
117  color: root.active ? "white" : UbuntuColors.slate
118  height: parent.height
119  width: parent.width - buttons.width - parent.anchors.rightMargin - parent.anchors.leftMargin
120  verticalAlignment: Text.AlignVCenter
121  fontSize: "medium"
122  font.weight: root.active ? Font.Light : Font.Medium
123  elide: Text.ElideRight
124  opacity: overlayShown ? 0 : 1
125  visible: opacity == 1
126  Behavior on opacity {
127  OpacityAnimator { duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
128  }
129  }
130  }
131 }