ginput/src/ui/MainWindow.vala
vhaudiquet ee60060dc6 Moved UI from C to Vala
Multiple changes coming with that
Massive cleanup
2023-11-22 11:37:04 +01:00

81 lines
2.1 KiB
Vala

using Device;
[GtkTemplate (ui="/v/ginput/main-window.ui")]
public class MainWindow : Adw.ApplicationWindow
{
[GtkChild]
protected unowned Gtk.ListBox device_list;
[GtkChild]
protected unowned Gtk.Stack stack;
protected Panel active_panel;
public MainWindow(Adw.Application app)
{
Object(application:app, resizable:true, title:"Input devices", icon_name:"ginput-icon", show_menubar:false);
}
construct
{
init_template();
set_default_size(1024, 768);
device_list.row_selected.connect(select_device);
}
void activate_panel(Panel panel)
{
var pname = panel.get_name();
if(stack.get_child_by_name(pname) == null)
stack.add_named(panel, pname);
stack.set_visible_child_name(pname);
active_panel = panel;
}
void select_device(Gtk.ListBox list, Gtk.ListBoxRow? row)
{
if(row == null)
return;
void* device = row.get_data("ginput_device");
var name = Device.get_name(device);
Panel devpane;
var capacity = Device.get_capacity(device);
if((capacity & Device.Capacity.MOUSE) != 0)
devpane = new MousePanel(name, device);
else
{
stderr.printf("Warning: selected device with unsupported capacity\n");
return;
}
activate_panel(devpane);
}
public void add_device(void* device)
{
Gtk.ListBoxRow row = new Gtk.ListBoxRow();
Gtk.Label label = new Gtk.Label(Device.get_name(device));
label.add_css_class("body");
label.set_margin_top(7);
label.set_margin_bottom(7);
row.set_child(label);
row.set_data("ginput_device", device);
device_list.append(row);
}
public override void constructed()
{
// Add all devices to the window
var devices = Device.get_array();
for(size_t i = 0; i < Array.count(devices); i++)
add_device(Array.get(devices, i));
// Activate empty panel by default
activate_panel(new EmptyPanel());
base.constructed();
}
}