Skip to content

Commit

Permalink
feat(gradient): [Wasm] Added support for gradient text
Browse files Browse the repository at this point in the history
  • Loading branch information
carldebilly committed May 7, 2020
1 parent fe5d86c commit 92aa150
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<StackPanel Spacing="10">
<TextBlock FontSize="15">Radial Gradient as background on a Rectangle + Border</TextBlock>
<StackPanel Orientation="Horizontal" Spacing="10">
<Rectangle Width="125" Height="125">
<Rectangle Width="100" Height="100">
<Rectangle.Fill>
<RadialGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
Expand All @@ -22,7 +22,7 @@
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Border Width="125" Height="125">
<Border Width="100" Height="100">
<Border.Background>
<RadialGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
Expand All @@ -37,7 +37,7 @@
</StackPanel>
<TextBlock FontSize="15">Radial Gradient as border/stroke on a Rectangle + Border</TextBlock>
<StackPanel Orientation="Horizontal" Spacing="10">
<Rectangle Width="125" Height="125" StrokeThickness="40">
<Rectangle Width="100" Height="100" StrokeThickness="40">
<Rectangle.Stroke>
<RadialGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
Expand All @@ -49,7 +49,7 @@
</RadialGradientBrush>
</Rectangle.Stroke>
</Rectangle>
<Border Width="125" Height="125" BorderThickness="40">
<Border Width="100" Height="100" BorderThickness="40">
<Border.BorderBrush>
<RadialGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
Expand All @@ -64,7 +64,7 @@
</StackPanel>
<TextBlock FontSize="15">Linear Gradient as background on a Rectangle + Border (relative + absolute)</TextBlock>
<StackPanel Orientation="Horizontal" Spacing="10">
<Rectangle Width="125" Height="125">
<Rectangle Width="100" Height="100">
<Rectangle.Fill>
<LinearGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
Expand All @@ -76,7 +76,7 @@
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Border Width="125" Height="125">
<Border Width="100" Height="100">
<Border.Background>
<LinearGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
Expand All @@ -88,9 +88,9 @@
</LinearGradientBrush>
</Border.Background>
</Border>
<Rectangle Width="125" Height="125">
<Rectangle Width="100" Height="100">
<Rectangle.Fill>
<LinearGradientBrush MappingMode="Absolute" EndPoint="125,125">
<LinearGradientBrush MappingMode="Absolute" EndPoint="100,100">
<GradientStop Color="Blue" Offset="0.0" />
<GradientStop Color="Yellow" Offset="0.2" />
<GradientStop Color="LimeGreen" Offset="0.4" />
Expand All @@ -100,9 +100,9 @@
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Border Width="125" Height="125">
<Border Width="100" Height="100">
<Border.Background>
<LinearGradientBrush MappingMode="Absolute" EndPoint="125,125">
<LinearGradientBrush MappingMode="Absolute" EndPoint="100,100">
<GradientStop Color="Blue" Offset="0.0" />
<GradientStop Color="Yellow" Offset="0.2" />
<GradientStop Color="LimeGreen" Offset="0.4" />
Expand All @@ -115,7 +115,7 @@
</StackPanel>
<TextBlock FontSize="15">Linear Gradient as border/stroke on a Rectangle + Border (relative + absolute)</TextBlock>
<StackPanel Orientation="Horizontal" Spacing="10">
<Rectangle Width="125" Height="125" StrokeThickness="40">
<Rectangle Width="100" Height="100" StrokeThickness="40">
<Rectangle.Stroke>
<LinearGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
Expand All @@ -127,7 +127,7 @@
</LinearGradientBrush>
</Rectangle.Stroke>
</Rectangle>
<Border Width="125" Height="125" BorderThickness="40">
<Border Width="100" Height="100" BorderThickness="40">
<Border.BorderBrush>
<LinearGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
Expand All @@ -139,9 +139,9 @@
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
<Rectangle Width="125" Height="125" StrokeThickness="40">
<Rectangle Width="100" Height="100" StrokeThickness="40">
<Rectangle.Stroke>
<LinearGradientBrush MappingMode="Absolute" EndPoint="125,125">
<LinearGradientBrush MappingMode="Absolute" EndPoint="100,100">
<GradientStop Color="Blue" Offset="0.0" />
<GradientStop Color="Yellow" Offset="0.2" />
<GradientStop Color="LimeGreen" Offset="0.4" />
Expand All @@ -151,9 +151,9 @@
</LinearGradientBrush>
</Rectangle.Stroke>
</Rectangle>
<Border Width="125" Height="125" BorderThickness="40">
<Border Width="100" Height="100" BorderThickness="40">
<Border.BorderBrush>
<LinearGradientBrush MappingMode="Absolute" EndPoint="125,125">
<LinearGradientBrush MappingMode="Absolute" EndPoint="100,100">
<GradientStop Color="Blue" Offset="0.0" />
<GradientStop Color="Yellow" Offset="0.2" />
<GradientStop Color="LimeGreen" Offset="0.4" />
Expand All @@ -164,5 +164,32 @@
</Border.BorderBrush>
</Border>
</StackPanel>
<TextBlock FontSize="15">Text with Radial/Linear gradient as foreground brush</TextBlock>
<StackPanel Orientation="Horizontal" Spacing="10">
<TextBlock Text="UNO" FontSize="110" FontWeight="Bold">
<TextBlock.Foreground>
<RadialGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
<GradientStop Color="Yellow" Offset="0.2" />
<GradientStop Color="LimeGreen" Offset="0.4" />
<GradientStop Color="LightBlue" Offset="0.6" />
<GradientStop Color="Blue" Offset="0.8" />
<GradientStop Color="LightGray" Offset="1" />
</RadialGradientBrush>
</TextBlock.Foreground>
</TextBlock>
<TextBlock Text="UNO" FontSize="110" FontWeight="Bold">
<TextBlock.Foreground>
<LinearGradientBrush>
<GradientStop Color="Blue" Offset="0.0" />
<GradientStop Color="Yellow" Offset="0.2" />
<GradientStop Color="LimeGreen" Offset="0.4" />
<GradientStop Color="LightBlue" Offset="0.6" />
<GradientStop Color="Blue" Offset="0.8" />
<GradientStop Color="LightGray" Offset="1" />
</LinearGradientBrush>
</TextBlock.Foreground>
</TextBlock>
</StackPanel>
</StackPanel>
</Page>
4 changes: 4 additions & 0 deletions src/Uno.UI.Wasm/WasmCSS/Uno.UI.css
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ embed.uno-frameworkelement.uno-unarranged {
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version, currently supported by Chrome and Opera */

-ms-background-clip: text !important;
-webkit-background-clip: text !important;
background-clip: text !important;
}

