How to create/use themes in a Flex project - apache-flex

I just started out with Flex, and while I have worked with Actionscript2 for a few years, I'm still getting my head around Actionscript3. I've been diving into Flex for the past week, and can't figure this one thing out.
I am looking into the best way (or 'a' way) to allow me and other folks working on this project to easily create new themes for kind-of an video player. I show two videos, and need to be able to reposition them, style them, add images and anything else, really. I'm not looking to create a full-on templating engine, but if I could have a separate file/folder for each template, that would totally make my day.
I imagined having a solution in which I would have a folder inside src/templates/, and inside that folder I would have view.mxml that contains everything I need to display the videos. I would then be able to add other files such as images and such to make this template look the way it should.
Which template should be used should be defined by a variable in the main video file. This will either be provided to the swf file on playback, or provided by the video loading an external configuration file (but this is not part of my question :).
So, in short; How do I include mxml files that are inside one or two sub-folders inside src, and how do I do this in a way that the file I wish to include can be chosen via a variable.
Thanks so much in advance!
-Dave

You'd be surprised to what degree a Flex application can change its appearance through styling. Check out the following project:
http://www.fillcolors.com/
If you need more flexibility still, you could have an application with multiple views, one for each theme you want to show. An easy way to do this in Flex is to make use of the ViewStack component which basically layers view displays, showing only one at a given time.
As a very simple example, you could have something along these lines:
<mx:ViewStack selectedIndex="{currentIndex}">
<themes:CustomTheme1/>
<themes:CustomTheme2/>
<themes:CustomTheme3/>
</mx:ViewStack>
Here, currentIndex is a variable bound to the ViewStack, determining which of the three children is currently visible, each of which represent a custom MXML component (in your case, a theme).
Response to comment:
Yes, the above example is designed to do exactly that, but it's really not a feature of Flex, but of AS3. The CustomThemeX components are located using the themes namespace. The namespace is defined within the parent components root tag. In this example, the parent is a Canvas component:
<mx:Canvas xmlns:themes="themes">
This tells the compiler to look for these custom components within the themes sub-folder of your application; in other words, the src/themes folder, which contains the CustomThemeX components.
If the preceding paragraph didn't really sense to you, I'd recommend picking up a book/reading through the documentation about AS3/Flex and learning more about some of the core concepts of AS3 and object oriented programming. I wish I could help out more, but this question is starting to get a little too broad. I hope you understand. :)

I'm not an expert on Flex. But I've seen a tutorial on skins in Flex on gotoAndLearn.com. Maybe that kan give you some hints: Introduction to Flex: Part 3

Related

Flex navigate to another Mxml page?

I am having difficulty navigating to different MXML pages in my Flex application. I checked some other questions on SO about it but they seemed to be to do with tab navigation.
I am trying to navigate from one MXML page to another via Actionscript code. How is this possible?
Cheers
Edit:
I am a real beginner in Flex and I worked out that I actually needed to use States, and have now discovered how to use them. Thanks for your help.
You don't navigate between MXML pages. MXMLs are not pages to begin with. Instead they are components, that are displayed in the application. So if you want to change the current display, then you need to remove the already added components, and add others that should be displayed now.
There are different approaches to do this. The very raw way of manually removing elements is rarely used, instead there are two main methods: ViewStacks and display states. Display states should be used when there are not many different changes in a view, for example when you click a checkbox that adds new options to an existing formular. If you want to change the whole displayed content (this does not have to be the whole application content though, think in components instead), you can use ViewStacks.
ViewStacks are like a stack of paper, where each paper reflects a single view. So if you want to display something else, you move the new view to the top, hiding all others below. Many components, including TabNavigators, are just ViewStacks with an additional menu to select the view that should be displayed. But you can also just use a ViewStack and manually change to what you want to show.
Flex is not sidebased like html.
You have to instantiate or remove classes, components or all this stuff.
i would use the states.
You can register handler to buttons and swap the state.
Then you are able to instatiate or remove components via the tsates.
Which flex version dou you use? The management of the states are changing between flex3 and flex4
Assume, you have one app with two content areas, home and gallery. First you have to create these two areas with project->new->component and named there related.
After that, you instanciate both components in your app.
Define two states, home and gallery and swap these with a button or two.
set the propert excludefrom or include with the name of the states. finally, you have an app with two content areas, but you never swap pages, you instanciate or remove components in runtime.
br
Frank
BR
Frank

