libsysprof-ui: fix can-save propagation

This commit is contained in:
Christian Hergert
2019-05-19 17:11:53 -07:00
parent 2c8d6c0e3e
commit 6edf258732
4 changed files with 135 additions and 46 deletions

View File

@ -75,6 +75,24 @@ sysprof_display_new (void)
return g_object_new (SYSPROF_TYPE_DISPLAY, NULL);
}
static void
sysprof_display_load_cb (SysprofCaptureView *view,
GAsyncResult *result,
gpointer user_data)
{
g_autoptr(SysprofDisplay) self = user_data;
g_autoptr(GError) error = NULL;
g_assert (SYSPROF_IS_CAPTURE_VIEW (view));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (SYSPROF_IS_DISPLAY (self));
if (!sysprof_capture_view_load_finish (view, result, &error))
g_warning ("Failed to load capture: %s", error->message);
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CAN_SAVE]);
}
static void
sysprof_display_profiler_failed_cb (SysprofDisplay *self,
const GError *error,
@ -120,7 +138,11 @@ sysprof_display_profiler_stopped_cb (SysprofDisplay *self,
goto notify;
}
sysprof_capture_view_load_async (priv->capture_view, reader, NULL, NULL, NULL);
sysprof_capture_view_load_async (priv->capture_view,
reader,
NULL,
(GAsyncReadyCallback) sysprof_display_load_cb,
g_object_ref (self));
gtk_stack_set_visible_child (priv->stack, GTK_WIDGET (priv->capture_view));
}
@ -293,19 +315,6 @@ sysprof_display_get_property (GObject *object,
}
}
static void
sysprof_display_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
sysprof_display_class_init (SysprofDisplayClass *klass)
{
@ -314,7 +323,6 @@ sysprof_display_class_init (SysprofDisplayClass *klass)
object_class->finalize = sysprof_display_finalize;
object_class->get_property = sysprof_display_get_property;
object_class->set_property = sysprof_display_set_property;
widget_class->parent_set = sysprof_display_parent_set;
@ -436,7 +444,11 @@ sysprof_display_open_cb (GObject *object,
return;
}
sysprof_capture_view_load_async (priv->capture_view, reader, NULL, NULL, NULL);
sysprof_capture_view_load_async (priv->capture_view,
reader,
NULL,
(GAsyncReadyCallback) sysprof_display_load_cb,
g_object_ref (self));
gtk_stack_set_visible_child (priv->stack, GTK_WIDGET (priv->capture_view));
}

View File

