Add NumericUpDown control for UI

This commit is contained in:
Peter Kirmeier 2022-10-28 23:21:27 +02:00
parent 41a8b90477
commit d44731d079
4 changed files with 186 additions and 102 deletions

View file

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2022-2022 Peter Kirmeier -->
<UserControl x:Class="SystemTrayMenu.UserInterface.NumericUpDown"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SystemTrayMenu.UserInterface"
mc:Ignorable="d" d:Height="20">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" d:MinWidth="100" />
<ColumnDefinition Width="18" />
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" x:Name="txtbox" x:FieldModifier="private" VerticalContentAlignment="Center"
PreviewTextInput="Txtbox_PreviewTextInput" DataObject.Pasting="Txtbox_Pasting" PreviewMouseWheel="Txtbox_PreviewMouseWheel" TextChanged="Txtbox_TextChanged"/>
<Button Grid.Row="0" Grid.Column="1" Padding="0" Click="ButtonUp_Click">
<Polygon Points="2,4 6,1 10,4" Stroke="Black" Fill="Black" />
</Button>
<Button Grid.Row="1" Grid.Column="1" Padding="0" Click="ButtonDown_Click">
<Polygon Points="2,1 10,1 6,4" Stroke="Black" Fill="Black" />
</Button>
</Grid>
</UserControl>

View file

@ -0,0 +1,114 @@
// <copyright file="NumericUpDown.xaml.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
#nullable enable
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
/// <summary>
/// Logic of NumericUpDown .
/// </summary>
public partial class NumericUpDown : UserControl
{
// TODO: Not catched yet when something like "0--8" is entered
private static readonly Regex RegexNonNumeric = new Regex("[^0-9-]+");
private string lastTextOK;
private bool WithinChanged;
public NumericUpDown()
{
InitializeComponent();
Value = 0;
}
public int Value
{
get => int.Parse(txtbox.Text);
set
{
txtbox.Text = value.ToString();
}
}
public int Minimum { get; set; } = int.MinValue;
public int Maximum { get; set; } = int.MaxValue;
public int Increment { get; set; } = 1;
private static bool IsTextAllowed(string text) => !RegexNonNumeric.IsMatch(text);
private void Txtbox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text);
}
private void Txtbox_Pasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(string)))
{
if (!IsTextAllowed((string)e.DataObject.GetData(typeof(string))))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
private void Txtbox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (e.Delta > 0)
{
Value = Math.Min(Value + Increment, Maximum);
}
else
{
Value = Math.Max(Value - Increment, Minimum);
}
e.Handled = true;
}
private void Txtbox_TextChanged(object sender, TextChangedEventArgs e)
{
if (int.TryParse(txtbox.Text, out int result))
{
if (result < Minimum)
{
lastTextOK = Minimum.ToString();
}
else if (Maximum < result)
{
lastTextOK = Maximum.ToString();
}
else
{
lastTextOK = txtbox.Text;
return;
}
}
if (!WithinChanged)
{
WithinChanged = true;
txtbox.Text = lastTextOK;
WithinChanged = false;
}
}
private void ButtonUp_Click(object sender, RoutedEventArgs e) => Value = Math.Min(Value + Increment, Maximum);
private void ButtonDown_Click(object sender, RoutedEventArgs e) => Value = Math.Max(Value - Increment, Minimum);
}
}

View file

