Skip to content

Commit

Permalink
Hand/grab cursors
Browse files Browse the repository at this point in the history
Add checkmark next to selected thumbnail sizes
  • Loading branch information
RupertAvery committed Apr 25, 2023
1 parent c5274cd commit 2a1b4f0
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 101 deletions.
5 changes: 5 additions & 0 deletions Diffusion.Toolkit/Controls/PreviewPane.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@
<RowDefinition Height="*" />
<RowDefinition Height="32"/>
</Grid.RowDefinitions>

<ScrollViewer x:Name="ScrollViewer" Grid.Row="0" Grid.RowSpan="3"
HorizontalScrollBarVisibility="{Binding MainModel.FitToPreview, Converter={StaticResource scrollBarVisibility}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
VerticalScrollBarVisibility="{Binding MainModel.FitToPreview, Converter={StaticResource scrollBarVisibility}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
MouseMove="ScrollViewer_OnMouseMove"
MouseLeave="ScrollViewer_OnMouseLeave"
MouseDown="ScrollViewer_OnMouseDown"
MouseUp="ScrollViewer_OnMouseUp"
PreviewKeyDown="ScrollViewer_OnPreviewKeyDown"
PreviewMouseWheel="UIElement_OnMouseWheel"
PreviewKeyUp="ScrollViewer_OnPreviewKeyUp"
Expand Down
72 changes: 59 additions & 13 deletions Diffusion.Toolkit/Controls/PreviewPane.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Diffusion.Toolkit.Models;
using Point = System.Windows.Point;

namespace Diffusion.Toolkit.Controls
{
Expand Down Expand Up @@ -62,11 +65,30 @@ public ImageViewModel Image
public PreviewPane()
{
InitializeComponent();
InitIcons();
_scrollDragger = new ScrollDragger(Preview, ScrollViewer, grabCursor);
}

private Cursor handCursor;
private Cursor grabCursor;

_scrollDragger = new ScrollDragger(Preview, ScrollViewer);
private void InitIcons()
{
Uri handIconUri = new Uri("pack://application:,,,/Icons/hand.cur", UriKind.RelativeOrAbsolute);
handCursor = new Cursor(Application.GetResourceStream(handIconUri).Stream);
Uri grabIconUri = new Uri("pack://application:,,,/Icons/grab.cur", UriKind.RelativeOrAbsolute);
grabCursor = new Cursor(Application.GetResourceStream(grabIconUri).Stream);
//Unloaded += OnUnloaded;
}


//private void OnUnloaded(object sender, RoutedEventArgs e)
//{
// handCursor.Dispose();
// grabCursor.Dispose();
//}


public void ResetZoom()
{
Preview.LayoutTransform = new ScaleTransform(1, 1);
Expand All @@ -85,7 +107,7 @@ private void UIElement_OnMouseWheel(object sender, MouseWheelEventArgs e)

// Calculate the new zoom level based on the mouse wheel delta
double zoomDelta = e.Delta > 0 ? 0.1 : -0.1;

zoomDelta = Preview.LayoutTransform.Value.M11 * zoomDelta;

double newZoom = Math.Min(Math.Max(Preview.LayoutTransform.Value.M11 + zoomDelta, 0.1), 10);
Expand All @@ -106,22 +128,22 @@ private void UIElement_OnMouseWheel(object sender, MouseWheelEventArgs e)
}
else
{
Key vkey = e.Delta > 0 ? Key.Left : e.Delta < 0 ? Key.Right : Key.None;
//Key vkey = e.Delta > 0 ? Key.Left : e.Delta < 0 ? Key.Right : Key.None;

var ps = PresentationSource.FromVisual((ScrollViewer)sender);
//var ps = PresentationSource.FromVisual((ScrollViewer)sender);

if (vkey == Key.None)
{
OnPreviewKeyUp(new KeyEventArgs(null, ps, 0, vkey));
}
else
{
OnPreviewKeyDown(new KeyEventArgs(null, ps, 0, vkey));
}
//if (vkey == Key.None)
//{
// OnPreviewKeyUp(new KeyEventArgs(null, ps, 0, vkey));
//}
//else
//{
// OnPreviewKeyDown(new KeyEventArgs(null, ps, 0, vkey));
//}

}
}

