Skip to content

Commit

Permalink
make setting track comment undoable, fix track comment not being edit…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
alex-tee committed Jan 24, 2021
1 parent 272cd3c commit efa35d2
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 15 deletions.
17 changes: 14 additions & 3 deletions inc/actions/tracklist_selections.h
Expand Up @@ -67,6 +67,7 @@ typedef enum EditTracksActionType
EDIT_TRACK_ACTION_TYPE_RENAME,

EDIT_TRACK_ACTION_TYPE_COLOR,
EDIT_TRACK_ACTION_TYPE_COMMENT,
EDIT_TRACK_ACTION_TYPE_ICON,
} EditTracksActionType;

Expand All @@ -80,6 +81,7 @@ static const cyaml_strval_t
{ "direct out", EDIT_TRACK_ACTION_TYPE_DIRECT_OUT },
{ "Rename", EDIT_TRACK_ACTION_TYPE_RENAME },
{ "Color", EDIT_TRACK_ACTION_TYPE_COLOR },
{ "comment", EDIT_TRACK_ACTION_TYPE_COMMENT },
{ "Icon", EDIT_TRACK_ACTION_TYPE_ICON },
};

Expand Down Expand Up @@ -168,7 +170,7 @@ typedef struct TracklistSelectionsAction

GdkRGBA new_color;

char * new_icon;
char * new_txt;

/** Skip do if true. */
bool already_edited;
Expand Down Expand Up @@ -227,7 +229,7 @@ static const cyaml_schema_field_t
TracklistSelectionsAction, new_color,
gdk_rgba_fields_schema),
YAML_FIELD_STRING_PTR_OPTIONAL (
TracklistSelectionsAction, new_icon),
TracklistSelectionsAction, new_txt),

CYAML_FIELD_END
};
Expand Down Expand Up @@ -273,7 +275,7 @@ tracklist_selections_action_new (
const GdkRGBA * color_new,
float val_before,
float val_after,
const char * new_icon,
const char * new_txt,
bool already_edited);

#define tracklist_selections_action_new_create( \
Expand Down Expand Up @@ -398,6 +400,15 @@ tracklist_selections_action_new (
false, false, NULL, \
0.f, 0.f, icon, false)

#define tracklist_selections_action_new_edit_comment( \
tls,comment) \
tracklist_selections_action_new ( \
TRACKLIST_SELECTIONS_ACTION_EDIT, \
tls, NULL, NULL, 0, NULL, NULL, -1, NULL, \
-1, EDIT_TRACK_ACTION_TYPE_COMMENT, NULL, \
false, false, NULL, \
0.f, 0.f, comment, false)

