Skip to content

Commit 19103d4

Browse files
committed
Improvement: combobox and slider(support mousewheel control)
1 parent 3871d4f commit 19103d4

File tree

4 files changed

+102
-32
lines changed

4 files changed

+102
-32
lines changed

Avalonia.CpuLimiter/ViewModels/MainWindowViewModel.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public MainWindowViewModel()
3131
// }
3232
// }
3333

34-
35-
ChooseExeFileCommand = ReactiveCommand.CreateFromTask(ChooseExeFile);
34+
// ChooseExeFileCommand = ReactiveCommand.CreateFromTask(ChooseExeFile);
3635
RunGameCommand = ReactiveCommand.Create(RunGame);
3736
OpenAboutWindowCommand = ReactiveCommand.CreateFromTask(OpenAboutWindowAsync);
3837
OpenProjWebsiteCommand = ReactiveCommand.CreateFromTask(OpenProjWebsiteAsync);
@@ -71,14 +70,11 @@ public MainWindowViewModel()
7170
}));
7271

7372
}
74-
75-
7673
}
7774

7875
public void RunGame() => AdminRunner.RunAsAdmin(4, GamePath);
7976

80-
81-
public ICommand ChooseExeFileCommand { get; }
77+
// public ICommand ChooseExeFileCommand { get; }
8278

8379
public ICommand RunGameCommand { get; }
8480

@@ -102,8 +98,7 @@ public string GamePath
10298
}
10399
}
104100

105-
106-
private async Task ChooseExeFile()
101+
public async Task ChooseExeFile()
107102
{
108103
try
109104
{
@@ -114,11 +109,18 @@ private async Task ChooseExeFile()
114109

115110
var file = await fileService.OpenFileAsync();
116111
if (file != null)
117-
GamePath = file.Path.AbsolutePath;
112+
GamePath = file.Path.LocalPath;
113+
114+
HistoryItems.Add(new HistoryItemViewModel()
115+
{
116+
CPUCoreUsed = CpuCoreCount,
117+
LastUsed = new DateTime(),
118+
Path = GamePath
119+
});
118120

119121
// extension judgement
120122
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !GamePath.EndsWith(".exe"))
121-
throw new PlatformNotSupportedException("File extension is not supported on windows");
123+
throw new PlatformNotSupportedException($"File extension: {Path.GetExtension(GamePath)} is not supported on windows");
122124

123125
}
124126
catch (Exception e)
@@ -233,5 +235,24 @@ private async Task RemoveHistoryItemAsync(HistoryItemViewModel historyItem)
233235
HistoryItems.Remove(historyItem);
234236
}
235237

238+
public async Task PrintLevel(object o)
239+
{
240+
Console.WriteLine(o);
241+
}
242+
243+
private int _screenWidth;
244+
public int ScreenWidth
245+
{
246+
get
247+
{
248+
if (string.IsNullOrWhiteSpace(GamePath))
249+
return 400;
250+
else return 3 * GamePath.Length;
251+
}
252+
253+
set => this.RaiseAndSetIfChanged(ref _screenWidth, value);
254+
}
255+
256+
236257
}
237258
}

Avalonia.CpuLimiter/Views/AboutWindow.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Title="About Game Launcher"
1010
TransparencyLevelHint="AcrylicBlur"
1111
Background="Transparent"
12-
ExtendClientAreaToDecorationsHint="False"
12+
ExtendClientAreaToDecorationsHint="True"
1313
WindowStartupLocation="CenterOwner"
1414
CornerRadius="10">
1515

@@ -25,7 +25,7 @@
2525
MaterialOpacity="0.25" />
2626
</ExperimentalAcrylicBorder.Material>
2727
</ExperimentalAcrylicBorder>
28-
<StackPanel HorizontalAlignment="Center">
28+
<StackPanel HorizontalAlignment="Center" Margin="0 20">
2929
<!-- Title -->
3030

3131
<TextBlock Text="Game Launcher 1.0" FontSize="16"

Avalonia.CpuLimiter/Views/MainWindow.axaml

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
xmlns:lang="using:Avalonia.CpuLimiter.Lang"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7-
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="400"
8-
Width="600" Height="400"
7+
mc:Ignorable="d" d:DesignWidth="650" d:DesignHeight="400"
8+
Width= "620" Height="400"
9+
MaxWidth="800"
910
x:Class="Avalonia.CpuLimiter.Views.MainWindow"
1011
x:DataType="vm:MainWindowViewModel"
1112
Icon="/Assets/avalonia-logo.ico"
1213
Title="Game Launcher"
1314
TransparencyLevelHint="AcrylicBlur"
1415
Background="Transparent"
15-
ExtendClientAreaToDecorationsHint="False"
16+
ExtendClientAreaToDecorationsHint="True"
1617
WindowStartupLocation="CenterScreen">
1718

1819
<Design.DataContext>
@@ -35,9 +36,9 @@
3536

3637
<!-- window menu -->
3738
<DockPanel>
38-
<Menu DockPanel.Dock="Top">
39+
<Menu DockPanel.Dock="Top" HorizontalAlignment="Left" FontSize="14">
3940
<MenuItem Name="FileButton" Header="{x:Static lang:Resources.FileButton}">
40-
<MenuItem Header="Open" Command="{Binding ChooseExeFileCommand}" FontSize="13"/>
41+
<MenuItem Header="Open" Command="{Binding ChooseExeFile}" FontSize="13"/>
4142
<MenuItem Header="Save" Command="{Binding}" FontSize="13"/>
4243
<Separator/>
4344
<MenuItem Header="Exit" Click="OnExitButtonClicked" FontSize="13"/>
@@ -79,20 +80,32 @@
7980
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
8081
<!-- SelectedItem="{Binding HistoryItems[0]}" -->
8182
<ComboBox Name="HistoryComboBox" HorizontalAlignment="Center" Padding="15 0 15 5"
82-
FontSize="16" FontWeight="Bold"
83-
PlaceholderText="Input the Program Path"
84-
PlaceholderForeground="Yellow"
83+
FontSize="16"
84+
MinWidth="400"
85+
Height="40"
8586
ItemsSource="{Binding HistoryItems}"
87+
SelectionChanged="OnSelectionChanged"
8688
ToolTip.Tip="Store no more than five records">
8789

