File: MainWindow.vala

package info (click to toggle)
tootle 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,508 kB
  • sloc: xml: 70; makefile: 22; python: 11
file content (211 lines) | stat: -rw-r--r-- 7,485 bytes parent folder | download
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
using Gtk;

public class Tootle.MainWindow: Gtk.Window {
    
    private Overlay overlay;
    private Granite.Widgets.Toast toast;
    private Grid grid;
    private Stack primary_stack;
    private Stack secondary_stack;
    
    public HeaderBar header;
    public Granite.Widgets.ModeButton button_mode;
    private AccountsButton button_accounts;
    private Spinner spinner;
    private Button button_toot;
    private Button button_back;
    
    public HomeView home = new HomeView ();
    public NotificationsView notifications = new NotificationsView ();
    public LocalView local = new LocalView ();
    public FederatedView federated = new FederatedView ();

    construct {
        var provider = new Gtk.CssProvider ();
        provider.load_from_resource ("/com/github/bleakgrey/tootle/app.css");
        StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
        
        settings.changed.connect (update_theme);
        update_theme ();

        secondary_stack = new Stack();
        secondary_stack.transition_type = Gtk.StackTransitionType.SLIDE_LEFT_RIGHT;
        secondary_stack.show ();
        primary_stack = new Stack();
        primary_stack.transition_type = Gtk.StackTransitionType.SLIDE_LEFT_RIGHT;
        primary_stack.show ();
        primary_stack.add_named (secondary_stack, "0");
        primary_stack.hexpand = true;
        primary_stack.vexpand = true;
        
        spinner = new Spinner ();
        spinner.active = true;

        button_accounts = new AccountsButton ();
        
        button_back = new Button ();
        button_back.valign = Align.CENTER;
        button_back.label = _("Back");
        button_back.get_style_context ().add_class (Granite.STYLE_CLASS_BACK_BUTTON);
        button_back.clicked.connect (() => back ());
        
        button_toot = new Button ();
        button_toot.valign = Align.CENTER;
        button_toot.tooltip_text = _("Toot");
        button_toot.image = new Image.from_icon_name ("document-edit-symbolic", IconSize.LARGE_TOOLBAR);
        button_toot.clicked.connect (() => PostDialog.open ());

        button_mode = new Granite.Widgets.ModeButton ();
        button_mode.get_style_context ().add_class ("mode");
        button_mode.vexpand = true;
        button_mode.valign = Align.FILL;
        button_mode.mode_changed.connect (on_mode_changed);
        button_mode.show ();
        
        header = new HeaderBar ();
        header.get_style_context ().add_class ("compact");
        header.show_close_button = true;
        header.title = _("Tootle");
        header.custom_title = button_mode;
        header.pack_start (button_back);
        header.pack_start (button_toot);
        header.pack_end (button_accounts);
        header.pack_end (spinner);
        header.show_all ();
        
        grid = new Grid ();
        grid.attach (primary_stack, 0, 0, 1, 1);
        
        add_header_view (home);
        add_header_view (notifications);
        add_header_view (local);
        add_header_view (federated);
        button_mode.set_active (0);
        
        toast = new Granite.Widgets.Toast ("");
        overlay = new Overlay ();
        overlay.add_overlay (grid);
        overlay.add_overlay (toast);
        overlay.set_size_request (450, 600);
        add (overlay);
        show_all ();
    }
    
    public MainWindow (Gtk.Application _app) {
        application = _app;
        icon_name = "com.github.bleakgrey.tootle";
        resizable = true;
        window_position = WindowPosition.CENTER;
        set_titlebar (header);
        update_header ();
        
        app.toast.connect (on_toast);
        network.started.connect (() => spinner.show ());
        network.finished.connect (() => spinner.hide ());
        accounts.updated (accounts.saved_accounts);
        button_press_event.connect ((event) => {
            if (event.button == 8) {
                back ();
                return true;
            }
            return false;
        });
    }
    
    private void add_header_view (AbstractView view) {
        var img = new Image.from_icon_name (view.get_icon (), IconSize.LARGE_TOOLBAR);
        img.tooltip_text = view.get_name ();
        button_mode.append (img);
        view.image = img;
        secondary_stack.add_named (view, view.get_name ());
        
        if (view is NotificationsView)
            img.pixel_size = 20; // For some reason Notifications icon is too small without this
    }
    
    public int get_visible_id () {
        return int.parse (primary_stack.get_visible_child_name ());
    }
    
    public void open_view (AbstractView widget) {
        var i = get_visible_id ();
        i++;
        widget.stack_pos = i;
        widget.show ();
        primary_stack.add_named (widget, i.to_string ());
        primary_stack.set_visible_child_name (i.to_string ());
        update_header ();
    }
    
    public void back () {
        var i = get_visible_id ();
        if (i == 0)
            return;
        
        var child = primary_stack.get_child_by_name (i.to_string ());
        primary_stack.set_visible_child_name ((i-1).to_string ());
        child.destroy ();
        update_header ();
    }
    
    public void reopen_view (int view_id) {
        var i = get_visible_id ();
        while (i != view_id && view_id != 0) {
            back ();
            i = get_visible_id ();
        }
    }
    
    public override bool delete_event (Gdk.EventAny event) {
        this.destroy.connect (() => {
            if (!settings.always_online || accounts.is_empty ())
                app.remove_window (window_dummy);
            window = null;
        });
        return false;
    }
    
    public void switch_timeline (int32 timeline_no) {
        button_mode.set_active (timeline_no);
    }
    
    private void update_theme () {
        var provider = new Gtk.CssProvider ();
        var is_dark = settings.dark_theme;
        var theme = is_dark ? "dark" : "light";
        provider.load_from_resource ("/com/github/bleakgrey/tootle/%s.css".printf (theme));
        StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
        Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = is_dark;

        string[] icon_search_path;
        Gtk.IconTheme.get_default ().get_search_path (out icon_search_path);
        icon_search_path[0] = "/usr/share/icons/elementary-xfce/status/symbolic";
        Gtk.IconTheme.get_default ().set_search_path (icon_search_path);
    }
    
    private void update_header () {
        bool primary_mode = get_visible_id () == 0;
        button_mode.sensitive = primary_mode;
        button_mode.opacity = primary_mode ? 1 : 0; //Prevent HeaderBar height jitter
        button_toot.set_visible (primary_mode);
        button_back.set_visible (!primary_mode);
        button_accounts.set_visible (true);
    }

    private void on_toast (string msg){
        toast.title = msg;
        toast.send_notification ();
    }

    private void on_mode_changed (Widget widget) {
        var visible = secondary_stack.get_visible_child () as AbstractView;
        visible.current = false;
        
        secondary_stack.set_visible_child_name (widget.tooltip_text);
        
        visible = secondary_stack.get_visible_child () as AbstractView;
        visible.current = true;
        visible.on_set_current ();
    }

}