#define tracklist_selections_action_new_move( \
tls,track_pos) \
tracklist_selections_action_new ( \
Expand Down
13 changes: 12 additions & 1 deletion inc/audio/track.h
Expand Up @@ -1052,12 +1052,23 @@ track_activate_all_plugins (

/**
* Comment setter.
*
* @note This creates an undoable action.
*/
void
track_set_comment (
track_comment_setter (
void * track,
const char * comment);

/**
* @param undoable Create an undable action.
*/
void
track_set_comment (
Track * self,
const char * comment,
bool undoable);

/**
* Comment getter.
*/
Expand Down
6 changes: 6 additions & 0 deletions inc/gui/widgets/text_expander.h
Expand Up @@ -77,6 +77,12 @@ typedef struct _TextExpanderWidget
/** Editor buffer. */
GtkSourceBuffer * buffer;

GtkLabel * label;

GtkMenuButton * edit_btn;

GtkPopover * popover;

bool has_focus;
} TextExpanderWidget;

Expand Down
19 changes: 15 additions & 4 deletions src/actions/tracklist_selections.c
Expand Up @@ -84,7 +84,7 @@ tracklist_selections_action_new (
const GdkRGBA * color_new,
float val_before,
float val_after,
const char * new_icon,
const char * new_txt,
bool already_edited)
{
TracklistSelectionsAction * self =
Expand Down Expand Up @@ -257,9 +257,9 @@ tracklist_selections_action_new (
{
self->new_color = *color_new;
}
if (new_icon)
if (new_txt)
{
self->new_icon = g_strdup (new_icon);
self->new_txt = g_strdup (new_txt);
}

return ua;
Expand Down Expand Up @@ -1101,10 +1101,18 @@ do_or_undo_edit (
track_set_icon (
track,
_do ?
self->new_icon :
self->new_txt :
own_track_before->icon_name,
F_NOT_UNDOABLE, F_PUBLISH_EVENTS);
break;
case EDIT_TRACK_ACTION_TYPE_COMMENT:
track_set_comment (
track,
_do ?
self->new_txt :
own_track_before->comment,
F_NOT_UNDOABLE);
break;
}

EVENTS_PUSH (ET_TRACK_STATE_CHANGED, track);
Expand Down Expand Up @@ -1241,6 +1249,9 @@ tracklist_selections_action_stringize (
case EDIT_TRACK_ACTION_TYPE_ICON:
return g_strdup (
_("Change icon"));
case EDIT_TRACK_ACTION_TYPE_COMMENT:
return g_strdup (
_("Change comment"));
default:
g_return_val_if_reached (
g_strdup (""));
Expand Down
33 changes: 30 additions & 3 deletions src/audio/track.c
Expand Up @@ -2353,17 +2353,44 @@ track_activate_all_plugins (

/**
* Comment setter.
*
* @note This creates an undoable action.
*/
void
track_set_comment (
track_comment_setter (
void * track,
const char * comment)
{
Track * self = (Track *) track;
g_return_if_fail (IS_TRACK (self));

g_free (self->comment);
self->comment = g_strdup (comment);
track_set_comment (self, comment, F_UNDOABLE);
}

/**
* @param undoable Create an undable action.
*/
void
track_set_comment (
Track * self,
const char * comment,
bool undoable)
{
if (undoable)
{
track_select (
self, F_SELECT, F_EXCLUSIVE,
F_NO_PUBLISH_EVENTS);
UndoableAction * ua =
tracklist_selections_action_new_edit_comment (
TRACKLIST_SELECTIONS, comment);
undo_manager_perform (UNDO_MANAGER, ua);
}
else
{
g_free_and_null (self->comment);
self->comment = g_strdup (comment);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/inspector_track.c
Expand Up @@ -152,7 +152,7 @@ inspector_track_widget_show_tracks (

text_expander_widget_setup (
self->comment, track_get_comment,
track_set_comment, track);
track_comment_setter, track);
expander_box_widget_set_label (
Z_EXPANDER_BOX_WIDGET (self->comment),
_("Comment"));
Expand Down
43 changes: 40 additions & 3 deletions src/gui/widgets/text_expander.c
Expand Up @@ -66,6 +66,7 @@ on_focus_out (
GTK_TEXT_BUFFER (self->buffer),
&start_iter, &end_iter, false);
self->setter (self->obj, content);
text_expander_widget_refresh (self);
}

return FALSE;
Expand All @@ -83,6 +84,8 @@ text_expander_widget_refresh (
gtk_text_buffer_set_text (
GTK_TEXT_BUFFER (self->buffer),
self->getter (self->obj), -1);
gtk_label_set_text (
self->label, self->getter (self->obj));
}
}

Expand Down Expand Up @@ -113,6 +116,10 @@ static void
text_expander_widget_init (
TextExpanderWidget * self)
{
GtkWidget * box =
gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_visible (box, true);

self->scroll =
GTK_SCROLLED_WINDOW (
gtk_scrolled_window_new (
Expand All @@ -125,6 +132,8 @@ text_expander_widget_init (
self->scroll, GTK_SHADOW_ETCHED_IN);
gtk_widget_set_size_request (
GTK_WIDGET (self->scroll), -1, 124);
gtk_container_add (
GTK_CONTAINER (box), GTK_WIDGET (self->scroll));

self->viewport =
GTK_VIEWPORT (
Expand All @@ -135,19 +144,47 @@ text_expander_widget_init (
GTK_CONTAINER (self->scroll),
GTK_WIDGET (self->viewport));

self->label =
GTK_LABEL (gtk_label_new (""));
gtk_container_add (
GTK_CONTAINER (self->viewport),
GTK_WIDGET (self->label));
gtk_widget_set_visible (
GTK_WIDGET (self->label), true);
gtk_widget_set_vexpand (
GTK_WIDGET (self->label), true);

self->edit_btn =
GTK_MENU_BUTTON (gtk_menu_button_new ());
gtk_container_add (
GTK_CONTAINER (box),
GTK_WIDGET (self->edit_btn));
gtk_widget_set_visible (
GTK_WIDGET (self->edit_btn), true);
z_gtk_button_set_icon_name (
GTK_BUTTON (self->edit_btn), "edit");

self->popover =
GTK_POPOVER (
gtk_popover_new (
GTK_WIDGET (self->edit_btn)));
gtk_menu_button_set_popover (
GTK_MENU_BUTTON (self->edit_btn),
GTK_WIDGET (self->popover));

GtkSourceLanguageManager * manager =
gtk_source_language_manager_get_default ();
GtkSourceLanguage * lang =
gtk_source_language_manager_get_language (
manager, "ini");
manager, "markdown");
self->buffer =
gtk_source_buffer_new_with_language (lang);
self->editor =
GTK_SOURCE_VIEW (
gtk_source_view_new_with_buffer (
self->buffer));
gtk_container_add (
GTK_CONTAINER (self->viewport),
GTK_CONTAINER (self->popover),
GTK_WIDGET (self->editor));
gtk_widget_set_visible (
GTK_WIDGET (self->editor), true);
Expand Down Expand Up @@ -175,7 +212,7 @@ text_expander_widget_init (

expander_box_widget_add_content (
Z_EXPANDER_BOX_WIDGET (self),
GTK_WIDGET (self->scroll));
GTK_WIDGET (box));

expander_box_widget_set_icon_name (
Z_EXPANDER_BOX_WIDGET (self), "text-bubble");
Expand Down
31 changes: 31 additions & 0 deletions tests/actions/tracklist_selections_edit.c
Expand Up @@ -413,6 +413,37 @@ _test_edit_tracks (
g_free (icon_before);
}
break;
case EDIT_TRACK_ACTION_TYPE_COMMENT:
{
const char * new_icon = "icon2";
char * icon_before =
g_strdup (ins_track->comment);
track_set_comment (
ins_track, new_icon, F_UNDOABLE);
g_assert_true (
string_is_equal (
ins_track->comment, new_icon));

test_project_save_and_reload ();

ins_track = get_ins_track ();

/* undo/redo and re-verify */
undo_manager_undo (UNDO_MANAGER);
g_assert_true (
string_is_equal (
ins_track->comment, icon_before));
undo_manager_redo (UNDO_MANAGER);
g_assert_true (
string_is_equal (
ins_track->comment, new_icon));

/* undo to go back to original state */
undo_manager_undo (UNDO_MANAGER);

g_free (icon_before);
}
break;
default:
break;
}
Expand Down

0 comments on commit efa35d2

Please sign in to comment.