Improve window restoration of text view

This commit is contained in:
1024jp 2015-07-24 21:45:10 +09:00
parent f9d2bd7f74
commit 2596744213
2 changed files with 45 additions and 3 deletions

View File

@ -21,7 +21,9 @@ develop
- Better file encoding handling on revert action.
- Set access-group `com.coteditor.CotEditor.edit` to CotEditor's script definition.
- Change behavior to save `com.apple.TextEncoding` xattr on saving if the file had no content.
- Improve to restore also the last syntax style mode of unsaved documents.
- Improve window restoration:
- To restore also the last scroll position and cursor position.
- To restore also the last syntax style mode of unsaved documents.
- Optimize saving process.
@ -134,7 +136,7 @@ develop
- Fix an issue where application could hang up on text editing.
- Improve general stability.
a
2.1.0
--------------------------
@ -228,7 +230,7 @@ develop
### Additions/Changes
a
- Drop support for __OS X Lion.__
- Migrate document drawer to sidebar style.
- Add “show document inspector” option to preferences.

View File

@ -48,6 +48,9 @@
// constant
const NSInteger kNoMenuItem = -1;
NSString *const CESelectedRangesKey = @"selectedRange";
NSString *const CEVisibleRectKey = @"visibleRect";
@interface CETextView ()
@ -98,6 +101,9 @@ static NSPoint kTextContainerOrigin;
{
self = [super initWithFrame:frameRect textContainer:aTextContainer];
if (self) {
// set class identifier for window restoration
[self setIdentifier:@"coreTextView"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// This method is partly based on Smultron's SMLTextView by Peter Borg. (2006-09-09)
@ -181,6 +187,40 @@ static NSPoint kTextContainerOrigin;
}
// ------------------------------------------------------
/// store UI state for the window restoration
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
// ------------------------------------------------------
{
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:[self selectedRanges] forKey:CESelectedRangesKey];
[coder encodeRect:[self visibleRect]forKey:CEVisibleRectKey];
}
// ------------------------------------------------------
/// restore UI state on the window restoration
- (void)restoreStateWithCoder:(NSCoder *)coder
// ------------------------------------------------------
{
[super restoreStateWithCoder:coder];
if ([coder containsValueForKey:CEVisibleRectKey]) {
NSRect visibleRect = [coder decodeRectForKey:CEVisibleRectKey];
NSArray *selectedRanges = [coder decodeObjectForKey:CESelectedRangesKey];
[self setSelectedRanges:selectedRanges];
// perform scroll
__unsafe_unretained typeof(self) weakSelf = self; // NSTextView cannnot be weak
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf scrollRectToVisible:visibleRect];
});
}
}
// ------------------------------------------------------
/// first responder
- (BOOL)becomeFirstResponder