~bigwhale/gwibber/UI-fixes

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * 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 warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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 <njpatel@gmail.com>
 */

using GwibberGtk;

public class WriteItem : TabBarItem{

  public WriteItem ()
  {
    Object (stream:"", icon_name:"document-new", tooltip:"New Message");
  }

  construct
  {
  }

  public override Gtk.Widget? get_toolbar ()
  {
    return null;
  }
  
  public override Gtk.Widget? get_view ()
  {
    return null;
  }
}

public class TabBar : Gtk.VBox
{  
  private Gtk.Alignment _align;
  private Gtk.VBox _vbox;
  private Gtk.HBox _tab_box;
  private Gtk.HBox _toolbar_box;

  private Gtk.Layout _layout;
  private int _last_width;
  private int _last_height;

  private Gtk.HBox _view_box;
  private Gtk.SizeGroup _view_group;

  private WriteItem _write_item;
  private StreamEntry? _entry = null;

  private TabBarItem? _active_item = null;
  private int _active_item_n = 0;

  public signal void tab_changed (TabBarItem item);

  private uint _anim_id = 0;
  private float _anim_offset = 0.0f;
  private int64 _anim_start = 0;

  public TabBar ()
  {
    Object (homogeneous:false, spacing:0);
  }

  construct
  {
    _align = new Gtk.Alignment (0.0f, 0.0f, 1.0f, 1.0f);
    _align.get_style_context ().add_class ("primary-toolbar");
    _align.get_style_context ().add_class ("toolbar");
    _align.draw.connect (on_align_draw);
    pack_start (_align, false, true, 0);

    _vbox = new Gtk.VBox (false, 8);
    _align.add (_vbox);

    _tab_box = new Gtk.HBox (false, 0);
    _vbox.pack_start (_tab_box, false, false, 0);

    _toolbar_box = new Gtk.HBox (false, 0);
    _vbox.pack_start (_toolbar_box, false, true, 0);

    _layout = new Gtk.Layout (null, null);
    pack_start (_layout, true, true, 0);
   
    _view_box = new Gtk.HBox (true, 0);
    _layout.put (_view_box, 0, 0);

    _view_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.BOTH);

    _write_item = new WriteItem ();
    _write_item.clicked.connect (on_write_clicked);
    _tab_box.pack_end (_write_item, false, true, 0);

    _entry = new StreamEntry ();
    _layout.put (_entry, 0, 0);
    _entry.notify["showing"].connect (()=>
    {
      if (!_entry.showing) {
        _entry.text_view.reset ();
        unmake_room();
      } else {
        make_room();
      }
      _view_box.set_sensitive (!_entry.showing);
    });

    _layout.size_allocate.connect_after ((a) => {
      if (_last_width != a.width || _last_height != a.height)
      {
        _last_width = a.width;
        _last_height = a.height;
        
        var children = _view_box.get_children ();
        foreach (Gtk.Widget child in children)
        {
          child.set_size_request (_last_width, _last_height);
          child.queue_draw ();
        }
        
        _view_box.set_size_request (_last_width * (int)children.length (),
                                    _last_height);
        _entry.set_size_request (_last_width,
                                 _last_height);

        queue_draw ();
      }
    });

