Skip to content

Commit

Permalink
Added taskbar icon image preview
Browse files Browse the repository at this point in the history
  • Loading branch information
CPKreu committed Apr 30, 2021
1 parent 6a37118 commit 13ebc77
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PixiEditor.ViewModels.SubViewModels.UserPreferences.Settings
{
public class GeneralSettings : SettingsGroup
{
private bool imagePreviewInTaskbar = GetPreference(nameof(ImagePreviewInTaskbar), false);

public bool ImagePreviewInTaskbar
{
get => imagePreviewInTaskbar;
set => RaiseAndUpdatePreference(ref imagePreviewInTaskbar, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
{
public class SettingsViewModel : SubViewModel<SettingsWindowViewModel>
{
public GeneralSettings General { get; set; } = new GeneralSettings();

public FileSettings File { get; set; } = new FileSettings();

public UpdateSettings Update { get; set; } = new UpdateSettings();
Expand Down
2 changes: 2 additions & 0 deletions PixiEditor/Views/Dialogs/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
<Grid Visibility="{Binding SelectedCategory, Converter={StaticResource EqualityBoolToVisibilityConverter},
ConverterParameter='General'}">
<StackPanel Orientation="Vertical">
<CheckBox Content="Show Document Preview in Taskbar" Margin="25,30,0,0"
IsChecked="{Binding SettingsSubViewModel.General.ImagePreviewInTaskbar}"/>
<Label Content="File" Style="{StaticResource Header1}"/>
<StackPanel Orientation="Vertical" Margin="50 0 50 0">
<CheckBox Content="Show Startup Window"
Expand Down
50 changes: 49 additions & 1 deletion PixiEditor/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using System.Diagnostics;
using System.Linq;
using PixiEditor.Views.Dialogs;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using PixiEditor.Models.DataHolders;

namespace PixiEditor
{
Expand All @@ -17,16 +20,24 @@ namespace PixiEditor
/// </summary>
public partial class MainWindow : Window
{
private static WriteableBitmap pixiEditorLogo;

private PreferencesSettings preferences;

public new ViewModelMain DataContext { get => (ViewModelMain)base.DataContext; set => base.DataContext = value; }

public MainWindow()
{
InitializeComponent();

preferences = new PreferencesSettings();

IServiceCollection services = new ServiceCollection()
.AddSingleton<IPreferences>(new PreferencesSettings())
.AddSingleton<IPreferences>(preferences)
.AddSingleton(new StylusViewModel());

pixiEditorLogo = BitmapFactory.FromResource(@"/Images/PixiEditorLogo.png");

DataContext = new ViewModelMain(services.BuildServiceProvider());

StateChanged += MainWindowStateChangeRaised;
Expand All @@ -35,6 +46,19 @@ public MainWindow()
MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
DataContext.CloseAction = Close;
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;

DataContext.BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
preferences.AddCallback("ImagePreviewInTaskbar", x =>
{
if ((bool)x)
{
UpdateTaskbarIcon(DataContext.BitmapManager.ActiveDocument);
}
else
{
UpdateTaskbarIcon(null);
}
});
}

protected override void OnClosing(CancelEventArgs e)
Expand All @@ -49,6 +73,30 @@ private static void CloseHelloThereIfRelease()
Application.Current.Windows.OfType<HelloTherePopup>().ToList().ForEach(x => { if (!x.IsClosing) x.Close(); });
}

private void BitmapManager_DocumentChanged(object sender, Models.Events.DocumentChangedEventArgs e)
{
if (preferences.GetPreference("ImagePreviewInTaskbar", false))
{
UpdateTaskbarIcon(e.NewDocument);
}
}

private void UpdateTaskbarIcon(Document document)
{
if (document?.PreviewImage == null)
{
Icon = pixiEditorLogo;
return;
}

var previewCopy = document.PreviewImage.Clone()
.Resize(512, 512, WriteableBitmapExtensions.Interpolation.NearestNeighbor);

previewCopy.Blit(new Rect(256, 256, 256, 256), pixiEditorLogo, new Rect(0, 0, 512, 512));

Icon = previewCopy;
}

private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
Expand Down

0 comments on commit 13ebc77

Please sign in to comment.