Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite Canvas dropdown font selector 2 #3914

Merged
13 commits merged into from
Apr 20, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class InfiniteCanvasCreateTextBoxCommand : IInfiniteCanvasCommand
private readonly List<IDrawable> _drawableList;
private readonly TextDrawable _drawable;

public InfiniteCanvasCreateTextBoxCommand(List<IDrawable> drawableList, double x, double y, double width, double height, int textFontSize, string text, Color color, bool isBold, bool isItalic)
public InfiniteCanvasCreateTextBoxCommand(List<IDrawable> drawableList, double x, double y, double width, double height, float textFontSize, string text, Color color, bool isBold, bool isItalic)
{
_drawable = new TextDrawable(
x,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal void ExecuteUpdateTextBoxFontSize(float newValue)
ExecuteCommand(command);
}

internal void ExecuteCreateTextBox(double x, double y, double width, double height, int textFontSize, string text, Color color, bool isBold, bool isItalic)
internal void ExecuteCreateTextBox(double x, double y, double width, double height, float textFontSize, string text, Color color, bool isBold, bool isItalic)
{
var command = new InfiniteCanvasCreateTextBoxCommand(_drawableList, x, y, width, height, textFontSize, text, color, isBold, isItalic);
ExecuteCommand(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,20 @@ public partial class InfiniteCanvas
"Right",
"Up",
"Left",
"Down"
"Down",
"Enter"
};

private Point _lastInputPoint;

private TextDrawable SelectedTextDrawable => _drawingSurfaceRenderer.GetSelectedTextDrawable();

private int _lastValidTextFontSizeValue = DefaultFontValue;
private float _textFontSize = DefaultFontValue;

private int TextFontSize
private void SetFontSize(float newSize)
{
get
{
if (!string.IsNullOrWhiteSpace(_canvasTextBoxFontSizeTextBox.Text) &&
Regex.IsMatch(_canvasTextBoxFontSizeTextBox.Text, "^[0-9]*$"))
{
var fontSize = int.Parse(_canvasTextBoxFontSizeTextBox.Text);
_lastValidTextFontSizeValue = fontSize;
}

return _lastValidTextFontSizeValue;
}
_textFontSize = newSize;
_canvasTextBox.UpdateFontSize(newSize);
}

private void InkScrollViewer_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
Expand Down Expand Up @@ -93,13 +85,34 @@ private void CanvasTextBoxItalicButton_Clicked(object sender, RoutedEventArgs e)
}
}

private void CanvasTextBoxFontSizeTextBox_TextChanged(object sender, TextChangedEventArgs e)
private void CanvasComboBoxFontSizeTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_canvasTextBox.UpdateFontSize(TextFontSize);
if (SelectedTextDrawable != null)
if (sender is ComboBox s
&& s.SelectedItem is ComboBoxItem selectedItem
&& selectedItem.Content is string selectedText
&& float.TryParse(selectedText, out var sizeNumb))
{
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(TextFontSize);
ReDrawCanvas();
SetFontSize(sizeNumb);

if (SelectedTextDrawable != null)
{
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(sizeNumb);
ReDrawCanvas();
}
}
}

private void CanvasComboBoxFontSizeTextBox_TextSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs args)
{
if (float.TryParse(args.Text, out var size))
{
SetFontSize(size);

if (SelectedTextDrawable != null)
{
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(size);
ReDrawCanvas();
}
}
}

Expand Down Expand Up @@ -147,20 +160,22 @@ private void CanvasTextBox_TextChanged(object sender, string text)
ReDrawCanvas();
return;
}
else
{
_drawingSurfaceRenderer.ExecuteCreateTextBox(
_lastInputPoint.X,
_lastInputPoint.Y,
_canvasTextBox.GetEditZoneWidth(),
_canvasTextBox.GetEditZoneHeight(),
_textFontSize,
text,
_canvasTextBoxColorPicker.Color,
_canvasTextBoxBoldButton.IsChecked ?? false,
_canvasTextBoxItalicButton.IsChecked ?? false);

_drawingSurfaceRenderer.ExecuteCreateTextBox(
_lastInputPoint.X,
_lastInputPoint.Y,
_canvasTextBox.GetEditZoneWidth(),
_canvasTextBox.GetEditZoneHeight(),
TextFontSize,
text,
_canvasTextBoxColorPicker.Color,
_canvasTextBoxBoldButton.IsChecked ?? false,
_canvasTextBoxItalicButton.IsChecked ?? false);

ReDrawCanvas();
_drawingSurfaceRenderer.UpdateSelectedTextDrawable();
ReDrawCanvas();
_drawingSurfaceRenderer.UpdateSelectedTextDrawable();
}
}

