Merge pull request #209 from NativeScript/background-image-css

Background image css property implemented
This commit is contained in:
Vladimir Enchev
2015-03-20 14:49:56 +02:00
6 changed files with 115 additions and 25 deletions

View File

@ -543,6 +543,7 @@
<Content Include="apps\gallery-app\layouts\dock-layout.xml" /> <Content Include="apps\gallery-app\layouts\dock-layout.xml" />
<Content Include="apps\template-master-detail\details-view.xml" /> <Content Include="apps\template-master-detail\details-view.xml" />
<Content Include="apps\template-master-detail\main-page.minWH600.xml" /> <Content Include="apps\template-master-detail\main-page.minWH600.xml" />
<Content Include="apps\TelerikNEXT\images\background.jpg" />
<Content Include="apps\template-settings\app.css" /> <Content Include="apps\template-settings\app.css" />
<Content Include="apps\tests\app\binding_tests.xml" /> <Content Include="apps\tests\app\binding_tests.xml" />
<Content Include="apps\tests\ui\label\label-tests-wrong.css" /> <Content Include="apps\tests\ui\label\label-tests-wrong.css" />
@ -1517,7 +1518,7 @@
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile> <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties> </WebProjectProperties>
</FlavorProperties> </FlavorProperties>
<UserProperties ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2linear-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" /> <UserProperties ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2linear-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" />
</VisualStudio> </VisualStudio>
</ProjectExtensions> </ProjectExtensions>
</Project> </Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB

View File

@ -23,8 +23,9 @@
</tsb:SideBar.slideContent> </tsb:SideBar.slideContent>
<tsb:SideBar.mainContent> <tsb:SideBar.mainContent>
<GridLayout rows="auto, auto, auto, *"> <GridLayout rows="auto, auto, *">
<StackLayout style="background-image: url('~/app/images/background.jpg')">
<SegmentedBar selectedIndex="{{ selectedIndex }}" style="background-color: transparent;color: white;" selectedBackgroundColor="#fac950"> <SegmentedBar selectedIndex="{{ selectedIndex }}" style="background-color: transparent;color: white;" selectedBackgroundColor="#fac950">
<SegmentedBar.items> <SegmentedBar.items>
<SegmentedBarItem title="MAY 3" /> <SegmentedBarItem title="MAY 3" />
@ -33,7 +34,7 @@
</SegmentedBar.items> </SegmentedBar.items>
</SegmentedBar> </SegmentedBar>
<Label row="1" style="horizontal-align: center;margin: 15;"> <Label style="horizontal-align: center;margin: 15;">
<Label.formattedText> <Label.formattedText>
<FormattedString fontSize="18" foregroundColor="#fac950"> <FormattedString fontSize="18" foregroundColor="#fac950">
<FormattedString.spans> <FormattedString.spans>
@ -42,10 +43,11 @@
</FormattedString> </FormattedString>
</Label.formattedText> </Label.formattedText>
</Label> </Label>
</StackLayout>
<SearchBar text="{{ search }}" hint="Search" style="background-color: #fac950; color: #fac950;" textFieldBackgroundColor="white" row="2" /> <SearchBar text="{{ search }}" hint="Search" style="background-color: #fac950; color: #fac950;" textFieldBackgroundColor="white" row="1" />
<ListView items="{{ sessions }}" row="3" separatorColor="#fac950"> <ListView items="{{ sessions }}" row="2" separatorColor="#fac950">
<ListView.itemTemplate> <ListView.itemTemplate>
<GridLayout columns="auto, *"> <GridLayout columns="auto, *">

View File

@ -9,6 +9,7 @@ import stylers = require("ui/styling/stylers");
import styleProperty = require("ui/styling/style-property"); import styleProperty = require("ui/styling/style-property");
import converters = require("ui/styling/converters"); import converters = require("ui/styling/converters");
import enums = require("ui/enums"); import enums = require("ui/enums");
import imageSource = require("image-source");
// key is the property id and value is Dictionary<string, StylePropertyChangedHandler>; // key is the property id and value is Dictionary<string, StylePropertyChangedHandler>;
var _registeredHandlers = {}; var _registeredHandlers = {};
@ -34,6 +35,13 @@ export class Style extends observable.DependencyObservable implements styling.St
this._setValue(backgroundColorProperty, value, observable.ValueSource.Local); this._setValue(backgroundColorProperty, value, observable.ValueSource.Local);
} }
get backgroundImage(): string {
return this._getValue(backgroundImageProperty);
}
set backgroundImage(value: string) {
this._setValue(backgroundImageProperty, value, observable.ValueSource.Local);
}
get fontSize(): number { get fontSize(): number {
return this._getValue(fontSizeProperty); return this._getValue(fontSizeProperty);
} }
@ -332,6 +340,26 @@ export var colorProperty = new styleProperty.Property("color", "color",
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.Inheritable, undefined, undefined, color.Color.equals), new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.Inheritable, undefined, undefined, color.Color.equals),
converters.colorConverter); converters.colorConverter);
export var backgroundImageProperty = new styleProperty.Property("backgroundImage", "background-image",
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.None, onBackgroundImagePropertyChanged));
function onBackgroundImagePropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var pattern: RegExp = /url\(('|")(.*?)\1\)/;
var url = (<string>data.newValue).match(pattern)[2];
if (imageSource.isFileOrResourcePath(url)) {
style._setValue(backgroundImageSourceProperty, imageSource.fromFileOrResource(url), observable.ValueSource.Local);
} else if (types.isString(url)) {
imageSource.fromUrl(url).then(r=> {
style._setValue(backgroundImageSourceProperty, r, observable.ValueSource.Local);
});
}
}
export var backgroundImageSourceProperty = new styleProperty.Property("backgroundImageSource", "background-image-source",
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.None, undefined, undefined, undefined));
export var backgroundColorProperty = new styleProperty.Property("backgroundColor", "background-color", export var backgroundColorProperty = new styleProperty.Property("backgroundColor", "background-color",
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.None, undefined, undefined, color.Color.equals), new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.None, undefined, undefined, color.Color.equals),
converters.colorConverter); converters.colorConverter);

