315 lines
8.4 KiB
C
315 lines
8.4 KiB
C
#define G_LOG_DOMAIN "main-window"
|
|
|
|
#include "main-window.h"
|
|
#include "panels/empty-panel.h"
|
|
#include "panels/mouse-panel.h"
|
|
#include "device/device.h"
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
struct _MainWindow
|
|
{
|
|
AdwApplicationWindow parent;
|
|
|
|
AdwHeaderBar* header;
|
|
AdwLeaflet* main_leaflet;
|
|
GtkListBox *device_list;
|
|
GtkBox* sidebar_box;
|
|
AdwWindowTitle* sidebar_title_widget;
|
|
GtkStack* stack;
|
|
|
|
GtkWidget* current_panel;
|
|
char* current_panel_id;
|
|
|
|
GtkWidget* custom_titlebar;
|
|
|
|
// CcShellModel *store;
|
|
|
|
Panel *active_panel;
|
|
GSettings* settings;
|
|
|
|
gboolean folded;
|
|
|
|
// CcPanelListView previous_list_view;
|
|
};
|
|
|
|
G_DEFINE_TYPE(MainWindow, main_window, ADW_TYPE_APPLICATION_WINDOW)
|
|
enum
|
|
{
|
|
PROP_0,
|
|
PROP_ACTIVE_PANEL,
|
|
PROP_MODEL,
|
|
PROP_FOLDED,
|
|
};
|
|
|
|
static gboolean activate_panel(MainWindow* self, Panel* panel, const gchar* id)
|
|
{
|
|
self->current_panel = GTK_WIDGET(panel);
|
|
self->active_panel = panel;
|
|
gtk_stack_add_named(self->stack, self->current_panel, id);
|
|
gtk_stack_set_visible_child_name (self->stack, id);
|
|
}
|
|
|
|
/*
|
|
* @Override from GtkWidget
|
|
*/
|
|
static void
|
|
main_window_map(GtkWidget* widget)
|
|
{
|
|
MainWindow* self = (MainWindow*) widget;
|
|
GTK_WIDGET_CLASS(main_window_parent_class)->map(widget);
|
|
}
|
|
|
|
/*
|
|
* @Override from GtkWidget
|
|
*/
|
|
static void
|
|
main_window_unmap(GtkWidget* widget)
|
|
{
|
|
MainWindow* self = GINPUT_MAIN_WINDOW(widget);
|
|
gboolean maximized;
|
|
gint height;
|
|
gint width;
|
|
|
|
maximized = gtk_window_is_maximized(GTK_WINDOW(self));
|
|
gtk_window_get_default_size(GTK_WINDOW(self), &width, &height);
|
|
|
|
g_settings_set(self->settings,
|
|
"window-state",
|
|
"(iib)",
|
|
width,
|
|
height,
|
|
maximized);
|
|
|
|
GTK_WIDGET_CLASS(main_window_parent_class)->unmap(widget);
|
|
}
|
|
|
|
static void
|
|
main_window_get_property(GObject* object, guint property_id, GValue* value, GParamSpec* pspec)
|
|
{
|
|
MainWindow* self = GINPUT_MAIN_WINDOW(object);
|
|
|
|
switch(property_id)
|
|
{
|
|
case PROP_ACTIVE_PANEL:
|
|
g_value_set_object (value, self->active_panel);
|
|
break;
|
|
|
|
// case PROP_MODEL:
|
|
// g_value_set_object (value, self->store);
|
|
// break;
|
|
|
|
case PROP_FOLDED:
|
|
g_value_set_boolean(value, self->folded);
|
|
break;
|
|
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
|
|
}
|
|
}
|
|
|
|
static void
|
|
main_window_set_property(GObject* object,
|
|
guint property_id,
|
|
const GValue* value,
|
|
GParamSpec* pspec)
|
|
{
|
|
MainWindow* self = GINPUT_MAIN_WINDOW(object);
|
|
|
|
switch(property_id)
|
|
{
|
|
// case PROP_ACTIVE_PANEL:
|
|
// set_active_panel(self, g_value_get_object(value));
|
|
// break;
|
|
|
|
// case PROP_MODEL:
|
|
// g_assert(self->store == NULL);
|
|
// self->store = g_value_dup_object(value);
|
|
// break;
|
|
|
|
case PROP_FOLDED:
|
|
self->folded = g_value_get_boolean(value);
|
|
break;
|
|
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
|
|
}
|
|
}
|
|
|
|
static void
|
|
load_window_state(MainWindow* self)
|
|
{
|
|
gint current_width = -1;
|
|
gint current_height = -1;
|
|
gboolean maximized = FALSE;
|
|
|
|
g_settings_get(self->settings,
|
|
"window-state",
|
|
"(iib)",
|
|
¤t_width,
|
|
¤t_height,
|
|
&maximized);
|
|
|
|
if(current_width != -1 && current_height != -1)
|
|
gtk_window_set_default_size(GTK_WINDOW(self), current_width, current_height);
|
|
if(maximized)
|
|
gtk_window_maximize(GTK_WINDOW(self));
|
|
}
|
|
|
|
void main_window_add_all_devices(MainWindow* self)
|
|
{
|
|
// Obtain devices, and iterate over the array to add them
|
|
array_t* devices = device_get_array();
|
|
for(size_t i = 0; i < array_count(devices); i++)
|
|
main_window_add_device_to_list(self, array_get(devices, i));
|
|
}
|
|
|
|
void main_window_device_selected(GtkListBox* self, GtkListBoxRow* row, gpointer user_data)
|
|
{
|
|
MainWindow* main_window = (MainWindow*) gtk_widget_get_root(GTK_WIDGET(self));
|
|
|
|
// Selection cleared
|
|
if(!row)
|
|
{
|
|
// TODO: set to empty panel ?
|
|
return;
|
|
}
|
|
|
|
device_t* device = (device_t*) g_object_get_data(G_OBJECT(row), "ginput_device");
|
|
MousePanel* mp = (Panel*) mouse_panel_new();
|
|
mouse_panel_set_device(mp, device);
|
|
|
|
// TODO : change and use already added child if possible
|
|
activate_panel(main_window, GINPUT_PANEL(mp), device_get_name(device));
|
|
}
|
|
|
|
static void
|
|
main_window_constructed(GObject* object)
|
|
{
|
|
MainWindow* self = GINPUT_MAIN_WINDOW(object);
|
|
|
|
load_window_state(self);
|
|
|
|
main_window_add_all_devices(self);
|
|
|
|
/* Add the panels */
|
|
// setup_model (self);
|
|
|
|
/* After everything is loaded, select the last used panel, if any,
|
|
* or the first visible panel. We do that in an idle handler so we
|
|
* have a chance to skip it when another panel has been explicitly
|
|
* activated from commandline parameter or from DBus method */
|
|
// g_idle_add_once ((GSourceOnceFunc) maybe_load_last_panel, self);
|
|
|
|
// g_signal_connect_swapped (self->panel_list,
|
|
// "notify::view",
|
|
// G_CALLBACK (update_headerbar_buttons),
|
|
// self);
|
|
|
|
// update_headerbar_buttons (self);
|
|
// adw_leaflet_set_visible_child (self->main_leaflet,
|
|
// GTK_WIDGET (self->sidebar_box));
|
|
|
|
G_OBJECT_CLASS(main_window_parent_class)->constructed(object);
|
|
}
|
|
|
|
static void
|
|
main_window_dispose(GObject* object)
|
|
{
|
|
MainWindow* self = GINPUT_MAIN_WINDOW(object);
|
|
|
|
g_clear_pointer(&self->current_panel_id, g_free);
|
|
// g_clear_object (&self->store);
|
|
// g_clear_object (&self->active_panel);
|
|
|
|
G_OBJECT_CLASS(main_window_parent_class)->dispose(object);
|
|
}
|
|
|
|
static void
|
|
main_window_finalize(GObject* object)
|
|
{
|
|
MainWindow* self = GINPUT_MAIN_WINDOW(object);
|
|
|
|
g_clear_object(&self->settings);
|
|
|
|
G_OBJECT_CLASS(main_window_parent_class)->finalize(object);
|
|
}
|
|
|
|
static void
|
|
main_window_class_init(MainWindowClass* klass)
|
|
{
|
|
GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass);
|
|
GObjectClass* object_class = G_OBJECT_CLASS(klass);
|
|
|
|
object_class->get_property = main_window_get_property;
|
|
object_class->set_property = main_window_set_property;
|
|
object_class->constructed = main_window_constructed;
|
|
object_class->dispose = main_window_dispose;
|
|
object_class->finalize = main_window_finalize;
|
|
|
|
widget_class->map = main_window_map;
|
|
widget_class->unmap = main_window_unmap;
|
|
|
|
g_object_class_install_property(object_class, PROP_FOLDED,
|
|
g_param_spec_boolean("folded", "Folded", "Whether the window is foled", FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
gtk_widget_class_set_template_from_resource(widget_class, "/v/ginput/main-window.ui");
|
|
|
|
gtk_widget_class_bind_template_child(widget_class, MainWindow, header);
|
|
gtk_widget_class_bind_template_child(widget_class, MainWindow, main_leaflet);
|
|
gtk_widget_class_bind_template_child(widget_class, MainWindow, device_list);
|
|
gtk_widget_class_bind_template_child(widget_class, MainWindow, sidebar_box);
|
|
gtk_widget_class_bind_template_child(widget_class, MainWindow, sidebar_title_widget);
|
|
gtk_widget_class_bind_template_child(widget_class, MainWindow, stack);
|
|
}
|
|
|
|
static void
|
|
main_window_init(MainWindow* self)
|
|
{
|
|
gtk_widget_init_template(GTK_WIDGET(self));
|
|
|
|
self->settings = g_settings_new("v.ginput");
|
|
|
|
g_object_bind_property(self->main_leaflet,
|
|
"folded",
|
|
self,
|
|
"folded",
|
|
G_BINDING_SYNC_CREATE);
|
|
|
|
activate_panel(self, empty_panel_new(), "empty");
|
|
|
|
g_signal_connect(self->device_list, "row_selected", G_CALLBACK(main_window_device_selected), NULL);
|
|
}
|
|
|
|
// TODO use translation
|
|
#define _(x) x
|
|
|
|
MainWindow*
|
|
main_window_new(AdwApplication* application)
|
|
{
|
|
g_return_val_if_fail(GTK_IS_APPLICATION(application), NULL);
|
|
|
|
return g_object_new(main_window_get_type(),
|
|
"application", application,
|
|
"resizable", TRUE,
|
|
"title", _("Input devices"),
|
|
"icon-name", "input-devices",
|
|
"show-menubar", FALSE,
|
|
NULL);
|
|
}
|
|
|
|
void main_window_add_device_to_list(MainWindow* self, device_t* device)
|
|
{
|
|
// Setup row
|
|
GtkListBoxRow* row = (GtkListBoxRow*) gtk_list_box_row_new();
|
|
GtkLabel* label = (GtkLabel*) gtk_label_new(device_get_name(device));
|
|
gtk_widget_add_css_class(GTK_WIDGET(label), "body");
|
|
gtk_widget_set_margin_top(GTK_WIDGET(label), 7);
|
|
gtk_widget_set_margin_bottom(GTK_WIDGET(label), 7);
|
|
gtk_list_box_row_set_child(row, GTK_WIDGET(label));
|
|
g_object_set_data(G_OBJECT(row), "ginput_device", device);
|
|
|
|
// Add row to listbox
|
|
gtk_list_box_append(GTK_LIST_BOX(self->device_list), GTK_WIDGET(row));
|
|
}
|