@ -28,6 +28,14 @@
G_DEFINE_TYPE (SysprofNotebook, sysprof_notebook, GTK_TYPE_NOTEBOOK)
enum {
PROP_0,
PROP_CAN_SAVE,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/**
* sysprof_notebook_new:
*
@ -43,6 +51,17 @@ sysprof_notebook_new (void)
return g_object_new (SYSPROF_TYPE_NOTEBOOK, NULL);
}
static void
sysprof_notebook_notify_can_save_cb (SysprofNotebook *self,
GParamSpec *pspec,
SysprofDisplay *display)
{
g_assert (SYSPROF_IS_NOTEBOOK (self));
g_assert (SYSPROF_IS_DISPLAY (display));
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CAN_SAVE]);
}
static void
sysprof_notebook_page_added (GtkNotebook *notebook,
GtkWidget *child,
@ -57,6 +76,14 @@ sysprof_notebook_page_added (GtkNotebook *notebook,
gtk_notebook_set_tab_label (notebook, child, tab);
gtk_notebook_set_tab_reorderable (notebook, child, TRUE);
g_signal_connect_object (child,
"notify::can-save",
G_CALLBACK (sysprof_notebook_notify_can_save_cb),
notebook,
G_CONNECT_SWAPPED);
g_object_notify_by_pspec (G_OBJECT (notebook), properties [PROP_CAN_SAVE]);
}
gtk_notebook_set_show_tabs (notebook,
@ -79,19 +106,68 @@ sysprof_notebook_page_removed (GtkNotebook *notebook,
child = sysprof_display_new ();
gtk_container_add (GTK_CONTAINER (notebook), child);
gtk_widget_show (child);
g_signal_handlers_disconnect_by_func (child,
G_CALLBACK (sysprof_notebook_notify_can_save_cb),
notebook);
g_object_notify_by_pspec (G_OBJECT (notebook), properties [PROP_CAN_SAVE]);
}
gtk_notebook_set_show_tabs (notebook,
gtk_notebook_get_n_pages (notebook) > 1);
}
static void
sysprof_notebook_switch_page (GtkNotebook *notebook,
GtkWidget *widget,
guint page)
{
g_assert (GTK_IS_NOTEBOOK (notebook));
g_assert (GTK_IS_WIDGET (widget));
GTK_NOTEBOOK_CLASS (sysprof_notebook_parent_class)->switch_page (notebook, widget, page);
g_object_notify_by_pspec (G_OBJECT (notebook), properties [PROP_CAN_SAVE]);
}
static void
sysprof_notebook_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
SysprofNotebook *self = (SysprofNotebook *)object;
switch (prop_id)
{
case PROP_CAN_SAVE:
g_value_set_boolean (value, sysprof_notebook_get_can_save (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
sysprof_notebook_class_init (SysprofNotebookClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass);
object_class->get_property = sysprof_notebook_get_property;
notebook_class->page_added = sysprof_notebook_page_added;
notebook_class->page_removed = sysprof_notebook_page_removed;
notebook_class->switch_page = sysprof_notebook_switch_page;
properties [PROP_CAN_SAVE] =
g_param_spec_boolean ("can-save",
"Can Save",
"If the current display can save a recording",
FALSE,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
}
static void
@ -184,3 +260,16 @@ sysprof_notebook_save (SysprofNotebook *self)
if ((display = sysprof_notebook_get_current (self)))
sysprof_display_save (display);
}
gboolean
sysprof_notebook_get_can_save (SysprofNotebook *self)
{
SysprofDisplay *display;
g_return_val_if_fail (SYSPROF_IS_NOTEBOOK (self), FALSE);
if ((display = sysprof_notebook_get_current (self)))
return sysprof_display_get_can_save (display);
return FALSE;
}

View File

@ -51,5 +51,7 @@ void sysprof_notebook_open (SysprofNotebook *self,
GFile *file);
SYSPROF_AVAILABLE_IN_ALL
void sysprof_notebook_save (SysprofNotebook *self);
SYSPROF_AVAILABLE_IN_ALL
gboolean sysprof_notebook_get_can_save (SysprofNotebook *self);
G_END_DECLS

View File

@ -32,8 +32,6 @@ struct _SysprofWindow
{
GtkApplicationWindow parent_instance;
DzlBindingGroup *display_bindings;
SysprofNotebook *notebook;
GtkMenuButton *menu_button;
};
@ -55,6 +53,19 @@ sysprof_window_new (SysprofApplication *application)
NULL);
}
static void
sysprof_window_notify_can_save_cb (SysprofWindow *self,
GParamSpec *pspec,
SysprofNotebook *notebook)
{
g_assert (SYSPROF_IS_WINDOW (self));
g_assert (SYSPROF_IS_NOTEBOOK (notebook));
dzl_gtk_widget_action_set (GTK_WIDGET (self), "win", "save-capture",
"enabled", sysprof_notebook_get_can_save (notebook),
NULL);
}
static void
new_tab_cb (GSimpleAction *action,
GVariant *param,
@ -121,29 +132,9 @@ save_capture_cb (GSimpleAction *action,
sysprof_notebook_save (self->notebook);
}
static void
sysprof_window_switch_page_cb (SysprofWindow *self,
GtkWidget *widget,
guint page_num,
SysprofNotebook *notebook)
{
SysprofDisplay *current;
g_assert (SYSPROF_IS_WINDOW (self));
g_assert (SYSPROF_IS_NOTEBOOK (notebook));
current = sysprof_notebook_get_current (notebook);
dzl_binding_group_set_source (self->display_bindings, current);
}
static void
sysprof_window_finalize (GObject *object)
{
SysprofWindow *self = SYSPROF_WINDOW (object);
dzl_binding_group_set_source (self->display_bindings, NULL);
g_clear_object (&self->display_bindings);
G_OBJECT_CLASS (sysprof_window_parent_class)->finalize (object);
}
@ -167,7 +158,6 @@ sysprof_window_class_init (SysprofWindowClass *klass)
static void
sysprof_window_init (SysprofWindow *self)
{
GAction *action;
static GActionEntry actions[] = {
{ "close-tab", close_tab_cb },
{ "new-tab", new_tab_cb },
@ -181,16 +171,12 @@ sysprof_window_init (SysprofWindow *self)
actions,
G_N_ELEMENTS (actions),
self);
action = g_action_map_lookup_action (G_ACTION_MAP (self), "save-capture");
self->display_bindings = dzl_binding_group_new ();
dzl_binding_group_bind (self->display_bindings, "can-save", action, "enabled", G_BINDING_SYNC_CREATE);
g_signal_connect_object (self->notebook,
"switch-page",
G_CALLBACK (sysprof_window_switch_page_cb),
"notify::can-save",
G_CALLBACK (sysprof_window_notify_can_save_cb),
self,
G_CONNECT_SWAPPED | G_CONNECT_AFTER);
G_CONNECT_SWAPPED);
dzl_gtk_widget_action_set (GTK_WIDGET (self), "win", "save-capture",
"enabled", FALSE,