.uno-buttonbase {
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,12 @@ Brush Foreground
get { return (Brush)this.GetValue(ForegroundProperty); }
set
{
#if !__WASM__
if (!(Foreground is SolidColorBrush))
{
throw new NotSupportedException();
}
#endif

this.SetValue(ForegroundProperty, value);
}
Expand Down
11 changes: 10 additions & 1 deletion src/Uno.UI/UI/Xaml/Documents/UIElementTextHelper.wasm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Web;
using Windows.Foundation;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand Down Expand Up @@ -139,12 +140,20 @@ internal static void SetForeground(this UIElement element, object localValue)
case SolidColorBrush scb:
element.SetStyle("color", scb.ColorWithOpacity.ToHexString());
break;
case GradientBrush gradient:
element.SetStyle(
("background", gradient.ToCssString(element.RenderSize)),
("color", "transparent"),
("background-clip", "text")
);
break;

case UnsetValue uv:


// TODO: support other foreground types
default:
element.ResetStyle("color");
element.ResetStyle("color", "background", "background-clip");
break;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Uno.UI/UI/Xaml/Media/LinearGradientBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ public Point EndPoint
typeof(LinearGradientBrush),
new PropertyMetadata(new Point(1,1))
);

}
}

0 comments on commit 92aa150

Please sign in to comment.