@ -74,44 +74,36 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!-- TODO WPF NumericUpDown.. -->
<TextBox Grid.Row="0" Grid.Column="0" x:Name="numericUpDownSizeInPercent" VerticalContentAlignment="Center" />
<Label Grid.Row="0" Grid.Column="1" Content="UpDown" Background="Red" Padding="0" VerticalAlignment="Center"/>
<Label Grid.Row="0" Grid.Column="2" Content="{u:Translate 'Application size'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="1" Grid.Column="0" x:Name="numericUpDownIconSizeInPercent" VerticalContentAlignment="Center" />
<Label Grid.Row="1" Grid.Column="1" Content="UpDown" Background="Red" Padding="0" VerticalAlignment="Center"/>
<Label Grid.Row="1" Grid.Column="2" Content="{u:Translate 'Icon size'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="2" Grid.Column="0" x:Name="numericUpDownRowHeighteInPercentage" VerticalContentAlignment="Center" />
<Label Grid.Row="2" Grid.Column="1" Content="UpDown" Background="Red" Padding="0" VerticalAlignment="Center"/>
<Label Grid.Row="2" Grid.Column="2" Content="{u:Translate 'Row height'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="3" Grid.Column="0" x:Name="numericUpDownMenuWidth" VerticalContentAlignment="Center" />
<Label Grid.Row="3" Grid.Column="1" Content="UpDown" Background="Red" Padding="0" VerticalAlignment="Center"/>
<Label Grid.Row="3" Grid.Column="2" Content="{u:Translate 'Maximum menu width'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
<TextBox Grid.Row="4" Grid.Column="0" x:Name="numericUpDownMenuHeight" VerticalContentAlignment="Center" />
<Label Grid.Row="4" Grid.Column="1" Content="UpDown" Background="Red" Padding="0" VerticalAlignment="Center"/>
<Label Grid.Row="4" Grid.Column="2" Content="{u:Translate 'Maximum menu height'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
<local:NumericUpDown Grid.Row="0" Grid.Column="0" x:Name="numericUpDownSizeInPercent" Minimum="100" Maximum="200" Increment="5" />
<Label Grid.Row="0" Grid.Column="1" Content="{u:Translate 'Application size'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
<local:NumericUpDown Grid.Row="1" Grid.Column="0" x:Name="numericUpDownIconSizeInPercent" Minimum="100" Maximum="200" Increment="5" />
<Label Grid.Row="1" Grid.Column="1" Content="{u:Translate 'Icon size'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
<local:NumericUpDown Grid.Row="2" Grid.Column="0" x:Name="numericUpDownRowHeightInPercentage" Minimum="50" Maximum="200" Increment="5" />
<Label Grid.Row="2" Grid.Column="1" Content="{u:Translate 'Row height'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
<local:NumericUpDown Grid.Row="3" Grid.Column="0" x:Name="numericUpDownMenuWidth" Minimum="25" Maximum="400" Increment="5" />
<Label Grid.Row="3" Grid.Column="1" Content="{u:Translate 'Maximum menu width'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
<local:NumericUpDown Grid.Row="4" Grid.Column="0" x:Name="numericUpDownMenuHeight" Minimum="25" Maximum="400" Increment="5" />
<Label Grid.Row="4" Grid.Column="1" Content="{u:Translate 'Maximum menu height'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
</Grid>
</GroupBox>
<GroupBox Header="{u:Translate 'Main menu appears'}">
<StackPanel>
<RadioButton x:Name="radioButtonAppearAtTheBottomRight" GroupName="MainMenuAppearsAt" Content="{u:Translate 'Bottom right'}" />
<RadioButton x:Name="radioButtonAppearAtTheBottomLeft" GroupName="MainMenuAppearsAt" Content="{u:Translate 'Bottom left'}" />
<RadioButton x:Name="radioButtonUseCustomLocation" GroupName="MainMenuAppearsAt" Content="{u:Translate 'Custom (drag it to the appropriate position)'}" />
<RadioButton x:Name="radioButtonAppearAtMouseLocation" GroupName="MainMenuAppearsAt" Content="{u:Translate 'At mouse location'}" />
<RadioButton x:Name="radioButtonAppearAtTheBottomRight" GroupName="MainMenuAppearsAt" Content="{u:Translate 'Bottom right'}" VerticalContentAlignment="Center" />
<RadioButton x:Name="radioButtonAppearAtTheBottomLeft" GroupName="MainMenuAppearsAt" Content="{u:Translate 'Bottom left'}" VerticalContentAlignment="Center" />
<RadioButton x:Name="radioButtonUseCustomLocation" GroupName="MainMenuAppearsAt" Content="{u:Translate 'Custom (drag it to the appropriate position)'}" VerticalContentAlignment="Center" />
<RadioButton x:Name="radioButtonAppearAtMouseLocation" GroupName="MainMenuAppearsAt" Content="{u:Translate 'At mouse location'}" VerticalContentAlignment="Center" />
</StackPanel>
</GroupBox>
<GroupBox Header="{u:Translate 'Sub menu appears'}">
<StackPanel>
<RadioButton x:Name="radioButtonNextToPreviousMenu" GroupName="SubMenuAppearsAt" Content="{u:Translate 'Next to the previous one'}" />
<RadioButton x:Name="radioButtonNextToPreviousMenu" GroupName="SubMenuAppearsAt" Content="{u:Translate 'Next to the previous one'}" VerticalContentAlignment="Center" />
<StackPanel Orientation="Horizontal">
<RadioButton x:Name="radioButtonOverlapping" GroupName="SubMenuAppearsAt" Content="{u:Translate 'Overlapping'}" Checked="RadioButtonOverlapping_Checked" Unchecked="RadioButtonOverlapping_Unchecked" />
<!-- TODO WPF NumericUpDown -->
<TextBox x:Name="numericUpDownOverlappingOffsetPixels" Width="50" VerticalContentAlignment="Center" Margin="15,0,0,0" />
<Label Content="UpDown" Background="Red" Padding="0" VerticalAlignment="Center"/>
<RadioButton x:Name="radioButtonOverlapping" GroupName="SubMenuAppearsAt" Content="{u:Translate 'Overlapping'}" Checked="RadioButtonOverlapping_Checked" Unchecked="RadioButtonOverlapping_Unchecked" VerticalContentAlignment="Center" />
<local:NumericUpDown x:Name="numericUpDownOverlappingOffsetPixels" Width="70" VerticalContentAlignment="Center" Margin="10,0,0,0" />
<Label Content="{u:Translate 'Offset by pixels'}" Padding="5,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</StackPanel>

