deskcandy/src/window.vala

107 lines
3.0 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]
public unowned Gtk.Stack main_stack { get; private set; }
[GtkChild]
private unowned Gtk.Label candy_status;
private BaseCandy active_candy;
private Subprocess proc;
private XScreensaver xss;
private Video video;
private EditorPage editor_page;
public Window (Application app) {
Object (application: app);
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);
var presets_page = new PresetsPage(this);
this.main_stack.add_titled(presets_page, "presets", "Presets");
this.editor_page = new EditorPage(this);
this.editor_page.preset_type_changed.connect(this.on_preset_type_changed);
this.main_stack.add_titled(this.editor_page, "editor", "Edit Preset");
}
public void on_initialized(BaseCandy[] candies) {
this.xss = (XScreensaver)candies[0];
this.video = (Video)candies[1];
this.active_candy = this.xss;
foreach (BaseCandy candy in candies) {
candy.notify.connect(this.refresh_socket);
}
this.editor_page.on_initialized(candies);
}
private void on_preset_type_changed(string type) {
switch (type) {
case "video":
this.active_candy = this.video;
break;
case "screensaver":
this.active_candy = this.xss;
break;
}
if (this.active_candy.name != null && this.active_candy.name != "") {
this.refresh_socket();
}
}
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_main_stack_visible_child_name_notify(Object o, ParamSpec param) {
if (this.main_stack.visible_child_name == null) {
return;
}
message(@"page: $(this.main_stack.visible_child_name)");
}
private void on_notify_enabled(Object o, GLib.ParamSpec param) {
var val = ((Application)o).enabled;
this.candy_status.label = val ? "Candy started" : "Candy stopped";
}
}
}