~brandontschaefer/unity/bump-to-new-nux-abi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2012-2013 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * 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/>.
 *
 * Authored by: Jason Smith <jason.smith@canonical.com>
 *              Marco Trevisan <marco.trevisan@canonical.com>
 *              Brandon Schaefer <brandon.schaefer@canonical.com>
 */

#include <NuxCore/Logger.h>

#include "UnitySettings.h"
#include "UnityWindowStyle.h"
#include "UScreen.h"
#include "config.h"

#include <unordered_set>

namespace unity
{
namespace ui
{
namespace
{
  const char* const SWITCHER_TOP    = PKGDATADIR"/switcher_top.png";
  const char* const SWITCHER_LEFT   = PKGDATADIR"/switcher_left.png";
  const char* const SWITCHER_CORNER = PKGDATADIR"/switcher_corner.png";

  const char* const DIALOG_CLOSE     = PKGDATADIR"/dialog_close.png";
  const char* const DIALOG_HIGHLIGHT = PKGDATADIR"/dialog_close_highlight.png";
  const char* const DIALOG_PRESS     = PKGDATADIR"/dialog_close_press.png";


  RawPixel const INTERNAL_OFFSET = 20_em;
  RawPixel const BORDER_SIZE     = 30_em;
  RawPixel const CLOSE_PADDING   =  3_em;
}

DECLARE_LOGGER(logger, "unity.ui.unity.window.style");


UnityWindowStyle::UnityWindowStyle()
{
  unsigned monitors = UScreen::GetDefault()->GetPluggedMonitorsNumber();
  auto& settings = Settings::Instance();

  // Pre-load scale values per monitor
  for (unsigned i = 0; i < monitors; ++i)
  {
    double scale = settings.Instance().em(i)->DPIScale();

    if (unity_window_textures_.find(scale) == unity_window_textures_.end())
      LoadAllTextureInScale(scale);
  }

  settings.Instance().dpi_changed.connect(sigc::mem_fun(this, &UnityWindowStyle::CleanUpUnusedTextures));
  UScreen::GetDefault()->changed.connect(sigc::mem_fun(this, &UnityWindowStyle::OnMonitorChanged));
}

void UnityWindowStyle::LoadAllTextureInScale(double scale)
{
  auto& window_textures = unity_window_textures_[scale];

  window_textures[unsigned(WindowTextureType::BACKGROUND_TOP)]    = LoadTexture(scale, SWITCHER_TOP);
  window_textures[unsigned(WindowTextureType::BACKGROUND_LEFT)]   = LoadTexture(scale, SWITCHER_LEFT);
  window_textures[unsigned(WindowTextureType::BACKGROUND_CORNER)] = LoadTexture(scale, SWITCHER_CORNER);

  window_textures[unsigned(WindowTextureType::CLOSE_ICON)]             = LoadTexture(scale, DIALOG_CLOSE);
  window_textures[unsigned(WindowTextureType::CLOSE_ICON_HIGHLIGHTED)] = LoadTexture(scale, DIALOG_HIGHLIGHT);
  window_textures[unsigned(WindowTextureType::CLOSE_ICON_PRESSED)]     = LoadTexture(scale, DIALOG_PRESS);
}

nux::BaseTexture* UnityWindowStyle::LoadTexture(double scale, const char* const texture_name) const
{
  RawPixel max_size = GetDefaultMaxTextureSize(texture_name);
  return nux::CreateTexture2DFromFile(texture_name, max_size.CP(scale), true);
}

RawPixel UnityWindowStyle::GetDefaultMaxTextureSize(const char* const texture_name) const
{
  nux::Size size;
  gdk_pixbuf_get_file_info(texture_name, &size.width, &size.height);
  RawPixel max_size = std::max(std::round(size.width), std::round(size.height));

  return max_size;
}

void UnityWindowStyle::OnMonitorChanged(int primary, std::vector<nux::Geometry> const& monitors)
{
  CleanUpUnusedTextures();
}

// Get current in use scale values, if a scaled value is allocated, but
// not in use clean up the scaled textures in unity_window_textures
void UnityWindowStyle::CleanUpUnusedTextures()
{
  unsigned monitors = UScreen::GetDefault()->GetPluggedMonitorsNumber();
  auto& settings = Settings::Instance();
  std::unordered_set<double> used_scales;

  for (unsigned i = 0; i < monitors; ++i)
    used_scales.insert(settings.em(i)->DPIScale());

  for (auto it = unity_window_textures_.begin(); it != unity_window_textures_.end();)
  {
    if (used_scales.find(it->first) == used_scales.end())
    {
      it = unity_window_textures_.erase(it);
    }
    else
    {
      ++it;
    }
  }
}

UnityWindowStyle::Ptr const& UnityWindowStyle::Get()
{
  // This is set only the first time;
  static UnityWindowStyle::Ptr instance(new UnityWindowStyle());
  return instance;
}

int UnityWindowStyle::GetBorderSize(double scale) const
{
  return BORDER_SIZE.CP(scale); // as measured from textures
}

int UnityWindowStyle::GetInternalOffset(double scale) const
{
  return INTERNAL_OFFSET.CP(scale);
}

int UnityWindowStyle::GetCloseButtonPadding(double scale) const
{
  return CLOSE_PADDING.CP(scale);
}

UnityWindowStyle::BaseTexturePtr UnityWindowStyle::GetTexture(double scale, WindowTextureType const& type)
{
  auto it = unity_window_textures_.find(scale);
  if (it == unity_window_textures_.end())
  {
    LoadAllTextureInScale(scale);

    it = unity_window_textures_.find(scale);
    if (it == unity_window_textures_.end())
    {
      LOG_ERROR(logger) << "Failed to create unity window style textures, for scale size: " << scale;
      return BaseTexturePtr(nullptr);
    }
  }

  return it->second[unsigned(type)];
}

} // namespace ui
} // namespace unity