Shared NSColorPanel in one controller

789 Views Asked by At

I have shared NSControlPanel in one controller. I'm changing color to my NSTextView background and text foreground color. But when I'm changing color in panel for background, text color has been changed automatically too. Where's the problem? Thx for reply!

- (IBAction)showColorPanel:(id)sender {
    NSColorPanel *panel = [NSColorPanel sharedColorPanel];
    [panel orderFront:nil];
    [panel setAction:@selector(changeColorForBackground:)];
    [panel setTarget:self];
    [panel makeKeyAndOrderFront:self];
    isFontPanel = NO;
}

- (void)changeColorForBackground:(id)sender {
    if (!isFontPanel) {
        DesktopController *desktopController = [self getDesktopController];
        [desktopController updateCellBackgroundColor:[sender color]];
    }
}

#pragma mark - font+color

- (IBAction)showFontPanel:(id)sender {
    DesktopViewText *dvt = (DesktopViewText *)[[[self getDesktopController] startCell] subcell];
    NSFontPanel *panel = [NSFontPanel sharedFontPanel];
    NSFontManager *manager = [NSFontManager sharedFontManager];

    if (dvt) {
        [manager setSelectedFont:[[dvt label] font] isMultiple:NO];
    }

    [panel orderFront:nil];
    [manager setAction:@selector(changeFont:)];
    [manager setTarget:self];
    isFontPanel = YES;
}

- (void)changeFont:(id)sender {
    DesktopViewText *dvt = (DesktopViewText *)[[[self getDesktopController] startCell] subcell];

    if (dvt) {
        NSFont *oldFont = [dvt font];
        NSFont *newFont = [sender convertFont:oldFont];
        [[dvt textStorage] addAttribute:NSFontAttributeName value:newFont range:[dvt selectedRange]];
    }
}


 - (void)changeAttributes:(id)sender {
     if (isFontPanel) {
         DesktopViewText *dvt = (DesktopViewText *)[[[self getDesktopController] startCell] subcell];

     if (dvt) {
        NSDictionary *oldAttributes = [[dvt textStorage] fontAttributesInRange:[dvt selectedRange]];
        NSDictionary *newAttributes = [sender convertAttributes: oldAttributes];
        [[dvt textStorage] setAttributes:newAttributes range:[dvt selectedRange]];
         //[[dvt textStorage] addAttribute:NSForegroundColorAttributeName value:[newAttributes ] range:[dvt selectedRange]];
    }
 }
}

I thought It was error in (void)changeAttributes:(id)sender so I set breakpoint. But when I'm changing background color, changeAttributes has not been called, so It's ok.

1

There are 1 best solutions below

1
On

The problem can only be located in -[DesktopController updateCellBackgroundColor:] since it seems this is the one place that gets called when a color is picked. With no access to this code I can't tell you how it changes your model.

A couple of additional notes and suggestions:

  • what is the isFontPanel variable good for? NSColorPanel and NSFontPanel take a action selector which is enough to make the distinction.
  • don't prefix you getter by get, this is not following the Cocoa conventions and most likely to fail if you use Key Value coding / observing.
  • your controller is accessing the internals of another controller, which defeats object orientation. You should define methods on DesktopController, instead of changing its state directly.
  • likewise, changing the attributes of the NSAttributedString storage of the NSTextView actually belongs to your domain model.