Skip to content

Commit 59a8f0d

Browse files
authored
Merge pull request #77 from diamondburned/add/css-path
Add {panel,dock}/css_path and custom CSS support
2 parents 3fed052 + 12d47f1 commit 59a8f0d

6 files changed

Lines changed: 62 additions & 0 deletions

File tree

metadata/dock.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
<plugin name="dock">
44
<_short>Dock</_short>
55
<category>Shell</category>
6+
<option name="css_path" type="string">
7+
<_short>CSS Path</_short>
8+
<default></default>
9+
</option>
610
<option name="autohide_duration" type="int">
711
<default>300</default>
812
</option>

metadata/panel.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<category>Shell</category>
66
<group>
77
<_short>General</_short>
8+
<option name="css_path" type="string">
9+
<_short>CSS Path</_short>
10+
<default></default>
11+
</option>
812
<option name="widgets_left" type="string">
913
<_short>Widgets Left</_short>
1014
<default>spacing4 menu spacing18 launchers</default>

src/dock/dock.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <wf-autohide-window.hpp>
1212

1313
#include "dock.hpp"
14+
#include "../util/gtk-utils.hpp"
1415

1516
class WfDock::impl
1617
{
@@ -20,6 +21,8 @@ class WfDock::impl
2021

2122
Gtk::HBox box;
2223

24+
WfOption<std::string> css_path{"dock/css_path"};
25+
2326
public:
2427
impl(WayfireOutput *output)
2528
{
@@ -34,6 +37,19 @@ class WfDock::impl
3437
window->signal_size_allocate().connect_notify(
3538
sigc::mem_fun(this, &WfDock::impl::on_allocation));
3639
window->add(box);
40+
41+
if ((std::string)css_path != "")
42+
{
43+
auto css = load_css_from_path(css_path);
44+
if (css)
45+
{
46+
auto screen = Gdk::Screen::get_default();
47+
auto style_context = Gtk::StyleContext::create();
48+
style_context->add_provider_for_screen(
49+
screen, css, GTK_STYLE_PROVIDER_PRIORITY_USER);
50+
}
51+
}
52+
3753
window->show_all();
3854
_wl_surface = gdk_wayland_window_get_wl_surface(
3955
window->get_window()->gobj());

src/panel/panel.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <map>
1414

1515
#include "panel.hpp"
16+
#include "../util/gtk-utils.hpp"
1617

1718
#include "widgets/battery.hpp"
1819
#include "widgets/menu.hpp"
@@ -131,6 +132,7 @@ class WayfirePanel::impl
131132
};
132133

133134
WfOption<int> minimal_panel_height{"panel/minimal_height"};
135+
WfOption<std::string> css_path{"panel/css_path"};
134136

135137
void create_window()
136138
{
@@ -148,6 +150,18 @@ class WayfirePanel::impl
148150
autohide_opt.set_callback(autohide_opt_updated);
149151
autohide_opt_updated(); // set initial autohide status
150152

153+
if ((std::string)css_path != "")
154+
{
155+
auto css = load_css_from_path(css_path);
156+
if (css)
157+
{
158+
auto screen = Gdk::Screen::get_default();
159+
auto style_context = Gtk::StyleContext::create();
160+
style_context->add_provider_for_screen(
161+
screen, css, GTK_STYLE_PROVIDER_PRIORITY_USER);
162+
}
163+
}
164+
151165
window->show_all();
152166
init_widgets();
153167
init_layout();

src/util/gtk-utils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ Glib::RefPtr<Gdk::Pixbuf> load_icon_pixbuf_safe(std::string icon_path, int size)
2828
}
2929
}
3030

31+
Glib::RefPtr<Gtk::CssProvider> load_css_from_path(std::string path)
32+
{
33+
try
34+
{
35+
auto css = Gtk::CssProvider::create();
36+
css->load_from_path(path);
37+
return css;
38+
}
39+
catch(Glib::Error& err)
40+
{
41+
std::cerr << "Failed to load CSS: " << err.what() << std::endl;
42+
return {};
43+
}
44+
catch(...)
45+
{
46+
std::cerr << "Failed to load CSS at: " << path << std::endl;
47+
return {};
48+
}
49+
}
50+
3151
void invert_pixbuf(Glib::RefPtr<Gdk::Pixbuf>& pbuff)
3252
{
3353
int channels = pbuff->get_n_channels();

src/util/gtk-utils.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
#define WF_GTK_UTILS
33

44
#include <gtkmm/image.h>
5+
#include <gtkmm/cssprovider.h>
56
#include <string>
67

78
/* Loads a pixbuf with the given size from the given file, returns null if unsuccessful */
89
Glib::RefPtr<Gdk::Pixbuf> load_icon_pixbuf_safe(std::string icon_path, int size);
910

11+
/* Loads a CssProvider from the given path to the file, returns null if unsuccessful*/
12+
Glib::RefPtr<Gtk::CssProvider> load_css_from_path(std::string path);
13+
1014
struct WfIconLoadOptions
1115
{
1216
int user_scale = -1;

0 commit comments

Comments
 (0)