public void ZoomPreview(double zoomDelta)
{
var mouseAtScrollViewer = new Point(ScrollViewer.ViewportWidth / 2, ScrollViewer.ViewportHeight / 2);
Expand Down Expand Up @@ -271,5 +293,29 @@ private void ScrollViewer_OnPreviewKeyUp(object sender, KeyEventArgs e)
{
OnPreviewKeyUp(e);
}

private void ScrollViewer_OnMouseMove(object sender, MouseEventArgs e)
{
Window window = Window.GetWindow(this);
window.Cursor = handCursor;
}

private void ScrollViewer_OnMouseLeave(object sender, MouseEventArgs e)
{
Window window = Window.GetWindow(this);
window.Cursor = Cursors.Arrow;
}

private void ScrollViewer_OnMouseDown(object sender, MouseButtonEventArgs e)
{
Window window = Window.GetWindow(this);
window.Cursor = grabCursor;
}

private void ScrollViewer_OnMouseUp(object sender, MouseButtonEventArgs e)
{
Window window = Window.GetWindow(this);
window.Cursor = handCursor;
}
}
}
5 changes: 3 additions & 2 deletions Diffusion.Toolkit/Controls/ScrollDragger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ public class ScrollDragger
{
private readonly ScrollViewer _scrollViewer;
private readonly UIElement _content;
private readonly Cursor _dragCursor = Cursors.Hand;
private readonly Cursor _dragCursor;
private double _scrollMouseX;
private double _scrollMouseY;
private int _updateCounter = 0;

public ScrollDragger(UIElement content, ScrollViewer scrollViewer)
public ScrollDragger(UIElement content, ScrollViewer scrollViewer, Cursor dragCursor)
{
_scrollViewer = scrollViewer;
_dragCursor = dragCursor;
_content = content;

content.MouseLeftButtonDown += scrollViewer_MouseLeftButtonDown;
Expand Down
18 changes: 18 additions & 0 deletions Diffusion.Toolkit/Converters/ThumbnailSizeCheckedConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Globalization;
using System.Windows.Data;

namespace Diffusion.Toolkit.Converters;

public class ThumbnailSizeCheckedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value == int.Parse((string)parameter);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
6 changes: 6 additions & 0 deletions Diffusion.Toolkit/Diffusion.Toolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<None Remove="Icons\Dark\trash-32.png" />
<None Remove="Icons\favorite-32.png" />
<None Remove="Icons\filter-32.png" />
<None Remove="Icons\grab.cur" />
<None Remove="Icons\grab.ico" />
<None Remove="Icons\hand.cur" />
<None Remove="Icons\hand.ico" />
<None Remove="Icons\icons8-blue-heart-48.png" />
<None Remove="Icons\Light\copy-32.png" />
<None Remove="Icons\Light\favorite-32.png" />
Expand Down Expand Up @@ -89,6 +93,8 @@
<Resource Include="Icons\Dark\search-32.png" />
<Resource Include="Icons\Dark\settings-32.png" />
<Resource Include="Icons\Dark\trash-32.png" />
<Resource Include="Icons\grab.cur" />
<Resource Include="Icons\hand.cur" />
<Resource Include="Icons\icons8-blue-heart-48.png" />
<Resource Include="Icons\Light\copy-32.png" />
<Resource Include="Icons\Light\favorite-32.png" />
Expand Down
Binary file added Diffusion.Toolkit/Icons/grab.cur
Binary file not shown.
Binary file added Diffusion.Toolkit/Icons/hand.cur
Binary file not shown.
12 changes: 7 additions & 5 deletions Diffusion.Toolkit/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<Window.Resources>
<ResourceDictionary>
<converters:BoolToVisibilityCollapsedConverter x:Key="boolToVisCol"></converters:BoolToVisibilityCollapsedConverter>
<converters:ThumbnailSizeCheckedConverter x:Key="thumbSizeChecked"></converters:ThumbnailSizeCheckedConverter>

</ResourceDictionary>
</Window.Resources>

Expand Down Expand Up @@ -62,11 +64,11 @@
<MenuItem Header="Refresh" Command="{Binding Refresh}" InputGestureText="F5"/>
<Separator/>
<MenuItem Header="Thumbnails">
<MenuItem Header="128x128" Command="{Binding SetThumbnailSize}" CommandParameter="128"/>
<MenuItem Header="192x192" Command="{Binding SetThumbnailSize}" CommandParameter="192"/>
<MenuItem Header="256x256" Command="{Binding SetThumbnailSize}" CommandParameter="256"/>
<MenuItem Header="384x384" Command="{Binding SetThumbnailSize}" CommandParameter="384"/>
<MenuItem Header="512x512" Command="{Binding SetThumbnailSize}" CommandParameter="512"/>
<MenuItem Header="128x128" IsChecked="{Binding ThumbnailSize, Converter={StaticResource thumbSizeChecked}, ConverterParameter=128}" Command="{Binding SetThumbnailSize}" CommandParameter="128"/>
<MenuItem Header="192x192" IsChecked="{Binding ThumbnailSize, Converter={StaticResource thumbSizeChecked}, ConverterParameter=192}" Command="{Binding SetThumbnailSize}" CommandParameter="192"/>
<MenuItem Header="256x256" IsChecked="{Binding ThumbnailSize, Converter={StaticResource thumbSizeChecked}, ConverterParameter=256}" Command="{Binding SetThumbnailSize}" CommandParameter="256"/>
<MenuItem Header="384x384" IsChecked="{Binding ThumbnailSize, Converter={StaticResource thumbSizeChecked}, ConverterParameter=384}" Command="{Binding SetThumbnailSize}" CommandParameter="384"/>
<MenuItem Header="512x512" IsChecked="{Binding ThumbnailSize, Converter={StaticResource thumbSizeChecked}, ConverterParameter=512}" Command="{Binding SetThumbnailSize}" CommandParameter="512"/>
</MenuItem>
<Separator/>
<MenuItem Header="Show/Hide _Info" Command="{Binding ToggleInfo}" InputGestureText="I"/>
Expand Down
4 changes: 3 additions & 1 deletion Diffusion.Toolkit/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ private void SetThumbnailSize(int size)
{
_settings.ThumbnailSize = size;
ThumbnailLoader.Instance.Size = _settings.ThumbnailSize;
_model.ThumbnailSize = _settings.ThumbnailSize;
_search.SetThumbnailSize(_settings.ThumbnailSize);

}

private void SystemEventsOnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
Expand Down Expand Up @@ -389,6 +389,8 @@ private async void OnLoaded(object sender, RoutedEventArgs e)


ThumbnailLoader.Instance.Size = _settings.ThumbnailSize;

_model.ThumbnailSize = _settings.ThumbnailSize;
_search.SetThumbnailSize(_settings.ThumbnailSize);

_search.OnPopout = () => PopoutPreview();
Expand Down
7 changes: 7 additions & 0 deletions Diffusion.Toolkit/Models/MainModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class MainModel : BaseNotify
private ICommand _toggleAlbum;
private ICommand _refresh;
private ICommand _quickCopy;
private int _thumbnailSize;

public MainModel()
{
Expand Down Expand Up @@ -323,4 +324,10 @@ public ICommand QuickCopy
get => _quickCopy;
set => SetField(ref _quickCopy, value);
}

public int ThumbnailSize
{
get => _thumbnailSize;
set => SetField(ref _thumbnailSize, value);
}
}
8 changes: 8 additions & 0 deletions Diffusion.Toolkit/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
<TextBox Height="26" VerticalContentAlignment="Center" Text="{Binding ModelRootPath, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"></TextBox>
<Button Height="26" Grid.Column="1" Click="BrowseModelPath_OnClick">Browse</Button>
</Grid>
<TextBlock Margin="10" TextWrapping="Wrap">
Set the Model Root to the path of the AUTOMATIC111 models\Stable-diffusion folder to list
all your models in the Model Tab
</TextBlock>
<Label>A1111 Cache (cache.json)</Label>
<Grid>
<Grid.ColumnDefinitions>
Expand All @@ -124,6 +128,10 @@
<TextBox Height="26" VerticalContentAlignment="Center" Text="{Binding HashCache, UpdateSourceTrigger=PropertyChanged}" Margin="0,0,10,0"></TextBox>
<Button Height="26" Grid.Column="1" Click="BrowseHashCache_OnClick">Browse</Button>
</Grid>
<TextBlock Margin="10" TextWrapping="Wrap">
Set the A1111 Cache to the path of the AUTOMATIC111 cache.json file to allow model name searches
if your metadata contains only the hash. SHA256 model hashes are also read from this file.
</TextBlock>
</StackPanel>
</TabItem>
<TabItem Header="Images">
Expand Down
Loading

0 comments on commit 2a1b4f0

Please sign in to comment.