Skip to content

Commit

Permalink
Merge pull request #1514 from WilliamABradley/TextToolbar-CustomForma…
Browse files Browse the repository at this point in the history
…tterButtonFix

TextToolbar - Switch Format from Custom Formatter
IbraheemOsama authored Oct 29, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 818f687 + d1a3525 commit a520062
Showing 4 changed files with 67 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Microsoft.Toolkit.Uwp.SampleApp/Common/EnumConverter.cs
Original file line number Diff line number Diff line change
@@ -26,12 +26,12 @@ public EnumConverter(Type enumType)

public object Convert(object value, Type targetType, object parameter, string language)
{
return value;
return value.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return Enum.Parse(_enumType, value.ToString());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
<controls:TextToolbar x:Name="Toolbar"
Editor="{Binding ElementName=EditZone}"
IsEnabled="@[IsEnabled:Bool:True]"
Format="@[Format:Enum:Format.RichText]"
Format="@[Format:Enum:Format.RichText]@"
Background="#4C4F4F4F" />

<ScrollViewer Grid.Row="1">
Original file line number Diff line number Diff line change
@@ -11,10 +11,13 @@
// ******************************************************************

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.SampleApp.Models;
using Microsoft.Toolkit.Uwp.SampleApp.SamplePages.TextToolbarSamples;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarButtons;
using Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarFormats;
using Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarFormats.MarkDown;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Windows.System;
@@ -38,17 +41,21 @@ public void OnXamlRendered(FrameworkElement control)
{
_toolbar = control.FindChildByName("Toolbar") as TextToolbar;

var editZone = control.FindChildByName("EditZone") as RichEditBox;
if (editZone != null)
if (control.FindChildByName("EditZone") is RichEditBox editZone)
{
editZone.TextChanged += EditZone_TextChanged;
}

_previewer = control.FindChildByName("Previewer") as MarkdownTextBlock;
if (_previewer != null)
if (control.FindChildByName("Previewer") is MarkdownTextBlock previewer)
{
_previewer = previewer;
_previewer.LinkClicked += Previewer_LinkClicked;
}

if (ToolbarFormat != null && (Format)ToolbarFormat.Value == Format.Custom)
{
UseCustomFormatter();
}
}

protected override void OnNavigatedTo(NavigationEventArgs e)
@@ -60,7 +67,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
var button = _toolbar?.GetDefaultButton(ButtonType.Bold);
if (button != null)
{
button.Visibility = button.Visibility == Windows.UI.Xaml.Visibility.Visible ? Windows.UI.Xaml.Visibility.Collapsed : Windows.UI.Xaml.Visibility.Visible;
button.Visibility = button.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
}
});

@@ -73,22 +80,45 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
UseCustomFormatter();
});

Shell.Current.RegisterNewCommand("Reset Layout", (sender, args) =>
{
ResetLayout();
});
}

private void UseCustomFormatter()
private void ResetLayout()
{
if (_toolbar == null)
{
return;
}

_toolbar.CustomButtons.Clear();
if (_toolbar.DefaultButtons != null)
{
foreach (var item in _toolbar.DefaultButtons)
{
if (item is ToolbarButton button)
{
button.Visibility = Visibility.Visible;
}
}
}
}

private void UseCustomFormatter()
{
if (_toolbar == null || ToolbarFormat == null)
{
return;
}

var formatter = new SampleFormatter(_toolbar);
_toolbar.Format = UI.Controls.TextToolbarFormats.Format.Custom;
ToolbarFormat.Value = Format.Custom;
_toolbar.Formatter = formatter;
}

private int DemoCounter { get; set; } = 0;

private void AddCustomButton()
{
if (_toolbar == null)
@@ -116,8 +146,7 @@ private void AddCustomButton()
ShortcutKey = shortcut,
Activation = (b) =>
{
var md = _toolbar.Formatter as MarkDownFormatter;
if (md != null)
if (_toolbar.Formatter is MarkDownFormatter md)
{
md.SetSelection($"[{demoText}]", $"[/{demoText}]");
}
@@ -143,19 +172,36 @@ private void Previewer_LinkClicked(object sender, LinkClickedEventArgs e)
}
}

private void EditZone_TextChanged(object sender, Windows.UI.Xaml.RoutedEventArgs e)
private void EditZone_TextChanged(object sender, RoutedEventArgs e)
{
if (_toolbar == null || _previewer == null)
{
return;
}

var md = _toolbar.Formatter as MarkDownFormatter;
if (md != null)
if (_toolbar.Formatter is MarkDownFormatter md)
{
string text = md.Text;
_previewer.Text = string.IsNullOrWhiteSpace(text) ? "Nothing to Preview" : text;
}
}

private int DemoCounter { get; set; } = 0;

private ValueHolder ToolbarFormat
{
get
{
if (DataContext is Sample sample)
{
if (sample.PropertyDescriptor.Expando is IDictionary<string, object> properties && properties.TryGetValue("Format", out var format))
{
return format as ValueHolder;
}
}

return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -205,6 +205,10 @@ private void OnButtonMapModified(object sender, NotifyCollectionChangedEventArgs
}

break;

case NotifyCollectionChangedAction.Reset:
BuildBar();
break;
}
}
}

0 comments on commit a520062

Please sign in to comment.