diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/tab-label.c | 114 |
1 files changed, 112 insertions, 2 deletions
diff --git a/src/tab-label.c b/src/tab-label.c index b9fcbbd..ec3429c 100644 --- a/src/tab-label.c +++ b/src/tab-label.c @@ -44,6 +44,8 @@ struct _MqTabLabel { gchar *scrolled_title; gchar *scrolled_markup; GtkWidget *popover; + GtkWidget *name_popover; + GtkWidget *name_entry; }; enum { @@ -243,13 +245,121 @@ set_web_view(MqTabLabel *tab_label, MqWebView *web_view) G_CALLBACK(title_cb), tab_label); } +static void +set_custom_title(MqTabLabel *tab_label, const gchar *title) +{ + /* TODO: Handle empty string. Set custom title instead of overriding + * (temporarily) regular title. */ + mq_tab_page_set_title(tab_label->tab_page, title); + set_title(tab_label, title); +} + +static gboolean +name_entry_key_press_event_cb(GtkEntry G_GNUC_UNUSED *entry, GdkEventKey *event, + MqTabLabel *tab_label) +{ + switch (event->keyval) { + case GDK_KEY_Escape: + gtk_widget_hide(tab_label->name_popover); + return TRUE; + case GDK_KEY_Return: + case GDK_KEY_KP_Enter: + case GDK_KEY_ISO_Enter: + set_custom_title(tab_label, gtk_entry_get_text( + GTK_ENTRY(tab_label->name_entry))); + gtk_widget_hide(tab_label->name_popover); + return TRUE; + default: + return FALSE; + } +} + +static void +name_entry_changed_cb(GtkEditable *editable, G_GNUC_UNUSED gpointer user_data) +{ + GtkEntry *entry; + const gchar *text; + const gchar *icon; + + entry = GTK_ENTRY(editable); + + text = gtk_entry_get_text(entry); + if (text && text[0]) { + icon = "edit-clear-symbolic"; + } else { + icon = NULL; + } + + gtk_entry_set_icon_from_icon_name(entry, GTK_ENTRY_ICON_SECONDARY, + icon); + gtk_entry_set_icon_activatable(entry, GTK_ENTRY_ICON_SECONDARY, + icon ? TRUE : FALSE); + gtk_entry_set_icon_sensitive(entry, GTK_ENTRY_ICON_SECONDARY, + icon ? TRUE : FALSE); +} + +static void +name_entry_icon_press_cb(GtkEntry *entry, GtkEntryIconPosition icon_pos, + G_GNUC_UNUSED GdkEvent *event, G_GNUC_UNUSED gpointer user_data) +{ + if (icon_pos == GTK_ENTRY_ICON_SECONDARY) { + gtk_entry_set_text(entry, ""); + } +} + +static void +name_close_clicked_cb(G_GNUC_UNUSED GtkButton *button, MqTabLabel *tab_label) +{ + set_custom_title(tab_label, gtk_entry_get_text( + GTK_ENTRY(tab_label->name_entry))); + gtk_widget_hide(tab_label->name_popover); +} + +static void +create_name_popover(MqTabLabel *tab_label) +{ + GtkWidget *close_button; + GtkWidget *box; + + /* Title entry */ + tab_label->name_entry = gtk_entry_new(); + g_signal_connect(tab_label->name_entry, "key-press-event", + G_CALLBACK(name_entry_key_press_event_cb), tab_label); + g_signal_connect(GTK_EDITABLE(tab_label->name_entry), "changed", + G_CALLBACK(name_entry_changed_cb), NULL); + g_signal_connect(GTK_ENTRY(tab_label->name_entry), "icon-press", + G_CALLBACK(name_entry_icon_press_cb), NULL); + gtk_entry_set_text(GTK_ENTRY(tab_label->name_entry), tab_label->title); + + /* Close button */ + close_button = gtk_button_new_from_icon_name("window-close", + GTK_ICON_SIZE_BUTTON); + gtk_button_set_relief(GTK_BUTTON(close_button), GTK_RELIEF_NONE); + gtk_widget_set_tooltip_text(close_button, _("Rename tab")); + gtk_widget_set_can_focus(close_button, FALSE); + g_signal_connect(close_button, "clicked", + G_CALLBACK(name_close_clicked_cb), tab_label); + + /* Box */ + box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); + gtk_box_pack_start(GTK_BOX(box), tab_label->name_entry, FALSE, FALSE, + 0); + gtk_box_pack_end(GTK_BOX(box), close_button, FALSE, FALSE, 0); + + /* Set up the popover. */ + tab_label->name_popover = gtk_popover_new(GTK_WIDGET(tab_label)); + gtk_container_add(GTK_CONTAINER(tab_label->name_popover), box); + + /* NB: gtk_popover_popup() is new in GTK+ 3.22. */ + gtk_widget_show_all(tab_label->name_popover); +} + static gboolean button_press_cb(GtkWidget *widget, GdkEventButton *event, MqTabLabel *tab_label) { if (event->type == GDK_2BUTTON_PRESS) { - mq_tab_page_set_title(tab_label->tab_page, "Renamed"); - set_title(tab_label, "Renamed"); + create_name_popover(tab_label); } else if (event->button == 2) { mq_tab_page_close(tab_label->tab_page); } else if (event->button == 3) { |