    show_all ();
  }

  public bool on_align_draw (Cairo.Context cr)
  {
    cr.set_operator (Cairo.Operator.OVER);

    // Menu background
    Gtk.render_background (_align.get_style_context (),
                           cr,
                           0,
                           -1,
                           _align.get_allocated_width (),
                           _align.get_allocated_height () + 2);

    // Arrow
    if (_active_item is Gtk.Widget && !_entry.get_visible ())
    {
      Gtk.Allocation a;
      _active_item.get_allocation (out a);

      cr.set_line_width (1.0);
      cr.set_source_rgb (1.0f, 1.0f, 1.0f);

      // Main Arrow
      cr.move_to (a.x + a.width/2.0, _align.get_allocated_height () - 5);
      cr.line_to (a.x + a.width/2.0 + 5, _align.get_allocated_height ());
      cr.line_to (a.x + a.width/2.0 - 5, _align.get_allocated_height ());
      cr.close_path ();
      
      cr.fill ();

      if (_active_item.get_toolbar () is Gtk.Widget)
      {
        cr.save ();
        cr.translate (0.5, 0.5);

        draw_arrowed_line (cr, 0, a.height + 3, _align.get_allocated_width (), (int)(a.x + a.width/2.0));
        cr.set_source_rgba (1.0f, 1.0f, 1.0f, 0.2f);
        cr.stroke ();

        draw_arrowed_line (cr, 0, a.height + 2, _align.get_allocated_width (), (int)(a.x + a.width/2.0));
        cr.set_source_rgba (0.0f, 0.0f, 0.0f, 0.5f);
        cr.stroke ();

        cr.restore ();
      }
    }
    
    _align.propagate_draw (_vbox, cr);

    return true;
  }

  private void draw_arrowed_line (Cairo.Context cr, int x, int y, int width, int point)
  {
    int size = 5;

    cr.move_to (x, y);
    cr.line_to (point - size, y);
    cr.line_to (point, y - size);
    cr.line_to (point + size, y); 
    cr.line_to (x + width, y);
  }

  public void add_item (TabBarItem item)
  {
    item.clicked.connect (on_tab_clicked);
    _tab_box.pack_start (item, false, false, 0);

    if (item.get_toolbar () is Gtk.Widget)
    {
      var toolbar = item.get_toolbar ();
      toolbar.set_no_show_all (true);
      toolbar.hide ();
      _toolbar_box.pack_start (toolbar);
    }

    if (item.get_view () is Gtk.Widget)
    {
      var view = item.get_view ();
      item.entry = _entry;
      view.show ();

      _view_box.pack_start (view, false, false, 0);
      _view_group.add_widget (view);

      var children = _view_box.get_children ();
      _view_box.set_size_request (_last_width * (int)children.length (),
                                  _last_height);
    }

    queue_draw ();
  }

  public void on_tab_clicked (Gtk.Button? gtk_item)
  {
    _entry.showing = false;
    _entry.text_view.reset ();

    TabBarItem? item = gtk_item as TabBarItem;

    if (_active_item == item || gtk_item == null)
      return;

    if (_active_item is TabBarItem)
    {
      _active_item.active = false;
      if (_active_item.get_toolbar () is Gtk.Widget)
        _active_item.get_toolbar ().hide ();
     }

    _active_item = item;
    _active_item.active = true;;

    if (_active_item.get_toolbar () is Gtk.Widget)
    {
      _active_item.get_toolbar ().show ();
      _align.set_padding (0, 8, 0, 0);
    }
    else
      _align.set_padding (0, 0, 0, 0);

    if (_active_item.get_view () is Gtk.Widget)
    {
      var children = _view_box.get_children ();
      int i = 0;
      foreach (Gtk.Widget child in children)
      {
        if (child == _active_item.get_view ())
          break;
        i++;
      }
      //_layout.move (_view_box, _layout.get_allocated_width () * i * -1, 0);
      _active_item_n = i;

      if (_anim_id != 0)
        Source.remove (_anim_id);

      _anim_start = G.get_monotonic_time ();
      
      _anim_id = Timeout.add (15, ()=>
      {
        float LENGTH = 200000.0f;
        int64 diff = G.get_monotonic_time () - _anim_start;
        float progress = diff/LENGTH;
        Gtk.Allocation a;
        _view_box.get_allocation (out a);

        float target_x = _active_item_n * _layout.get_allocated_width () * -1;
        float current_x = a.x;
        float new_x = 0;

        if (target_x > current_x)
        {
          new_x = current_x + ((target_x - current_x) * progress);
        }
        else if (target_x < current_x)
        {
          new_x = current_x - ((current_x - target_x) * progress);
        }

        _layout.move (_view_box, (int)new_x, 0);

        queue_draw ();

        if (diff > LENGTH)
        {
          _anim_id = 0;
          _anim_offset = 1.0f;
          _layout.move (_view_box, _layout.get_allocated_width () * _active_item_n * -1, 0);
          return false;
        }

        return true;
      });

      _active_item.get_view ().queue_draw ();
      _view_box.queue_draw ();
      _layout.queue_draw ();
    }

    _entry.queue_draw ();

    tab_changed (item);

    queue_draw ();
  }

  public void show_entry ()
  {
  }

  public void on_write_clicked ()
  {
    if (_entry.showing)
    {
      _entry.showing = false;
      _entry.text_view.reset ();
      _view_box.set_sensitive (true);
    }
    else
    {
      _entry.showing = true;
      _entry.text_view.grab_focus ();
      _view_box.set_sensitive (false);
    }
    queue_draw ();
  }

  public void make_room()
  {
    Gtk.Allocation alloc;
    _view_box.get_allocation(out alloc);
    _layout.move(_view_box, alloc.x, 90);
  }

  public void unmake_room()
  {
    Gtk.Allocation alloc;
    _view_box.get_allocation(out alloc);
    _layout.move(_view_box, alloc.x, 0);
  }

}