Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/update scripts #234

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
Fixing issue where activation shows beachball
  • Loading branch information
monkeydom committed Mar 25, 2023
commit c39d1957ade8f0b5279e88752d84302d236f942f
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Updated and fixed applescripts. Changed some of them to JSX
* Updated expired certificate on the see tool installer
* Fixed issue with badly exported UTIs
* Fixed issue where activation could show beachball when working with slow/remote disks

#### Improved modes:

Expand Down
155 changes: 86 additions & 69 deletions SubEthaEdit-Mac/Source/PlainTextDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -3904,6 +3904,7 @@ - (NSData *)dataOfType:(NSString *)aType error:(NSError **)outError {
}

- (BOOL)onDiskRepresentationHasChanged {
[NSThread sleepForTimeInterval:2.0];
NSURL *fileURL = self.fileURL;
NSString *fileName = fileURL.path;

Expand Down Expand Up @@ -3953,82 +3954,86 @@ - (BOOL)onDiskRepresentationHasChanged {

}

- (BOOL)TCM_validateDocument {
NSString *fileName = [[self fileURL] path];
DEBUGLOG(@"FileIOLogDomain", SimpleLogLevel, @"Validate document: %@", fileName);

- (void)showDocumentHasChangedDialog {
DEBUGLOG(@"FileIOLogDomain", DetailedLogLevel, @"Document has been changed externally");

if (![NSFileManager.defaultManager fileExistsAtPath:fileName] || ![self onDiskRepresentationHasChanged]) {
return YES;
BOOL isDocumentEdited = [self isDocumentEdited];

NSModalResponse revertResponseCode;
NSString *message, *details, *firstButton, *secondButton;
id alertAdjustment;

if (isDocumentEdited) {
// We intentionally do have a more alerting message if the document already had some changes.
message = NSLocalizedString(@"The file has been modified by another application. Do you want to keep the changes made in SubEthaEdit?", nil);
details = NSLocalizedString(@"If you revert the file to the version on disk the changes you made in SubEthaEdit will be lost.", nil);
firstButton = NSLocalizedString(@"Keep Changes", nil);
secondButton = NSLocalizedString(@"Revert", nil);
revertResponseCode = NSAlertSecondButtonReturn;
} else {
DEBUGLOG(@"FileIOLogDomain", DetailedLogLevel, @"Document has been changed externally");
if ([self keepDocumentVersion]) {
DEBUGLOG(@"FileIOLogDomain", DetailedLogLevel, @"Keep document version");
return YES;
}

BOOL isDocumentEdited = [self isDocumentEdited];
message = NSLocalizedString(@"The document's file has been modified by another application. Do you want to revert the document?", nil);
details = NSLocalizedString(@"If you revert the document to the version on disk the document's content will be replaced with the content of the file.", nil);
firstButton = NSLocalizedString(@"Revert Document", nil);
secondButton = NSLocalizedString(@"Don't Revert Document", nil);
revertResponseCode = NSAlertFirstButtonReturn;

NSModalResponse revertResponseCode;
NSString *message, *details, *firstButton, *secondButton;
id alertAdjustment;

if (isDocumentEdited) {
// We intentionally do have a more alerting message if the document already had some changes.
message = NSLocalizedString(@"The file has been modified by another application. Do you want to keep the changes made in SubEthaEdit?", nil);
details = NSLocalizedString(@"If you revert the file to the version on disk the changes you made in SubEthaEdit will be lost.", nil);
firstButton = NSLocalizedString(@"Keep Changes", nil);
secondButton = NSLocalizedString(@"Revert", nil);
revertResponseCode = NSAlertSecondButtonReturn;
alertAdjustment = ^(NSAlert *alert) {
// add the default cmd-d shortcut for this "don't" option
NSButton *dontButton = alert.buttons[1];
[dontButton setKeyEquivalent:@"d"];
[dontButton setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
};
}

BOOL wasDocumentEdited = isDocumentEdited;

SEEAlertRecipe *warning = [SEEAlertRecipe warningWithMessage:message
details:details
buttons:@[firstButton, secondButton]
completionHandler:^(PlainTextDocument *document, NSModalResponse returnCode) {
if (returnCode == revertResponseCode) {
DEBUGLOG(@"FileIOLogDomain", DetailedLogLevel, @"Revert document");
NSError *error = nil;
if ([document revertToContentsOfURL:document.fileURL ofType:document.fileType error:&error]) {
[document updateChangeCount:NSChangeCleared];
} else {
[document presentError:error];
}
} else {
message = NSLocalizedString(@"The document's file has been modified by another application. Do you want to revert the document?", nil);
details = NSLocalizedString(@"If you revert the document to the version on disk the document's content will be replaced with the content of the file.", nil);
firstButton = NSLocalizedString(@"Revert Document", nil);
secondButton = NSLocalizedString(@"Don't Revert Document", nil);
revertResponseCode = NSAlertFirstButtonReturn;
[self setKeepDocumentVersion:YES];

alertAdjustment = ^(NSAlert *alert) {
// add the default cmd-d shortcut for this "don't" option
NSButton *dontButton = alert.buttons[1];
[dontButton setKeyEquivalent:@"d"];
[dontButton setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
};
}

BOOL wasDocumentEdited = isDocumentEdited;

SEEAlertRecipe *warning = [SEEAlertRecipe warningWithMessage:message
details:details
buttons:@[firstButton, secondButton]
completionHandler:^(PlainTextDocument *document, NSModalResponse returnCode) {
if (returnCode == revertResponseCode) {
DEBUGLOG(@"FileIOLogDomain", DetailedLogLevel, @"Revert document");
NSError *error = nil;
if ([document revertToContentsOfURL:document.fileURL ofType:document.fileType error:&error]) {
[document updateChangeCount:NSChangeCleared];
} else {
[document presentError:error];
}
} else {
[self setKeepDocumentVersion:YES];

if (!wasDocumentEdited) {
// Ensure we do show a change although we didn't have changes before, as we now differ from the version on disk
[self updateChangeCount:NSChangeDone];
}
if (!wasDocumentEdited) {
// Ensure we do show a change although we didn't have changes before, as we now differ from the version on disk
[self updateChangeCount:NSChangeDone];
}
}];
if (!isDocumentEdited) {
warning.safeToDismissAutomatically = YES;
}


warning.coalescingIdentifier = @"RevertDialog";
warning.alertAdjustment = alertAdjustment;

}];
if (!isDocumentEdited) {
warning.safeToDismissAutomatically = YES;
}


warning.coalescingIdentifier = @"RevertDialog";
warning.alertAdjustment = alertAdjustment;

[NSOperationQueue TCM_performBlockOnMainThreadIsAsynchronous:^{
[self showOrEnqueueAlertRecipe:warning];

return NO;
}];
}

- (BOOL)TCM_validateDocument {
NSString *fileName = [[self fileURL] path];
DEBUGLOG(@"FileIOLogDomain", SimpleLogLevel, @"Validate document: %@", fileName);

if (![self keepDocumentVersion]) {
if (![NSFileManager.defaultManager fileExistsAtPath:fileName] || ![self onDiskRepresentationHasChanged]) {
return YES;
} else {
[self showDocumentHasChangedDialog];
return NO;
}
} else {
return YES;
}
}

Expand Down Expand Up @@ -5029,13 +5034,25 @@ - (void)setPreparedForTermination:(BOOL)aFlag {

#pragma mark -

static dispatch_queue_t FileCheckQueue(void) {
static dispatch_queue_t queue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT, QOS_CLASS_UTILITY, 0);
queue = dispatch_queue_create("FileCheckQueue", attr);
});
return queue;
}

- (void)applicationDidBecomeActive:(NSNotification *)aNotification {
DEBUGLOG(@"FileIOLogDomain", SimpleLogLevel, @"applicationDidBecomeActive: %@", [self fileURL]);
if (![self fileURL]) {
return;
}

(void)[self TCM_validateDocument];
dispatch_async(FileCheckQueue(), ^{
(void)[self TCM_validateDocument];
});
}

- (void)clearChangeCount {
Expand Down