Qt and UI Skinning

I wanted to consult with the sages here regarding Qt and skinning, get your opinion and chart a path for my development. My requirements are as follows:
My Qt/C++ application (cross platform with Mac, Windows and Linux versions) needs to have modular skins.
A skin is defined as a set of one or more elements: - Window background texture - Look/feel of UI controls such as edit boxes, drop down, radio buttons, buttons etc. - Look/feel of window "caption", resize grips etc.
Skins will be installed with the application installer, allowing the user to choose which one he/she wants to use. Users should be able to change skins on the fly.
Can I go the QML route? should this be custom and based on simple resources which are built into the application? Any design advice will be appreciated.
Thanks.
If I understood you correctly then stylesheet is the best way forward. You can create stylesheets similar to CSS and then pass them as command line option to your application or load on invocation to style your application at runtime. That way you can create multiple stylesheets each having a different look and feel and allow user to load them at will. Since its CSS it doesn't need any new learning and you can keep all your styling outside your source code.
Here are a list of resources that can get you up and running quickly:
http://blog.qt.io/blog/2007/11/27/theming-qt-for-fun-and-profit/
http://doc.qt.io/qt-5/stylesheet.html
I haven't played with QML yet, but you could also create a custom QStyle implementation that supports your resource format. Note that you'd lose style sheet support if you went this route.
Changing window captions is a little trickier if you want portability.
QML, if I understand correctly, doesn't really skin the widgets, it mainly deals with GUI layout etc etc.
QStyle is used to change the looks. It is a bit low-level though, and requires programming, so if you want to load different user-created skins (from an XML or so) it might be tricky to support extensive skinning. Chaining colors and a few items are easy enough though. (There might be someone else who've done something you could re-use.. not sure.)
For modifying widgets, use QStyle::polish(). You could use that to change the background picture (if it's a top-level window, or of a certain class). There are numerous repaint method to change almost every part of every widget.
Store/load the style using QSettings, by reading and setting the desired Style just after QApplication but before your main window is constructed.

What is your workflow for skinning Flex apps?

Also, where do you start if you want to create a complete Flex 4 skin? Is there a list of all the "pieces" (component parts) you need to create graphics for somewhere, or some complete, example skins out there to use as a template? And do your skins render correctly in Flash Builder Design View? (including embedded fonts)
Most people will point you to Flash Catalyst, but the last time I looked it was not all-inclusive in terms of creating a complete skin/theme, not to mention other issues inherent to 1.0 software. I start by copying the default skin files and modifying them, as well as creating a new CSS file based on the defaults.css file that comes with the SDK. The trickier part is that not all components (Tree, DataGrid, DateChooser, ColorPicker, etc) are available as Spark components, which means to have a complete skin/theme you need to create a bunch of mx skins as well. Bottom line: If you are serious about doing this properly, you'll need to spend a lot of time understanding defaults.css and the skin classes it refers to.
To answer your original question, my personal workflow is to create a mockup of the skin in OmniGraffle, then use that as a guide to modify copies of the default skin files (look in spark.skins, and mx.spark.skins) by manipulating the mxml directly (as opposed to using a graphical tool such as Illustrator or Catalyst.)
Hope that helps.
Basically, you have many options when skinning Flex 4 apps :
Using "general" styles (chromeColor, selectionColor, focusColor, ...). This is usually called "Styling" instead of Skinning because you only change the overall look of the application. It's by far the easiest way to change the appareance of your application quickly without knowing anything about FXG, MXMLG, ... The drawback is it will still look like a Flex app, but with different colors/fonts
Using Adobe tools to produce FXG files. These tools can be Illustrator, Photoshop, Flash CS5. You design each of the spark component with these tools and export the result in .fxg format. FXG is great because it's optimized, but you can't use things such as Data Binding in it
Write your skin in MXMLG , by hand (starting from scratch or copying the default spark skin) or with the help of Flash Catalyst. Catalyst can take your Illustrator or Photoshop design and convert it to MXMLG. Experience shows it's still painfull, even with the last version. And you often end up doing things like this.
In our team, there is no such thing like designer/developer workflow with Flash Catalyst. The next version looks better but it's still far from what Microsoft is doing with Visual Studio/Blend.
That being said, the new Spark architecture is awesome. Skinning is much easier, flexible and readable. With this architecture, a developer codes the component and the designer skins it only by knowing the contract (skinparts, skinstates, data)
This also true for views if you use the Presentation Model pattern.
There is no "template" skins as the one available in Flex 3 because skinning doesn't use symbol anymore. What could be done though is a Flex 4 style explorer.
A final word, it has already been said here, but don't forget that with the current version (4.1), there is no spark equivalent for all of the components, so you will still use Flex 3 skinning techniques for components such as DataGrid, Tree, ...
This is where I normally "start"
http://examples.adobe.com/flex2/inproduct/sdk/explorer/explorer.html
Once I figure out what components I have to skin, and I can't change what I want to change in Flex with CSS, then I go into Flash and start breaking the components apart and tweaking them.
http://www.adobe.com/devnet/flex/articles/skins_styles.html
(Also, I've never had anything render "correctly" in design view)

How to reference the DisplayTemplates defined in an Area from main project?

I defined a few display templates for classes and they work well when I put them in views/shared/DisplayTemplates. However after I move them into an area, looks like ASP.NET MVC won't look inside Area to find the templates.
How to reference the DisplayTemplates defined in an Area from main project? Is that a good practise?
I don't think you can reference display templates that exist inside an area from the main project or a different area.
This is very logical and I think you should follow this constraint and not try to workaround it. Think of an area as a separated logical part of your application - it should have all its resources inside the area. However, when you have a resource like a master layout or a logo image that should be used from other areas as well, then this resource should be placed on the main project folders and not in specific areas to make it available to all.
I don't totally agree with Shay -- in case of a shared reusable area, templates can be specified as default and overruled in the main website project if needed. The TemplateHelper class should take areas in account when locating templates.
This is quite easy to fix, tho -- see my other post here

Is there a benefit to embedding ActionScript in MXML when developing Flex apps?

I always thought that separating the UI from the logic is the way to go like the use of class files in Flash CS3/CS4 or MVC in web frameworks but recently there are plenty of examples and posts using ActionScript embedded in MXML.
Is there a benefit to doing this? Am I missing something.
Don't get too caught up on the "rules" of MVC and the like. Ultimately you want your code to be setup in such a way that it will minimize the impact/churn of future changes. It's less important "where" the code is and more important what and how it's interacting with other code. I see far to many people take a simple component like a login screen and make models, view, controllers, view helpers, service facades, etc. It ends up being like 75+ lines of code for something that should have been about 20. It also spreads the code amoungst a bunch of files rather than one. So in the end, you've made maintenace hard, not easy. I'm not arguing for ignoring design patterns, but rather being practical about it. Hope this helps.
In my experience, there's not much of a difference whether you write a component in ActionScript + MXML, or solely in Actionscript. Personally, if I'm writing something that's layout heavy with a little logic, I'll do it in MXML. Conversely, if it's code heavy and has little layout, I'll write it in Actionscript.
I suppose you could always separate the view and model, but then you have more files to maintain. Sometimes it makes sense, but I have yet to see a compelling reason to do it all the time.
I go by a few simple rules:
If a bit of code is just a single short line and it isn't something you can reuse elsewhere -- mere glue code -- put it directly in the MXML tag.
If it's too long to fit comfortably in the MXML tag or it is something multiple components in that MXML file can use, put it in a function in a Script tag within the same file, and call the function from the MXML tag(s).
Everything else -- full classes, large functions, things used by multiple MXML files -- goes in a separate .as file. This includes single-LOC methods that are part of a larger class, as opposed to the standalone functions the other rules cover.
I agree with Erich Douglass, to a point. Generally, if I can do the layout with MXML it's just easier to maintain. I mean, who wants to write all that crap in a createChildren override if you don't have to? Besides, the MXML is easier to read.
Whatever you do, though, remember that if you are merely calling a separate script file for an MXML component (Foo.mxml calls Foo.as for its ActionScript) you are losing the code highlighting feature that helps you work between the MXML and the ActionScript. I like to keep them both in the same place, so when I click on an ID in an MXML element I can see at a glance where it's being referenced in the AS.

Resources