Transforming sources

This sample introduces the ways of manipulating sources and layouts in Linquid.
First let's make some controls which will produce values.
In this example, it will be a couple of sliders, which allow selection of integers in range of 0..10:

from i1 in UIM.Slider(0, 10).Title("Value 1")
from i2 in UIM.Slider(0, 10).Title("Value 2")
Here, the method Title places controls generated by UI action in a group box with specified caption.
Let's add another input to allow selection of operation performed on these inputs.

from op in UIM.Choice("Sum", "Sum", "Product").Title("Operation")

By default, change of value in any of these three inputs will cause immediate change
in any sources connected to them. Suppose we want to override this behaviour and produce
output only when user clicks a button. Let's add it to the layout:

from click in UIM.Button("Go")
Sources produced by buttons are only useful for their synchronization capabilities, which
will be shown shortly.

Now we have 4 controls that are placed on top of each other, which doesn't look very interesting.
This behavior can be overriden using the OrderBy method which accepts a value of type
LayoutDirection. Available layouts can be accessed via static properties of LayoutDirection.

from i1 in UIM.Slider(0, 10).Title("Value 1")
from i2 in UIM.Slider(0, 10).Title("Value 2")
from op in UIM.Choice("Sum", "Sum", "Product").Title("Operation")
orderby LayoutDirection.FromTop
from click in UIM.Button("Go")
orderby LayoutDirection.FromLeft

Now, it's time to perform actual transformation. For that, we need to extract the values of all input sources:

let result = 
	from x in i1
	from y in i2
	from name in op
	orderby click
	let r = name == "Sum" ? x + y : x * y						
	select r 
Keyword orderby in query to sources means that the rest of the query is delayed until
click source produces a value. Alternatively, method call source.When(click) can be used.

Now, the only thing left is to display the result:

from _ in UIM.Output(result).Title("Result")
orderby LayoutDirection.FromTop
group result by "The ultimate arithmetic machine";
Expression
group result by "The ultimate arithmetic machine";
causes UI action to title whole layout with given string and return the result.

Here is the complete query:

from i1 in UIM.Slider(0, 10).Title("Value 1")
from i2 in UIM.Slider(0, 10).Title("Value 2")
from op in UIM.Choice("Sum", "Sum", "Product").Title("Operation")
orderby LayoutDirection.FromTop
from click in UIM.Button("Go")
orderby LayoutDirection.FromLeft
let result = 
	from x in i1
	from y in i2
	from name in op
	orderby click
	let r = name == "Sum" ? x + y : x * y				
	select r 
from _ in UIM.Output(result).Title("Result")
orderby LayoutDirection.FromTop
group result by "The ultimate arithmetic machine";

When executed, the action produces the following:

Sample2.jpg

Last edited Dec 7, 2008 at 12:16 PM by Yuuki, version 10

Comments

No comments yet.