Hide zavai when running an app
[gregoa/zavai.git] / src / app_wm.vala
1 /*
2  * app_wm - zavai window management functions
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace ui {
25 namespace wm {
26
27 /*
28 static void print_window_state(Gtk.Window w)
29 {
30         Gdk.WindowState state = w.window.get_state();
31         int istate = (int) state;
32         bool is_icon = (state & Gdk.WindowState.ICONIFIED) != 0;
33         bool is_visible = w.visible;
34         bool is_focus = w.is_focus;
35         bool has_focus = ((Gtk.Widget)w).has_focus;
36         bool is_active = w.is_active;
37         bool is_toplevel = w.is_toplevel();
38         bool is_tfocus = w.has_toplevel_focus;
39
40         stderr.printf("istate: %d; is_icon: %s; is_visible: %s; is_focus: %s; has_focus: %s, is_active: %s; is_toplevel: %s; is_tfocus: %s\n",
41                         istate,
42                         is_icon ? "true" : "false",
43                         is_visible ? "true" : "false",
44                         is_focus ? "true" : "false",
45                         has_focus ? "true" : "false",
46                         is_active ? "true" : "false",
47                         is_toplevel ? "true" : "false",
48                         is_tfocus ? "true" : "false"
49                      );
50 }
51 */
52
53 public class RaiseIcon : Gtk.StatusIcon
54 {
55         public RaiseIcon()
56         {
57                 activate += on_activate;
58                 zavai.app.visibility_changed += on_visibility_changed;
59                 update_icon();
60         }
61
62         private void on_visibility_changed(bool visible)
63         {
64                 update_icon();
65         }
66
67         private void on_activate()
68         {
69                 zavai.app.toggle_visibility();
70                 update_icon();
71         }
72
73         protected void update_icon()
74         {
75                 string name = zavai.config.icondir + "/";
76                 if (!zavai.app.visibility)
77                         name += "zavai_on.png";
78                 else
79                         name += "zavai_off.png";
80                 set_from_file(name);
81         }
82 }
83
84 public class CloseOrBack : Gtk.StatusIcon
85 {
86         public CloseOrBack()
87         {
88                 activate += on_activate;
89                 zavai.app.visibility_changed += on_visibility_changed;
90                 update_icon();
91         }
92
93         private void on_visibility_changed(bool visible)
94         {
95                 update_icon();
96         }
97
98         private void on_activate()
99         {
100                 if (zavai.app.visibility)
101                 {
102                         // Back
103                         zavai.app.back();
104                 } else {
105                         // Close current app
106                         Gdk.Window w = zavai.app.get_screen().get_active_window();
107                         if (w != zavai.app.window)
108                         {
109                                 w.destroy();
110                         }
111                 }
112         }
113
114         protected void update_icon()
115         {
116                 string name = zavai.config.icondir + "/";
117                 if (!zavai.app.visibility)
118                         name += "quit_on.png";
119                 else
120                         name += "quit_off.png";
121                 set_from_file(name);
122         }
123 }
124
125 public class WindowList : Applet
126 {
127         protected Wnck.Tasklist selector;
128         protected AppletPushLink launcher_link;
129
130         public WindowList(string label)
131         {
132                 _label = label;
133                 selector = new Wnck.Tasklist(Wnck.Screen.get_default());
134                 pack_start(selector, true, true, 0);
135
136                 launcher_link = new AppletPushLink("wm.launcher");
137         button_box.pack_start(launcher_link, true, true, 0);
138         }
139 }
140
141 public class LauncherButton : Gtk.Button
142 {
143         private string exec;
144
145         public LauncherButton(string name, string exec)
146         {
147                 label = name;
148                 this.exec = exec;
149                 clicked += on_clicked;
150                 set_size_request(0, zavai.config.min_button_height);
151         }
152
153         public void on_clicked()
154         {
155                 zavai.log.info("Run program: " + exec);
156                 string[] args = exec.split(" ");
157                 string[] args1 = new string[args.length + 1];
158                 int cout = 0;
159                 for (int cin = 0; cin < args.length; ++cin)
160                 {
161                         if (args[cin][0] == '%') continue;
162                         args1[cout++] = args[cin];
163                 }
164                 args1[cout] = null;
165                 Pid pid;
166                 try {
167                         Process.spawn_async(
168                                 Environment.get_home_dir(),
169                                 args1,
170                                 null,
171                                 SpawnFlags.SEARCH_PATH,
172                                 null,
173                                 out pid);
174                 } catch (SpawnError e) {
175                         zavai.log.error("Launching " + exec + ": " + e.message);
176                 }
177         }
178 }
179
180 public class Launcher: Applet
181 {
182         static const string DENTRY_GROUP = "Desktop Entry";
183
184         public Launcher(string label)
185         {
186                 _label = label;
187
188                 var dir = File.new_for_path(zavai.config.homedir);
189                 var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
190
191                 FileInfo file_info;
192                 var icon_theme = Gtk.IconTheme.get_default();
193                 Gee.ArrayList<LauncherButton> buttons = new Gee.ArrayList<LauncherButton>();
194                 while ((file_info = enumerator.next_file(null)) != null)
195                 {
196                         if (!file_info.get_name().has_suffix(".desktop")) continue;
197                                 
198                         string pathname = zavai.config.homedir + "/" + file_info.get_name();
199                         //stdout.printf("Load %s\n", pathname);
200
201                         string icon = null;
202                         var kf = new KeyFile();
203                         try {
204                                 kf.load_from_file(pathname, KeyFileFlags.NONE);
205                                 if (! kf.has_group(DENTRY_GROUP)) continue;
206                                 if (! kf.has_key(DENTRY_GROUP, "Name")) continue;
207                                 if (! kf.has_key(DENTRY_GROUP, "Exec")) continue;
208                                 if (kf.has_key(DENTRY_GROUP, "Icon"))
209                                         icon = kf.get_string(DENTRY_GROUP, "Icon");
210                         } catch (Error e) {
211                                 zavai.log.error("Skipping file " + pathname + ": " + e.message);
212                                 continue;
213                         }
214                         var name = kf.get_string(DENTRY_GROUP, "Name");
215                         var exec = kf.get_string(DENTRY_GROUP, "Exec");
216                         var button = new LauncherButton(name, exec);
217                         if (icon != null && icon_theme.has_icon(icon))
218                         {
219                                 try {
220                                         var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0);
221                                         button.image = new Gtk.Image.from_pixbuf(pb);
222                                 } catch (Error e) {
223                                         zavai.log.error("Skipping icon " + icon + ": " + e.message);
224                                 }
225                         }
226                         button.clicked += a => { this.back_to_main(); zavai.app.ensure_hidden(); };
227                         buttons.add(button);
228                 }
229
230                 // Create the table with the launcher buttons
231                 uint ROWMAX = 5;        // Maximum number of rows
232                 uint cols = 1 + (buttons.size + 1) / ROWMAX; // ceil(size/ROWMAX)
233                 uint rows = (buttons.size + 1) / cols;   // ceil(size/cols)
234                 var table = new Gtk.Table(rows, cols, true);
235
236                 // Attach the buttons in the table
237                 uint row = 0;
238                 uint col = 0;
239                 foreach (LauncherButton b in buttons)
240                 {
241                         table.attach(b, col, col+1, row, row+1,
242                                 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
243                                 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
244                                 0, 0);
245                         ++col;
246                         if (col == cols)
247                         {
248                                 col = 0;
249                                 ++row;
250                         }
251                 }
252
253                 pack_start(table, true, true, 0);
254
255 /*
256     try {
257     } catch (Error e) {
258         stderr.printf ("Error: %s\n", e.message);
259         return 1;
260     }
261
262                 for (string file in confdir)
263                 {
264                 }
265 */
266         }
267 }
268
269 public class AppShortcut : Object
270 {
271         private StatusIcon icon;
272
273         public AppShortcut()
274         {
275                 icon = new StatusIcon();
276                 icon.install();
277                 icon.clicked += on_icon_clicked;
278                 icon.set_from_file(zavai.config.icondir + "/apps.png");
279         }
280
281         public void on_icon_clicked()
282         {
283                 zavai.app.push_applet("wm.list");
284         }
285 }
286
287 RaiseIcon raise_icon;
288 CloseOrBack close_or_back;
289 WindowList window_list;
290 Launcher launcher;
291 AppShortcut app_shortcut;
292
293 public void init()
294 {
295         raise_icon = new RaiseIcon();
296         raise_icon.set_visible(true);
297
298         close_or_back = new CloseOrBack();
299         close_or_back.set_visible(true);
300
301         app_shortcut = new AppShortcut();
302
303         launcher = new Launcher("Run program");
304         zavai.registry.register_applet("wm.launcher", launcher);
305
306         window_list = new WindowList("Apps");
307         zavai.registry.register_applet("wm.list", window_list);
308         zavai.registry.getmenu("menu.main").add_applet("wm.list");
309
310         //zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
311 }
312
313 }
314 }
315 }