Skip to content

Commit cb90f66

Browse files
Implement settings and add icon
1 parent 4b93eac commit cb90f66

File tree

7 files changed

+109
-21
lines changed

7 files changed

+109
-21
lines changed

InvLock/Assets/logo.ico

259 KB
Binary file not shown.
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
using System.Reflection;
1+
using System.ComponentModel;
2+
using System.Reflection;
23

34
namespace InvLock.DataContexts;
45

5-
internal class MainWindowDataContext
6+
internal class MainWindowDataContext : INotifyPropertyChanged
67
{
78
#if DEBUG
89
public string Title => $"{App.AppName} - Dev {AppVersion}";
910
#else
10-
public string Title => $"{App.AppName} - {AppVersion}";
11+
12+
public event PropertyChangedEventHandler? PropertyChanged;
13+
14+
public string Title => $"{App.AppName} - {AppVersion}";
1115
#endif
1216

1317
public string AppName => App.AppName;
1418

1519
public string AppVersion => Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "Unknown";
20+
21+
public Settings Settings { get; } = Settings.Load();
1622
}

InvLock/InvLock.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
<ImplicitUsings>enable</ImplicitUsings>
88
<UseWPF>true</UseWPF>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10+
<ApplicationIcon>Assets\logo.ico</ApplicationIcon>
1011
</PropertyGroup>
1112

13+
<ItemGroup>
14+
<None Remove="icon.ico" />
15+
</ItemGroup>
16+
1217
<ItemGroup>
1318
<PackageReference Include="CuteUtils" Version="1.0.0" />
1419
<PackageReference Include="SharpHook" Version="5.3.8" />
1520
<PackageReference Include="WPF-UI" Version="3.0.5" />
1621
<PackageReference Include="WPF-UI.Tray" Version="3.0.5" />
1722
</ItemGroup>
23+
24+
<ItemGroup>
25+
<Resource Include="Assets\logo.ico" />
26+
</ItemGroup>
1827

1928
</Project>

