Skip to content

Commit

Permalink
Merge pull request #464 from AvaloniaUI/watermark-support
Browse files Browse the repository at this point in the history
Add watermark support
  • Loading branch information
Gillibald authored Nov 11, 2024
2 parents f0db95e + 64babb9 commit d4a0d38
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/AvaloniaEdit.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Visible"
FontWeight="Light"
FontSize="14" >
FontSize="14"
Watermark="Start typing to bring your ideas to life...">
<AvalonEdit:TextEditor.ContextFlyout>
<MenuFlyout>
<MenuItem Header="Copy" InputGesture="ctrl+C" Command="{Binding CopyMouseCommmand}" CommandParameter="{Binding #Editor.TextArea}"></MenuItem>
Expand Down
18 changes: 18 additions & 0 deletions src/AvaloniaEdit/Editing/TextArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ internal void RemoveChild(Visual visual)

#endregion

#region Watermark
/// <summary>
/// Defines the <see cref="Watermark"/> property
/// </summary>
public static readonly StyledProperty<string> WatermarkProperty =
AvaloniaProperty.Register<TextArea, string>(nameof(Watermark));

/// <summary>
/// Gets or sets the placeholder or descriptive text that is displayed even if the <see cref="Text"/>
/// property is not yet set.
/// </summary>
public string Watermark
{
get => GetValue(WatermarkProperty);
set => SetValue(WatermarkProperty, value);
}
#endregion

/// <summary>
/// Defines the <see cref="IScrollable.Offset" /> property.
/// </summary>
Expand Down
16 changes: 14 additions & 2 deletions src/AvaloniaEdit/Editing/TextArea.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,20 @@
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<ContentPresenter Name="PART_CP" Cursor="IBeam"
Focusable="False" Background="{TemplateBinding Background}" />
<Panel>
<ContentPresenter Name="PART_CP"
Cursor="IBeam"
Focusable="False"
Background="{TemplateBinding Background}" />
<TextBlock Name="PART_Watermark"
Opacity="0.5"
Text="{TemplateBinding Watermark}"
Foreground="{TemplateBinding Foreground}">
<TextBlock.IsVisible>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Document.Text" Converter="{x:Static StringConverters.IsNullOrEmpty}"/>
</TextBlock.IsVisible>
</TextBlock>
</Panel>
</DockPanel>
</ControlTemplate>
</Setter>
Expand Down
10 changes: 10 additions & 0 deletions src/AvaloniaEdit/TextEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ private void OnTextChanged(object sender, EventArgs e)
#endregion

#region Text property
/// <summary>
/// Gets or sets the placeholder or descriptive text that is displayed even if the <see cref="Text"/>
/// property is not yet set.
/// </summary>
public string Watermark
{
get => textArea.Watermark;
set => textArea.Watermark = value;
}

/// <summary>
/// Gets/Sets the text of the current document.
/// </summary>
Expand Down

0 comments on commit d4a0d38

Please sign in to comment.