Skip to content

Commit 961322c

Browse files
authored
Merge pull request #452 from TheJoeFin/dev
Dev
2 parents ec91ab3 + cf9fbd6 commit 961322c

33 files changed

+574
-209
lines changed

.github/dependabot.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ version: 2
77
updates:
88
- package-ecosystem: "nuget" # See documentation for possible values
99
directory: "/" # Location of package manifests
10-
target-branch: "dev"
1110
schedule:
1211
interval: "daily"
12+
target-branch: "dev"

Text-Grab-Package/Package.appxmanifest

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<Identity
1212
Name="40087JoeFinApps.TextGrab"
1313
Publisher="CN=153F3B0F-BA3D-4964-8098-71AC78A1DF6A"
14-
Version="4.4.0.0" />
14+
Version="4.4.1.0" />
1515

1616
<Properties>
1717
<DisplayName>Text Grab</DisplayName>

Text-Grab/App.xaml.cs

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.Toolkit.Uwp.Notifications;
22
using Microsoft.Win32;
3-
using Microsoft.Windows.Themes;
43
using RegistryUtils;
54
using System;
65
using System.Collections.Generic;
@@ -18,7 +17,6 @@
1817
using Text_Grab.Views;
1918
using Wpf.Ui;
2019
using Wpf.Ui.Appearance;
21-
using Wpf.Ui.Extensions;
2220

2321
namespace Text_Grab;
2422

