Skip to content

Commit

Permalink
undo: set appropriate undo action name
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Jan 28, 2024
1 parent 8725a7b commit 6895572
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 23 deletions.
6 changes: 6 additions & 0 deletions include/deadbeef/deadbeef.h
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,13 @@ typedef struct {
ddb_playItem_t * (*streamer_get_playing_track_safe) (void);
#endif

/// Called to create an undo action from all actions
/// accumulated since the previous calls
void (*undo_process)(void);

/// Set the action name to be displayed in Undo/Redo menu item.
/// The name is reset to null for each new action when undo_process is called.
void (*undo_set_action_name) (const char *name);
} DB_functions_t;

// NOTE: an item placement must be selected like this
Expand Down
35 changes: 21 additions & 14 deletions plugins/cocoaui/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -450,25 +450,25 @@ - (void)openFiles:(BOOL)clear play:(BOOL)play {
}
}
}
deadbeef->plt_add_files_end (plt, 0);
if (!fileadd_cancelled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (clear) {
deadbeef->plt_clear(plt_curr);
deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CONTENT, 0);
}
ddb_playItem_t *tail = deadbeef->plt_get_tail_item(plt_curr, PL_MAIN);
deadbeef->undo_set_action_name("Add Files");
[PlaylistUtil.shared moveItemsFromPlaylist:plt toPlaylist:plt_curr afterItem:tail];
if (tail != NULL) {
deadbeef->pl_item_unref (tail);
}
deadbeef->pl_save_current();
deadbeef->plt_add_files_end (plt, 0);
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);

