Skip to content

Commit

Permalink
feat: Use Velopack for Auto-Updating
Browse files Browse the repository at this point in the history
fix: re-bind the KeyBinding and remove unreachable bindings
  • Loading branch information
realybin committed Oct 13, 2024
1 parent 128b521 commit 61fc0d3
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ A application brings the ability to draw on your screen
- [Material Design in XAML](https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit)
- [ValueConverters.NET](https://github.com/thomasgalliker/ValueConverters.NET)
- [WPF.JoshSmith.Controls.DragCanvas](https://github.com/denxorz/WPF.JoshSmith.Controls.DragCanvas)
- [Velopack](https://github.com/velopack/velopack)
- Moq.AutoMocker
- System.Text.Json
- Microsoft.Extensions.Hosting
Expand Down
22 changes: 22 additions & 0 deletions SketchNow/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CommunityToolkit.Mvvm.Messaging;

using MaterialDesignThemes.Wpf;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -11,6 +13,7 @@
using System.Windows.Threading;

using Velopack;
using Velopack.Sources;

namespace SketchNow;

Expand All @@ -22,10 +25,29 @@ public partial class App : Application
[STAThread]
private static void Main(string[] args)
{
#if !DEBUG
VelopackApp.Build().Run();
UpdateMyApp().GetAwaiter().GetResult();
#endif
MainAsync(args).GetAwaiter().GetResult();
}
#if !DEBUG
private static async Task UpdateMyApp()
{
var mgr = new UpdateManager(new GithubSource("https://github.com/SketchNow/SketchNow.WPF", null, false ,null));

// check for new version
var newVersion = await mgr.CheckForUpdatesAsync();
if (newVersion == null)
return; // no update available

// download new version
await mgr.DownloadUpdatesAsync(newVersion);

// install new version and restart app
mgr.ApplyUpdatesAndRestart(newVersion);
}
#endif
private static async Task MainAsync(string[] args)
{
using IHost host = CreateHostBuilder(args).Build();
Expand Down
46 changes: 38 additions & 8 deletions SketchNow/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,49 @@ public partial class MainWindowViewModel : ObservableObject
{
[ObservableProperty] private CanvasPages _canvasPages = new();
private int _previousPageIndex = 0;
[ObservableProperty] private ObservableCollection<Color> _colorList = [Color.FromRgb(28, 27, 31), Colors.White, Color.FromRgb(255, 26, 0), Color.FromRgb(47, 47, 255), Color.FromRgb(0, 174, 128), Color.FromRgb(157, 118, 241), Color.FromRgb(255, 219, 29), Color.FromRgb(234, 43, 180)];

[ObservableProperty] private ObservableCollection<Color> _colorList =
[
Color.FromRgb(28, 27, 31), Colors.White, Color.FromRgb(255, 26, 0), Color.FromRgb(47, 47, 255),
Color.FromRgb(0, 174, 128), Color.FromRgb(157, 118, 241), Color.FromRgb(255, 219, 29),
Color.FromRgb(234, 43, 180)
];

[ObservableProperty] private Color _selectedColor = Colors.Transparent;
[ObservableProperty] private ObservableCollection<double> _strokeSizeList = [5, 7, 9, 11, 13, 20];
[ObservableProperty] private double _selectedStrokeSize;
[ObservableProperty] private DrawingAttributes _currentDrawingAttributes = new() { Color = Colors.Transparent, IgnorePressure = false, FitToCurve = true, Height = 5, Width = 5 };

[ObservableProperty] private DrawingAttributes _currentDrawingAttributes = new()
{
Color = Colors.Transparent,
IgnorePressure = false,
FitToCurve = true,
Height = 5,
Width = 5
};

[ObservableProperty] private InkCanvasEditingMode _selectedEditingMode = InkCanvasEditingMode.None;

public enum WhiteBoardMode
{
Screen = 0,
MuiltPages = 1
MultiPages = 1
}

[ObservableProperty] private int _selectedToolIndex = 0;
[ObservableProperty] private int _selectedCanvasModeIndex = (int)WhiteBoardMode.Screen;
[ObservableProperty] private bool _isMultiPageMode = false;
[ObservableProperty] private bool _useEraseByStroke = true;
[ObservableProperty] private bool _useFitToCurve = true;
[ObservableProperty] private Brush _inkCanvasBackground = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

partial void OnSelectedStrokeSizeChanged(double value) => CurrentDrawingAttributes.Width = CurrentDrawingAttributes.Height = value;

partial void OnSelectedStrokeSizeChanged(double value) =>
CurrentDrawingAttributes.Width = CurrentDrawingAttributes.Height = value;

partial void OnSelectedColorChanged(Color value) => CurrentDrawingAttributes.Color = value;
partial void OnUseEraseByStrokeChanged(bool value) => OnSelectedToolIndexChanged(SelectedToolIndex);
partial void OnUseFitToCurveChanged(bool value) => CurrentDrawingAttributes.FitToCurve = value;

partial void OnSelectedToolIndexChanged(int value)
{
SelectedEditingMode = value switch
Expand All @@ -48,9 +69,13 @@ partial void OnSelectedToolIndexChanged(int value)
_ => InkCanvasEditingMode.None
};
}

[RelayCommand]
private void ToggleEditMode(int value) => SelectedToolIndex = value;

partial void OnSelectedCanvasModeIndexChanged(int value)
{
IsMultiPageMode = value == (int)WhiteBoardMode.MuiltPages;
IsMultiPageMode = value == (int)WhiteBoardMode.MultiPages;
switch (value)
{
case (int)WhiteBoardMode.Screen:
Expand All @@ -59,7 +84,7 @@ partial void OnSelectedCanvasModeIndexChanged(int value)
_previousPageIndex = CanvasPages.SelectedIndex;
CanvasPages.SelectedIndex = 0;
break;
case (int)WhiteBoardMode.MuiltPages:
case (int)WhiteBoardMode.MultiPages:
InkCanvasBackground = new SolidColorBrush(Colors.White);
if (CanvasPages.SelectedIndex == 0)
if (CanvasPages.Length == 1)
Expand All @@ -69,9 +94,14 @@ partial void OnSelectedCanvasModeIndexChanged(int value)
break;
}
}

[RelayCommand]
private void ToggleMultiPageMode(bool value) => SelectedCanvasModeIndex =
value ? (int)WhiteBoardMode.MultiPages : (int)WhiteBoardMode.Screen;

[RelayCommand]
private static void CloseProgram()
{
Application.Current.Shutdown();
}
}
}
78 changes: 58 additions & 20 deletions SketchNow/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vms="clr-namespace:SketchNow.ViewModels"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
x:Name="window"
Title="SketchNow"
Width="800"
Expand All @@ -26,16 +27,49 @@
<converters:ColorToBrushConverter x:Key="ColorToBrushConverter" />
</Window.Resources>
<Window.InputBindings>
<KeyBinding Key="C" Command="{Binding ToolUseCursorCommand}" />
<KeyBinding Key="I" Command="{Binding ToolUseInkCommand}" />
<KeyBinding Key="E" Command="{Binding ToolUseEraserCommand}" />
<KeyBinding Key="S" Command="{Binding ToolUseSelectCommand}" />
<KeyBinding Key="C"
Command="{Binding ToggleEditModeCommand}"
Modifiers="Shift" >
<KeyBinding.CommandParameter>
<system:Int32>0</system:Int32>
</KeyBinding.CommandParameter>
</KeyBinding>
<KeyBinding Key="I"
Command="{Binding ToggleEditModeCommand}"
Modifiers="Shift" >
<KeyBinding.CommandParameter>
<system:Int32>1</system:Int32>
</KeyBinding.CommandParameter>
</KeyBinding>
<KeyBinding Key="E"
Command="{Binding ToggleEditModeCommand}"
Modifiers="Shift" >
<KeyBinding.CommandParameter>
<system:Int32>2</system:Int32>
</KeyBinding.CommandParameter>
</KeyBinding>
<KeyBinding Key="S"
Command="{Binding ToolUseScreenCommand}"
Modifiers="Alt" />
Command="{Binding ToggleEditModeCommand}"
Modifiers="Shift" >
<KeyBinding.CommandParameter>
<system:Int32>3</system:Int32>
</KeyBinding.CommandParameter>
</KeyBinding>
<KeyBinding Key="S"
Command="{Binding ToggleMultiPageModeCommand}"
Modifiers="Alt">
<KeyBinding.CommandParameter>
<system:Boolean>False</system:Boolean>
</KeyBinding.CommandParameter>
</KeyBinding>
<KeyBinding Key="B"
Command="{Binding ToolUseBoardCommand}"
Modifiers="Alt" />
Command="{Binding ToggleMultiPageModeCommand}"

Modifiers="Alt">
<KeyBinding.CommandParameter>
<system:Boolean>True</system:Boolean>
</KeyBinding.CommandParameter>
</KeyBinding>
<KeyBinding Key="Z"
Command="{Binding CanvasPages.SelectedPage.UndoCommand}"
Modifiers="Ctrl" />
Expand Down Expand Up @@ -193,7 +227,8 @@
<Run Text="At " />
<Run Text="{Binding CanvasPages.SelectedIndex, Mode=OneWay}" />
<Run Text="of " />
<Run Text="{Binding CanvasPages.Length, Mode=OneWay, ConverterParameter=-1, Converter={StaticResource MathAddConverter}}" />
<Run
Text="{Binding CanvasPages.Length, Mode=OneWay, ConverterParameter=-1, Converter={StaticResource MathAddConverter}}" />
<Run Text=" Pages" />
</TextBlock>
<Button Height="48"
Expand All @@ -207,10 +242,11 @@
Content="{materialDesign:PackIcon Kind=Plus}" />
</StackPanel>
<jas:DragCanvas>
<StackPanel Canvas.Left="{Binding (Canvas.Left), ElementName=ToolBar, ConverterParameter=81, Converter={StaticResource MathAddConverter}}"
Canvas.Top="{Binding (Canvas.Top), ElementName=ToolBar, ConverterParameter=65, Converter={StaticResource MathAddConverter}}"
jas:DragCanvas.CanBeDragged="False"
Orientation="Horizontal">
<StackPanel
Canvas.Left="{Binding (Canvas.Left), ElementName=ToolBar, ConverterParameter=81, Converter={StaticResource MathAddConverter}}"
Canvas.Top="{Binding (Canvas.Top), ElementName=ToolBar, ConverterParameter=65, Converter={StaticResource MathAddConverter}}"
jas:DragCanvas.CanBeDragged="False"
Orientation="Horizontal">
<Button Command="{Binding CanvasPages.SelectedPage.UndoCommand}"
Content="{materialDesign:PackIcon Kind=Undo}"
Style="{StaticResource MaterialDesignPaperDarkButton}" />
Expand Down Expand Up @@ -274,11 +310,12 @@
</ListBoxItem>
</ListBox>
</StackPanel>
<StackPanel Canvas.Left="{Binding (Canvas.Left), Converter={StaticResource MathAddConverter}, ElementName=ToolBar, ConverterParameter=80}"
Canvas.Top="{Binding (Canvas.Top), ConverterParameter=-36, Converter={StaticResource MathAddConverter}, ElementName=ToolBar}"
jas:DragCanvas.CanBeDragged="False"
Orientation="Horizontal"
Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=ToolBarPen}">
<StackPanel
Canvas.Left="{Binding (Canvas.Left), Converter={StaticResource MathAddConverter}, ElementName=ToolBar, ConverterParameter=80}"
Canvas.Top="{Binding (Canvas.Top), ConverterParameter=-36, Converter={StaticResource MathAddConverter}, ElementName=ToolBar}"
jas:DragCanvas.CanBeDragged="False"
Orientation="Horizontal"
Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=ToolBarPen}">
<ListBox x:Name="ColorList"
Margin="1"
ItemsSource="{Binding ColorList}"
Expand All @@ -287,7 +324,8 @@
Style="{StaticResource MaterialDesignToolToggleListBox}">
<ListBox.ItemTemplate>
<DataTemplate DataType="materialDesign:PackIcon">
<materialDesign:PackIcon Foreground="{Binding Converter={StaticResource ColorToBrushConverter}}" Kind="Circle" />
<materialDesign:PackIcon Foreground="{Binding Converter={StaticResource ColorToBrushConverter}}"
Kind="Circle" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Expand All @@ -307,4 +345,4 @@
</StackPanel>
</jas:DragCanvas>
</Grid>
</Window>
</Window>

0 comments on commit 61fc0d3

Please sign in to comment.