[kwin] Provide composited outline as QML instead of hand drawn FrameSvg

In order to get all our UI being written in QML, the Outline for
compositing is ported to QtQuick. It creates a QQmlContext and
QQmlComponent and loads the (currently hardcoded) qml file. The context
gets to properties exported: outline. The outline property allows the qml
to get the geometry it should use. The QQmlEngine is used from Scripting
thus all general scripting properties are exported (e.g. workspace).

The qml script is expected to create a QQuickWindow as it's root item
and style it.

The qml file is stored in a new qml subdirectory. The idea is that each
of the qml types we have gets an own directory there and for each
implementation there should be a further subdirectory.

Thus we have outline/plasma/ with outline being the type and plasma
being the implementation. But at the moment the script location is still
hardcoded.

REVIEW: 116123
This commit is contained in:
Martin Gräßlin 2014-02-27 15:25:03 +01:00
parent 869c087e21
commit 22fe6a2857
5 changed files with 142 additions and 64 deletions

View file

@ -349,5 +349,6 @@ install( FILES scripting/kwinscript.desktop DESTINATION ${SERVICETYPES_INSTALL_D
ecm_install_icons( ${ICON_INSTALL_DIR} )
add_subdirectory(qml)
add_subdirectory(autotests)
add_subdirectory(tests)

View file

@ -22,13 +22,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "outline.h"
// KWin
#include "composite.h"
#include "scripting/scripting.h"
// KWin libs
#include <kwinxrenderutils.h>
#include "workspace.h"
// Plasma
#include <Plasma/FrameSvg>
// Qt
#include <QPainter>
#include <QDebug>
#include <QQmlComponent>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickWindow>
#include <QStandardPaths>
// xcb
#include <xcb/render.h>
@ -50,6 +54,7 @@ Outline::~Outline()
void Outline::show()
{
m_active = true;
emit activeChanged();
if (m_visual.isNull()) {
createHelper();
}
@ -66,6 +71,7 @@ void Outline::hide()
return;
}
m_active = false;
emit activeChanged();
if (m_visual.isNull()) {
return;
}
@ -80,7 +86,11 @@ void Outline::show(const QRect& outlineGeometry)
void Outline::setGeometry(const QRect& outlineGeometry)
{
if (m_outlineGeometry == outlineGeometry) {
return;
}
m_outlineGeometry = outlineGeometry;
emit geometryChanged();
}
void Outline::createHelper()
@ -113,17 +123,11 @@ OutlineVisual::~OutlineVisual()
}
CompositedOutlineVisual::CompositedOutlineVisual(Outline *outline)
: QWidget(NULL, Qt::X11BypassWindowManagerHint)
, OutlineVisual(outline)
, m_background(new Plasma::FrameSvg(this))
: OutlineVisual(outline)
, m_qmlContext()
, m_qmlComponent()
, m_mainItem(nullptr)
{
setAttribute(Qt::WA_TranslucentBackground);
QPalette pal = palette();
pal.setColor(backgroundRole(), Qt::transparent);
setPalette(pal);
m_background->setImagePath(QStringLiteral("widgets/translucentbackground"));
m_background->setCacheAllRenderedFrames(true);
m_background->setEnabledBorders(Plasma::FrameSvg::AllBorders);
}
CompositedOutlineVisual::~CompositedOutlineVisual()
@ -132,56 +136,33 @@ CompositedOutlineVisual::~CompositedOutlineVisual()
void CompositedOutlineVisual::hide()
{
QWidget::hide();
if (QQuickWindow *w = qobject_cast<QQuickWindow*>(m_mainItem)) {
w->hide();
w->destroy();
}
}
void CompositedOutlineVisual::show()
{
const QRect &outlineGeometry = outline()->geometry();
m_background->resizeFrame(outlineGeometry.size());
setGeometry(outlineGeometry);
// check which borders to enable
bool left, right, top, bottom;
left = right = top = bottom = false;
const QRect maximizedArea = Workspace::self()->clientArea(MaximizeArea, outlineGeometry.center(), 1);
if (outlineGeometry.x() == maximizedArea.x()) {
left = true;
if (m_qmlContext.isNull()) {
m_qmlContext.reset(new QQmlContext(Scripting::self()->qmlEngine()));
m_qmlContext->setContextProperty(QStringLiteral("outline"), outline());
}
if (outlineGeometry.y() == maximizedArea.y()) {
top = true;
if (m_qmlComponent.isNull()) {
m_qmlComponent.reset(new QQmlComponent(Scripting::self()->qmlEngine()));
// TODO: fileName should be configurable
const QString fileName = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral(KWIN_NAME) + QStringLiteral("/outline/plasma/outline.qml"));
if (fileName.isEmpty()) {
qDebug() << "Could not locate outline.qml";
return;
}
m_qmlComponent->loadUrl(QUrl::fromLocalFile(fileName));
if (m_qmlComponent->isError()) {
qDebug() << "Component failed to load: " << m_qmlComponent->errors();
} else {
m_mainItem = m_qmlComponent->create(m_qmlContext.data());
}
}
if (outlineGeometry.right() == maximizedArea.right()) {
right = true;
}
if (outlineGeometry.bottom() == maximizedArea.bottom()) {
bottom = true;
}
Plasma::FrameSvg::EnabledBorders borders = Plasma::FrameSvg::AllBorders;
if (left) {
borders = borders & ~Plasma::FrameSvg::LeftBorder;
}
if (right) {
borders = borders & ~Plasma::FrameSvg::RightBorder;
}
if (top) {
borders = borders & ~Plasma::FrameSvg::TopBorder;
}
if (bottom) {
borders = borders & ~Plasma::FrameSvg::BottomBorder;
}
if (left && right && bottom && top) {
borders = Plasma::FrameSvg::AllBorders;
}
m_background->setEnabledBorders(borders);
QWidget::show();
}
void CompositedOutlineVisual::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
m_background->paintFrame(&painter);
}
NonCompositedOutlineVisual::NonCompositedOutlineVisual(Outline *outline)

View file

@ -25,9 +25,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QRect>
#include <QWidget>
namespace Plasma {
class FrameSvg;
}
class QQmlContext;
class QQmlComponent;
namespace KWin {
class OutlineVisual;
@ -45,6 +44,8 @@ class OutlineVisual;
*/
class Outline : public QObject {
Q_OBJECT
Q_PROPERTY(QRect geometry READ geometry NOTIFY geometryChanged)
Q_PROPERTY(bool active READ isActive NOTIFY activeChanged)
public:
~Outline();
@ -80,9 +81,15 @@ public:
const QRect &geometry() const;
bool isActive() const;
private Q_SLOTS:
void compositingChanged();
Q_SIGNALS:
void activeChanged();
void geometryChanged();
private:
void createHelper();
QScopedPointer<OutlineVisual> m_visual;
@ -105,17 +112,17 @@ private:
Outline *m_outline;
};
class CompositedOutlineVisual : public QWidget, public OutlineVisual
class CompositedOutlineVisual : public OutlineVisual
{
public:
CompositedOutlineVisual(Outline *outline);
virtual ~CompositedOutlineVisual();
virtual void show();
virtual void hide();
protected:
virtual void paintEvent(QPaintEvent *);
private:
Plasma::FrameSvg *m_background;
QScopedPointer<QQmlContext> m_qmlContext;
QScopedPointer<QQmlComponent> m_qmlComponent;
QObject *m_mainItem;
};
class NonCompositedOutlineVisual : public OutlineVisual
@ -137,6 +144,12 @@ private:
Xcb::Window m_leftOutline;
};
inline
bool Outline::isActive() const
{
return m_active;
}
inline
const QRect &Outline::geometry() const
{

1
qml/CMakeLists.txt Normal file
View file

@ -0,0 +1 @@
install( DIRECTORY outline/plasma DESTINATION ${DATA_INSTALL_DIR}/${KWIN_NAME}/outline )

View file

@ -0,0 +1,82 @@
/*
* Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.1;
import QtQuick.Window 2.1;
import org.kde.plasma.core 2.0 as PlasmaCore;
Window {
id: window
flags: Qt.BypassWindowManagerHint
color: "transparent"
// outline is a context property
x: outline.geometry.x
y: outline.geometry.y
width: outline.geometry.width
height: outline.geometry.height
visible: outline.active
PlasmaCore.FrameSvgItem {
function updateBorders() {
var maximizedArea = workspace.clientArea(workspace.MaximizeArea, Qt.point(outline.geometry.x, outline.geometry.y), workspace.currentDesktop);
var left = false;
var right = false;
var top = false;
var bottom = false;
if (outline.geometry.x == maximizedArea.x) {
left = true;
}
if (outline.geometry.y == maximizedArea.y) {
top = true;
}
if (outline.geometry.x + outline.geometry.width == maximizedArea.x + maximizedArea.width) {
right = true;
}
if (outline.geometry.y + outline.geometry.height == maximizedArea.y + maximizedArea.height) {
bottom = true;
}
var borders = PlasmaCore.FrameSvgItem.AllBorders;
if (left) {
borders = borders & ~PlasmaCore.FrameSvgItem.LeftBorder;
}
if (right) {
borders = borders & ~PlasmaCore.FrameSvgItem.RightBorder;
}
if (top) {
borders = borders & ~PlasmaCore.FrameSvgItem.TopBorder;
}
if (bottom) {
borders = borders & ~PlasmaCore.FrameSvgItem.BottomBorder;
}
if (left && right && bottom && top) {
borders = PlasmaCore.FrameSvgItem.AllBorders;
}
svg.enabledBorders = borders;
}
id: svg
imagePath: "widgets/translucentbackground"
anchors.fill: parent
Connections {
target: outline
onGeometryChanged: svg.updateBorders()
}
Component.onCompleted: svg.updateBorders()
}
}