Skip to content

Commit

Permalink
Multiple workspace backgrounds support
Browse files Browse the repository at this point in the history
- Fix background not updated after setting a new wallpaper
- Fix there're chances that wm dbus are not ready while
  launcher's trying to get the wallpaper.

Change-Id: I434f82978a400cdfb532a52200185b36af770ba3
  • Loading branch information
hualet committed Dec 16, 2016
1 parent f8e612a commit 38cd364
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 19 deletions.
76 changes: 76 additions & 0 deletions boxframe/backgroundmanager.cpp
@@ -0,0 +1,76 @@
/**
* Copyright (C) 2016 Deepin Technology Co., Ltd.
*
* 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 3 of the License, or
* (at your option) any later version.
**/

#include "backgroundmanager.h"

#include <QGSettings>

using namespace com::deepin;

static const QString DefaultWallpaper = "/usr/share/backgrounds/default_background.jpg";

BackgroundManager::BackgroundManager(QObject *parent)
: QObject(parent),
m_currentWorkspace(0),
m_wmInter(new wm("com.deepin.wm", "/com/deepin/wm", QDBusConnection::sessionBus(), this)),
m_gsettings(new QGSettings("com.deepin.dde.appearance", "", this))
{
auto updateBackgrounds = [this] {
setBackgrounds(m_gsettings->get("background-uris"));
};
auto updateWorkspace = [this] (int, int to) {
setCurrentWorkspace(to);
};

connect(m_gsettings, &QGSettings::changed, updateBackgrounds);
connect(m_wmInter, &__wm::WorkspaceSwitched, updateWorkspace);

updateBackgrounds();
updateWorkspace(0, 0);
}

QString BackgroundManager::currentWorkspaceBackground() const
{
if (m_backgrounds.length() >= m_currentWorkspace + 1) {
return m_backgrounds.at(m_currentWorkspace);
}

return DefaultWallpaper;
}

void BackgroundManager::setBackgrounds(QVariant backgrounsVariant)
{
m_backgrounds = backgrounsVariant.toStringList();

QString background = currentWorkspaceBackground();
if (background != m_currentWorkspaceBackground) {
m_currentWorkspaceBackground = background;

emit currentWorkspaceBackgroundChanged(background);
}
}

int BackgroundManager::currentWorkspace() const
{
return m_currentWorkspace;
}

void BackgroundManager::setCurrentWorkspace(int currentWorkspace)
{
if (m_currentWorkspace != currentWorkspace) {
m_currentWorkspace = currentWorkspace;
QString background = currentWorkspaceBackground();

if (m_currentWorkspaceBackground != background) {
m_currentWorkspaceBackground = background;

emit currentWorkspaceBackgroundChanged(background);
}
}
}
45 changes: 45 additions & 0 deletions boxframe/backgroundmanager.h
@@ -0,0 +1,45 @@
/**
* Copyright (C) 2016 Deepin Technology Co., Ltd.
*
* 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 3 of the License, or
* (at your option) any later version.
**/

#ifndef BACKGROUNDMANAGER_H
#define BACKGROUNDMANAGER_H

#include <QObject>

#include <com_deepin_wm.h>

class QGSettings;

class BackgroundManager : public QObject
{
Q_OBJECT
public:
explicit BackgroundManager(QObject *parent = 0);

QString currentWorkspaceBackground() const;

int currentWorkspace() const;
void setCurrentWorkspace(int currentWorkspace);

signals:
void currentWorkspaceBackgroundChanged(const QString &background);

private slots:
void setBackgrounds(QVariant backgroundsVariant);

private:
int m_currentWorkspace;
QString m_currentWorkspaceBackground;
QStringList m_backgrounds;

com::deepin::wm *m_wmInter;
QGSettings *m_gsettings;
};

#endif // BACKGROUNDMANAGER_H
2 changes: 2 additions & 0 deletions boxframe/boxframe.pri
Expand Up @@ -4,6 +4,8 @@ QT += dbus core

HEADERS += \
$$PWD/boxframe.h \
$$PWD/backgroundmanager.h

SOURCES += \
$$PWD/boxframe.cpp \
$$PWD/backgroundmanager.cpp
23 changes: 7 additions & 16 deletions mainframe.cpp
Expand Up @@ -2,6 +2,7 @@
#include "mainframe.h"
#include "global_util/constants.h"
#include "global_util/xcb_misc.h"
#include "backgroundmanager.h"

#include <QApplication>
#include <QDesktopWidget>
Expand All @@ -21,9 +22,9 @@ static const QString DisplayModeCategory = "category";

MainFrame::MainFrame(QWidget *parent) :
BoxFrame(parent),
m_wmInter(new com::deepin::wm("com.deepin.wm", "/com/deepin/wm", QDBusConnection::sessionBus(), this)),
m_launcherGsettings(new QGSettings("com.deepin.dde.launcher",
"/com/deepin/dde/launcher/", this)),
m_backgroundManager(new BackgroundManager(this)),
m_displayInter(new DBusDisplay(this)),

m_calcUtil(CalculateUtil::instance(this)),
Expand Down Expand Up @@ -466,23 +467,13 @@ void MainFrame::initUI()
m_scrollAnimation->setEasingCurve(QEasingCurve::OutQuad);

// setup background.
auto callback = [this] (int, int) {
QDBusPendingCall call = m_wmInter->GetCurrentWorkspaceBackground();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished, [this, call] {
if (!call. isError()) {
QDBusReply<QString> reply = call.reply();
const QString uri = reply.value();
const QString background = QUrl(uri).toLocalFile();
setBackground(background);
} else {
qWarning() << "get current workspace background error: " << call.error().message();
}
});
auto updateBackground = [this] (const QString &uri) {
const QString background = QUrl(uri).toLocalFile();
setBackground(background);
};

callback(0, 0);
connect(m_wmInter, &__wm::WorkspaceSwitched, callback);
connect(m_backgroundManager, &BackgroundManager::currentWorkspaceBackgroundChanged, updateBackground);
updateBackground(m_backgroundManager->currentWorkspaceBackground());
}

// FIXME:
Expand Down
5 changes: 2 additions & 3 deletions mainframe.h
Expand Up @@ -26,10 +26,9 @@

#include <dboxwidget.h>

#include <com_deepin_wm.h>

DWIDGET_USE_NAMESPACE

class BackgroundManager;
class DBusLauncherService;
class MainFrame : public BoxFrame
{
Expand Down Expand Up @@ -113,8 +112,8 @@ private slots:
double rightMarginRation = 1;
DisplayMode m_displayMode = Search;
AppsListModel::AppCategory m_currentCategory = AppsListModel::All;
com::deepin::wm *m_wmInter;
QGSettings *m_launcherGsettings;
BackgroundManager *m_backgroundManager;

DBusDisplay *m_displayInter;

Expand Down

0 comments on commit 38cd364

Please sign in to comment.