~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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2011 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: Neil Jagdish Patel <neil.patel@canonical.com>
 */

#include "SearchBarSpinner.h"

#include <Nux/VLayout.h>

#include "unity-shared/DashStyle.h"
#include "unity-shared/RawPixel.h"

namespace unity
{

NUX_IMPLEMENT_OBJECT_TYPE(SearchBarSpinner);

SearchBarSpinner::SearchBarSpinner()
  : nux::View(NUX_TRACKER_LOCATION)
  , scale(1.0)
  , state_(STATE_READY)
  , search_timeout_(-1)
  , rotation_(0.0f)
{
  rotate_.Identity();
  rotate_.Rotate_z(0.0);
  UpdateScale(scale);

  scale.changed.connect(sigc::mem_fun(this, &SearchBarSpinner::UpdateScale));
}

void SearchBarSpinner::UpdateScale(double scale)
{
  auto& style = dash::Style::Instance();

  magnify_ = style.GetSearchMagnifyIcon(scale);
  circle_ = style.GetSearchCircleIcon(scale);
  close_ = style.GetSearchCloseIcon(scale);
  spin_ = style.GetSearchSpinIcon(scale);

  SetMinMaxSize(magnify_->GetWidth(), magnify_->GetHeight());
  QueueDraw();
}

void SearchBarSpinner::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
{
  nux::Geometry const& geo = GetGeometry();
  nux::TexCoordXForm texxform;

  GfxContext.PushClippingRectangle(geo);

  nux::GetPainter().PaintBackground(GfxContext, geo);

  texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
  texxform.SetWrap(nux::TEXWRAP_CLAMP_TO_BORDER, nux::TEXWRAP_CLAMP_TO_BORDER);
  texxform.SetFilter(nux::TEXFILTER_LINEAR, nux::TEXFILTER_LINEAR);

  unsigned int current_alpha_blend;
  unsigned int current_src_blend_factor;
  unsigned int current_dest_blend_factor;
  GfxContext.GetRenderStates().GetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
  GfxContext.GetRenderStates().SetBlend(true,  GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

  if (state_ == STATE_READY)
  {
    nux::Size magnifier_size(magnify_->GetWidth(), magnify_->GetHeight());

    GfxContext.QRP_1Tex(geo.x + ((geo.width - magnifier_size.width) / 2),
                        geo.y + ((geo.height - magnifier_size.height) / 2),
                        magnifier_size.width,
                        magnifier_size.height,
                        magnify_->GetDeviceTexture(),
                        texxform,
                        nux::color::White);
  }
  else if (state_ == STATE_SEARCHING)
  {
    nux::Size spin_size(spin_->GetWidth(), spin_->GetHeight());
    nux::Geometry spin_geo(geo.x + ((geo.width - spin_size.width) / 2),
                           geo.y + ((geo.height - spin_size.height) / 2),
                           spin_size.width,
                           spin_size.height);
    // Geometry (== Rect) uses integers which were rounded above,
    // hence an extra 0.5 offset for odd sizes is needed
    // because pure floating point is not being used.
    int spin_offset_w = !(geo.width % 2) ? 0 : 1;
    int spin_offset_h = !(geo.height % 2) ? 0 : 1;

    nux::Matrix4 matrix_texture;
    matrix_texture = nux::Matrix4::TRANSLATE(-spin_geo.x - (spin_geo.width + spin_offset_w) / 2.0f,
                                          -spin_geo.y - (spin_geo.height + spin_offset_h) / 2.0f, 0) * matrix_texture;
    matrix_texture = rotate_ * matrix_texture;
    matrix_texture = nux::Matrix4::TRANSLATE(spin_geo.x + (spin_geo.width + spin_offset_w) / 2.0f,
                                             spin_geo.y + (spin_geo.height + spin_offset_h) / 2.0f, 0) * matrix_texture;

    GfxContext.SetModelViewMatrix(GfxContext.GetModelViewMatrix() * matrix_texture);

    GfxContext.QRP_1Tex(spin_geo.x,
                        spin_geo.y,
                        spin_geo.width,
                        spin_geo.height,
                        spin_->GetDeviceTexture(),
                        texxform,
                        nux::color::White);

    // revert to model view matrix stack
    GfxContext.ApplyModelViewMatrix();
  }
  else
  {
    nux::Size circle_size(circle_->GetWidth(), circle_->GetHeight());
    GfxContext.QRP_1Tex(geo.x + ((geo.width - circle_size.width) / 2),
                        geo.y + ((geo.height - circle_size.height) / 2),
                        circle_size.width,
                        circle_size.height,
                        circle_->GetDeviceTexture(),
                        texxform,
                        nux::color::White);

    nux::Size close_size(close_->GetWidth(), close_->GetHeight());
    GfxContext.QRP_1Tex(geo.x + ((geo.width - close_size.width) / 2),
                        geo.y + ((geo.height - close_size.height) / 2),
                        close_size.width,
                        close_size.height,
                        close_->GetDeviceTexture(),
                        texxform,
                        nux::color::White);
  }
  GfxContext.PopClippingRectangle();

  GfxContext.GetRenderStates().SetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);

  if (state_ == STATE_SEARCHING && !frame_timeout_)
  {
    frame_timeout_.reset(new glib::Timeout(22, sigc::mem_fun(this, &SearchBarSpinner::OnFrameTimeout)));
  }
}

void
SearchBarSpinner::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw)
{
}

bool
SearchBarSpinner::OnFrameTimeout()
{
  rotation_ += 0.1f;

  if (rotation_ >= 360.0f)
    rotation_ = 0.0f;

  rotate_.Rotate_z(rotation_);
  QueueDraw();

  frame_timeout_.reset();
  return false;
}

void
SearchBarSpinner::SetSpinnerTimeout(int timeout)
{
  search_timeout_ = timeout;
}

void
SearchBarSpinner::SetState(SpinnerState state)
{
  if (state_ == state)
    return;

  state_ = state;
  spinner_timeout_.reset();
  rotate_.Rotate_z(0.0f);
  rotation_ = 0.0f;

  if (search_timeout_ > 0 && state_== STATE_SEARCHING)
  {
    spinner_timeout_.reset(new glib::Timeout(search_timeout_, [this] {
      state_ = STATE_READY;
      return false;
    }));
  }

  QueueDraw();
}

SpinnerState
SearchBarSpinner::GetState() const
{
  return state_;
}

std::string
SearchBarSpinner::GetName() const
{
  return "SearchBarSpinner";
}

void SearchBarSpinner::AddProperties(debug::IntrospectionData& introspection)
{
  introspection.add(GetAbsoluteGeometry());
}

//
// Key navigation
//
bool
SearchBarSpinner::AcceptKeyNavFocus()
{
  return false;
}

}