@@ -27,6 +25,12 @@ namespace Text_Grab;
2725
/// </summary>
2826
public partial class App : System.Windows.Application
2927
{
28+
#region Fields
29+
30+
readonly static Settings _defaultSettings = AppUtilities.TextGrabSettings;
31+
32+
#endregion Fields
33+
3034
#region Properties
3135

3236
public List<int> HotKeyIds { get; set; } = new();
@@ -38,7 +42,7 @@ public partial class App : System.Windows.Application
3842

3943
public static void DefaultLaunch()
4044
{
41-
TextGrabMode defaultLaunchSetting = Enum.Parse<TextGrabMode>(Settings.Default.DefaultLaunch, true);
45+
TextGrabMode defaultLaunchSetting = Enum.Parse<TextGrabMode>(_defaultSettings.DefaultLaunch, true);
4246

4347
switch (defaultLaunchSetting)
4448
{
@@ -65,7 +69,7 @@ public static void DefaultLaunch()
6569
}
6670
public static void SetTheme(object? sender = null, EventArgs? e = null)
6771
{
68-
bool gotTheme = Enum.TryParse<AppTheme>(Settings.Default.AppTheme.ToString(), true, out AppTheme currentAppTheme);
72+
bool gotTheme = Enum.TryParse(_defaultSettings.AppTheme.ToString(), true, out AppTheme currentAppTheme);
6973

7074
if (!gotTheme)
7175
return;
@@ -135,8 +139,8 @@ private static async Task<bool> HandleStartupArgs(string[] args)
135139
if (arg == "--windowless")
136140
{
137141
isQuiet = true;
138-
Settings.Default.FirstRun = false;
139-
Settings.Default.Save();
142+
_defaultSettings.FirstRun = false;
143+
_defaultSettings.Save();
140144
}
141145

142146
if (currentArgument.Contains("ToastActivated"))
@@ -151,7 +155,7 @@ private static async Task<bool> HandleStartupArgs(string[] args)
151155
return true;
152156
}
153157

154-
bool isStandardMode = Enum.TryParse<TextGrabMode>(currentArgument, true, out TextGrabMode launchMode);
158+
bool isStandardMode = Enum.TryParse(currentArgument, true, out TextGrabMode launchMode);
155159

156160
if (isStandardMode)
157161
{
@@ -195,8 +199,8 @@ private static void ShowAndSetFirstRun()
195199
FirstRunWindow frw = new();
196200
frw.Show();
197201

198-
Settings.Default.FirstRun = false;
199-
Settings.Default.Save();
202+
_defaultSettings.FirstRun = false;
203+
_defaultSettings.Save();
200204
}
201205

202206
private static async Task<bool> TryToOpenFile(string possiblePath, bool isQuiet)
@@ -253,14 +257,14 @@ async void appStartup(object sender, StartupEventArgs e)
253257
if (handledArgument)
254258
{
255259
// arguments were passed, so don't show firstRun dialog
256-
Settings.Default.FirstRun = false;
257-
Settings.Default.Save();
260+
_defaultSettings.FirstRun = false;
261+
_defaultSettings.Save();
258262
return;
259263
}
260264

261-
if (Settings.Default.FirstRun)
265+
if (_defaultSettings.FirstRun)
262266
{
263-
Settings.Default.CorrectToLatin = LanguageUtilities.IsCurrentLanguageLatinBased();
267+
_defaultSettings.CorrectToLatin = LanguageUtilities.IsCurrentLanguageLatinBased();
264268
ShowAndSetFirstRun();
265269
return;
266270
}
@@ -277,11 +281,11 @@ private void CurrentDispatcherUnhandledException(object sender, DispatcherUnhand
277281

278282
private bool HandleNotifyIcon()
279283
{
280-
if (Settings.Default.RunInTheBackground && NumberOfRunningInstances < 2)
284+
if (_defaultSettings.RunInTheBackground && NumberOfRunningInstances < 2)
281285
{
282286
NotifyIconUtilities.SetupNotifyIcon();
283287

284-
if (Settings.Default.StartupOnLogin)
288+
if (_defaultSettings.StartupOnLogin)
285289
return true;
286290
}
287291

@@ -295,11 +299,11 @@ private void LaunchFromToast(ToastNotificationActivatedEventArgsCompat toastArgs
295299
return;
296300

297301
// Need to dispatch to UI thread if performing UI operations
298-
Dispatcher.BeginInvoke((Action)(() =>
302+
Dispatcher.BeginInvoke(() =>
299303
{
300304
EditTextWindow mtw = new(argsInvoked);
301305
mtw.Show();
302-
}));
306+
});
303307
}
304308
#endregion Methods
305309
}

Text-Grab/Controls/BottomBarSettings.xaml.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Text_Grab.Controls;
1212

1313
public partial class BottomBarSettings : FluentWindow
1414
{
15+
private readonly Settings DefaultSettings = AppUtilities.TextGrabSettings;
16+
1517
#region Constructors
1618

1719
public BottomBarSettings()
@@ -30,8 +32,8 @@ public BottomBarSettings()
3032
ButtonsInLeftList = new(allBtns);
3133
LeftListBox.ItemsSource = ButtonsInLeftList;
3234

33-
ShowCursorTextCheckBox.IsChecked = Settings.Default.ShowCursorText;
34-
ShowScrollbarCheckBox.IsChecked = Settings.Default.ScrollBottomBar;
35+
ShowCursorTextCheckBox.IsChecked = DefaultSettings.ShowCursorText;
36+
ShowScrollbarCheckBox.IsChecked = DefaultSettings.ScrollBottomBar;
3537
}
3638

3739
#endregion Constructors
@@ -115,9 +117,9 @@ private void MoveUpButton_Click(object sender, RoutedEventArgs e)
115117
}
116118
private void SaveBTN_Click(object sender, RoutedEventArgs e)
117119
{
118-
Settings.Default.ShowCursorText = ShowCursorTextCheckBox.IsChecked ?? true;
119-
Settings.Default.ScrollBottomBar = ShowScrollbarCheckBox.IsChecked ?? true;
120-
Settings.Default.Save();
120+
DefaultSettings.ShowCursorText = ShowCursorTextCheckBox.IsChecked ?? true;
121+
DefaultSettings.ScrollBottomBar = ShowScrollbarCheckBox.IsChecked ?? true;
122+
DefaultSettings.Save();
121123

122124
CustomBottomBarUtilities.SaveCustomBottomBarItemsSetting(ButtonsInRightList.ToList());
123125
if (Owner is EditTextWindow etw)

Text-Grab/Controls/WordBorder.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ private void WordBorderControl_MouseDoubleClick(object sender, MouseButtonEventA
361361

362362
try { Clipboard.SetDataObject(Word, true); } catch { }
363363

364-
if (Settings.Default.ShowToast
364+
if (AppUtilities.TextGrabSettings.ShowToast
365365
&& !IsFromEditWindow)
366366
NotificationUtilities.ShowToast(Word);
367367

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using Windows.Storage;
5+
using Windows.Storage.Streams;
6+
7+
namespace Text_Grab.Helpers;
8+
9+
// Use these extension methods to store and retrieve local and roaming app data
10+
// More details regarding storing and retrieving app data at https://docs.microsoft.com/windows/apps/design/app-settings/store-and-retrieve-app-data
11+
public static class SettingsStorageExtensions
12+
{
13+
private const string FileExtension = ".json";
14+
15+
public static bool IsRoamingStorageAvailable(this ApplicationData appData)
16+
{
17+
return appData.RoamingStorageQuota == 0;
18+
}
19+
20+
public static async Task SaveAsync<T>(this StorageFolder folder, string name, T content)
21+
{
22+
if (content is null)
23+
return;
24+
25+
StorageFile file = await folder.CreateFileAsync(GetFileName(name), CreationCollisionOption.ReplaceExisting);
26+
string fileContent = await Json.StringifyAsync(content);
27+
28+
await FileIO.WriteTextAsync(file, fileContent);
29+
}
30+
31+
public static async Task<T?> ReadAsync<T>(this StorageFolder folder, string name)
32+
{
33+
if (!File.Exists(Path.Combine(folder.Path, GetFileName(name))))
34+
{
35+
return default;
36+
}
37+
38+
StorageFile file = await folder.GetFileAsync($"{name}.json");
39+
string fileContent = await FileIO.ReadTextAsync(file);
40+
41+
return await Json.ToObjectAsync<T>(fileContent);
42+
}
43+
44+
public static async Task SaveAsync<T>(this ApplicationDataContainer settings, string key, T? value)
45+
{
46+
if (value is null)
47+
return;
48+
49+
settings.SaveString(key, await Json.StringifyAsync(value));
50+
}
51+
52+
public static void SaveString(this ApplicationDataContainer settings, string key, string value)
53+
{
54+
settings.Values[key] = value;
55+
}
56+
57+
public static async Task<T?> ReadAsync<T>(this ApplicationDataContainer settings, string key)
58+
{
59+
if (settings.Values.TryGetValue(key, out object? obj))
60+
return await Json.ToObjectAsync<T>((string)obj);
61+
62+
return default;
63+
}
64+
65+
public static async Task<StorageFile> SaveFileAsync(this StorageFolder folder, byte[] content, string fileName, CreationCollisionOption options = CreationCollisionOption.ReplaceExisting)
66+
{
67+
ArgumentNullException.ThrowIfNull(content);
68+
69+
if (string.IsNullOrEmpty(fileName))
70+
throw new ArgumentException("File name is null or empty. Specify a valid file name", nameof(fileName));
71+
72+
StorageFile storageFile = await folder.CreateFileAsync(fileName, options);
73+
await FileIO.WriteBytesAsync(storageFile, content);
74+
return storageFile;
75+
}
76+
77+
public static async Task<byte[]?> ReadFileAsync(this StorageFolder folder, string fileName)
78+
{
79+
IStorageItem item = await folder.TryGetItemAsync(fileName).AsTask().ConfigureAwait(false);
80+
81+
if ((item != null) && item.IsOfType(StorageItemTypes.File))
82+
{
83+
StorageFile storageFile = await folder.GetFileAsync(fileName);
84+
byte[]? content = await storageFile.ReadBytesAsync();
85+
return content;
86+
}
87+
88+
return null;
89+
}
90+
91+
public static async Task<byte[]?> ReadBytesAsync(this StorageFile file)
92+
{
93+
if (file == null)
94+
return null;
95+
96+
using IRandomAccessStream stream = await file.OpenReadAsync();
97+
using DataReader reader = new(stream.GetInputStreamAt(0));
98+
await reader.LoadAsync((uint)stream.Size);
99+
byte[] bytes = new byte[stream.Size];
100+
reader.ReadBytes(bytes);
101+
return bytes;
102+
}
103+
104+
private static string GetFileName(string name)
105+
{
106+
return string.Concat(name, FileExtension);
107+
}
108+
}

Text-Grab/Models/OcrOutput.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public record OcrOutput
1717

1818
public void CleanOutput()
1919
{
20-
if (Settings.Default is not Settings userSettings
20+
if (AppUtilities.TextGrabSettings is not Settings userSettings
2121
|| Kind == OcrOutputKind.Barcode)
2222
return;
2323

Text-Grab/Pages/DangerSettings.xaml.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Text_Grab.Pages;
1111
/// </summary>
1212
public partial class DangerSettings : Page
1313
{
14+
private readonly Settings DefaultSettings = AppUtilities.TextGrabSettings;
15+
1416
public DangerSettings()
1517
{
1618
InitializeComponent();
@@ -23,7 +25,7 @@ private void ResetSettingsButton_Click(object sender, RoutedEventArgs e)
2325
if (areYouSure != MessageBoxResult.Yes)
2426
return;
2527

26-
Settings.Default.Reset();
28+
DefaultSettings.Reset();
2729
Singleton<HistoryService>.Instance.DeleteHistory();
2830
App.Current.Shutdown();
2931
}

Text-Grab/Pages/GeneralSettings.xaml

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
99
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
1010
Title="GeneralSettings"
11-
d:DesignHeight="450"
11+
d:DesignHeight="1450"
1212
d:DesignWidth="800"
1313
Loaded="Page_Loaded"
1414
mc:Ignorable="d">
@@ -27,7 +27,7 @@
2727
x:Name="VersionTextblock"
2828
VerticalAlignment="Center"
2929
Style="{StaticResource TextBodyNormal}"
30-
Text="Version 4.4.0" />
30+
Text="Version 4.4.1" />
3131

3232
<ui:HyperlinkButton
3333
x:Name="OpenExeFolderButton"
@@ -203,7 +203,6 @@
203203
<ui:ToggleSwitch
204204
Name="ReadBarcodesBarcode"
205205
Checked="ReadBarcodesBarcode_Checked"
206-
IsChecked="True"
207206
Unchecked="ReadBarcodesBarcode_Unchecked">
208207
<TextBlock Style="{StaticResource TextBodyNormal}">
209208
Try to read barcodes
@@ -299,7 +298,6 @@
299298
<ui:ToggleSwitch
300299
Name="HistorySwitch"
301300
Checked="HistorySwitch_Checked"
302-
IsChecked="True"
303301
Unchecked="HistorySwitch_Unchecked">
304302
<TextBlock Style="{StaticResource TextBodyNormal}">
305303
Keep recent history of Grabs and Edit Text Windows

0 commit comments

Comments
 (0)