/gpump/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/gpump/trunk

« back to all changes in this revision

Viewing changes to src/app.js

  • Committer: Gustav Hartvigsson
  • Date: 2014-02-08 17:53:39 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140208175339-yxn38uceduzzvswx
Initial code.

Non of the code is actualy functional.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of GPump, a Pump.io client.
2
 
 *
3
 
 * GPump (THE SOFTWARE) is made available under the terms and conditions of the
4
 
 * GNU Lesser General Public Licence 3.0. A copy of the licence can be read
5
 
 * in the file lgpl-3.0.txt in the root of this project.
6
 
 */
7
 
 
8
 
 
9
 
const Gettext = imports.gettext;
10
 
const Gio = imports.gi.Gio;
11
 
const GLib = imports.gi.GLib;
12
 
const GObject = imports.gi.GObject;
13
 
const Gtk = imports.gi.Gtk;
14
 
const _ = imports.gettext.gettext;
15
 
const Lang = imports.lang;
16
 
 
17
 
const SettingsData = imports.settings_data;
18
 
/* const Style = imports.style; */
19
 
const PreferencesUI = imports.preferences_ui;
20
 
const UserMenu = imports.user_menu;
21
 
const UserButton = imports.user_button;
22
 
 
23
 
/** @class
24
 
 * This class is the main application class, it creates the main wiev
25
 
 * and basically is what runs the program.
26
 
 */
27
 
