Additional work on undo support

This commit is contained in:
Damiano Galassi committed 2026-10-03 10:13:39 +02:00
1 parent f883d07809
commit bb12164229
4 files changed
+74 -22

No files matched your search

+6 -6
View File
@@ -230,9 +230,9 @@ MP42_OBJC_DIRECT_MEMBERS
- (void)setFallbackTrack:(MP42Track *)newFallbackTrack - (void)setFallbackTrack:(MP42Track *)newFallbackTrack
{ {
MP42Track *_fallbackTrackStrongRef = _fallbackTrack; MP42Track *fallbackTrackStrongRef = _fallbackTrack;
if (_fallbackTrackStrongRef != newFallbackTrack) { if (fallbackTrackStrongRef != newFallbackTrack) {
[[self.undo prepareWithInvocationTarget:self] setFallbackTrack:_fallbackTrackStrongRef]; [[self.undo prepareWithInvocationTarget:self] setFallbackTrack:fallbackTrackStrongRef];
} }
_fallbackTrack = newFallbackTrack; _fallbackTrack = newFallbackTrack;
@@ -246,9 +246,9 @@ MP42_OBJC_DIRECT_MEMBERS
- (void)setFollowsTrack:(MP42Track *)newFollowsTrack - (void)setFollowsTrack:(MP42Track *)newFollowsTrack
{ {
MP42Track *_followsTrackStrongRef = _followsTrack; MP42Track *followsTrackStrongRef = _followsTrack;
if (_followsTrackStrongRef != newFollowsTrack) { if (followsTrackStrongRef != newFollowsTrack) {
[[self.undo prepareWithInvocationTarget:self] setFollowsTrack:_followsTrackStrongRef]; [[self.undo prepareWithInvocationTarget:self] setFollowsTrack:followsTrackStrongRef];
} }
_followsTrack = newFollowsTrack; _followsTrack = newFollowsTrack;
+50 -14
View File
@@ -12,7 +12,6 @@
#import "MP42PrivateUtilities.h" #import "MP42PrivateUtilities.h"
#import "MP42MediaFormat.h" #import "MP42MediaFormat.h"
MP42_OBJC_DIRECT_MEMBERS
@implementation MP42ChapterTrack { @implementation MP42ChapterTrack {
@private @private
NSMutableArray<MP42TextSample *> *_chapters; NSMutableArray<MP42TextSample *> *_chapters;
@@ -102,19 +101,24 @@ MP42_OBJC_DIRECT_MEMBERS
return [[MP42ChapterTrack alloc] initWithTextFile:URL]; return [[MP42ChapterTrack alloc] initWithTextFile:URL];
} }
- (NSString *)defaultName { - (NSString *)defaultName
{
NSBundle *bundle = [NSBundle bundleForClass:[MP42ChapterTrack class]]; NSBundle *bundle = [NSBundle bundleForClass:[MP42ChapterTrack class]];
return NSLocalizedStringFromTableInBundle(@"Chapter Track", @"Localizable", bundle, @"Default Chapter Track name"); return NSLocalizedStringFromTableInBundle(@"Chapter Track", @"Localizable", bundle, @"Default Chapter Track name");
} }
- (NSUInteger)addChapter:(MP42TextSample *)chapter - (NSUInteger)addChapter:(MP42TextSample *)chapter
{ {
self.edited = YES;
_areChaptersEdited = YES;
[_chapters addObject:chapter]; [_chapters addObject:chapter];
[_chapters sortUsingSelector:@selector(compare:)]; [_chapters sortUsingSelector:@selector(compare:)];
[[self.undo prepareWithInvocationTarget:self] removeChapter:chapter];
if (!(self.undo.isUndoing || self.undo.isRedoing)) {
self.edited = YES;
_areChaptersEdited = YES;
}
return [_chapters indexOfObject:chapter]; return [_chapters indexOfObject:chapter];
} }
@@ -129,7 +133,8 @@ MP42_OBJC_DIRECT_MEMBERS
return idx; return idx;
} }
- (NSUInteger)addChapter:(NSString *)title image:(MP42Image *)image duration:(uint64_t)timestamp { - (NSUInteger)addChapter:(NSString *)title image:(MP42Image *)image duration:(uint64_t)timestamp
{
MP42TextSample *newChapter = [[MP42TextSample alloc] init]; MP42TextSample *newChapter = [[MP42TextSample alloc] init];
newChapter.title = title; newChapter.title = title;
newChapter.image = image; newChapter.image = image;
@@ -140,10 +145,22 @@ MP42_OBJC_DIRECT_MEMBERS
return idx; return idx;
} }
- (NSUInteger)indexOfChapter:(MP42TextSample *)chapterSample { - (NSUInteger)indexOfChapter:(MP42TextSample *)chapterSample
{
return [_chapters indexOfObject:chapterSample]; return [_chapters indexOfObject:chapterSample];
} }
- (void)removeChapter:(MP42TextSample *)chapter
{
[[self.undo prepareWithInvocationTarget:self] addChapter:chapter];
[_chapters removeObject:chapter];
if (!(self.undo.isUndoing || self.undo.isRedoing)) {
self.edited = YES;
_areChaptersEdited = YES;
}
}
- (void)removeChapterAtIndex:(NSUInteger)index - (void)removeChapterAtIndex:(NSUInteger)index
{ {
[self removeChaptersAtIndexes:[NSIndexSet indexSetWithIndex:index]]; [self removeChaptersAtIndexes:[NSIndexSet indexSetWithIndex:index]];
@@ -151,24 +168,43 @@ MP42_OBJC_DIRECT_MEMBERS
- (void)removeChaptersAtIndexes:(NSIndexSet *)indexes - (void)removeChaptersAtIndexes:(NSIndexSet *)indexes
{ {
self.edited = YES; NSArray<MP42TextSample *> *toBeRemoved = [_chapters objectsAtIndexes:indexes];
_areChaptersEdited = YES;
[_chapters removeObjectsAtIndexes:indexes]; for (MP42TextSample *chapter in toBeRemoved) {
[self removeChapter:chapter];
}
} }
- (void)setTimestamp:(MP4Duration)timestamp forChapter:(MP42TextSample *)chapterSample - (void)setTimestamp:(MP4Duration)timestamp forChapter:(MP42TextSample *)chapterSample
{ {
self.edited = YES; MP4Duration previousTimestamp = chapterSample.timestamp;
_areChaptersEdited = YES;
if (previousTimestamp != timestamp) {
[[self.undo prepareWithInvocationTarget:self] setTimestamp:previousTimestamp forChapter:chapterSample];
}
[chapterSample setTimestamp:timestamp]; [chapterSample setTimestamp:timestamp];
[_chapters sortUsingSelector:@selector(compare:)]; [_chapters sortUsingSelector:@selector(compare:)];
if (!(self.undo.isUndoing || self.undo.isRedoing)) {
self.edited = YES;
_areChaptersEdited = YES;
}
} }
- (void)setTitle:(NSString *)title forChapter:(MP42TextSample *)chapterSample - (void)setTitle:(NSString *)title forChapter:(MP42TextSample *)chapterSample
{ {
self.edited = YES; NSString *previousTitle = chapterSample.title;
_areChaptersEdited = YES; if ([previousTitle isEqualToString:title] == NO) {
[[self.undo prepareWithInvocationTarget:self] setTitle:previousTitle forChapter:chapterSample];
}
[chapterSample setTitle:title]; [chapterSample setTitle:title];
if (!(self.undo.isUndoing || self.undo.isRedoing)) {
self.edited = YES;
_areChaptersEdited = YES;
}
} }
- (MP42TextSample *)chapterAtIndex:(NSUInteger)index - (MP42TextSample *)chapterAtIndex:(NSUInteger)index
+8
View File
@@ -490,6 +490,10 @@ MP42_OBJC_DIRECT_MEMBERS
track.duration = self.duration; track.duration = self.duration;
} }
track.undo = self.undo;
[[self.undo prepareWithInvocationTarget:self] removeTracks:@[track]];
[self.itracks addObject:track]; [self.itracks addObject:track];
} }
@@ -518,6 +522,10 @@ MP42_OBJC_DIRECT_MEMBERS
} }
} }
for (MP42Track *track in tracks) {
[(MP42File *)[self.undo prepareWithInvocationTarget:self] addTrack:track];
}
[self.itracks removeObjectsInArray:tracks]; [self.itracks removeObjectsInArray:tracks];
} }
+10 -2
View File
@@ -396,10 +396,18 @@ fail:
- (void)setForcedTrack:(MP42Track *)newForcedTrack - (void)setForcedTrack:(MP42Track *)newForcedTrack
{ {
MP42Track *forcedTrackStrongRef = _forcedTrack;
if (forcedTrackStrongRef != newForcedTrack) {
[[self.undo prepareWithInvocationTarget:self] setForcedTrack:forcedTrackStrongRef];
}
_forcedTrack = newForcedTrack; _forcedTrack = newForcedTrack;
_forcedTrackId = 0; _forcedTrackId = 0;
self.edited = YES;
self.updatedProperty[@"forced"] = @YES; if (!(self.undo.isUndoing || self.undo.isRedoing)) {
self.edited = YES;
self.updatedProperty[@"forced"] = @YES;
}
} }
#pragma mark - NSCopying #pragma mark - NSCopying