discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSTabViewItem doesn't show contents initially


From: Sebastian Reitenbach
Subject: NSTabViewItem doesn't show contents initially
Date: Tue, 24 Sep 2024 23:38:19 +0200
User-agent: SOGoMail 5.11.0

Hi,

I ran into some trouble, I need help with. Probably something simple I'm 
missing.
I created a window with NSTabView in Gorm. The NSTabView has a couple of 
NSTabViewItems.
The contents of the first NSTabViewItem is made up entirely in Gorm, it doesn't 
contain
further Sub NSTabViews. When the window opens, this NSTabView is open and shows 
it's contents.
The other NSTabViews in the other each contains embedded NSTabViews, created in 
code.
They may have at least 1 or more NSTabViews. The issue I'm facing is, when I 
open one of the
other tabs in the main NSTabView, then the NSTabViewItem that is open and 
visible in that Views SubNSTabView, is empty.
When I activate any other of the other SubNSTabViews, then these other tabs 
show their contents, and when I 
switch back to the first one that was initially empty, it will also populate 
its contents.
When there's more than one tab in the subNSTabView, then clicking around is a 
bit annoying,
but works around the issue. But I have one, where there's usually only one 
NSTabViewItem in it, so 
can't activate another one and switch back to the first empty one, and 
therefore don't ever get
to see its contents.

Below the code to popuplate one of these tabs. At the end of the method I tried 
to 
"make it visible" but without success.

The NSFlippedView *innerView is just a SubClass of NSView, see code at the end.

Can anyone spot what I'm missing?

thanks,
Sebastian

- (void) populateOtherTalentsTab
{
  DSACharacterDocument *document = (DSACharacterDocument *)self.document;
  DSACharacterHero *model = (DSACharacterHero *)document.model;

  NSTabViewItem *mainTabItem = [self.tabViewMain tabViewItemAtIndex: 
[self.tabViewMain indexOfTabViewItemWithIdentifier:@"item 3"]];
  NSRect subTabViewFrame = mainTabItem.view ? mainTabItem.view.bounds : 
NSMakeRect(0, 0, 400, 300);
  NSTabView *subTabView = [[NSTabView alloc] initWithFrame:subTabViewFrame];  
  [subTabView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
  
  NSMutableArray *otherTalents = [NSMutableArray array];
  NSMutableSet *talentCategories = [NSMutableSet set];
  
  // enumerate talents to find all categories
  [model.talents enumerateKeysAndObjectsUsingBlock:^(id key, DSAOtherTalent 
*obj, BOOL *stop)
    {
      if (![[obj category] isEqualToString:@"Kampftechniken"])
        {
          [otherTalents addObject: obj];
          [talentCategories addObject: [obj category]];
        }
    }];
  
  for (NSString *category in talentCategories)
    {
      NSTabViewItem *innerTabItem = [[NSTabViewItem alloc] initWithIdentifier: 
category];
      innerTabItem.label = category;
      [subTabView addTabViewItem:innerTabItem];
      NSFlippedView *innerView = [[NSFlippedView alloc] initWithFrame: 
subTabView.bounds];
      [innerView setAutoresizingMask:(NSViewWidthSizable | 
NSViewHeightSizable)];
      
      NSInteger Offset = 0;
      for (DSAOtherTalent *talent in otherTalents)
        {
          if ([talent.category isEqualTo: category])
            {
              Offset += 22;
              NSRect fieldRect = NSMakeRect(10,Offset, 300, 20);
              NSTextField *talentField = [[NSTextField alloc] initWithFrame: 
fieldRect];
              [talentField setIdentifier: [NSString stringWithFormat: 
@"talentField%@", talent]];
              [talentField setSelectable: NO];
              [talentField setEditable: NO];
              [talentField setBordered: NO];
              [talentField setBezeled: NO];
              [talentField setBackgroundColor: [NSColor lightGrayColor]];       
      
              [talentField setStringValue: [NSString stringWithFormat: @"%@ 
(%@) (%@)",
                                                                         
talent.name,
                                                                         
[talent.test componentsJoinedByString:@"/"],
                                                                         
talent.maxUpPerLevel]];
              NSFont *boldFont = [NSFont boldSystemFontOfSize:[NSFont 
systemFontSize]];
              [talentField setFont:boldFont];                                   
                                      
              NSRect fieldValueRect = NSMakeRect(320, Offset, 20, 20);
              NSTextField *talentFieldValue = [[NSTextField alloc] 
initWithFrame: fieldValueRect];
              [talentFieldValue setIdentifier: [NSString stringWithFormat: 
@"talentFieldValue%@", talent]];
              [talentFieldValue setSelectable: NO];
              [talentFieldValue setEditable: NO];
              [talentFieldValue setBordered: NO];
              [talentFieldValue setBezeled: NO];
              [talentFieldValue setBackgroundColor: [NSColor lightGrayColor]];
              [talentFieldValue setStringValue: [talent.level stringValue]];  
              [talentFieldValue bind:NSValueBinding 
                            toObject:talent 
                         withKeyPath:@"level" 
                             options:@{NSContinuouslyUpdatesValueBindingOption: 
@YES}];
              NSMutableParagraphStyle *paragraphStyle = 
[[NSMutableParagraphStyle alloc] init];
              [paragraphStyle setAlignment:NSTextAlignmentRight];
              NSDictionary *attributes = @{NSParagraphStyleAttributeName: 
paragraphStyle};
              [talentFieldValue setAttributedStringValue:[[NSAttributedString 
alloc] initWithString:[talent.level stringValue] attributes:attributes]];
              
              [innerView addSubview: talentField];
              [innerView addSubview: talentFieldValue];
            }
        }
      [innerTabItem setView: innerView];
    }
  
    // Set the subTabView for item 2
    [mainTabItem setView:subTabView];
    
    // Programmatically select the tab to force loading its content
    [self.tabViewMain selectTabViewItem:mainTabItem];
    
    // Force layout to ensure views render immediately
    [subTabView layoutSubtreeIfNeeded];
    [mainTabItem.view layoutSubtreeIfNeeded];
    
    // Use setNeedsDisplay and displayIfNeeded for immediate rendering
    [subTabView setNeedsDisplay:YES];
    [subTabView displayIfNeeded];
    [mainTabItem.view setNeedsDisplay:YES];
    [mainTabItem.view displayIfNeeded];
    
    // Iterate through all sub-tabs to force them to layout
    for (NSTabViewItem *item in subTabView.tabViewItems) {
        [item.view setNeedsDisplay:YES];
        [item.view displayIfNeeded];
    }
    // Use performSelector:withObject:afterDelay: to force updates in the next 
run loop cycle
    [self performSelector:@selector(forceViewUpdate:) withObject:subTabView 
afterDelay:0.0];
        
  [self.tabViewMain selectTabViewItemAtIndex:0];  
    
}


@implementation NSFlippedView
-(BOOL) isFlipped
{
  return YES;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
    
    // Custom drawing code
    [[NSColor lightGrayColor] setFill];
    NSRectFill(dirtyRect);
}
@end




reply via email to

[Prev in Thread] Current Thread [Next in Thread]