Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit eb3e381

Browse files
committed
Allow slider adjust by mouse wheel/click
1 parent 5767262 commit eb3e381

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Microsoft.Xaml.Behaviors;
2+
using System.Windows.Controls;
3+
using System.Windows.Input;
4+
5+
namespace OnnxStack.UI.Behaviors
6+
{
7+
public class SliderMouseWheelBehavior : Behavior<Slider>
8+
{
9+
/// <summary>
10+
/// Handles the PreviewMouseWheel event of the AssociatedObject control.
11+
/// </summary>
12+
/// <param name="sender">The source of the event.</param>
13+
/// <param name="e">The <see cref="MouseWheelEventArgs"/> instance containing the event data.</param>
14+
private void AssociatedObject_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
15+
{
16+
var slider = (Slider)sender;
17+
if (e.Delta > 0)
18+
{
19+
slider.Value += slider.TickFrequency;
20+
}
21+
else
22+
{
23+
slider.Value -= slider.TickFrequency;
24+
}
25+
}
26+
27+
28+
/// <summary>
29+
/// Called after the behavior is attached to an AssociatedObject.
30+
/// </summary>
31+
/// <remarks>
32+
/// Override this to hook up functionality to the AssociatedObject.
33+
/// </remarks>
34+
protected override void OnAttached()
35+
{
36+
base.OnAttached();
37+
AssociatedObject.PreviewMouseWheel += AssociatedObject_PreviewMouseWheel;
38+
}
39+
40+
41+
/// <summary>
42+
/// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
43+
/// </summary>
44+
/// <remarks>
45+
/// Override this to unhook functionality from the AssociatedObject.
46+
/// </remarks>
47+
protected override void OnDetaching()
48+
{
49+
base.OnDetaching();
50+
AssociatedObject.PreviewMouseWheel -= AssociatedObject_PreviewMouseWheel;
51+
}
52+
}
53+
}

OnnxStack.UI/OnnxStack.UI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
<ItemGroup>
5050
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.16.1" />
51+
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
5152
<PackageReference Include="OnnxStack.StableDiffusion" Version="0.4.0" Condition=" '$(Configuration)' == 'Release' " />
5253
<ProjectReference Include="..\OnnxStack.StableDiffusion\OnnxStack.StableDiffusion.csproj" />
5354
</ItemGroup>

OnnxStack.UI/UserControls/SchedulerControl.xaml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
67
xmlns:local="clr-namespace:OnnxStack.UI.Views"
8+
xmlns:behaviors="clr-namespace:OnnxStack.UI.Behaviors"
79
xmlns:userControls="clr-namespace:OnnxStack.UI.UserControls"
810
mc:Ignorable="d"
911
d:DesignWidth="500" Name="UI">
@@ -51,14 +53,22 @@
5153
<Label>Inference Steps</Label>
5254
<TextBlock Text="{Binding ElementName=SliderInferenceSteps, Path=Value, StringFormat={}{0}}" VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="10" Margin="0,0,6,0" FontWeight="Medium" />
5355
</DockPanel>
54-
<Slider Name="SliderInferenceSteps" Value="{Binding SchedulerOptions.InferenceSteps}" Minimum="4" Maximum="100" TickFrequency="1" IsSnapToTickEnabled="true"/>
56+
<Slider Name="SliderInferenceSteps" Value="{Binding SchedulerOptions.InferenceSteps}" Minimum="4" Maximum="100" TickFrequency="1" IsSnapToTickEnabled="True" SmallChange="0.1" LargeChange="0.1" >
57+
<i:Interaction.Behaviors>
58+
<behaviors:SliderMouseWheelBehavior />
59+
</i:Interaction.Behaviors>
60+
</Slider>
5561
</StackPanel>
5662
<StackPanel Margin="5,0,0,0">
5763
<DockPanel>
5864
<Label>Guidance Scale</Label>
5965
<TextBlock Text="{Binding ElementName=SliderGuidanceScale, Path=Value, StringFormat={}{0:F2}}" VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="10" Margin="0,0,6,0" FontWeight="Medium" />
6066
</DockPanel>
61-
<Slider Name="SliderGuidanceScale" Value="{Binding SchedulerOptions.GuidanceScale}" Minimum="0" Maximum="30" TickFrequency="0.1" IsSnapToTickEnabled="true"/>
67+
<Slider Name="SliderGuidanceScale" Value="{Binding SchedulerOptions.GuidanceScale}" Minimum="0" Maximum="30" TickFrequency="0.1" IsSnapToTickEnabled="True" SmallChange="0.1" LargeChange="0.1">
68+
<i:Interaction.Behaviors>
69+
<behaviors:SliderMouseWheelBehavior />
70+
</i:Interaction.Behaviors>
71+
</Slider>
6272
</StackPanel>
6373
</UniformGrid>
6474

@@ -78,15 +88,23 @@
7888
<Label>Initial Noise</Label>
7989
<TextBlock Text="{Binding ElementName=SliderInitialNoiseLevel, Path=Value, StringFormat={}{0:F2}}" VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="10" Margin="0,0,6,0" FontWeight="Medium" />
8090
</DockPanel>
81-
<Slider Name="SliderInitialNoiseLevel" Value="{Binding SchedulerOptions.InitialNoiseLevel}" Minimum="-1" Maximum="1" TickFrequency="0.1" IsSnapToTickEnabled="true"/>
91+
<Slider Name="SliderInitialNoiseLevel" Value="{Binding SchedulerOptions.InitialNoiseLevel}" Minimum="-1" Maximum="1" TickFrequency="0.1" IsSnapToTickEnabled="true" SmallChange="0.1" LargeChange="0.1">
92+
<i:Interaction.Behaviors>
93+
<behaviors:SliderMouseWheelBehavior />
94+
</i:Interaction.Behaviors>
95+
</Slider>
8296
</StackPanel>
8397

8498
<StackPanel Margin="5,0,0,0">
8599
<DockPanel>
86100
<Label>Strength</Label>
87101
<TextBlock Text="{Binding ElementName=SliderStrength, Path=Value, StringFormat={}{0:F2}}" VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="10" Margin="0,0,6,0" FontWeight="Medium" />
88102
</DockPanel>
89-
<Slider Name="SliderStrength" Value="{Binding SchedulerOptions.Strength}" Minimum="0" Maximum="1" TickFrequency="0.01" IsSnapToTickEnabled="true"/>
103+
<Slider Name="SliderStrength" Value="{Binding SchedulerOptions.Strength}" Minimum="0" Maximum="1" TickFrequency="0.01" IsSnapToTickEnabled="true" SmallChange="0.01" LargeChange="0.01">
104+
<i:Interaction.Behaviors>
105+
<behaviors:SliderMouseWheelBehavior />
106+
</i:Interaction.Behaviors>
107+
</Slider>
90108
</StackPanel>
91109
</UniformGrid>
92110

0 commit comments

Comments
 (0)