deskcandy/src/window.vala

158 lines
4.8 KiB
Vala

using GLib;
using Deskcandy.Candy;
namespace Deskcandy {
[GtkTemplate (ui = "/moe/solarpunk/Deskcandy/window.ui")]
public class Window : Gtk.ApplicationWindow {
private Gtk.Socket socket;
[GtkChild]
private unowned Gtk.MenuButton open_menu_button;
[GtkChild]
private unowned Gtk.Bin preview;
[GtkChild]
private unowned Gtk.Stack main_stack;
[GtkChild]
private unowned Gtk.Stack preset_type_stack;
[GtkChild]
private unowned Gtk.Label candy_status;
[GtkChild]
private unowned Gtk.ListStore screensavers_liststore;
[GtkChild]
private unowned Gtk.FileChooserButton video_chooser;
private BaseCandy active_candy;
private Subprocess proc;
private XScreensaver xss;
private Video video;
public Window (Application app) {
Object (application: app);
var preset_action_group = new SimpleActionGroup();
var create_preset = new SimpleAction("create", null);
preset_action_group.add_action(create_preset);
create_preset.activate.connect(this.on_create_preset);
var delete_preset = new SimpleAction("delete", null);
delete_preset.activate.connect(this.on_delete_preset);
preset_action_group.add_action(delete_preset);
this.insert_action_group("presets", preset_action_group);
var builder = new Gtk.Builder.from_resource("/moe/solarpunk/Deskcandy/main_menu.ui");
var menu = (GLib.MenuModel)builder.get_object("main_menu");
this.open_menu_button.menu_model = menu;
app.initialized.connect(this.on_initialized);
app.notify["enabled"].connect(this.on_notify_enabled);
}
public void on_initialized(BaseCandy[] candies) {
this.xss = (XScreensaver)candies[0];
Gtk.TreeIter iter;
this.screensavers_liststore.clear();
foreach (XScreensaverConfig config in this.xss.configs) {
this.screensavers_liststore.append(out iter);
this.screensavers_liststore.set(iter, 0, config.label, 1, config.description, 2, config.name);
}
this.video = (Video)candies[1];
this.active_candy = this.xss;
}
private void refresh_socket() {
if (this.socket != null) {
this.preview.remove(this.socket);
this.proc.send_signal(15);
}
this.socket = new Gtk.Socket();
this.socket.visible = true;
this.preview.add(this.socket);
try {
this.proc = this.active_candy.launch((uint)this.socket.get_id());
} catch (Error e) {
error(e.message);
}
}
[GtkCallback]
private void on_window_destroy(Gtk.Widget w) {
message("bye");
this.application.quit();
}
[GtkCallback]
private void on_screensaver_chooser_row_activated(Gtk.TreeView treeview, Gtk.TreePath path, Gtk.TreeViewColumn col) {
Gtk.TreeIter iter;
if (treeview.model.get_iter(out iter, path)) {
string name;
treeview.model.get(iter, 2, out name);
this.xss.name = name;
this.refresh_socket();
}
}
[GtkCallback]
private void on_video_chooser_file_set() {
this.video.name = this.video_chooser.get_filename();
this.refresh_socket();
}
private void on_create_preset(SimpleAction a, Variant? v) {
this.main_stack.visible_child_name = "editor_page";
}
[GtkCallback]
private void on_cancel_edit_preset_button_clicked(Gtk.Widget w) {
this.main_stack.visible_child_name = "presets_page";
}
[GtkCallback]
private void on_cancel_new_preset_button_clicked(Gtk.Widget w) {
var popover = (Gtk.Popover)w.get_ancestor(typeof(Gtk.Popover));
popover.popdown();
this.main_stack.visible_child_name = "presets_page";
}
[GtkCallback]
private void on_cancel_delete_preset_button_clicked(Gtk.Widget w) {
var popover = (Gtk.Popover)w.get_ancestor(typeof(Gtk.Popover));
popover.popdown();
}
[GtkCallback]
private void on_main_stack_visible_child_name_notify(Object o, ParamSpec param) {
message(@"page: $(this.main_stack.visible_child_name)");
}
[GtkCallback]
private void on_preset_type_stack_visible_child_name_notify(Object o, ParamSpec param) {
message(@"preset type: $(this.preset_type_stack.visible_child_name)");
switch (this.preset_type_stack.visible_child_name) {
case "video_page":
this.active_candy = this.video;
break;
case "screensaver_page":
this.active_candy = this.xss;
break;
}
if (this.active_candy.name != null && this.active_candy.name != "") {
this.refresh_socket();
}
}
private void on_delete_preset(SimpleAction a, Variant? v) {
message("delete");
}
private void on_notify_enabled(Object o, GLib.ParamSpec param) {
var val = ((Application)o).enabled;
this.candy_status.label = val ? "Candy started" : "Candy stopped";
}
}
}