Merge lp:~agateau/unity-2d/themed-window-buttons into lp:unity-2d

Proposed by Aurélien Gâteau
Status: Superseded
Proposed branch: lp:~agateau/unity-2d/themed-window-buttons
Merge into: lp:unity-2d
Prerequisite: lp:~agateau/unity-2d/use-gtk-rendering
Diff against target: 256 lines (+124/-17)
3 files modified
panel/applets/appname/appnameapplet.cpp (+21/-17)
panel/applets/common/panelstyle.cpp (+87/-0)
panel/applets/common/panelstyle.h (+16/-0)
To merge this branch: bzr merge lp:~agateau/unity-2d/themed-window-buttons
Reviewer Review Type Date Requested Status
Florian Boucault Pending
Review via email: mp+69490@code.launchpad.net

This proposal supersedes a proposal from 2011-07-25.

This proposal has been superseded by a proposal from 2011-07-27.

Description of the change

This branch gets the window button pixmaps from the active window manager theme instead of using the hardcoded Ambiance theme. It also provides a fallback in case the theme is not Ambiance or Radiance.

To post a comment you must log in.
700. By Aurélien Gâteau

Sync with unity-2d/4.0

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'panel/applets/appname/appnameapplet.cpp'
2--- panel/applets/appname/appnameapplet.cpp 2011-07-27 15:51:45 +0000
3+++ panel/applets/appname/appnameapplet.cpp 2011-07-27 15:51:45 +0000
4@@ -25,6 +25,7 @@
5 // Local
6 #include "croppedlabel.h"
7 #include "menubarwidget.h"
8+#include "panelstyle.h"
9 #include "windowhelper.h"
10
11 // Unity-2d
12@@ -46,8 +47,6 @@
13 #include <QApplication>
14 #include <QDesktopWidget>
15
16-static const char* METACITY_DIR = "/usr/share/themes/Ambiance/metacity-1";
17-
18 static const int WINDOW_BUTTONS_RIGHT_MARGIN = 4;
19
20 static const int APPNAME_LABEL_LEFT_MARGIN = 6;
21@@ -58,13 +57,11 @@
22 class WindowButton : public QAbstractButton
23 {
24 public:
25- WindowButton(const QString& prefix, QWidget* parent = 0)
26+ WindowButton(const PanelStyle::WindowButtonType& buttonType, QWidget* parent = 0)
27 : QAbstractButton(parent)
28- , m_prefix(prefix)
29- , m_normalPix(loadPix("normal"))
30- , m_hoverPix(loadPix("prelight"))
31- , m_downPix(loadPix("pressed"))
32+ , m_buttonType(buttonType)
33 {
34+ loadPixmaps();
35 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
36 setAttribute(Qt::WA_Hover);
37 }
38@@ -75,6 +72,14 @@
39 }
40
41 protected:
42+ bool event(QEvent* ev)
43+ {
44+ if (ev->type() == QEvent::PaletteChange) {
45+ loadPixmaps();
46+ }
47+ return QAbstractButton::event(ev);
48+ }
49+
50 void paintEvent(QPaintEvent*)
51 {
52 QPainter painter(this);
53@@ -90,18 +95,17 @@
54 }
55
56 private:
57- QString m_prefix;
58+ PanelStyle::WindowButtonType m_buttonType;
59 QPixmap m_normalPix;
60 QPixmap m_hoverPix;
61 QPixmap m_downPix;
62
63- QPixmap loadPix(const QString& name)
64+ void loadPixmaps()
65 {
66- QString path = QString("%1/%2_focused_%3.png")
67- .arg(METACITY_DIR)
68- .arg(m_prefix)
69- .arg(name);
70- return QPixmap(path);
71+ PanelStyle* style = PanelStyle::instance();
72+ m_normalPix = style->windowButtonPixmap(m_buttonType, PanelStyle::NormalState);
73+ m_hoverPix = style->windowButtonPixmap(m_buttonType, PanelStyle::PrelightState);
74+ m_downPix = style->windowButtonPixmap(m_buttonType, PanelStyle::PressedState);
75 }
76 };
77
78@@ -135,9 +139,9 @@
79 QHBoxLayout* layout = new QHBoxLayout(m_windowButtonWidget);
80 layout->setContentsMargins(0, 0, WINDOW_BUTTONS_RIGHT_MARGIN, 0);
81 layout->setSpacing(0);
82- m_closeButton = new WindowButton("close");
83- m_minimizeButton = new WindowButton("minimize");
84- m_maximizeButton = new WindowButton("unmaximize");
85+ m_closeButton = new WindowButton(PanelStyle::CloseWindowButton);
86+ m_minimizeButton = new WindowButton(PanelStyle::MinimizeWindowButton);
87+ m_maximizeButton = new WindowButton(PanelStyle::UnmaximizeWindowButton);
88 layout->addWidget(m_closeButton);
89 layout->addWidget(m_minimizeButton);
90 layout->addWidget(m_maximizeButton);
91
92=== modified file 'panel/applets/common/panelstyle.cpp'
93--- panel/applets/common/panelstyle.cpp 2011-07-27 15:51:45 +0000
94+++ panel/applets/common/panelstyle.cpp 2011-07-27 15:51:45 +0000
95@@ -30,10 +30,13 @@
96 // Qt
97 #include <QApplication>
98 #include <QPalette>
99+#include <QStyle>
100
101 // GTK
102 #include <gtk/gtk.h>
103
104+static const char* METACITY_THEME_DIR = "/usr/share/themes/%1/metacity-1";
105+
106 class PanelStylePrivate
107 {
108 public:
109@@ -41,6 +44,8 @@
110 GObjectScopedPointer<GtkStyleContext> m_styleContext;
111 GConnector m_gConnector;
112
113+ QString m_themeName;
114+
115 static void onThemeChanged(GObject*, GParamSpec*, gpointer data)
116 {
117 PanelStylePrivate* priv = reinterpret_cast<PanelStylePrivate*>(data);
118@@ -49,6 +54,11 @@
119
120 void updatePalette()
121 {
122+ gchar* themeName = 0;
123+ g_object_get(gtk_settings_get_default(), "gtk-theme-name", &themeName, NULL);
124+ m_themeName = QString::fromUtf8(themeName);
125+ g_free(themeName);
126+
127 GtkStyleContext* context = m_styleContext.data();
128 gtk_style_context_invalidate(context);
129
130@@ -71,6 +81,71 @@
131 gtk_render_background(m_styleContext.data(), cr.data(), 0, 0, image.width(), image.height());
132 return QBrush(image);
133 }
134+
135+ QPixmap windowButtonPixmapFromWMTheme(PanelStyle::WindowButtonType type, PanelStyle::WindowButtonState state)
136+ {
137+ QString dir = QString(METACITY_THEME_DIR).arg(m_themeName);
138+
139+ QString typeString, stateString;
140+ switch (type) {
141+ case PanelStyle::CloseWindowButton:
142+ typeString = "close";
143+ break;
144+ case PanelStyle::MinimizeWindowButton:
145+ typeString = "minimize";
146+ break;
147+ case PanelStyle::UnmaximizeWindowButton:
148+ typeString = "unmaximize";
149+ break;
150+ }
151+
152+ switch (state) {
153+ case PanelStyle::NormalState:
154+ stateString = "";
155+ break;
156+ case PanelStyle::PrelightState:
157+ stateString = "_focused_prelight";
158+ break;
159+ case PanelStyle::PressedState:
160+ stateString = "_focused_pressed";
161+ break;
162+ }
163+
164+ QString path = QString("%1/%2%3.png")
165+ .arg(dir)
166+ .arg(typeString)
167+ .arg(stateString);
168+ return QPixmap(path);
169+ }
170+
171+ QPixmap genericWindowButtonPixmap(PanelStyle::WindowButtonType type, PanelStyle::WindowButtonState state)
172+ {
173+ QStyle::StandardPixmap standardIcon;
174+ switch (type) {
175+ case PanelStyle::CloseWindowButton:
176+ standardIcon = QStyle::SP_TitleBarCloseButton;
177+ break;
178+ case PanelStyle::MinimizeWindowButton:
179+ standardIcon = QStyle::SP_TitleBarMinButton;
180+ break;
181+ case PanelStyle::UnmaximizeWindowButton:
182+ standardIcon = QStyle::SP_TitleBarNormalButton;
183+ break;
184+ }
185+
186+ QIcon icon = QApplication::style()->standardIcon(standardIcon);
187+ const int extent = 22;
188+ switch (state) {
189+ case PanelStyle::NormalState:
190+ return icon.pixmap(extent);
191+ case PanelStyle::PrelightState:
192+ return icon.pixmap(extent, QIcon::Active);
193+ case PanelStyle::PressedState:
194+ return icon.pixmap(extent, QIcon::Active, QIcon::On);
195+ }
196+ // Silence compiler
197+ return QPixmap();
198+ }
199 };
200
201 PanelStyle::PanelStyle(QObject* parent)
202@@ -111,4 +186,16 @@
203 return d->m_styleContext.data();
204 }
205
206+QPixmap PanelStyle::windowButtonPixmap(PanelStyle::WindowButtonType type, PanelStyle::WindowButtonState state)
207+{
208+ // According to Unity PanelStyle code, the buttons of some WM themes do not
209+ // match well with the panel background. So except for themes we provide,
210+ // fallback to generic button pixmaps.
211+ if (d->m_themeName == "Ambiance" || d->m_themeName == "Radiance") {
212+ return d->windowButtonPixmapFromWMTheme(type, state);
213+ } else {
214+ return d->genericWindowButtonPixmap(type, state);
215+ }
216+}
217+
218 #include "panelstyle.moc"
219
220=== modified file 'panel/applets/common/panelstyle.h'
221--- panel/applets/common/panelstyle.h 2011-07-27 15:51:45 +0000
222+++ panel/applets/common/panelstyle.h 2011-07-27 15:51:45 +0000
223@@ -26,6 +26,8 @@
224 // Qt
225 #include <QObject>
226
227+class QPixmap;
228+
229 struct _GtkStyleContext;
230
231 class PanelStylePrivate;
232@@ -43,10 +45,24 @@
233 PanelStyle(QObject* parent = 0);
234 ~PanelStyle();
235
236+ enum WindowButtonType {
237+ CloseWindowButton,
238+ MinimizeWindowButton,
239+ UnmaximizeWindowButton
240+ };
241+
242+ enum WindowButtonState {
243+ NormalState,
244+ PrelightState,
245+ PressedState
246+ };
247+
248 static PanelStyle* instance();
249
250 struct _GtkStyleContext* styleContext() const;
251
252+ QPixmap windowButtonPixmap(WindowButtonType, WindowButtonState);
253+
254 private:
255 friend class PanelStylePrivate;
256 // Use a pimpl to avoid the need for gtk includes here

Subscribers

People subscribed via source and target branches