owo/src/Widgets/CommandList.vala
2021-05-04 20:17:55 -05:00

137 lines
4.9 KiB
Vala

namespace Streamdeck.Widgets {
public class CommandList : Gtk.Frame {
private DeckInfo info;
private Gtk.ListBox list_box;
private Dialogs.NewCommandDialog? new_command_dialog;
private Gee.HashMap<uint8, CommandRow> row_map = new Gee.HashMap<uint8, CommandRow> ();
public CommandList(DeckInfo info) {
this.info = info;
var alert = new Granite.Widgets.AlertView (
_("No commands registered"),
_("Try adding a new command."),
""
);
alert.show_all ();
list_box = new Gtk.ListBox ();
list_box.selection_mode = Gtk.SelectionMode.SINGLE;
list_box.activate_on_single_click = true;
list_box.set_placeholder (alert);
var add_button = new Gtk.Button.from_icon_name (
"list-add-symbolic",
Gtk.IconSize.BUTTON
);
add_button.tooltip_text = _("Add");
var remove_button = new Gtk.Button.from_icon_name (
"list-remove-symbolic",
Gtk.IconSize.BUTTON
);
remove_button.tooltip_text = _("Remove");
var actionbar = new Gtk.ActionBar ();
actionbar.get_style_context ().add_class (Gtk.STYLE_CLASS_INLINE_TOOLBAR);
actionbar.add (add_button);
actionbar.add (remove_button);
actionbar.show_all ();
var scrolled = new Gtk.ScrolledWindow (null, null);
scrolled.expand = true;
scrolled.add (list_box);
var commands = Daemon.instance.get_commands (info.serial_number);
if (commands != null) {
foreach (CommandInfo cmd_info in commands) {
var command = Data.Command.parse (cmd_info.key, cmd_info.command);
if (command != null) {
var existing = row_map.get (cmd_info.key);
if (existing != null) {
list_box.remove (existing);
}
var row = new CommandRow (info.serial_number, command);
row_map.set (cmd_info.key, row);
list_box.add (row);
}
}
}
var grid = new Gtk.Grid ();
grid.attach (scrolled, 0, 0);
grid.attach (actionbar, 0, 1);
add (grid);
remove_button.clicked.connect (() => {
var row = list_box.get_selected_row ();
if (row == null) {
return;
}
unowned var command_row = (CommandRow) row;
var key = command_row.get_key ();
Daemon.instance.remove_command.begin (info.serial_number, key, (_obj, res) => {
try {
Daemon.instance.remove_command.end (res);
row_map.unset (key);
list_box.remove (row);
} catch (Error e) {
print ("Error removing command: %s\n", e.message);
}
});
});
add_button.clicked.connect (() => {
if (new_command_dialog == null) {
new_command_dialog = new Dialogs.NewCommandDialog (info.serial_number);
new_command_dialog.transient_for = (Gtk.Window) get_toplevel ();
new_command_dialog.show_all ();
new_command_dialog.response.connect ((response_id) => {
if (response_id == Gtk.ResponseType.REJECT) {
unowned var app = (MainWindow) get_toplevel ();
app.to_obs ();
}
new_command_dialog.destroy ();
});
new_command_dialog.destroy.connect (() => {
new_command_dialog = null;
});
}
new_command_dialog.present ();
});
Daemon.instance.commands_signal.connect ((_obj, serial_number, commands) => {
if (serial_number == info.serial_number) {
foreach (CommandInfo cmd_info in commands) {
var command = Data.Command.parse (cmd_info.key, cmd_info.command);
if (command != null) {
var existing = row_map.get (cmd_info.key);
if (existing != null) {
list_box.remove (existing);
}
var row = new CommandRow (serial_number, command);
row_map.set (cmd_info.key, row);
list_box.add (row);
}
}
}
});
show_all ();
}
}
}