View File

@ -35,6 +35,32 @@ export class DefaultStyler implements definition.stylers.Styler {
return drawable; return drawable;
} }
//Background image methods
private static setBackgroundImageSourceProperty(view: view.View, newValue: any) {
var nativeView = <android.view.View>view.android;
var bmp = <android.graphics.Bitmap>newValue;
var d = new android.graphics.drawable.BitmapDrawable(bmp);
d.setTileModeXY(android.graphics.Shader.TileMode.REPEAT, android.graphics.Shader.TileMode.REPEAT);
d.setDither(true);
nativeView.setBackgroundDrawable(d);
}
private static resetBackgroundImageSourceProperty(view: view.View, nativeValue: any) {
if (types.isDefined(nativeValue)) {
(<android.view.View>view.android).setBackgroundDrawable(nativeValue)
}
}
private static getNativeBackgroundImageSourceValue(view: view.View): any {
var drawable = view.android.getBackground();
if (drawable instanceof android.graphics.drawable.BitmapDrawable) {
return drawable;
}
return undefined;
}
//Visibility methods //Visibility methods
private static setVisibilityProperty(view: view.View, newValue: any) { private static setVisibilityProperty(view: view.View, newValue: any) {
var androidValue = (newValue === enums.Visibility.visible) ? android.view.View.VISIBLE : android.view.View.GONE; var androidValue = (newValue === enums.Visibility.visible) ? android.view.View.VISIBLE : android.view.View.GONE;
@ -78,6 +104,11 @@ export class DefaultStyler implements definition.stylers.Styler {
DefaultStyler.resetBackgroundProperty, DefaultStyler.resetBackgroundProperty,
DefaultStyler.getNativeBackgroundValue)); DefaultStyler.getNativeBackgroundValue));
style.registerHandler(style.backgroundImageSourceProperty, new stylersCommon.StylePropertyChangedHandler(
DefaultStyler.setBackgroundImageSourceProperty,
DefaultStyler.resetBackgroundImageSourceProperty,
DefaultStyler.getNativeBackgroundImageSourceValue));
style.registerHandler(style.visibilityProperty, new stylersCommon.StylePropertyChangedHandler( style.registerHandler(style.visibilityProperty, new stylersCommon.StylePropertyChangedHandler(
DefaultStyler.setVisibilityProperty, DefaultStyler.setVisibilityProperty,
DefaultStyler.resetVisibilityProperty)); DefaultStyler.resetVisibilityProperty));

View File

@ -32,6 +32,29 @@ export class DefaultStyler implements definition.stylers.Styler {
return undefined; return undefined;
} }
//Background image methods
private static setBackgroundImageSourceProperty(view: view.View, newValue: any) {
var nativeView: UIView = <UIView>view._nativeView;
if (nativeView) {
nativeView.backgroundColor = UIColor.alloc().initWithPatternImage(newValue);
}
}
private static resetBackgroundImageSourceProperty(view: view.View, nativeValue: any) {
var nativeView: UIView = <UIView>view._nativeView;
if (nativeView) {
nativeView.backgroundColor = nativeValue;
}
}
private static getNativeBackgroundImageSourceValue(view: view.View): any {
var nativeView: UIView = <UIView>view._nativeView;
if (nativeView) {
return nativeView.backgroundColor;
}
return undefined;
}
//Visibility methods //Visibility methods
private static setVisibilityProperty(view: view.View, newValue: any) { private static setVisibilityProperty(view: view.View, newValue: any) {
var nativeView: UIView = <UIView>view._nativeView; var nativeView: UIView = <UIView>view._nativeView;
@ -68,6 +91,11 @@ export class DefaultStyler implements definition.stylers.Styler {
DefaultStyler.resetBackgroundProperty, DefaultStyler.resetBackgroundProperty,
DefaultStyler.getNativeBackgroundValue)); DefaultStyler.getNativeBackgroundValue));
style.registerHandler(style.backgroundImageSourceProperty, new stylersCommon.StylePropertyChangedHandler(
DefaultStyler.setBackgroundImageSourceProperty,
DefaultStyler.resetBackgroundImageSourceProperty,
DefaultStyler.getNativeBackgroundImageSourceValue));
style.registerHandler(style.visibilityProperty, new stylersCommon.StylePropertyChangedHandler( style.registerHandler(style.visibilityProperty, new stylersCommon.StylePropertyChangedHandler(
DefaultStyler.setVisibilityProperty, DefaultStyler.setVisibilityProperty,
DefaultStyler.resetVisibilityProperty)); DefaultStyler.resetVisibilityProperty));