13 Replies Latest reply on Oct 8, 2009 7:33 AM by TomDelorenzi

    Skins Question

    TomDelorenzi Level 1

      I have worked in flex 3 for a while and I am trying to wrap my head around skins. I see how they work but my question is, how do you configure the skin in the mxml component that is using it? For example:

      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          xmlns:mx="library://ns.adobe.com/flex/halo"
          skinClass="com.company.components.skins.GradientSkin">

      ...

      How do i pass params to that skin to tell it what color the gradient should be? Also my skin defines the content area group which from what I can tell replaces teh padLeft,padRight etc. How would I specify in the application what the padding is? I don't want to put that in the skin class as hard coded because I cannot reuse it then.

      Any thoughts on how I am looking at this incorrectly or what I can do to paramatize my skin cleanly? Thanks.

        • 1. Re: Skins Question
          Matt Finitz (Adobe)

          You can wire up your skins to custom data by:

           

          1. Creating a custom component that inherits from the type of object you are skinning.

          2. Adding your custom properties.

          3. Binding within the skin.

           

          For example, if I wanted to skin a RadioButton, but pass through a backgroundColor, I might do something like the following:

           

          public class MyRadioButton

          {

             [Bindable] public var myBackgroundColor:int;

          }

           

          Then, in my skin, I would do something like:

           

          <Rect>

             <Fill>

               <SolidColor color="{hostComponent.myBackgroundColor}"/>

             </Fill>

          </Rect>

           

          Especially note the "hostComponent", which is the property that grants object access from the skin. Then, in your app, use your component:

           

          <Application ...>

             <myns:MyRadioButton myBackgroundColor="0xFF0000"/>

          </Application>

          1 person found this helpful
          • 2. Re: Skins Question
            TomDelorenzi Level 1

            Sorry for the delayed response, work got busy.

             

            This gets me part way there but I still see a problem. First before that though since I wanted to use this on the application subclassing is probably out right? So i did the following:

            <?xml version="1.0" encoding="utf-8"?>
            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/halo"
                xmlns:ec="components.*"
                xmlns:econ="com.ericsson.components.containers.*"
                skinClass="com.ericsson.components.skins.GradientSkin"
                width="100%" height="100%"
                creationComplete="loginComponent.activated()" xmlns:components="com.ericsson.components.*">
                <fx:Style source="ericsson.css" />
                <fx:Declarations>
                    <fx:int id="gradientAngle"/>
                    <fx:int id="gradientStartColor"/>
                    <fx:int id="gradientEndColor"/>
                </fx:Declarations>

             

            To give me 3 bindable variables without having to have an as. Is this ok?

            Ok now for the problem. In the examples I can find, when you do:

            <fx:Metadata>
                    [HostComponent("components.Grade")]
            </fx:Metadata>

             

            You have to specify a class that your skin is for. But that defeats the purpose of having a reusable gradient skin. I was hoping it would be useable on any component, not just instances of class X. Any thoughts on this? I would also like to avoid having to have a subclass of every class that I want to skin, which is why i tried the Decleration thing above.

            • 3. Re: Skins Question
              TomDelorenzi Level 1

              Also it seems to work if I specify the skinclass as a child rather then an attribute and then pass params to it via its attributes.

              So like this:

               

              <s:Application ..../>

              <s:layout>...</s:layout>

              <me:MySkin startColor="0x000000" endColor="0x000000"/>

              <s:Panel ... rest of content/>

               

              However , the skin then gets layed out and I assume that means its not really the skin, and just a child. Also to have a custom skin on your application, for example to have a gradient background for your app, is it a good practice to subclass the Application class so that you could specify bindable params if you were to do it the way you show above?

              • 4. Re: Skins Question
                rfrishbe Level 3

                There is a way you can define a skin inline: http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-fl ex-gumbo/ though I don't think that solution scales very well as you end up with lots of stuff in one file.

                 

                The better way to do this is through CSS styling, which is what we use in the components in the framework.  In the skin, you can do something like:

                 

                <s:Rect>

                <s:fill>

                <s:SolidColor color="{getStyle('myNewColor')}" />

                </s:fill>

                </s:Rect>

                 

                or you could bind it to your hostComponent's styles:

                 

                <s:Rect>

                <s:fill>

                <s:SolidColor color="{hostComponent.getStyle('myNewColor')}" />

                </s:fill>

                </s:Rect>

                 

                Rather than binding, you could also override updateDisplayList(w,h) to push the style in, like:

                 

                <s:Rect>

                <s:fill>

                <s:SolidColor id="myFill" color="{hostComponent.getStyle('myNewColor')}" />

                </s:fill>

                </s:Rect>

                 

                override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
                {
                    myFill .color = hostComponent.getStyle("myNewColor");
                     super.updateDisplayList(unscaledWidth, unscaledHeight);
                }

                 

                Without subclassing, you won't be able to set the styles directly on the component in MXML because you need to add the style metadata to the component, but you can style it through the other ways to set CSS without any problems.

                 

                Hope that helps,

                Ryan

                1 person found this helpful
                • 5. Re: Skins Question
                  TomDelorenzi Level 1

                  This was what I was looking for. I am 98% of the way there now. I implemented as you suggested but I still am not getting it right.

                  I removed almost all the code to make this as simple as possible. I just wanted to get a gradient on the applications background. Here is what I have.

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                      xmlns:s="library://ns.adobe.com/flex/spark"
                      skinClass="com.ericsson.components.skins.GradientSkin"
                      width="100%" height="100%">
                      <fx:Style>
                          @namespace s "library://ns.adobe.com/flex/spark";
                          s:Application
                          {
                              skinGradientRotation:90;
                              skinGradientStartColor:#000000;
                              skinGradientEndColor:#FFFFFF;
                            }
                      </fx:Style>
                  </s:Application>

                   

                  And the skin

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009"
                      xmlns:s="library://ns.adobe.com/flex/spark"
                      xmlns:mx="library://ns.adobe.com/flex/halo">
                      <fx:Metadata>
                          <![CDATA[
                              [HostComponent("spark.components.Application")]
                          ]]>
                      </fx:Metadata>
                   
                      <s:states>
                          <s:State name="normal" />
                          <s:State name="disabled" />
                      </s:states>

                   

                      <!-- fill -->
                      <s:Rect id="backgroundRect" left="0" right="0" top="0" bottom="0">
                          <s:fill>
                              <s:LinearGradient rotation="{hostComponent.getStyle('skinGradientRotation')}">
                                  <s:GradientEntry color="{hostComponent.getStyle('skinGradientStartColor')}" />
                                  <s:GradientEntry color="{hostComponent.getStyle('skinGradientEndColor')}" />
                              </s:LinearGradient>
                          </s:fill>
                      </s:Rect>

                   

                      <s:Group id="contentGroup"/>
                  </s:Skin>

                   

                  I even added a trace of the rotation value and its comming back as undefined. So I think I'm not quite linking the css right.

                  Any ideas? Also I plan to use HostComponent("mx.core.UIComponent") so its reusable for other classes. Is that legitamite to use the baseclass?

                  Thanks for all your help.

                  • 6. Re: Skins Question
                    rfrishbe Level 3

                    Everything in your example is right, except for one small thing.  With your code currently, you should be getting a warning, like:

                     

                    Type 's' in CSS selector 's' must be qualified with a namespace.

                     

                    The reasons is because you need to use: s|Application instead of s:Application

                     

                    Also, I mis-spoke a bit about the styling.  You can just use:

                     

                    <s:LinearGradient rotation="{getStyle('skinGradientRotation')}">

                     

                    There's no need to use hostComponent.getStyle() because the Skin's styles are the same as the hostComponent's styles.

                     

                    -Ryan

                    • 7. Re: Skins Question
                      TomDelorenzi Level 1

                      Thanks. They are comming through in traces now. The draws as black now(replacing those getStyles with 0x000000 and 0xFFFFFF fixes it so I know everything else is right). I will fiddle with the casting. I added as uint but that didn't help but I am convinced its something along those lines. I will post the working solution when I get it. On a side note can you confirm my understanding of the groups in a skin.

                      -Drawing done in the main body of the skin applies to the whole component

                      -Drawing done in a group(example top bar or content group) is drawn wherever that component places its top bar group.

                      • 8. Re: Skins Question
                        rfrishbe Level 3

                        Tom,

                         

                        Sorry, I don't think I understand your question.  I'm not sure what you mean by "drawing."  If you're just talking about where you place your Graphic objects, like Rect or Ellipse, then they are drawn in to their container (such as a Group).  If that Group is moved to be at (100,100) instead of (0,0), then all of the graphics objects inside of them (and all the containers with graphics) move as well.  The "Skin" object represents the visuals for the component, so if the component moves, the skin and everything inside of the skin moves as well.

                         

                        -Ryan

                        • 9. Re: Skins Question
                          TomDelorenzi Level 1

                          Right thats the drawing i was talking about. I was refering to how your skin can have multiple groups tied to the parent compenent. Like for example application requires content group. I was infering that meant if i had a title bar required group, my parent component probably places its title bar somewhere and all my rects, elipses are offset from that location if they are in the group that matches title bar.

                          • 10. Re: Skins Question
                            TomDelorenzi Level 1

                            so it turns out this solution doesn't appear to work. During the creation, the host component is not available and its styles are not yet part of the skin's styles. Atleast from what I can tell. The style values are 0 and if I try to get them directly from the host component it throws an error. This makes sense I guess as you probably create the skin, and then attach it to a host. So no styles during creation. So this leads me back to how can I use the host's styles\vars\something to configure the skin. Perhaps skins need to be setup during a later event? I liked the simplicity of building the skin right in the mxml though.. Here is what I did to get to my conclusion:

                            <fx:Script>
                                    <![CDATA[
                                        private function getRotation():uint
                                        {
                                            return getStyle("skinGradientRotation");
                                        }
                                        private function getStartColor():uint
                                        {
                                            return getStyle("skinGradientStartColor");
                                        }
                                        private function getEndColor():uint
                                        {//**********************************************************************

                            //Here if you have hostComponent it bombs, if you dont it will be 0, and                                                                                                 

                            //if you check this same style later(Like updateDisplayList) it has the right value
                                            var val:uint=hostComponent.getStyle("skinGradientEndColor");

                                                                                                                                     
                                            if(val is uint)
                                            {
                                                trace("yes");
                                            }
                                            trace(val);
                                            return val;
                                        }
                                    ]]>
                                </fx:Script>

                             

                                <!-- fill -->
                                <s:Rect id="backgroundRect" left="0" right="0" top="0" bottom="0">
                                    <s:fill>
                                        <s:LinearGradient rotation="{getRotation()}" >
                                            <s:GradientEntry color="{getStartColor()}"  />
                                            <s:GradientEntry color="{getEndColor()}" />
                                        </s:LinearGradient>
                                    </s:fill>
                                </s:Rect>

                            • 11. Re: Skins Question
                              TomDelorenzi Level 1

                              I think the way I am thinking about skins is the problem. I am looking for a reusable thing so i can stick an any color gradient on an application and never have to make a new skin. I am thinking the way skins are desigend is to give a specific look to everything. Like a "green frog" skin makes everything green. And you just make a new skin for every application that you want a different color scheme.

                              • 12. Re: Skins Question
                                rfrishbe Level 3

                                I think your issue is that you need to supply [HostComponent] metadata for the hostComponent property to actually work.

                                 

                                Also, as I said before, you don't need to use hostComponent.getStyle("myStyle"), but should just be able to use getStyle("myStyle").  If you use that in binding expressions, I don't think you should run in to any problems.

                                 

                                -Ryan

                                • 13. Re: Skins Question
                                  TomDelorenzi Level 1

                                  Hey. In my above post I mentioned I tried getStyle with and without host component. The host component is null during init which is the root problem. Try the example yourself and see. The styles are all there after init completes so the styles are being set and read correctly.