2019-07-02 12:36:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2019 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Felipe Borges <felipeborges@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cc-search-panel-row.h"
|
|
|
|
|
|
|
|
struct _CcSearchPanelRow
|
|
|
|
{
|
2022-01-22 14:57:42 -08:00
|
|
|
AdwActionRow parent_instance;
|
2019-07-02 12:36:42 +00:00
|
|
|
|
|
|
|
GAppInfo *app_info;
|
|
|
|
|
|
|
|
GtkImage *icon;
|
|
|
|
GtkSwitch *switcher;
|
2019-07-02 16:02:07 +02:00
|
|
|
|
|
|
|
GtkListBox *drag_widget;
|
2021-10-20 16:56:11 -03:00
|
|
|
gdouble drag_x;
|
|
|
|
gdouble drag_y;
|
2019-07-02 12:36:42 +00:00
|
|
|
};
|
|
|
|
|
2022-01-22 14:57:42 -08:00
|
|
|
G_DEFINE_TYPE (CcSearchPanelRow, cc_search_panel_row, ADW_TYPE_ACTION_ROW)
|
2019-07-02 12:36:42 +00:00
|
|
|
|
2019-07-02 16:02:07 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
SIGNAL_MOVE_ROW,
|
|
|
|
SIGNAL_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[SIGNAL_LAST] = { 0, };
|
|
|
|
|
2019-07-02 16:53:36 +02:00
|
|
|
static void
|
2022-01-22 15:25:23 -08:00
|
|
|
move_up_cb (GSimpleAction *action,
|
|
|
|
GVariant *parameter,
|
|
|
|
gpointer user_data)
|
2019-07-02 16:53:36 +02:00
|
|
|
{
|
2022-01-22 15:25:23 -08:00
|
|
|
CcSearchPanelRow *self = CC_SEARCH_PANEL_ROW (user_data);
|
2019-07-02 16:53:36 +02:00
|
|
|
GtkListBox *list_box = GTK_LIST_BOX (gtk_widget_get_parent (GTK_WIDGET (self)));
|
|
|
|
gint previous_idx = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (self)) - 1;
|
|
|
|
GtkListBoxRow *previous_row = gtk_list_box_get_row_at_index (list_box, previous_idx);
|
|
|
|
|
|
|
|
if (previous_row == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_signal_emit (self,
|
|
|
|
signals[SIGNAL_MOVE_ROW],
|
|
|
|
0,
|
|
|
|
previous_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-01-22 15:25:23 -08:00
|
|
|
move_down_cb (GSimpleAction *action,
|
|
|
|
GVariant *parameter,
|
|
|
|
gpointer user_data)
|
2019-07-02 16:53:36 +02:00
|
|
|
{
|
2022-01-22 15:25:23 -08:00
|
|
|
CcSearchPanelRow *self = CC_SEARCH_PANEL_ROW (user_data);
|
2019-07-02 16:53:36 +02:00
|
|
|
GtkListBox *list_box = GTK_LIST_BOX (gtk_widget_get_parent (GTK_WIDGET (self)));
|
|
|
|
gint next_idx = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (self)) + 1;
|
|
|
|
GtkListBoxRow *next_row = gtk_list_box_get_row_at_index (list_box, next_idx);
|
|
|
|
|
|
|
|
if (next_row == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_signal_emit (next_row,
|
|
|
|
signals[SIGNAL_MOVE_ROW],
|
|
|
|
0,
|
|
|
|
self);
|
|
|
|
}
|
|
|
|
|
2021-10-20 16:56:11 -03:00
|
|
|
static GdkContentProvider *
|
|
|
|
drag_prepare_cb (GtkDragSource *source,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
CcSearchPanelRow *self)
|
|
|
|
{
|
|
|
|
self->drag_x = x;
|
|
|
|
self->drag_y = y;
|
|
|
|
|
|
|
|
return gdk_content_provider_new_typed (CC_TYPE_SEARCH_PANEL_ROW, self);
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:02:07 +02:00
|
|
|
static void
|
2021-10-20 16:56:11 -03:00
|
|
|
drag_begin_cb (GtkDragSource *source,
|
|
|
|
GdkDrag *drag,
|
|
|
|
CcSearchPanelRow *self)
|
2019-07-02 16:02:07 +02:00
|
|
|
{
|
2021-10-20 16:56:11 -03:00
|
|
|
CcSearchPanelRow *panel_row;
|
2019-07-02 16:02:07 +02:00
|
|
|
GtkAllocation alloc;
|
2021-10-20 16:56:11 -03:00
|
|
|
GtkWidget *drag_icon;
|
2019-07-02 16:02:07 +02:00
|
|
|
|
|
|
|
gtk_widget_get_allocation (GTK_WIDGET (self), &alloc);
|
|
|
|
|
|
|
|
self->drag_widget = GTK_LIST_BOX (gtk_list_box_new ());
|
|
|
|
gtk_widget_set_size_request (GTK_WIDGET (self->drag_widget), alloc.width, alloc.height);
|
|
|
|
|
2021-10-20 16:56:11 -03:00
|
|
|
panel_row = cc_search_panel_row_new (self->app_info);
|
|
|
|
gtk_switch_set_active (panel_row->switcher, gtk_switch_get_active (self->switcher));
|
2019-07-02 16:02:07 +02:00
|
|
|
|
2022-01-22 14:57:42 -08:00
|
|
|
gtk_list_box_append (GTK_LIST_BOX (self->drag_widget), GTK_WIDGET (panel_row));
|
|
|
|
gtk_list_box_drag_highlight_row (self->drag_widget, GTK_LIST_BOX_ROW (panel_row));
|
2019-07-02 16:02:07 +02:00
|
|
|
|
2021-10-20 16:56:11 -03:00
|
|
|
drag_icon = gtk_drag_icon_get_for_drag (drag);
|
|
|
|
gtk_drag_icon_set_child (GTK_DRAG_ICON (drag_icon), GTK_WIDGET (self->drag_widget));
|
|
|
|
gdk_drag_set_hotspot (drag, self->drag_x, self->drag_y);
|
2019-07-02 16:02:07 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-20 16:56:11 -03:00
|
|
|
static gboolean
|
|
|
|
drop_cb (GtkDropTarget *drop_target,
|
|
|
|
const GValue *value,
|
|
|
|
gdouble x,
|
|
|
|
gdouble y,
|
|
|
|
CcSearchPanelRow *self)
|
2019-07-02 16:02:07 +02:00
|
|
|
{
|
|
|
|
CcSearchPanelRow *source;
|
|
|
|
|
2021-10-20 16:56:11 -03:00
|
|
|
g_message ("Drop");
|
|
|
|
|
|
|
|
if (!G_VALUE_HOLDS (value, CC_TYPE_SEARCH_PANEL_ROW))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
source = g_value_get_object (value);
|
2019-07-02 16:02:07 +02:00
|
|
|
|
|
|
|
g_signal_emit (source,
|
|
|
|
signals[SIGNAL_MOVE_ROW],
|
|
|
|
0,
|
|
|
|
self);
|
|
|
|
|
2021-10-20 16:56:11 -03:00
|
|
|
return TRUE;
|
|
|
|
}
|
2019-07-02 16:02:07 +02:00
|
|
|
|
2019-07-02 12:36:42 +00:00
|
|
|
static void
|
|
|
|
cc_search_panel_row_class_init (CcSearchPanelRowClass *klass)
|
|
|
|
{
|
2019-07-02 16:02:07 +02:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2019-07-02 12:36:42 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/search/cc-search-panel-row.ui");
|
|
|
|
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcSearchPanelRow, icon);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcSearchPanelRow, switcher);
|
2019-07-02 16:02:07 +02:00
|
|
|
|
|
|
|
signals[SIGNAL_MOVE_ROW] =
|
|
|
|
g_signal_new ("move-row",
|
|
|
|
G_TYPE_FROM_CLASS (object_class),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1, CC_TYPE_SEARCH_PANEL_ROW);
|
2019-07-02 12:36:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 15:25:23 -08:00
|
|
|
const GActionEntry row_entries[] = {
|
|
|
|
{ "move-up", move_up_cb, NULL, NULL, NULL, { 0 } },
|
|
|
|
{ "move-down", move_down_cb, NULL, NULL, NULL, { 0 } }
|
|
|
|
};
|
|
|
|
|
2019-07-02 12:36:42 +00:00
|
|
|
static void
|
|
|
|
cc_search_panel_row_init (CcSearchPanelRow *self)
|
|
|
|
{
|
2021-10-20 16:56:11 -03:00
|
|
|
GtkDragSource *drag_source;
|
|
|
|
GtkDropTarget *drop_target;
|
2022-01-22 15:25:23 -08:00
|
|
|
GSimpleActionGroup *group;
|
2021-10-20 16:56:11 -03:00
|
|
|
|
2019-07-02 12:36:42 +00:00
|
|
|
gtk_widget_init_template (GTK_WIDGET (self));
|
2019-07-02 16:02:07 +02:00
|
|
|
|
2021-10-20 16:56:11 -03:00
|
|
|
drag_source = gtk_drag_source_new ();
|
|
|
|
gtk_drag_source_set_actions (drag_source, GDK_ACTION_MOVE);
|
|
|
|
g_signal_connect (drag_source, "prepare", G_CALLBACK (drag_prepare_cb), self);
|
|
|
|
g_signal_connect (drag_source, "drag-begin", G_CALLBACK (drag_begin_cb), self);
|
|
|
|
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drag_source));
|
|
|
|
|
|
|
|
drop_target = gtk_drop_target_new (CC_TYPE_SEARCH_PANEL_ROW, GDK_ACTION_MOVE);
|
|
|
|
gtk_drop_target_set_preload (drop_target, TRUE);
|
|
|
|
g_signal_connect (drop_target, "drop", G_CALLBACK (drop_cb), self);
|
|
|
|
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drop_target));
|
2022-01-22 15:25:23 -08:00
|
|
|
|
|
|
|
group = g_simple_action_group_new ();
|
|
|
|
g_action_map_add_action_entries (G_ACTION_MAP (group),
|
|
|
|
row_entries,
|
|
|
|
G_N_ELEMENTS (row_entries),
|
|
|
|
self);
|
|
|
|
gtk_widget_insert_action_group (GTK_WIDGET (self), "row", G_ACTION_GROUP (group));
|
2019-07-02 12:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CcSearchPanelRow *
|
|
|
|
cc_search_panel_row_new (GAppInfo *app_info)
|
|
|
|
{
|
|
|
|
CcSearchPanelRow *self;
|
|
|
|
g_autoptr(GIcon) gicon = NULL;
|
|
|
|
|
|
|
|
self = g_object_new (CC_TYPE_SEARCH_PANEL_ROW, NULL);
|
|
|
|
self->app_info = g_object_ref (app_info);
|
|
|
|
|
|
|
|
gicon = g_app_info_get_icon (app_info);
|
|
|
|
if (gicon == NULL)
|
|
|
|
gicon = g_themed_icon_new ("application-x-executable");
|
|
|
|
else
|
|
|
|
g_object_ref (gicon);
|
2021-10-20 16:56:11 -03:00
|
|
|
gtk_image_set_from_gicon (self->icon, gicon);
|
2019-07-02 12:36:42 +00:00
|
|
|
|
2022-01-22 14:57:42 -08:00
|
|
|
adw_preferences_row_set_title (ADW_PREFERENCES_ROW (self),
|
|
|
|
g_app_info_get_name (app_info));
|
2019-07-02 12:36:42 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
GAppInfo *
|
|
|
|
cc_search_panel_row_get_app_info (CcSearchPanelRow *self)
|
|
|
|
{
|
|
|
|
return self->app_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
cc_search_panel_row_get_switch (CcSearchPanelRow *self)
|
|
|
|
{
|
|
|
|
return GTK_WIDGET (self->switcher);
|
|
|
|
}
|