private void InkScrollViewer_PointerPressed(object sender, PointerRoutedEventArgs e)
Expand All @@ -179,20 +194,17 @@ private void InkScrollViewer_PointerPressed(object sender, PointerRoutedEventArg

Canvas.SetLeft(_canvasTextBox, SelectedTextDrawable.Bounds.X);
Canvas.SetTop(_canvasTextBox, SelectedTextDrawable.Bounds.Y);
_canvasTextBox.UpdateFontSize(SelectedTextDrawable.FontSize);
_canvasTextBox.UpdateFontStyle(SelectedTextDrawable.IsItalic);
_canvasTextBox.UpdateFontWeight(SelectedTextDrawable.IsBold);

// Updating toolbar
_canvasTextBoxColorPicker.Color = SelectedTextDrawable.TextColor;
_canvasTextBoxFontSizeTextBox.Text = SelectedTextDrawable.FontSize.ToString();
_canvasTextBoxBoldButton.IsChecked = SelectedTextDrawable.IsBold;
_canvasTextBoxItalicButton.IsChecked = SelectedTextDrawable.IsItalic;

return;
}

_canvasTextBox.UpdateFontSize(TextFontSize);
_canvasTextBox.UpdateFontStyle(_canvasTextBoxItalicButton.IsChecked ?? false);
_canvasTextBox.UpdateFontWeight(_canvasTextBoxBoldButton.IsChecked ?? false);

Expand All @@ -210,7 +222,7 @@ private void ClearTextBoxValue()
_canvasTextBox.Clear();
}

private void CanvasTextBoxFontSizeTextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
private void CanvasComboBoxFontSizeTextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (_allowedCommands.Contains(e.Key.ToString()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
/// </summary>
[TemplatePart(Name = CanvasTextBoxToolsName, Type = typeof(StackPanel))]
[TemplatePart(Name = CanvasTextBoxColorPickerName, Type = typeof(Windows.UI.Xaml.Controls.ColorPicker))]
[TemplatePart(Name = CanvasTextBoxFontSizeTextBoxName, Type = typeof(TextBox))]
[TemplatePart(Name = CanvasComboBoxFontSizeTextBoxName, Type = typeof(TextBox))]
[TemplatePart(Name = CanvasTextBoxItalicButtonName, Type = typeof(ToggleButton))]
[TemplatePart(Name = CanvasTextBoxBoldButtonName, Type = typeof(ToggleButton))]
[TemplatePart(Name = DrawingSurfaceRendererName, Type = typeof(InfiniteCanvasVirtualDrawingSurface))]
Expand All @@ -45,7 +45,7 @@ public partial class InfiniteCanvas : Control

private const string CanvasTextBoxToolsName = "CanvasTextBoxTools";
private const string CanvasTextBoxColorPickerName = "CanvasTextBoxColorPicker";
private const string CanvasTextBoxFontSizeTextBoxName = "CanvasTextBoxFontSizeTextBox";
private const string CanvasComboBoxFontSizeTextBoxName = "CanvasComboBoxFontSizeTextBox";
private const string CanvasTextBoxItalicButtonName = "CanvasTextBoxItalicButton";
private const string CanvasTextBoxBoldButtonName = "CanvasTextBoxBoldButton";
private const string DrawingSurfaceRendererName = "DrawingSurfaceRenderer";
Expand All @@ -71,7 +71,7 @@ public partial class InfiniteCanvas : Control
private StackPanel _canvasTextBoxTools;
private Windows.UI.Xaml.Controls.ColorPicker _canvasTextBoxColorPicker;

private TextBox _canvasTextBoxFontSizeTextBox;
private ComboBox _canvasComboBoxFontSizeTextBox;
private ToggleButton _canvasTextBoxItalicButton;
private ToggleButton _canvasTextBoxBoldButton;
private Button _undoButton;
Expand Down Expand Up @@ -244,7 +244,7 @@ protected override void OnApplyTemplate()
{
_canvasTextBoxTools = (StackPanel)GetTemplateChild(CanvasTextBoxToolsName);
this._canvasTextBoxColorPicker = (Windows.UI.Xaml.Controls.ColorPicker)GetTemplateChild(CanvasTextBoxColorPickerName);
_canvasTextBoxFontSizeTextBox = (TextBox)GetTemplateChild(CanvasTextBoxFontSizeTextBoxName);
_canvasComboBoxFontSizeTextBox = (ComboBox)GetTemplateChild(CanvasComboBoxFontSizeTextBoxName);
_canvasTextBoxItalicButton = (ToggleButton)GetTemplateChild(CanvasTextBoxItalicButtonName);
_canvasTextBoxBoldButton = (ToggleButton)GetTemplateChild(CanvasTextBoxBoldButtonName);
_drawingSurfaceRenderer = (InfiniteCanvasVirtualDrawingSurface)GetTemplateChild(DrawingSurfaceRendererName);
Expand Down Expand Up @@ -296,7 +296,7 @@ protected override void OnApplyTemplate()

private void UnRegisterEvents()
{
_canvasTextBoxFontSizeTextBox.TextChanged -= CanvasTextBoxFontSizeTextBox_TextChanged;
_canvasComboBoxFontSizeTextBox.SelectionChanged -= CanvasComboBoxFontSizeTextBox_SelectionChanged;
_canvasTextBoxItalicButton.Click -= CanvasTextBoxItalicButton_Clicked;
_canvasTextBoxBoldButton.Click -= CanvasTextBoxBoldButton_Clicked;
_canvasTextBoxColorPicker.ColorChanged -= CanvasTextBoxColorPicker_ColorChanged;
Expand All @@ -314,13 +314,14 @@ private void UnRegisterEvents()
Unloaded -= InfiniteCanvas_Unloaded;
Application.Current.LeavingBackground -= Current_LeavingBackground;
_drawingSurfaceRenderer.CommandExecuted -= DrawingSurfaceRenderer_CommandExecuted;
_canvasTextBoxFontSizeTextBox.PreviewKeyDown -= CanvasTextBoxFontSizeTextBox_PreviewKeyDown;
_canvasComboBoxFontSizeTextBox.PreviewKeyDown -= CanvasComboBoxFontSizeTextBox_PreviewKeyDown;
_canvasComboBoxFontSizeTextBox.TextSubmitted -= CanvasComboBoxFontSizeTextBox_TextSubmitted;
Loaded -= InfiniteCanvas_Loaded;
}

private void RegisterEvents()
{
_canvasTextBoxFontSizeTextBox.TextChanged += CanvasTextBoxFontSizeTextBox_TextChanged;
_canvasComboBoxFontSizeTextBox.SelectionChanged += CanvasComboBoxFontSizeTextBox_SelectionChanged;
_canvasTextBoxItalicButton.Click += CanvasTextBoxItalicButton_Clicked;
_canvasTextBoxBoldButton.Click += CanvasTextBoxBoldButton_Clicked;
_canvasTextBoxColorPicker.ColorChanged += CanvasTextBoxColorPicker_ColorChanged;
Expand All @@ -338,7 +339,8 @@ private void RegisterEvents()
Unloaded += InfiniteCanvas_Unloaded;
Application.Current.LeavingBackground += Current_LeavingBackground;
_drawingSurfaceRenderer.CommandExecuted += DrawingSurfaceRenderer_CommandExecuted;
_canvasTextBoxFontSizeTextBox.PreviewKeyDown += CanvasTextBoxFontSizeTextBox_PreviewKeyDown;
_canvasComboBoxFontSizeTextBox.PreviewKeyDown += CanvasComboBoxFontSizeTextBox_PreviewKeyDown;
_canvasComboBoxFontSizeTextBox.TextSubmitted += CanvasComboBoxFontSizeTextBox_TextSubmitted;
Loaded += InfiniteCanvas_Loaded;
}

Expand Down Expand Up @@ -366,7 +368,7 @@ private void ConfigureControls()

SetCanvasWidthHeight();

_canvasTextBox.UpdateFontSize(TextFontSize);
SetFontSize(_textFontSize);
}

private void SetZoomFactor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,32 @@
</ToggleButton.Content>
</ToggleButton>

<TextBox x:Name="CanvasTextBoxFontSizeTextBox"
Width="64"
Height="32"
InputScope="Number"
MaxLength="3"
Text="22"
ToolTipService.ToolTip="Font Size" />
<ComboBox x:Name="CanvasComboBoxFontSizeTextBox"
MinWidth="64"
Height="32"
SelectedIndex="9"
Margin="0,0,12,0"
VerticalAlignment="Center"
ToolTipService.ToolTip="Font Size"
IsEditable="True"
>
<ComboBoxItem Content="8" />
<ComboBoxItem Content="9" />
<ComboBoxItem Content="10" />
<ComboBoxItem Content="11" />
<ComboBoxItem Content="12" />
<ComboBoxItem Content="14" />
<ComboBoxItem Content="16" />
<ComboBoxItem Content="18" />
<ComboBoxItem Content="20" />
<ComboBoxItem Content="22" />
<ComboBoxItem Content="24" />
<ComboBoxItem Content="26" />
<ComboBoxItem Content="28" />
<ComboBoxItem Content="36" />
<ComboBoxItem Content="48" />
<ComboBoxItem Content="72" />
</ComboBox>

</StackPanel>
</StackPanel>
Expand Down