View file

@ -333,72 +333,19 @@ namespace SystemTrayMenu.UserInterface
comboBoxLanguage.SelectedValue ??= "en";
}
#if TODO
numericUpDownSizeInPercent.Minimum = 100;
numericUpDownSizeInPercent.Maximum = 200;
numericUpDownSizeInPercent.Increment = 5;
numericUpDownSizeInPercent.MouseWheel += NumericUpDown_MouseWheel;
void NumericUpDown_MouseWheel(object sender, MouseEventArgs e)
{
NumericUpDown numericUpDown = (NumericUpDown)sender;
decimal newValue = numericUpDown.Value;
if (e.Delta > 0)
{
newValue += numericUpDown.Increment;
if (newValue > numericUpDown.Maximum)
{
newValue = (int)numericUpDown.Maximum;
}
}
else
{
newValue -= numericUpDown.Increment;
if (newValue < numericUpDown.Minimum)
{
newValue = (int)numericUpDown.Minimum;
}
}
numericUpDown.Value = newValue;
((HandledMouseEventArgs)e).Handled = true;
}
#endif
numericUpDownSizeInPercent.Text = Settings.Default.SizeInPercent.ToString();
#if TODO
numericUpDownIconSizeInPercent.Minimum = 50;
numericUpDownIconSizeInPercent.Maximum = 200;
numericUpDownIconSizeInPercent.Increment = 5;
numericUpDownIconSizeInPercent.MouseWheel += NumericUpDown_MouseWheel;
#endif
numericUpDownIconSizeInPercent.Text = Settings.Default.IconSizeInPercent.ToString();
#if TODO
numericUpDownRowHeighteInPercentage.Minimum = 50;
numericUpDownRowHeighteInPercentage.Maximum = 200;
numericUpDownRowHeighteInPercentage.Increment = 5;
numericUpDownRowHeighteInPercentage.MouseWheel += NumericUpDown_MouseWheel;
#endif
numericUpDownSizeInPercent.Value = Settings.Default.SizeInPercent;
numericUpDownIconSizeInPercent.Value = Settings.Default.IconSizeInPercent;
if (DllImports.NativeMethods.IsTouchEnabled())
{
numericUpDownRowHeighteInPercentage.Text = Settings.Default.RowHeighteInPercentageTouch.ToString();
numericUpDownRowHeightInPercentage.Value = Settings.Default.RowHeighteInPercentageTouch;
}
else
{
numericUpDownRowHeighteInPercentage.Text = Settings.Default.RowHeighteInPercentage.ToString();
numericUpDownRowHeightInPercentage.Value = Settings.Default.RowHeighteInPercentage;
}
#if TODO
numericUpDownMenuWidth.Minimum = 25;
numericUpDownMenuWidth.Maximum = 400;
numericUpDownMenuWidth.Increment = 5;
#endif
numericUpDownMenuWidth.Text = Settings.Default.WidthMaxInPercent.ToString();
#if TODO
numericUpDownMenuHeight.Minimum = 25;
numericUpDownMenuHeight.Maximum = 400;
numericUpDownMenuHeight.Increment = 5;
#endif
numericUpDownMenuHeight.Text = Settings.Default.HeightMaxInPercent.ToString();
numericUpDownMenuWidth.Value = Settings.Default.WidthMaxInPercent;
numericUpDownMenuHeight.Value = Settings.Default.HeightMaxInPercent;
if (Settings.Default.UseCustomLocation)
{
@ -417,7 +364,7 @@ namespace SystemTrayMenu.UserInterface
radioButtonAppearAtTheBottomRight.IsChecked = true;
}
numericUpDownOverlappingOffsetPixels.Text = Settings.Default.OverlappingOffsetPixels.ToString();
numericUpDownOverlappingOffsetPixels.Value = Settings.Default.OverlappingOffsetPixels;
if (Settings.Default.AppearNextToPreviousMenu)
{
radioButtonNextToPreviousMenu.IsChecked = true;
@ -873,21 +820,22 @@ namespace SystemTrayMenu.UserInterface
Settings.Default.HotKey = new KeysConverter().ConvertToInvariantString(textBoxHotkey.Hotkey | textBoxHotkey.HotkeyModifiers);
Settings.Default.CurrentCultureInfoName = comboBoxLanguage.SelectedValue.ToString();
Settings.Default.SizeInPercent = (int)numericUpDownSizeInPercent.Value;
Settings.Default.IconSizeInPercent = (int)numericUpDownIconSizeInPercent.Value;
#endif
Settings.Default.SizeInPercent = numericUpDownSizeInPercent.Value;
Settings.Default.IconSizeInPercent = numericUpDownIconSizeInPercent.Value;
if (DllImports.NativeMethods.IsTouchEnabled())
{
Settings.Default.RowHeighteInPercentageTouch = (int)numericUpDownRowHeighteInPercentage.Value;
Settings.Default.RowHeighteInPercentageTouch = numericUpDownRowHeightInPercentage.Value;
}
else
{
Settings.Default.RowHeighteInPercentage = (int)numericUpDownRowHeighteInPercentage.Value;
Settings.Default.RowHeighteInPercentage = numericUpDownRowHeightInPercentage.Value;
}
Settings.Default.WidthMaxInPercent = (int)numericUpDownMenuWidth.Value;
Settings.Default.HeightMaxInPercent = (int)numericUpDownMenuHeight.Value;
Settings.Default.WidthMaxInPercent = numericUpDownMenuWidth.Value;
Settings.Default.HeightMaxInPercent = numericUpDownMenuHeight.Value;
#if TODO
if (radioButtonUseCustomLocation.Checked)
{
Settings.Default.UseCustomLocation = true;
@ -912,8 +860,9 @@ namespace SystemTrayMenu.UserInterface
Settings.Default.AppearAtMouseLocation = false;
Settings.Default.AppearAtTheBottomLeft = false;
}
Settings.Default.OverlappingOffsetPixels = (int)numericUpDownOverlappingOffsetPixels.Value;
#endif
Settings.Default.OverlappingOffsetPixels = numericUpDownOverlappingOffsetPixels.Value;
#if TODO
if (radioButtonNextToPreviousMenu.Checked)
{
Settings.Default.AppearNextToPreviousMenu = true;
@ -1240,16 +1189,16 @@ namespace SystemTrayMenu.UserInterface
#endif
private void ButtonSizeAndLocationDefault_Click(object sender, RoutedEventArgs e)
{
numericUpDownSizeInPercent.Text = 100.ToString();
numericUpDownIconSizeInPercent.Text = 100.ToString();
numericUpDownRowHeighteInPercentage.Text = 100.ToString();
numericUpDownMenuWidth.Text = 100.ToString();
numericUpDownMenuHeight.Text = 100.ToString();
numericUpDownSizeInPercent.Value = 100;
numericUpDownIconSizeInPercent.Value = 100;
numericUpDownRowHeightInPercentage.Value = 100;
numericUpDownMenuWidth.Value = 100;
numericUpDownMenuHeight.Value = 100;
radioButtonAppearAtTheBottomLeft.IsChecked = true;
radioButtonNextToPreviousMenu.IsChecked = true;
numericUpDownOverlappingOffsetPixels.Text = 150.ToString();
numericUpDownOverlappingOffsetPixels.Value = 150;
}
#if TODO