const Application = new Lang.Class ({
28
 
  Name: "Application",
29
 
  Extends: Gtk.Application,
30
 
  
31
 
  _init: function () {
32
 
    this.settings = SettingsData.get_settings ();
33
 
    
34
 
    Gettext.bindtextdomain ("gpump", "./locale/"); //FIXME
35
 
    Gettext.textdomain ("gpump");
36
 
    GLib.set_prgname ("gpump");
37
 
    
38
 
    this.parent ({
39
 
      application_id: "org.gego.gpump",
40
 
      flags: Gio.ApplicationFlags.FLAGS_NONE,
41
 
      register_session: true,
42
 
    });
43
 
    GLib.set_application_name (_("GPump"));
44
 
    
45
 
  },
46
 
  
47
 
  vfunc_activate: function () {
48
 
    /* Guarantee that only one window is present at any time. */
49
 
    let w_list = this.get_windows ();
50
 
    if (w_list.length) {
51
 
      w_list[0].present ();
52
 
      if (!(this._is_shown ())) {
53
 
        if (!(this.settings.get_setting ("ui.x").data < 0 &&
54
 
            this.settings.get_setting ("ui.y").data < 0)){
55
 
          this.window.move (this.settings.get_setting ("ui.x").data,
56
 
                                           this.settings.get_setting ("ui.y").data);
57
 
        }
58
 
      }
59
 
      return;
60
 
    }
61
 
    
62
 
    this._prepare_window ();
63
 
    this._prepare_app_menu ();
64
 
    this._prepare_header_bar ();
65
 
    
66
 
    /* Style.load_css (); */
67
 
    
68
 
    this.window.show_all ();
69
 
  },
70
 
  
71
 
  /**
72
 
   * This function is used to prepare the window, it also initializes and adds
73
 
   * the headerbar to it.
74
 
   */
75
 
  _prepare_window: function () {
76
 
    
77
 
    this.window = new Gtk.ApplicationWindow ({
78
 
      application: this,
79
 
      type: Gtk.WindowType.TOPLEVEL,
80
 
      title: GLib.get_application_name (),
81
 
      default_height: 500,
82
 
      default_width: 500,
83
 
      height_request: 500,
84
 
      width_request: 500,
85
 
      window_position: Gtk.WindowPosition.CENTER
86
 
    });
87
 
    
88
 
    this.window.set_titlebar ((this.headerbar = new Gtk.HeaderBar({
89
 
      title: GLib.get_application_name (),
90
 
      subtitle: _("A Pump.io client"),
91
 
      show_close_button: true
92
 
    })));
93
 
    
94
 
    if (!(this.settings.get_setting ("ui.x").data < 0 &&
95
 
        this.settings.get_setting ("ui.y").data < 0)){
96
 
      this.window.move (this.settings.get_setting ("ui.x").data,
97
 
                                       this.settings.get_setting ("ui.y").data);
98
 
    }
99
 
    
100
 
    var gtk_settings = Gtk.Settings.get_default ();
101
 
      if ( this.settings.get_setting ("main.use_dark").data ) {
102
 
        gtk_settings["gtk_application_prefer_dark_theme"] = true;
103
 
      } else {
104
 
        gtk_settings["gtk_application_prefer_dark_theme"] = false;
105
 
      }
106
 
    
107
 
    print ("derp!\n");
108
 
    
109
 
    
110
 
    
111
 
    this.window.add ((this.layout = new Gtk.Box ({
112
 
      orientation: Gtk.Orientation.VERTICAL
113
 
    })));
114
 
    
115
 
    /* * Building the stack switched views */
116
 
    /* switcher */
117
 
    /* Hack to centre the damn switcher */
118
 
    this.layout.pack_start ((this.switcher_box = new Gtk.Box ({
119
 
      orientation: Gtk.Orientation.HORIZONTAL
120
 
    })), false, false, 0);
121
 
    
122
 
    this.switcher_box.set_center_widget (
123
 
      (this.stack_switcher = new Gtk.StackSwitcher ())
124
 
    );
125
 
    
126
 
    /* the stack */
127
 
    this.layout.pack_end ((this.stack = new Gtk.Stack ({
128
 
      homogeneous: true,
129
 
      transition_duration: 250,
130
 
      transition_type: Gtk.StackTransitionType.CROSSFADE
131
 
    })), true, true, 0);
132
 
    
133
 
    /* main view */
134
 
    this.stack.add_titled ((new Gtk.ScrolledWindow ({
135
 
      child: (this.main_list_box = new Gtk.ListBox()),
136
 
      margin: 1
137
 
    })), "main", _("Main"));
138
 
    
139
 
    
140
 
    /* 'mean while' view */
141
 
    this.stack.add_titled ((new Gtk.ScrolledWindow ({
142
 
      child: (this.mw_list_box = new Gtk.ListBox()),
143
 
      margin: 1
144
 
    })), "meanwhile", _("Meanwhile"));
145
 
    
146
 
    /* * Hook up the stack and the stack switcher */
147
 
    this.stack_switcher.set_stack (this.stack);
148
 
    
149
 
    /* * Hide the window so, so we can show it later */
150
 
    this.window.connect ('delete_event',
151
 
                         Lang.bind (this, function () {
152
 
      this._save_settings ();
153
 
      this.window.hide_on_delete ();
154
 
    }));
155
 
    
156
 
    this.add_window (this.window);
157
 
  },
158
 
  
159
 
  /**
160
 
   * This function is used to create the application menu.
161
 
   */
162
 
  _prepare_app_menu: function () {
163
 
    let menu = new Gio.Menu ();
164
 
    let section = new Gio.Menu ();
165
 
    
166
 
    section.append (_("Preferences"), "app.preferences");
167
 
    menu.append_section (null, section);
168
 
    
169
 
    section = new Gio.Menu ();
170
 
    section.append (_("About"), "app.about");
171
 
    section.append (_("Quit"), "app.quit");
172
 
    menu.append_section (null, section);
173
 
    
174
 
    this.set_app_menu (menu);
175
 
    
176
 
    let about_action = new Gio.SimpleAction ({name: "about"});
177
 
    about_action.connect ('activate', Lang.bind (this, function () {
178
 
      let about_dialog = new Gtk.AboutDialog ({use_header_bar: true,
179
 
        transient_for: this.window,
180
 
        modal: true,
181
 
        version: "pre-alpha ~BZR~",
182
 
        program_name: GLib.get_application_name (),
183
 
        copyright: "Gustav \'Gego/XAREN\' Hartvigsson, 2014",
184
 
        license_type: Gtk.License.LGPL_3_0});
185
 
      
186
 
      about_dialog.authors =
187
 
                          ["Gustav Hartvigsson <gustav.hartvigsson@gmail.com>"];
188
 
      about_dialog.connect ("response", function () {
189
 
        about_dialog.destroy ();
190
 
      });
191
 
      about_dialog.run ();
192
 
    }));
193
 
    this.add_action (about_action);
194
 
    
195
 
    let quit_action = new Gio.SimpleAction ({name: "quit"});
196
 
    quit_action.connect ('activate', Lang.bind (this, function () {
197
 
      this._save_settings ();
198
 
      this.quit ();
199
 
    }));
200
 
    this.add_action (quit_action);
201
 
    
202
 
    let preferences_action = new Gio.SimpleAction ({name: "preferences"});
203
 
    preferences_action.connect ('activate', Lang.bind (this, function (){
204
 
      let pref_dialog = new PreferencesUI.PreferencesUI ();
205
 
      pref_dialog.set_transient_for (this.window);
206
 
      pref_dialog.run ();
207
 
    }));
208
 
    this.add_action (preferences_action);
209
 
  },
210
 
  
211
 
  /**
212
 
   * This function is used to prepare the hearderbar by adding buttons and
213
 
   * assigning callback functions to them.
214
 
   */
215
 
  _prepare_header_bar: function () {
216
 
    
217
 
    let button_container = new Gtk.Box ({orientation: Gtk.Orientation.HORIZONTAL});
218
 
    
219
 
    button_container.pack_start ((this.new_post_btn = new Gtk.Button ({
220
 
      image: (new Gtk.Image ({icon_name: 'text-editor-symbolic'})),
221
 
      tooltip_text: _("Create new post"),
222
 
    })), false, false, null);
223
 
    
224
 
    button_container.pack_start ((this.refresh_btn = new Gtk.Button({
225
 
      image: (new Gtk.Image ({icon_name: 'emblem-synchronizing-symbolic'})),
226
 
      tooltip_text: _("Refresh the stream"),
227
 
    })), false, false, null);
228
 
    
229
 
    let special_btn_container_style_ctx = button_container.get_style_context ();
230
 
    special_btn_container_style_ctx.add_class ("linked");
231
 
    
232
 
    this.headerbar.pack_start (button_container);
233
 
    
234
 
    this.headerbar.pack_end ((this.user_menu_btn = new UserButton.UserButton ({
235
 
      image: (new Gtk.Image ({icon_name: 'mail-send-symbolic'})),
236
 
      tooltip_text: _("Switch and manage users"),
237
 
      popover: (this.user_menu = new UserMenu.UserMenu ()),
238
 
    })));
239
 
    
240
 
  },
241
 
  
242
 
  _save_settings: function () {
243
 
    let allocation = this.window.get_allocation ();
244
 
    this.settings.set_setting ("ui.h", allocation.height);
245
 
    this.settings.set_setting ("ui.w", allocation.width);
246
 
    
247
 
    let win_pos = this.window.get_position ();
248
 
    this.settings.set_setting ("ui.x", win_pos[0]);
249
 
    this.settings.set_setting ("ui.y", win_pos[1]);
250
 
    this.settings.commit_to_file ();
251
 
  },
252
 
  
253
 
  _is_shown: function () {
254
 
    let win_pos = this.window.get_position ();
255
 
    if (win_pos[0] == undefined || win_pos[1] == undefined) {
256
 
      return false;
257
 
    } else {
258
 
      return true;
259
 
    }
260
 
  }
261
 
  
262
 
});