Skip to content

Commit 9878a50

Browse files
committed
Only update font size on select font
or on enter for text input
1 parent 5b2434f commit 9878a50

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Media/InfiniteCanvas/InfiniteCanvas.TextBox.cs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,20 @@ public partial class InfiniteCanvas
2828
"Right",
2929
"Up",
3030
"Left",
31-
"Down"
31+
"Down",
32+
"Enter"
3233
};
3334

3435
private Point _lastInputPoint;
3536

3637
private TextDrawable SelectedTextDrawable => _drawingSurfaceRenderer.GetSelectedTextDrawable();
3738

38-
private float _lastValidTextFontSizeValue = DefaultFontValue;
39+
private float _textFontSize = DefaultFontValue;
3940

40-
private float TextFontSize
41+
private void SetFontSize(float newSize)
4142
{
42-
get
43-
{
44-
if (!string.IsNullOrWhiteSpace(_canvasComboBoxFontSizeTextBox.SelectedValue.ToString()) &&
45-
Regex.IsMatch(_canvasComboBoxFontSizeTextBox.Text, "^[0-9]*$"))
46-
{
47-
var fontSize = float.Parse((_canvasComboBoxFontSizeTextBox.SelectedItem as ComboBoxItem).Content.ToString());
48-
_lastValidTextFontSizeValue = fontSize;
49-
}
50-
51-
return _lastValidTextFontSizeValue;
52-
}
43+
_textFontSize = newSize;
44+
_canvasTextBox.UpdateFontSize(newSize);
5345
}
5446

5547
private void InkScrollViewer_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
@@ -95,11 +87,26 @@ private void CanvasTextBoxItalicButton_Clicked(object sender, RoutedEventArgs e)
9587

9688
private void CanvasComboBoxFontSizeTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
9789
{
98-
_canvasTextBox.UpdateFontSize(TextFontSize);
99-
if (SelectedTextDrawable != null)
90+
if (sender is ComboBox s
91+
&& s.SelectedItem is ComboBoxItem selectedItem
92+
&& selectedItem.Content is string selectedText
93+
&& float.TryParse(selectedText, out var sizeNumb))
10094
{
101-
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(TextFontSize);
102-
ReDrawCanvas();
95+
SetFontSize(sizeNumb);
96+
97+
if (SelectedTextDrawable != null)
98+
{
99+
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(sizeNumb);
100+
ReDrawCanvas();
101+
}
102+
}
103+
}
104+
105+
private void CanvasComboBoxFontSizeTextBox_TextSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs args)
106+
{
107+
if (float.TryParse(args.Text, out var size))
108+
{
109+
SetFontSize(size);
103110
}
104111
}
105112

@@ -147,20 +154,22 @@ private void CanvasTextBox_TextChanged(object sender, string text)
147154
ReDrawCanvas();
148155
return;
149156
}
157+
else
158+
{
159+
_drawingSurfaceRenderer.ExecuteCreateTextBox(
160+
_lastInputPoint.X,
161+
_lastInputPoint.Y,
162+
_canvasTextBox.GetEditZoneWidth(),
163+
_canvasTextBox.GetEditZoneHeight(),
164+
_textFontSize,
165+
text,
166+
_canvasTextBoxColorPicker.Color,
167+
_canvasTextBoxBoldButton.IsChecked ?? false,
168+
_canvasTextBoxItalicButton.IsChecked ?? false);
150169

151-
_drawingSurfaceRenderer.ExecuteCreateTextBox(
152-
_lastInputPoint.X,
153-
_lastInputPoint.Y,
154-
_canvasTextBox.GetEditZoneWidth(),
155-
_canvasTextBox.GetEditZoneHeight(),
156-
TextFontSize,
157-
text,
158-
_canvasTextBoxColorPicker.Color,
159-
_canvasTextBoxBoldButton.IsChecked ?? false,
160-
_canvasTextBoxItalicButton.IsChecked ?? false);
161-
162-
ReDrawCanvas();
163-
_drawingSurfaceRenderer.UpdateSelectedTextDrawable();
170+
ReDrawCanvas();
171+
_drawingSurfaceRenderer.UpdateSelectedTextDrawable();
172+
}
164173
}
165174

166175
private void InkScrollViewer_PointerPressed(object sender, PointerRoutedEventArgs e)
@@ -179,7 +188,6 @@ private void InkScrollViewer_PointerPressed(object sender, PointerRoutedEventArg
179188

180189
Canvas.SetLeft(_canvasTextBox, SelectedTextDrawable.Bounds.X);
181190
Canvas.SetTop(_canvasTextBox, SelectedTextDrawable.Bounds.Y);
182-
_canvasTextBox.UpdateFontSize(SelectedTextDrawable.FontSize);
183191
_canvasTextBox.UpdateFontStyle(SelectedTextDrawable.IsItalic);
184192
_canvasTextBox.UpdateFontWeight(SelectedTextDrawable.IsBold);
185193

@@ -192,7 +200,6 @@ private void InkScrollViewer_PointerPressed(object sender, PointerRoutedEventArg
192200
return;
193201
}
194202

195-
_canvasTextBox.UpdateFontSize(TextFontSize);
196203
_canvasTextBox.UpdateFontStyle(_canvasTextBoxItalicButton.IsChecked ?? false);
197204
_canvasTextBox.UpdateFontWeight(_canvasTextBoxBoldButton.IsChecked ?? false);
198205

Microsoft.Toolkit.Uwp.UI.Controls.Media/InfiniteCanvas/InfiniteCanvas.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ private void UnRegisterEvents()
315315
Application.Current.LeavingBackground -= Current_LeavingBackground;
316316
_drawingSurfaceRenderer.CommandExecuted -= DrawingSurfaceRenderer_CommandExecuted;
317317
_canvasComboBoxFontSizeTextBox.PreviewKeyDown -= CanvasComboBoxFontSizeTextBox_PreviewKeyDown;
318+
_canvasComboBoxFontSizeTextBox.TextSubmitted -= CanvasComboBoxFontSizeTextBox_TextSubmitted;
318319
Loaded -= InfiniteCanvas_Loaded;
319320
}
320321

@@ -339,6 +340,7 @@ private void RegisterEvents()
339340
Application.Current.LeavingBackground += Current_LeavingBackground;
340341
_drawingSurfaceRenderer.CommandExecuted += DrawingSurfaceRenderer_CommandExecuted;
341342
_canvasComboBoxFontSizeTextBox.PreviewKeyDown += CanvasComboBoxFontSizeTextBox_PreviewKeyDown;
343+
_canvasComboBoxFontSizeTextBox.TextSubmitted += CanvasComboBoxFontSizeTextBox_TextSubmitted;
342344
Loaded += InfiniteCanvas_Loaded;
343345
}
344346

@@ -366,7 +368,7 @@ private void ConfigureControls()
366368

367369
SetCanvasWidthHeight();
368370

369-
_canvasTextBox.UpdateFontSize(TextFontSize);
371+
SetFontSize(_textFontSize);
370372
}
371373

372374
private void SetZoomFactor()

0 commit comments

Comments
 (0)