if (play) {
deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CONTENT, 0);
deadbeef->sendmessage (DB_EV_PLAY_NUM, 0, 0, 0);
}
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);
});
}
else {
Expand Down Expand Up @@ -510,18 +510,18 @@ - (IBAction)addFoldersAction:(id)sender {
deadbeef->plt_add_dir2 (0, plt, fileName.UTF8String, NULL, NULL);
}
}
deadbeef->plt_add_files_end (plt, 0);
if (!fileadd_cancelled) {
dispatch_async(dispatch_get_main_queue(), ^{
ddb_playItem_t *tail = deadbeef->plt_get_tail_item(plt_curr, PL_MAIN);
[PlaylistUtil.shared moveItemsFromPlaylist:plt toPlaylist:plt_curr afterItem:tail];
if (tail != NULL) {
deadbeef->pl_item_unref (tail);
}
deadbeef->pl_save_current();
deadbeef->undo_set_action_name("Add Folders");
deadbeef->plt_add_files_end (plt, 0);
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);
deadbeef->pl_save_current();
deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CONTENT, 0);
});
}
else {
Expand All @@ -543,22 +543,30 @@ - (IBAction)addLocationAction:(id)sender {
if (returnCode == NSModalResponseOK) {
NSString *text = [self.addLocationTextField.stringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

ddb_playlist_t *plt = deadbeef->plt_get_curr ();
ddb_playlist_t *plt = deadbeef->plt_alloc ("add-location");
ddb_playlist_t *plt_curr = deadbeef->plt_get_curr ();
if (!deadbeef->plt_add_files_begin (plt, 0)) {
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(aQueue, ^{
DB_playItem_t *tail = deadbeef->plt_get_last (plt, PL_MAIN);
deadbeef->plt_insert_file2 (0, plt, tail, text.UTF8String, NULL, NULL, NULL);
deadbeef->plt_insert_file2 (0, plt, tail, text.UTF8String, NULL, NULL, NULL);
[PlaylistUtil.shared moveItemsFromPlaylist:plt toPlaylist:plt_curr afterItem:tail];
if (tail) {
deadbeef->pl_item_unref (tail);
}
deadbeef->plt_add_files_end (plt, 0);
deadbeef->plt_unref (plt);
deadbeef->pl_save_current ();

dispatch_async(dispatch_get_main_queue(), ^{
deadbeef->undo_set_action_name("Add Location");
deadbeef->plt_add_files_end (plt, 0);
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);
});
});
}
else {
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);
}
}
}];
Expand Down Expand Up @@ -857,15 +865,14 @@ - (IBAction)loadPlaylistAction:(id)sender {
if (!deadbeef->plt_add_files_begin (plt, 0)) {
int abort = 0;
deadbeef->plt_load2 (0, plt, NULL, fname.UTF8String, &abort, NULL, NULL);
deadbeef->plt_add_files_end (plt, 0);
if (!abort) {
dispatch_async(dispatch_get_main_queue(), ^{
deadbeef->plt_clear (plt_curr);
[PlaylistUtil.shared moveItemsFromPlaylist:plt toPlaylist:plt_curr afterItem:NULL];
deadbeef->plt_save_config (plt);
deadbeef->plt_add_files_end (plt, 0);
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);
deadbeef->sendmessage (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CONTENT, 0);
});
}
else {
Expand Down
3 changes: 3 additions & 0 deletions plugins/cocoaui/Playlist/PlaylistContentView.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
row = [self.dataModel rowForIndex:sel];
}


deadbeef->undo_set_action_name("Drag & drop");

if ([pboard.types containsObject:ddbPlaylistItemsUTIType]) {
NSArray *classes = @[[PlaylistLocalDragDropHolder class]];
NSDictionary *options = @{};
Expand Down
4 changes: 3 additions & 1 deletion plugins/cocoaui/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ int cocoaui_command (int command, ...) {
NSUndoManager *undoManager = appDelegate.mainWindow.window.undoManager;
DdbUndoBuffer *buffer = [[DdbUndoBuffer alloc] initWithUndoBuffer:undobuffer];

[undoManager setActionName:[NSString stringWithUTF8String:name]];
NSString *actionName = @(name ?: "");
actionName = [actionName stringByReplacingOccurrencesOfString:@"&" withString:@"&&"];
[undoManager setActionName:actionName];
[undoManager registerUndoBuffer:buffer];
}

Expand Down
9 changes: 8 additions & 1 deletion src/plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,15 @@ _viz_spectrum_listen_stub (void *ctx, void (*callback)(void *ctx, const ddb_audi

static void
_undo_process(void) {
undomanager_flush(undomanager_shared(), "Some Action");
undomanager_flush(undomanager_shared());
}

static void
_undo_set_action_name (const char *name) {
undomanager_set_action_name (undomanager_shared(), name);
}


// deadbeef api
static DB_functions_t deadbeef_api = {
.vmajor = DB_API_VERSION_MAJOR,
Expand Down Expand Up @@ -540,6 +546,7 @@ static DB_functions_t deadbeef_api = {

.streamer_get_playing_track_safe = (DB_playItem_t *(*) (void))streamer_get_playing_track,
.undo_process = _undo_process,
.undo_set_action_name = _undo_set_action_name,
};

DB_functions_t *deadbeef = &deadbeef_api;
Expand Down
28 changes: 22 additions & 6 deletions src/undo/undomanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
*/

#include <stdlib.h>
#include <string.h>
#include "undomanager.h"

extern DB_functions_t *deadbeef;

struct undomanager_s {
undobuffer_t *buffer;
char *action_name;
};

static undomanager_t *_shared;
Expand All @@ -44,6 +46,7 @@ undomanager_free (undomanager_t *undomanager) {
if (undomanager == _shared) {
_shared = NULL;
}
free (undomanager->action_name);
if (undomanager->buffer != NULL) {
undobuffer_free(undomanager->buffer);
}
Expand Down Expand Up @@ -87,17 +90,30 @@ _plug_get_gui (void) {
}

void
undomanager_flush(undomanager_t *undomanager, const char *name) {
undobuffer_t *undobuffer = undomanager_consume_buffer(undomanager);
if (!undobuffer_has_operations(undobuffer)) {
undomanager_set_action_name (undomanager_t *undomanager, const char *name) {
free (undomanager->action_name);
undomanager->action_name = name ? strdup (name) : NULL;
}

const char *
undomanager_get_action_name (undomanager_t *undomanager) {
return undomanager->action_name;
}

void
undomanager_flush(undomanager_t *undomanager) {
undobuffer_t *undobuffer = undomanager_consume_buffer (undomanager);
if (!undobuffer_has_operations (undobuffer)) {
return;
}

DB_plugin_t *ui_plugin = _plug_get_gui();
DB_plugin_t *ui_plugin = _plug_get_gui ();
if (ui_plugin && ui_plugin->command) {
ui_plugin->command (111, undobuffer, name);
ui_plugin->command (111, undobuffer, undomanager_get_action_name (undomanager));
}
else {
undobuffer_free(undobuffer); // lost
undobuffer_free (undobuffer); // lost
}

undomanager_set_action_name (undomanager, NULL);
}
9 changes: 8 additions & 1 deletion src/undo/undomanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ undomanager_consume_buffer (undomanager_t *undomanager);

// Send the accumulated undo buffer to the UI for registration
void
undomanager_flush(undomanager_t *undomanager, const char *name);
undomanager_flush(undomanager_t *undomanager);

void
undomanager_set_action_name (undomanager_t *undomanager, const char *name);

const char *
undomanager_get_action_name (undomanager_t *undomanager);


#pragma mark - Shared instance

Expand Down

0 comments on commit 6895572

Please sign in to comment.