Skip to content

Commit

Permalink
Support undo/redo for text formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
emsquared committed Jul 24, 2012
1 parent b370ab1 commit e1e001c
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Classes/Headers/IRCClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ typedef enum IRCDisconnectType : NSInteger {
- (void)sendNextCap;
- (void)pauseCap;
- (void)resumeCap;
- (BOOL)isCapAvailable:(NSString*)cap;
- (void)cap:(NSString*)cap result:(BOOL)supported;
- (BOOL)isCapAvailable:(NSString *)cap;
- (void)cap:(NSString *)cap result:(BOOL)supported;

- (void)joinChannels:(NSArray *)chans;
- (void)joinChannel:(IRCChannel *)channel;
Expand Down
4 changes: 4 additions & 0 deletions Classes/Headers/NSRectHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@

#import "TextualApplication.h"

/* This helper file is supposed to be for NSRect, but it was pointless to
create another file for NSRange so we are just shoving a helper for it in here. */
TEXTUAL_EXTERN BOOL NSRangeIsValid(NSRange r);

TEXTUAL_EXTERN NSPoint NSRectCenter(NSRect rect);
TEXTUAL_EXTERN NSRect NSRectAdjustInRect(NSRect r, NSRect bounds);
4 changes: 2 additions & 2 deletions Classes/Headers/TVCTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
- (NSInteger)selectedLineNumber;
- (NSInteger)numberOfLines;

- (dispatch_queue_t)formattingQueue;

- (void)keyDownToSuper:(NSEvent *)e;
- (void)setKeyHandlerTarget:(id)target;
- (void)registerKeyHandler:(SEL)selector key:(NSInteger)code modifiers:(NSUInteger)mods;
Expand All @@ -67,4 +65,6 @@

- (void)removeAttribute:(id)attr inRange:(NSRange)local;
- (void)setAttributes:(id)attrs inRange:(NSRange)local;

- (void)addUndoActionForAttributes:(NSDictionary *)attributes inRange:(NSRange)local;
@end
5 changes: 5 additions & 0 deletions Classes/Helpers/Cocoa (Objective-C)/NSRectHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@

#import "TextualApplication.h"

BOOL NSRangeIsValid(NSRange r)
{
return (NSDissimilarObjects(r.location, NSNotFound) && r.length >= 1);
}

NSPoint NSRectCenter(NSRect rect)
{
return NSMakePoint((rect.origin.x + (rect.size.width / 2)),
Expand Down
4 changes: 2 additions & 2 deletions Classes/IRC/IRCClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -4467,7 +4467,7 @@ - (void)resumeCap
[self sendNextCap];
}

- (BOOL)isCapAvailable:(NSString*)cap
- (BOOL)isCapAvailable:(NSString *)cap
{
return ([cap isEqualNoCase:@"identify-msg"] ||
[cap isEqualNoCase:@"identify-ctcp"] ||
Expand All @@ -4477,7 +4477,7 @@ - (BOOL)isCapAvailable:(NSString*)cap
([cap isEqualNoCase:@"sasl"] && NSObjectIsNotEmpty(self.config.nickPassword)));
}

- (void)cap:(NSString*)cap result:(BOOL)supported
- (void)cap:(NSString *)cap result:(BOOL)supported
{
if (supported) {
if ([cap isEqualNoCase:@"sasl"]) {
Expand Down
27 changes: 13 additions & 14 deletions Classes/Library/Color Formatting/IRCColorFormat.m
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,12 @@ - (BOOL)IRCFormatterAttributeSetInRange:(IRCTextFormatterEffectType)effect
- (void)sanitizeIRCCompatibleAttributedString:(BOOL)clearAttributes
{
if (clearAttributes) {
NSAttributedString *stringv = [NSAttributedString alloc];
NSAttributedString *stringn = [NSAttributedString emptyString];

NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

attrs[NSFontAttributeName] = TXDefaultTextFieldFont;
attrs[NSForegroundColorAttributeName] = TXDefaultTextFieldFontColor;

(void)[stringv initWithString:TXTLS(@"InputTextFieldPlaceholderValue") attributes:attrs];

[self setAttributedStringValue:stringv];
[self setAttributedStringValue:stringn];
NSDictionary *attributes = @{
NSForegroundColorAttributeName : TXDefaultTextFieldFontColor,
NSFontAttributeName : TXDefaultTextFieldFont
};

[self setTypingAttributes:attributes];
} else {
[self setFont:TXDefaultTextFieldFont];
}
Expand Down Expand Up @@ -492,7 +486,9 @@ - (void)setIRCFormatterAttribute:(IRCTextFormatterEffectType)effect
}
default: break;
}


[self addUndoActionForAttributes:dict inRange:effectiveRange];

[self setAttributes:newDict inRange:effectiveRange];

limitRange = NSMakeRange(NSMaxRange(effectiveRange),
Expand Down Expand Up @@ -521,7 +517,7 @@ - (void)removeIRCFormatterAttribute:(IRCTextFormatterEffectType)effect
{
if ([baseFont fontTraitSet:NSBoldFontMask]) {
baseFont = [_NSFontManager() convertFont:baseFont toNotHaveTrait:NSBoldFontMask];

if (baseFont) {
newDict[NSFontAttributeName] = baseFont;

Expand Down Expand Up @@ -556,6 +552,7 @@ - (void)removeIRCFormatterAttribute:(IRCTextFormatterEffectType)effect
newDict[NSForegroundColorAttributeName] = defaultColor;

[self setAttributes:newDict inRange:effectiveRange];

[self removeAttribute:NSBackgroundColorAttributeName inRange:effectiveRange];

break;
Expand All @@ -569,6 +566,8 @@ - (void)removeIRCFormatterAttribute:(IRCTextFormatterEffectType)effect
default: break;
}
}

[self addUndoActionForAttributes:dict inRange:effectiveRange];

limitRange = NSMakeRange(NSMaxRange(effectiveRange),
(NSMaxRange(limitRange) - NSMaxRange(effectiveRange)));
Expand Down
44 changes: 38 additions & 6 deletions Classes/Views/Input Text Field/TVCTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ - (id)initWithCoder:(NSCoder *)coder
return self;
}

- (dispatch_queue_t)formattingQueue
{
return _formattingQueue;
}

#pragma mark -
#pragma mark Keyboard Shorcuts

Expand Down Expand Up @@ -138,16 +133,53 @@ - (void)setStringValue:(NSString *)string
[self replaceCharactersInRange:[self fullSelectionRange] withString:string];
}

#pragma mark -
#pragma mark Attribute Management

- (void)addUndoActionForAttributes:(NSDictionary *)attributes inRange:(NSRange)local
{
if (NSObjectIsEmpty(attributes) || NSRangeIsValid(local) == NO) {
return;
}

DLog(@"%@; %@", attributes, NSStringFromRange(local));

[self.undoManager registerUndoWithTarget:self
selector:@selector(setAttributesWithContext:)
object:@[attributes, NSStringFromRange(local)]];
}

- (void)setAttributesWithContext:(NSArray *)contextArray /* @private */
{
NSRange local = NSRangeFromString(contextArray[1]);

NSDictionary *attrs = [self.attributedString attributesAtIndex:0
longestEffectiveRange:NULL
inRange:local];

[self.undoManager registerUndoWithTarget:self
selector:@selector(setAttributesWithContext:)
object:@[attrs, NSStringFromRange(local)]];

DLog(@"old: %@; new: %@", attrs, contextArray[0]);

[self setAttributes:contextArray[0] inRange:local];
}

#pragma mark -

- (void)removeAttribute:(id)attr inRange:(NSRange)local
{
[self.textStorage removeAttribute:attr range:local];
}

- (void)setAttributes:(id)attrs inRange:(NSRange)local
{
[self.textStorage setAttributes:attrs range:local];
[self.textStorage addAttributes:attrs range:local];
}

#pragma mark -

- (void)sanitizeTextField:(BOOL)paste
{
[self sanitizeIRCCompatibleAttributedString:BOOLReverseValue(paste)];
Expand Down
4 changes: 2 additions & 2 deletions Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
<key>TXBundleBuildCodeName</key>
<string>Turtle Soup</string>
<key>TXBundleBuildNumber</key>
<string>11976</string>
<string>11992</string>
<key>TXBundleBuildReference</key>
<string>2.1.1-247-g6c5f2b9-debug,llvm4.0</string>
<string>2.1.1-248-gb370ab1-debug,llvm4.0</string>
</dict>
</plist>
4 changes: 2 additions & 2 deletions Resources/Plugins/Brag Spam/TPIBragSpam.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
@interface TPIBragSpam : NSObject

- (void)messageSentByUser:(IRCClient*)client
message:(NSString*)messageString
command:(NSString*)commandString;
message:(NSString *)messageString
command:(NSString *)commandString;

- (NSArray*)pluginSupportsUserInputCommands;

Expand Down
4 changes: 2 additions & 2 deletions Resources/Plugins/Brag Spam/TPIBragSpam.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
@implementation TPIBragSpam

- (void)messageSentByUser:(IRCClient*)client
message:(NSString*)messageString
command:(NSString*)commandString
message:(NSString *)messageString
command:(NSString *)commandString
{
if ([commandString isEqualToString:@"BRAG"]) {
if (client.world.selectedChannel.isChannel == NO) return;
Expand Down

0 comments on commit e1e001c

Please sign in to comment.