~unity-team/unity/trunk

« back to all changes in this revision

Viewing changes to unity-shared/DecorationStyle.cpp

  • Committer: CI Train Bot
  • Author(s): Marco Trevisan (Treviño)
  • Date: 2016-03-10 10:09:06 UTC
  • mfrom: (4068.4.35 theme-settings)
  • Revision ID: ci-train-bot@canonical.com-20160310100906-zjsf612jq8szf4vp
ThemeSettings: add small class for reading gtk settings for theming

Implemented through a new GtkUtils class which includes some functions
and classes to handle Gtk elements.

The ThemeSettings utility class allows to read settings and being notified when
these changes. All in a single place. It also includes some methods

As bonus, some improvements to the dash theming (dash-widgets.json is now
themable and supports new scrollbars as well), and BFB icon can be overridden
as well.

More infos at: https://wiki.ubuntu.com/Unity/Theming Fixes: #1288998, #1543757
Approved by: Andrea Azzarone, PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include "config.h"
21
21
#include "DecorationStyle.h"
22
 
#include <gtk/gtk.h>
 
22
 
 
23
#include <math.h>
23
24
#include <NuxCore/Colors.h>
24
25
#include <NuxCore/Logger.h>
 
26
#include <UnityCore/ConnectionManager.h>
25
27
#include <UnityCore/GLibWrapper.h>
26
28
#include <UnityCore/GLibSignal.h>
27
 
#include <math.h>
 
29
 
 
30
#include "GtkUtils.h"
 
31
#include "ThemeSettings.h"
28
32
 
