Skip to content

Commit

Permalink
Add a copy button to the image viewer #1399
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Dec 12, 2024
1 parent 9f3014c commit d09e9c4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
45 changes: 27 additions & 18 deletions QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,34 @@
</Border.Resources>
</Border>

<Button x:Name="buttonMeta"
Width="24"
Height="24"
Margin="0,8,40,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Content="&#xE946;"
Style="{StaticResource CaptionButtonStyle}"
Visibility="{Binding ElementName=imagePanel, Path=MetaIconVisibility}" />
<StackPanel Margin="0,8,0,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Orientation="Horizontal">
<Button x:Name="buttonCopy"
Width="24"
Height="24"
Margin="0,0,8,0"
Content="&#xE8C8;"
Style="{StaticResource CaptionButtonStyle}"
Visibility="{Binding ElementName=imagePanel, Path=CopyIconVisibility}" />

<Button x:Name="buttonBackgroundColour"
Width="24"
Height="24"
Margin="0,8,8,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Content="&#xEF1F;"
Style="{StaticResource CaptionButtonStyle}"
Visibility="{Binding ElementName=imagePanel, Path=BackgroundVisibility}" />
<Button x:Name="buttonMeta"
Width="24"
Height="24"
Margin="0,0,8,0"
Content="&#xE946;"
Style="{StaticResource CaptionButtonStyle}"
Visibility="{Binding ElementName=imagePanel, Path=MetaIconVisibility}" />

<Button x:Name="buttonBackgroundColour"
Width="24"
Height="24"
Margin="0,0,8,0"
Content="&#xEF1F;"
Style="{StaticResource CaptionButtonStyle}"
Visibility="{Binding ElementName=imagePanel, Path=BackgroundVisibility}" />
</StackPanel>

<TextBlock x:Name="textMeta"
Margin="0,40,8,0"
Expand Down
37 changes: 36 additions & 1 deletion QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public partial class ImagePanel : UserControl, INotifyPropertyChanged, IDisposab
private bool _isZoomFactorFirstSet = true;
private DateTime _lastZoomTime = DateTime.MinValue;
private double _maxZoomFactor = 3d;
private Visibility _copyIconVisibility = Visibility.Visible;
private MetaProvider _meta;
private Visibility _metaIconVisibility = Visibility.Visible;
private double _minZoomFactor = 0.1d;
Expand All @@ -67,6 +68,8 @@ public ImagePanel()

Resources.MergedDictionaries.Clear();

buttonCopy.Click += OnCopyOnClick;

buttonMeta.Click += (sender, e) =>
textMeta.Visibility = textMeta.Visibility == Visibility.Collapsed
? Visibility.Visible
Expand Down Expand Up @@ -162,6 +165,16 @@ public Visibility MetaIconVisibility
}
}

public Visibility CopyIconVisibility
{
get => _copyIconVisibility;
set
{
_copyIconVisibility = value;
OnPropertyChanged();
}
}

public Visibility BackgroundVisibility
{
get => _backgroundVisibility;
Expand Down Expand Up @@ -274,6 +287,28 @@ public void Dispose()

public event PropertyChangedEventHandler PropertyChanged;

private void OnCopyOnClick(object sender, RoutedEventArgs e)
{
try
{
if (_source is not null)
{
Clipboard.SetImage(_source);
return;
}

if (viewPanelImage.Source is BitmapSource bitmapSource)
{
Clipboard.SetImage(bitmapSource);
return;
}
}
catch
{
///
}
}

private void OnBackgroundColourOnClick(object sender, RoutedEventArgs e)
{
Theme = Theme == Themes.Dark ? Themes.Light : Themes.Dark;
Expand Down Expand Up @@ -499,7 +534,7 @@ private void FireZoomChangedEvent()

Debug.WriteLine($"FireZoomChangedEvent fired: {Thread.CurrentThread.ManagedThreadId}");

Dispatcher.BeginInvoke(new Action(() => ZoomChanged?.Invoke(this, new EventArgs())),
Dispatcher.BeginInvoke(new Action(() => ZoomChanged?.Invoke(this, EventArgs.Empty)),
DispatcherPriority.Background);
});
}
Expand Down

0 comments on commit d09e9c4

Please sign in to comment.