tombenner / nui

Style iOS apps with a stylesheet, similar to CSS
MIT License
3.76k stars 462 forks source link

.nss variables accessible from code #335

Open Emailrus opened 8 years ago

Emailrus commented 8 years ago

Hi again :) This time I was thinking about access to variables defined in .nss in code. Very useful in cases, where you need to obtain some global vars, so you won't be having two files with same palette, for example:

/* nss file*/
@color-primary: #123456;

/* constants file */
#define kColorPrimary  @"color-primary";

/* some UI file */
- (void)someSetupMethod
{
    CALayer *border = [CALayer layer];
    bottomBorder.frame = CGRectMake(whatever);
    bottomBorder.backgroundColor = [[NUISettings getColorFromVar:kColorPrimary] CGColor];
    [self.myTextField.layer addSublayer:border];
}
Stunner commented 8 years ago

Why would you do that instead of merely doing:

/* nss file*/
MyTextField {
    border-color: #123456;
}

/* some UI file */
- (void)someSetupMethod
{
    self.myTextField.nuiClass = @"MyTextField";
}
Emailrus commented 8 years ago

By using CALayer I wanted to point out, that not everything is stylable by NUI. Some custom controls would still require manual approach. Point is, that I still need to access my styles (color palette for example) and since they are moved to .nss file and I don't want to have two files defining same styles (MyTheme.nss and MyTheme.h) it would be nice to do it via NUI.

Stunner commented 8 years ago

Can this sort of thing can be addressed by render customization? It has currently been partially implemented at a very granular level, but as per the discussion it needs to be re-architected.

Emailrus commented 8 years ago

Partially maybe. I can think of some other scenarios, where you don't have rendering element, like if you need some padding values from .nss file to include them in layout calculations. Also accessing variables might be much easier to implement and use.

Stunner commented 8 years ago

Fair enough. I'd like to hear your thoughts on this @timbodeit.

alexeyt820 commented 8 years ago

@Emailrus, while I agree feature is useful, you could you workaround, add dummy style colors you need, App { background-color: @minor; foreground-color: @main; } then in code use NUISettings.getColor("background-color", withClass: "App")

timbodeit commented 8 years ago

@Stunner I'm sorry, I have been inactive for a little while.

I would suggest the same thing as @alexeyt820. We have "Colors" classes for various components of our app. I don't think we need new API in NUI for this. I do like the idea of a constants file however. It would be awesome to have a code-generating script, that automatically extracts constants for the different Classes and attributes to make accessing them from code safer and a little bit more convenient.