29
33
namespace unity
30
34
{
53
57
const int DEFAULT_GLOW_SIZE = 10;
54
58
const nux::Color DEFAULT_GLOW_COLOR(221, 72, 20);
55
59
 
56
 
const std::array<std::string, 2> THEMED_FILE_EXTENSIONS = { "svg", "png" };
57
60
const std::array<std::string, size_t(WindowButtonType::Size)> WBUTTON_NAMES = { "close", "minimize", "unmaximize", "maximize" };
58
61
const std::array<std::string, size_t(WidgetState::Size)> WBUTTON_STATES = {"", "_focused_prelight", "_focused_pressed", "_unfocused",
59
62
                                                                           "_unfocused", "_unfocused_prelight", "_unfocused_pressed" };
157
160
    gtk_widget_path_append_type(widget_path.get(), unity_decoration_get_type());
158
161
    gtk_style_context_set_path(ctx_, widget_path.get());
159
162
 
160
 
    parent_->theme = glib::String(GetSettingValue<gchar*>("gtk-theme-name")).Str();
161
 
    parent_->font = glib::String(GetSettingValue<gchar*>("gtk-font-name")).Str();
 
163
    auto theme_settings = theme::Settings::Get();
 
164
    parent_->theme.SetGetterFunction([theme_settings] { return theme_settings->theme(); });
 
165
    parent_->font.SetGetterFunction([theme_settings] { return theme_settings->font(); });
162
166
    parent_->font_scale = 1.0;
163
167
    SetTitleFont();
164
168
 
166
170
    UpdateMenuItemPangoContext(parent_->font);
167
171
    UpdateThemedValues();
168
172
 
169
 
    GtkSettings* settings = gtk_settings_get_default();
170
 
    signals_.Add<void, GtkSettings*, GParamSpec*>(settings, "notify::gtk-theme-name", [this] (GtkSettings*, GParamSpec*) {
 
173
    connections_.Add(theme_settings->theme.changed.connect([this] (std::string const& theme) {
171
174
#if !GTK_CHECK_VERSION(3, 11, 0)
172
175
      gtk_style_context_invalidate(ctx_);
173
176
#endif
174
177
      UpdateThemedValues();
175
 
      parent_->theme = glib::String(GetSettingValue<gchar*>("gtk-theme-name")).Str();
176
 
      LOG_INFO(logger) << "gtk-theme-name changed to " << parent_->theme();
177
 
    });
 
178
      parent_->theme.EmitChanged(theme);
 
179
      LOG_INFO(logger) << "unity theme changed to " << parent_->theme();
 
180
    }));
178
181
 
179
 
    signals_.Add<void, GtkSettings*, GParamSpec*>(settings, "notify::gtk-font-name", [this] (GtkSettings*, GParamSpec*) {
180
 
      auto const& font = glib::String(GetSettingValue<gchar*>("gtk-font-name")).Str();
 
182
    connections_.Add(theme_settings->font.changed.connect([this] (std::string const& font) {
181
183
      UpdateMenuItemPangoContext(font);
182
 
      parent_->font = font;
 
184
      parent_->font.EmitChanged(font);
183
185
 
184
186
      if (g_settings_get_boolean(settings_, USE_SYSTEM_FONT_KEY.c_str()))
185
187
      {
186
188
        UpdateTitlePangoContext(parent_->font());
187
189
        parent_->title_font = parent_->font();
188
190
      }
189
 
 
190
 
      LOG_INFO(logger) << "gtk-font-name changed to " << parent_->font();
191
 
    });
 
191
      LOG_INFO(logger) << "unity font changed to " << parent_->font();
 
192
    }));
192
193
 
193
194
    parent_->font_scale.changed.connect([this] (bool scale) {
194
195
      UpdateTitlePangoContext(parent_->title_font);
297
298
    return value;
298
299
  }
299
300
 
300
 
  template <typename TYPE>
301
 
  inline TYPE GetSettingValue(std::string const& name)
302
 
  {
303
 
    TYPE value;
304
 
    g_object_get(gtk_settings_get_default(), name.c_str(), &value, nullptr);
305
 
    return value;
306
 
  }
307
 
 
308
301
  WMAction WMActionFromString(std::string const& action) const
309
302
  {
310
303
    if (action == "toggle-shade")
391
384
    gtk_style_context_restore(ctx_);
392
385
  }
393
386
 
394
 
  std::string ThemedFilePath(std::string const& base_filename, std::vector<std::string> const& extra_folders = {}) const
395
 
  {
396
 
    auto const& theme = parent_->theme();
397
 
    const char* home_dir = g_get_home_dir();
398
 
    const char* gtk_prefix = g_getenv("GTK_DATA_PREFIX");
399
 
    if (!gtk_prefix)
400
 
      gtk_prefix = GTK_PREFIX;
401
 
 
402
 
    for (auto const& extension : THEMED_FILE_EXTENSIONS)
403
 
    {
404
 
      auto filename = base_filename + '.' + extension;
405
 
      glib::String subpath(g_build_filename(theme.c_str(), "unity", filename.c_str(), nullptr));
406
 
 
407
 
      // Look in home directory
408
 
      if (home_dir)
409
 
      {
410
 
        glib::String local_file(g_build_filename(home_dir, ".local", "share", "themes", subpath.Value(), nullptr));
411
 
 
412
 
        if (g_file_test(local_file, G_FILE_TEST_EXISTS))
413
 
          return local_file.Str();
414
 
 
415
 
        glib::String home_file(g_build_filename(home_dir, ".themes", subpath.Value(), nullptr));
416
 
 
417
 
        if (g_file_test(home_file, G_FILE_TEST_EXISTS))
418
 
          return home_file.Str();
419
 
      }
420
 
 
421
 
      glib::String path(g_build_filename(gtk_prefix, "share", "themes", subpath.Value(), nullptr));
422
 
 
423
 
      if (g_file_test(path, G_FILE_TEST_EXISTS))
424
 
        return path.Str();
425
 
 
426
 
      for (auto const& folder : extra_folders)
427
 
      {
428
 
        glib::String path(g_build_filename(folder.c_str(), filename.c_str(), nullptr));
429
 
 
430
 
        if (g_file_test(path, G_FILE_TEST_EXISTS))
431
 
          return path.Str();
432
 
      }
433
 
    }
434
 
 
435
 
    return std::string();
436
 
  }
437
 
 
438
387
  std::string WindowButtonFile(WindowButtonType type, WidgetState state) const
439
388
  {
440
389
    auto base_filename = WBUTTON_NAMES[unsigned(type)] + WBUTTON_STATES[unsigned(state)];
441
 
    auto const& file_path = ThemedFilePath(base_filename);
 
390
    auto const& file_path = parent_->ThemedFilePath(base_filename);
442
391
 
443
392
    if (!file_path.empty())
444
393
      return file_path;
677
626
  glib::Object<GSettings> usettings_;
678
627
  glib::Object<PangoContext> title_pango_ctx_;
679
628
  glib::Object<PangoContext> menu_item_pango_ctx_;
 
629
  connection::Manager connections_;
680
630
  decoration::Border border_;
681
631
  decoration::Border input_edges_;
682
632
  decoration::Border radius_;
751
701
 
752
702
std::string Style::ThemedFilePath(std::string const& basename, std::vector<std::string> const& extra_folders) const
753
703
{
754
 
  return impl_->ThemedFilePath(basename, extra_folders);
 
704
  return theme::Settings::Get()->ThemedFilePath(basename, extra_folders);
755
705
}
756
706
 
757
707
std::string Style::WindowButtonFile(WindowButtonType type, WidgetState state) const
832
782
 
833
783
int Style::DoubleClickMaxDistance() const
834
784
{
835
 
  return impl_->GetSettingValue<int>("gtk-double-click-distance");
 
785
  return gtk::GetSettingValue<int>("gtk-double-click-distance");
836
786
}
837
787
 
838
788
int Style::DoubleClickMaxTimeDelta() const
839
789
{
840
 
  return impl_->GetSettingValue<int>("gtk-double-click-time");
 
790
  return gtk::GetSettingValue<int>("gtk-double-click-time");
841
791
}
842
792
 
843
793
nux::Size Style::TitleNaturalSize(std::string const& text)