~mial/ubuntu/oneiric/unity/bug-791810

« back to all changes in this revision

Viewing changes to unity-private/places/places-view.vala

Import the work done so far with Compiz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by Mirco "MacSlow" Müller <mirco.mueller@canonical.com>
 
17
 *             Neil Jagdish Patel <neil.patel@canonical.com>
 
18
 *
 
19
 */
 
20
using Unity;
 
21
using Unity.Testing;
 
22
 
 
23
namespace Unity.Places
 
24
{
 
25
  public class View : Ctk.Box
 
26
  {
 
27
    public static const float PADDING = 8.0f;
 
28
    /* Properties */
 
29
    public Shell shell { get; construct; }
 
30
 
 
31
    public PlaceModel model { get; construct; }
 
32
 
 
33
    private Ctk.VBox   content_box;
 
34
    private LayeredBin layered_bin;
 
35
 
 
36
    public  PlaceEntry            home_entry;
 
37
    public  PlaceSearchBar        search_bar;
 
38
    private Unity.Place.Renderer? renderer;
 
39
    private unowned PlaceEntry?   active_entry = null;
 
40
 
 
41
    private bool is_showing = false;
 
42
 
 
43
    public View (Shell shell, PlaceModel model)
 
44
    {
 
45
      Object (shell:shell, orientation:Ctk.Orientation.VERTICAL, model:model);
 
46
 
 
47
      Testing.ObjectRegistry.get_default ().register ("UnityPlacesView", this);
 
48
    }
 
49
 
 
50
    construct
 
51
    {
 
52
      about_to_show ();
 
53
 
 
54
      shell.get_stage ().captured_event.connect (on_stage_event_captured);
 
55
 
 
56
 
 
57
    }
 
58
 
 
59
    public void about_to_show ()
 
60
    {
 
61
      if (home_entry is PlaceHomeEntry)
 
62
        {
 
63
          return;
 
64
        }
 
65
 
 
66
      home_entry = new PlaceHomeEntry (shell, model);
 
67
 
 
68
      content_box = new Ctk.VBox (4);
 
69
      content_box.padding = {
 
70
        PADDING * 2.5f,
 
71
        PADDING,
 
72
        0.0f,
 
73
        PADDING
 
74
      };
 
75
      pack (content_box, true, true);
 
76
      content_box.show ();
 
77
 
 
78
      search_bar = new PlaceSearchBar ();
 
79
      content_box.pack (search_bar, false, true);
 
80
      search_bar.show ();
 
81
      search_bar.entry.text.captured_event.connect (on_stage_event_captured);
 
82
      search_bar.entry.text.activatable = true;
 
83
      search_bar.entry.text.activate.connect (() => {
 
84
        if (renderer is Clutter.Actor)
 
85
          {
 
86
            renderer.activate_default ();
 
87
          }
 
88
      });
 
89
 
 
90
      layered_bin = new LayeredBin ();
 
91
      content_box.pack (layered_bin, true, true);
 
92
      layered_bin.show ();
 
93
    }
 
94
 
 
95
    public void shown ()
 
96
    {
 
97
      is_showing = true;
 
98
 
 
99
      search_bar.reset ();
 
100
 
 
101
      on_entry_view_activated (home_entry, 0);
 
102
    }
 
103
 
 
104
    public void hidden ()
 
105
    {
 
106
      is_showing = false;
 
107
 
 
108
      if (active_entry is PlaceEntry)
 
109
        {
 
110
          active_entry.active = false;
 
111
        }
 
112
      active_entry = null;
 
113
      remove_current_renderer ();
 
114
    }
 
115
 
 
116
    public void on_entry_view_activated (PlaceEntry entry, uint section_id)
 
117
    {
 
118
      if (active_entry is PlaceEntry)
 
119
        {
 
120
          active_entry.active = false;
 
121
 
 
122
          if (active_entry != home_entry)
 
123
            active_entry.renderer_info_changed.disconnect (on_entry_renderer_info_changed);
 
124
        }
 
125
      active_entry = entry;
 
126
      entry.active = true;
 
127
 
 
128
      update_views (entry, section_id);
 
129
 
 
130
      entry.renderer_info_changed.connect (on_entry_renderer_info_changed);
 
131
    }
 
132
 
 
133
    /* Fade out previous results view if we had one, and set
 
134
     * this.renderer = null */
 
135
    private void remove_current_renderer ()
 
136
    {
 
137
      if (renderer is Clutter.Actor)
 
138
        {
 
139
          var anim = renderer.animate (Clutter.AnimationMode.EASE_OUT_QUAD,
 
140
                                       300,
 
141
                                       "opacity", 0);
 
142
          
 
143
          layered_bin.remove_actor (renderer);
 
144
          renderer = null;
 
145
        }
 
146
    }
 
147
 
 
148
    private void update_views (PlaceEntry entry, uint section_id=0)
 
149
    {
 
150
      search_bar.set_active_entry_view (entry, 0, section_id);
 
151
 
 
152
      remove_current_renderer ();
 
153
 
 
154
      /* Create the correct results view */
 
155
      renderer = lookup_renderer (entry);
 
156
 
 
157
      layered_bin.add_actor (renderer);
 
158
      renderer.opacity = 0;
 
159
      renderer.animate (Clutter.AnimationMode.EASE_OUT_QUAD, 300,
 
160
                        "opacity", 255);
 
161
      renderer.set_models (entry.entry_groups_model,
 
162
                           entry.entry_results_model,
 
163
                           entry.entry_renderer_hints);
 
164
      renderer.show ();
 
165
      renderer.activated.connect (on_result_activated);
 
166
    }
 
167
 
 
168
    private void on_entry_renderer_info_changed (PlaceEntry entry)
 
169
    {
 
170
      update_views (entry);
 
171
    }
 
172
 
 
173
    private Unity.Place.Renderer lookup_renderer (PlaceEntry entry)
 
174
    {
 
175
      string?      browser_path = null;
 
176
 
 
177
      /* FIXME: This is meant to be all automated, it's just we havent got
 
178
       * there just yet
 
179
       */
 
180
      if (entry.hints != null)
 
181
        {
 
182
          foreach (Gee.Map.Entry<string,string> e in entry.hints)
 
183
            {
 
184
              if (e.key == "UnityPlaceBrowserPath")
 
185
                browser_path = e.value;
 
186
            }
 
187
        }
 
188
 
 
189
      if (browser_path != null)
 
190
        return new FolderBrowserRenderer ();
 
191
      else if (entry.entry_renderer_name == "UnityHomeScreen")
 
192
        return new HomeRenderer ();
 
193
      else
 
194
        return new DefaultRenderer ();
 
195
    }
 
196
 
 
197
    private void on_result_activated (string uri, string mimetype)
 
198
    {
 
199
      ActivationStatus result;
 
200
 
 
201
      if (active_entry is PlaceHomeEntry)
 
202
        {
 
203
          var e = (active_entry as PlaceHomeEntry).get_entry_for_uri (uri);
 
204
 
 
205
          if (e is PlaceEntry)
 
206
            result = e.parent.activate (uri, mimetype);
 
207
          else
 
208
            {
 
209
              Place.activate_fallback.begin (uri);
 
210
              result = ActivationStatus.ACTIVATED_FALLBACK;
 
211
            }
 
212
        }
 
213
      else if (active_entry == null || active_entry.parent == null)
 
214
        {
 
215
          Place.activate_fallback.begin (uri);
 
216
          result = ActivationStatus.ACTIVATED_FALLBACK;
 
217
        }
 
218
      else
 
219
        {
 
220
          result = active_entry.parent.activate (uri, mimetype);
 
221
        }
 
222
      
 
223
      switch (result)
 
224
      {
 
225
        case ActivationStatus.ACTIVATED_SHOW_DASH:
 
226
          break;
 
227
        case ActivationStatus.NOT_ACTIVATED:
 
228
        case ActivationStatus.ACTIVATED_HIDE_DASH:          
 
229
        case ActivationStatus.ACTIVATED_FALLBACK:
 
230
          global_shell.hide_unity ();
 
231
          break;
 
232
        default:
 
233
          warning ("Unexpected activation status: %u", result);
 
234
          break;
 
235
      }
 
236
    }
 
237
 
 
238
    private bool on_stage_event_captured (Clutter.Event event)
 
239
    {
 
240
      if (event.type == Clutter.EventType.KEY_PRESS)
 
241
        {
 
242
          var ev = event.key;
 
243
 
 
244
          if (ev.keyval == 0xff1b) /* Escape */
 
245
            {
 
246
              /* Try and do a context pop */
 
247
              if (search_bar.sections.active_section_n != 0)
 
248
                {
 
249
                  search_bar.sections.set_active_section (0);
 
250
                }
 
251
              else
 
252
                {
 
253
                  shell.hide_unity ();
 
254
                }
 
255
              return true;
 
256
            }
 
257
        }
 
258
 
 
259
      return false;
 
260
    }
 
261
  }
 
262
}
 
263