Compare commits

...

2 commits

6 changed files with 145 additions and 41 deletions

View file

@ -22,7 +22,8 @@ app_files = files(
'src/Widgets/DeckList.vala',
'src/Widgets/DeckItem.vala',
'src/Widgets/DisconnectedPage.vala',
'src/Widgets/EmptyConfigPane.vala'
'src/Widgets/EmptyConfigPane.vala',
'src/Widgets/ScenesComboBox.vala'
)
executable(

View file

@ -60,6 +60,10 @@ namespace Streamdeck.Data {
public abstract string to_json ();
public bool is_type (CommandType rhs) {
return type == rhs;
}
public CommandType get_command_type () {
return type;
}

View file

@ -1,5 +1,7 @@
namespace Streamdeck.Dialogs {
public class NewCommandDialog : Granite.Dialog {
private Gtk.Stack stack;
private Views.ConfigCommand command_page;
private string serial_number;
private ReadInput key_info;
@ -24,11 +26,26 @@ namespace Streamdeck.Dialogs {
press_page.attach (press_label, 0, 0);
var command_page = new Views.ConfigCommand (serial_number);
var existing_key_label = new Gtk.Label (
_("Key already mapped, try pressing a new key")
);
existing_key_label.valign = Gtk.Align.CENTER;
existing_key_label.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);
var stack = new Gtk.Stack ();
var existing_key_page = new Gtk.Grid ();
existing_key_page.column_spacing = 12;
existing_key_page.row_spacing = 12;
existing_key_page.halign = Gtk.Align.CENTER;
existing_key_page.margin = 24;
existing_key_page.attach (existing_key_label, 0, 0);
command_page = new Views.ConfigCommand (serial_number);
stack = new Gtk.Stack ();
stack.add_named (disconnected_page, "disconnected");
stack.add_named (press_page, "keypress");
stack.add_named (existing_key_page, "existing");
stack.add_named (command_page, "command");
get_content_area ().add (stack);
@ -41,15 +58,7 @@ namespace Streamdeck.Dialogs {
show_all ();
key_press.connect ((_obj, key_info) => {
if (serial_number == key_info.serial_number) {
this.key_info = key_info;
command_page.key = key_info.key;
stack.set_visible_child_name ("command");
} else {
Daemon.instance.key_press.begin ();
}
});
key_press.connect (handle_key_press);
if (Daemon.instance.get_obs_state () != "Connected") {
stack.set_visible_child_name ("disconnected");
@ -58,5 +67,30 @@ namespace Streamdeck.Dialogs {
Daemon.instance.key_press.begin ();
}
}
private void handle_key_press (ReadInput key_info) {
if (serial_number == key_info.serial_number) {
if (already_pressed (key_info.key)) {
stack.set_visible_child_name ("existing");
Daemon.instance.key_press.begin ();
} else {
this.key_info = key_info;
command_page.set_key (key_info.key);
stack.set_visible_child_name ("command");
}
} else {
Daemon.instance.key_press.begin ();
}
}
private bool already_pressed (uint8 key) {
foreach (CommandInfo cmd in Daemon.instance.get_commands (serial_number)) {
if (cmd.key == key) {
return true;
}
}
return false;
}
}
}

View file

@ -1,12 +1,11 @@
namespace Streamdeck.Views {
public class ConfigCommand : Gtk.Grid {
private Gtk.Stack command_stack;
private Gtk.ComboBoxText scenes_combobox;
private Widgets.ScenesComboBox scenes_combobox;
private Widgets.CommandComboBox command_combobox;
private string serial_number;
public Data.Command? command;
public uint8? key;
private uint8? key;
private Data.Command? command;
public signal void changed (Data.Command command);
@ -14,6 +13,9 @@ namespace Streamdeck.Views {
this.serial_number = serial_number;
build ();
command_combobox.select_first ();
scenes_combobox.select_first ();
}
public ConfigCommand.from_existing (string serial_number, Data.Command command) {
@ -50,12 +52,7 @@ namespace Streamdeck.Views {
command_combobox = new Widgets.CommandComboBox ();
var scenes_label = new Gtk.Label (_("to"));
scenes_combobox = new Gtk.ComboBoxText ();
scenes_combobox.id_column = 1;
foreach (var scene in Daemon.instance.get_scenes ()) {
scenes_combobox.append (scene, scene);
}
scenes_combobox = new Widgets.ScenesComboBox ();
var switch_scene_grid = new Gtk.Grid ();
switch_scene_grid.attach (scenes_label, 0, 0);
@ -80,18 +77,6 @@ namespace Streamdeck.Views {
show_all ();
scenes_combobox.changed.connect (() => {
var scene_name = scenes_combobox.get_active_text ();
if (command != null && command.get_command_type () == Data.CommandType.SWITCH_SCENE) {
unowned var switch_scene = (Data.SwitchScene) command;
switch_scene.scene_name = scene_name;
changed (command);
} else if (key != null) {
command = new Data.SwitchScene (key, scene_name);
changed (command);
}
});
changed.connect ((_obj, cmd) => {
Daemon.instance.add_command.begin (
serial_number,
@ -99,8 +84,8 @@ namespace Streamdeck.Views {
cmd.to_json (),
(obj, res) => {
try {
Daemon.instance.add_command.end (res);
Daemon.instance.update_command_cache (serial_number);
Daemon.instance.add_command.end (res);
Daemon.instance.update_command_cache (serial_number);
} catch (Error e) {
print ("Error saving command %s\n", e.message);
}
@ -108,21 +93,43 @@ namespace Streamdeck.Views {
);
});
command_combobox.selected.connect ((type) => {
if (key != null) {
handle_type_change (type);
}
});
scenes_combobox.selected.connect (handle_scene_change);
command_combobox.selected.connect (handle_type_change);
}
public void set_key (uint8 key) {
this.key = key;
var type = command_combobox.get_selected_type ();
if (type != null) {
handle_type_change (type);
}
}
private void handle_type_change (Data.CommandType type) {
switch (type) {
case Data.CommandType.SWITCH_SCENE:
command_stack.set_visible_child_name ("SwitchScene");
var scene_name = scenes_combobox.get_selected_scene ();
if (scene_name != null) {
handle_scene_change (scene_name);
}
break;
default:
break;
}
}
private void handle_scene_change (string scene_name) {
if (command != null && command.is_type (Data.CommandType.SWITCH_SCENE)) {
unowned var switch_scene = (Data.SwitchScene) command;
switch_scene.scene_name = scene_name;
changed (command);
} else if (key != null) {
command = new Data.SwitchScene (key, scene_name);
changed (command);
}
}
}
}

View file

@ -28,5 +28,19 @@ namespace Streamdeck.Widgets {
public Data.CommandType? get_selected_type () {
return _selected;
}
public void select_first () {
var id = get_active_id ();
if (id != null) {
return;
}
var available = Data.Command.available ();
if (available.length == 0) {
return;
}
set_active_id (available[0].id);
}
}
}

View file

@ -0,0 +1,44 @@
namespace Streamdeck.Widgets {
public class ScenesComboBox : Gtk.ComboBoxText {
public string? _selected;
public signal void selected (string scene_name);
construct {
id_column = 1;
foreach (var scene in Daemon.instance.get_scenes ()) {
append (scene, scene);
}
show_all ();
changed.connect (() => {
var scene_name = get_active_text ();
if (scene_name != null) {
selected (scene_name);
}
_selected = scene_name;
});
}
public string? get_selected_scene () {
return _selected;
}
public void select_first () {
var scene_name = get_active_text ();
if (scene_name != null) {
return;
}
var scenes = Daemon.instance.get_scenes ();
if (scenes.length == 0) {
return;
}
set_active_id (scenes[0]);
}
}
}