8890
<ComboBox.ItemTemplate>
8991
<DataTemplate DataType="vm:HistoryItemViewModel">
90-
<Grid ColumnDefinitions="* Auto">
91-
<TextBlock Grid.Column="0" Text="{Binding Path}"
92+
<Grid ColumnDefinitions="* Auto"
93+
94+
RowDefinitions="30"
95+
Margin="0" MinWidth="40">
96+
<TextBox Grid.Column="0" Text="{Binding Path}"
97+
HorizontalAlignment="Left"
98+
VerticalContentAlignment="Center"
9299
FontSize="16"
93-
Width="300"
94-
Height="20"/>
95-
<Button Grid.Column="1" Padding="10"
100+
Height="20"
101+
Margin="0 5 1 5"
102+
Padding="0 0 0 0"
103+
BorderThickness="0"
104+
Background="Transparent"/>
105+
<Button Grid.Column="1" Padding="1 10 0 10"
106+
Margin="8 0"
107+
Width="38"
108+
Height="38"
96109
Command="{Binding }"
97110
CommandParameter="{Binding .}">
98111
<PathIcon Data="{StaticResource DeleteRegular}" Padding="0 10" IsVisible="True"/>
@@ -117,17 +130,16 @@
117130
Padding="10"
118131
Width="40"
119132
BorderBrush="Black"
120-
ToolTip.Tip="Open the file explorer to select exe file"
121-
Command="{Binding ChooseExeFileCommand}">
133+
ToolTip.Tip="Open the file explorer to select"
134+
Command="{Binding ChooseExeFile}">
122135
</Button>
123136
</StackPanel>
124-
125-
126-
<Separator Margin="0 20"/>
137+
<Separator Margin="0 30"/>
127138
<Slider Name="slider" BorderBrush="Orange"
128139
Minimum="1" Maximum="16" IsSnapToTickEnabled="True" TickFrequency="1" TickPlacement="TopLeft"
129140
Width="400" Value="{Binding CpuCoreCount}"
130-
ToolTip.Tip="Adjust the cpu core numbers used by the Game program">
141+
ToolTip.Tip="Adjust the cpu core numbers used by the Game program"
142+
PointerWheelChanged="OnPointerWheelChanged">
131143
</Slider>
132144

133145
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
@@ -137,8 +149,11 @@
137149
<!-- HorizontalAlignment="Center" FontSize="20" FontWeight="Bold" Padding="0"/> -->
138150

139151
<TextBox Margin="10 0 0 0" HorizontalAlignment="Center"
140-
Padding="10 10 10 10"
152+
VerticalContentAlignment="Center"
153+
Padding="0 0 0 0"
141154
Width="1"
155+
MinWidth="40"
156+
Height="40"
142157
TextAlignment="Center"
143158
Text="{Binding CpuCoreCount}" FontSize="18"
144159
FontWeight="Bold" IsReadOnly="False"></TextBox>
@@ -154,6 +169,11 @@
154169
<!-- <PathIcon Data="{StaticResource GamesRegular}"/> -->
155170
</Button>
156171

172+
<TextBlock Name="Auxiliary" Text="{Binding GamePath}" IsVisible="False"/>
173+
174+
175+
176+
157177

158178

159179
</StackPanel>

Avalonia.CpuLimiter/Views/MainWindow.axaml.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Avalonia.Controls;
44
using Avalonia.CpuLimiter.Models;
55
using Avalonia.CpuLimiter.ViewModels;
6+
using Avalonia.Input;
7+
using Avalonia.Input.Raw;
68
using Avalonia.Interactivity;
79
using Avalonia.ReactiveUI;
810
using MsBox.Avalonia;
@@ -61,5 +63,32 @@ private async void OnOpenAboutWindowClicked(object? sender, RoutedEventArgs e)
6163
var aboutWindow = new AboutWindow();
6264
aboutWindow.Show();
6365
}
66+
67+
68+
// Add a scroll event to change slider value. the unit change is 1
69+
private void OnPointerWheelChanged(object sender, PointerWheelEventArgs e)
70+
{
71+
Console.WriteLine(e.Delta);
72+
if (e.Delta.Y < 0)
73+
slider.Value -= 1;
74+
75+
if(e.Delta.Y > 0)
76+
slider.Value += 1;
77+
}
78+
79+
private async void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
80+
{
81+
var historyItemViewModel = (HistoryItemViewModel)e.AddedItems[0]!;
82+
slider.Value = (double)historyItemViewModel.CPUCoreUsed!;
83+
Auxiliary.Text = historyItemViewModel.Path;
84+
85+
if(Width < historyItemViewModel.Path.Length * 20 )
86+
Width = historyItemViewModel.Path.Length * 20;
87+
88+
Console.WriteLine(historyItemViewModel.CPUCoreUsed);
89+
Console.WriteLine(historyItemViewModel.Path);
90+
Console.WriteLine(historyItemViewModel.LastUsed);
91+
}
92+
6493
}
6594
}

0 commit comments

Comments
 (0)