InvLock/LockWindow.xaml.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ namespace InvLock;
1515
/// </summary>
1616
public partial class LockWindow : Window
1717
{
18-
private const bool HideWindows = false;
1918
private readonly SimpleGlobalHook hook = new SimpleGlobalHook();
2019
private readonly Dictionary<KeyCode, DateTime> pressedKeys = [];
20+
private readonly Settings settings;
2121
private List<IntPtr> windows = [];
2222
private bool isOpen = true;
2323
private bool suppressInput = false;
2424

25-
public LockWindow()
25+
public LockWindow(Settings settings)
2626
{
2727
InitializeComponent();
2828

@@ -42,6 +42,8 @@ public LockWindow()
4242

4343
Owner = w;
4444

45+
this.settings = settings;
46+
4547
hook.MouseMoved += Hook_Mouse;
4648
hook.MouseDragged += Hook_Mouse;
4749
hook.MousePressed += Hook_Mouse;
@@ -59,7 +61,7 @@ public LockWindow()
5961

6062
private void ActivateLockScreen()
6163
{
62-
if (HideWindows)
64+
if (settings.HideWindows)
6365
{
6466
MinimizeWindows();
6567
}
@@ -71,7 +73,7 @@ private void ActivateLockScreen()
7173
{
7274
_ = Activate();
7375

74-
textBlock.Text = "Lock Screen Active";
76+
textBlock.Text = settings.LockText;
7577
textBlock.Stroke = (Brush)FindResource("PaletteRedBrush");
7678

7779
Animation();
@@ -120,7 +122,7 @@ private async void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.Ses
120122
{
121123
if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionUnlock)
122124
{
123-
if (HideWindows)
125+
if (settings.HideWindows)
124126
{
125127
RestoreWindows();
126128
}
@@ -131,7 +133,7 @@ private async void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.Ses
131133

132134
await Task.Delay(500);
133135

134-
textBlock.Text = "Lock Screen Inactive";
136+
textBlock.Text = settings.UnlockText;
135137
textBlock.Stroke = (Brush)FindResource("PaletteGreenBrush");
136138

137139
Animation();

InvLock/MainWindow.xaml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@
3131
<RowDefinition Height="*" />
3232
</Grid.RowDefinitions>
3333

34-
<ui:TitleBar Title="{Binding Title}" Grid.Row="0" />
34+
<ui:TitleBar Title="{Binding Title}">
35+
<ui:TitleBar.Icon>
36+
<ui:ImageIcon Source="pack://application:,,,/Assets/logo.ico" />
37+
</ui:TitleBar.Icon>
38+
</ui:TitleBar>
3539

36-
<tray:NotifyIcon Grid.Row="0" FocusOnLeftClick="True" MenuOnRightClick="True" LeftClick="NotifyIcon_LeftClick" TooltipText="Wpf.Ui.Demo.Simple">
40+
<tray:NotifyIcon Grid.Row="0" FocusOnLeftClick="True" MenuOnRightClick="True" LeftClick="NotifyIcon_LeftClick" TooltipText="{Binding AppName}">
3741
<tray:NotifyIcon.Menu>
3842
<ContextMenu>
3943
<MenuItem Header="Settings" Click="SettingsMenuItem_Click" />
4044
<MenuItem Header="Quit" Click="QuitMenuItem_Click" />
4145
</ContextMenu>
4246
</tray:NotifyIcon.Menu>
47+
<tray:NotifyIcon.Icon>
48+
<BitmapImage UriSource="pack://application:,,,/Assets/logo.ico" />
49+
</tray:NotifyIcon.Icon>
4350
</tray:NotifyIcon>
4451

4552
<ui:DynamicScrollViewer Grid.Row="1" VirtualizingPanel.ScrollUnit="Pixel">
@@ -80,21 +87,21 @@
8087
<ui:CardControl.Header>
8188
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Show desktop while locked" />
8289
</ui:CardControl.Header>
83-
<ui:ToggleSwitch Grid.Column="1" IsChecked="False" OffContent="Off" OnContent="On" />
90+
<ui:ToggleSwitch Grid.Column="1" IsChecked="{Binding Settings.HideWindows}" OffContent="Off" OnContent="On" />
8491
</ui:CardControl>
8592

8693
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=LockClosed24}" Height="75">
8794
<ui:CardControl.Header>
8895
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Lock text" />
8996
</ui:CardControl.Header>
90-
<ui:TextBox Grid.Column="1" MinWidth="200" PlaceholderText="Lock screen active" />
97+
<ui:TextBox Grid.Column="1" MinWidth="200" Text="{Binding Settings.LockText, UpdateSourceTrigger=PropertyChanged}" PlaceholderText="Type your lock text here" />
9198
</ui:CardControl>
9299

93100
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=LockOpen24}" Height="75">
94101
<ui:CardControl.Header>
95102
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Unlock text" />
96103
</ui:CardControl.Header>
97-
<ui:TextBox Grid.Column="1" MinWidth="200" PlaceholderText="Lock screen inactive" />
104+
<ui:TextBox Grid.Column="1" MinWidth="200" Text="{Binding Settings.UnlockText, UpdateSourceTrigger=PropertyChanged}" PlaceholderText="Type your unlock text here" />
98105
</ui:CardControl>
99106

100107
<ui:TextBlock Margin="0,24,0,8" FontTypography="BodyStrong" Text="About" />

InvLock/MainWindow.xaml.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ namespace InvLock;
1212
/// </summary>
1313
public partial class MainWindow : FluentWindow
1414
{
15+
private readonly MainWindowDataContext mainWindowDataContext = new MainWindowDataContext();
1516
private LockWindow? lockWindow;
1617
private bool blockWindowClosing = true;
1718

1819
public MainWindow()
1920
{
20-
DataContext = new MainWindowDataContext();
21+
DataContext = mainWindowDataContext;
2122

2223
InitializeComponent();
2324

@@ -51,7 +52,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
5152
{
5253
Close();
5354

54-
lockWindow = new LockWindow();
55+
lockWindow = new LockWindow(mainWindowDataContext.Settings);
5556
lockWindow.Show();
5657
}
5758

@@ -62,6 +63,8 @@ private void NotifyIcon_LeftClick(Wpf.Ui.Tray.Controls.NotifyIcon sender, Routed
6263

6364
private void FluentWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
6465
{
66+
mainWindowDataContext.Settings.Save();
67+
6568
if (blockWindowClosing)
6669
{
6770
e.Cancel = true;

InvLock/Settings.cs

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,69 @@
1-
namespace InvLock;
1+
using System.ComponentModel;
2+
using System.IO;
3+
using System.Runtime.CompilerServices;
4+
using System.Text.Json;
25

3-
internal class Settings
6+
namespace InvLock;
7+
8+
public class Settings : INotifyPropertyChanged
49
{
5-
public bool HideWindows { get; set; } = false;
6-
public string LockText { get; set; } = "Lock Screen Active";
7-
public string UnlockText { get; set; } = "Lock Screen Inactive";
10+
public event PropertyChangedEventHandler? PropertyChanged;
11+
12+
private static readonly string settingsPath = Path.Combine(App.ApplicationDataPath, "settings.json");
13+
private bool hideWindows = false;
14+
private string lockText = "Lock Screen Active";
15+
16+
private string unlockText = "Lock Screen Inactive";
17+
18+
public bool HideWindows
19+
{
20+
get => hideWindows;
21+
set
22+
{
23+
hideWindows = value;
24+
OnPropertyChanged();
25+
}
26+
}
27+
28+
public string LockText
29+
{
30+
get => lockText;
31+
set
32+
{
33+
lockText = value;
34+
OnPropertyChanged();
35+
}
36+
}
37+
38+
public string UnlockText
39+
{
40+
get => unlockText;
41+
set
42+
{
43+
unlockText = value;
44+
OnPropertyChanged();
45+
}
46+
}
47+
48+
public static Settings Load()
49+
{
50+
if (File.Exists(settingsPath))
51+
{
52+
string json = File.ReadAllText(settingsPath);
53+
return JsonSerializer.Deserialize<Settings>(json) ?? new Settings();
54+
}
55+
56+
return new Settings();
57+
}
58+
59+
public void Save()
60+
{
61+
string json = JsonSerializer.Serialize(this);
62+
File.WriteAllText(settingsPath, json);
63+
}
64+
65+
protected void OnPropertyChanged([CallerMemberName] string? name = null)
66+
{
67+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
68+
}
869
}

0 commit comments

Comments
 (0)