Skip to content

Commit faf59ee

Browse files
committed
Code Clean Ups
1 parent 97518b2 commit faf59ee

File tree

6 files changed

+177
-110
lines changed

6 files changed

+177
-110
lines changed

QuickNoteWidget/MainWindow.xaml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
WindowStyle="None"
1414
Title="MainWindow"
1515
Height="350"
16-
Topmost="{Binding Settings.OnTop}"
17-
ShowInTaskbar="{Binding Settings.ShowInTaskbar}"
16+
Topmost="{Binding OnTop}"
17+
ShowInTaskbar="{Binding ShowInTaskbar}"
1818
AllowsTransparency="True"
1919
Opacity="{Binding TransparencyValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
2020
ContextMenu="{DynamicResource contextMenu}"
@@ -88,14 +88,14 @@
8888
Margin="5,0,5,0"
8989
Height="23"
9090
ItemsSource="{Binding Fonts, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
91-
SelectedItem="{Binding Settings.CurrentFont, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
91+
SelectedItem="{Binding CurrentFont, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
9292
</Grid>
9393
<CheckBox Content="On Top"
94-
IsChecked="{Binding Settings.OnTop}" />
94+
IsChecked="{Binding OnTop}" />
9595
<CheckBox Content="Display Details"
96-
IsChecked="{Binding Settings.DisplayDetails}" />
96+
IsChecked="{Binding DisplayDetails}" />
9797
<CheckBox Content="Show in Taskbar"
98-
IsChecked="{Binding Settings.ShowInTaskbar}" />
98+
IsChecked="{Binding ShowInTaskbar}" />
9999
</MenuItem>
100100
<MenuItem Header="Reset Settings"
101101
Command="{Binding ResetViewCommand}" />
@@ -108,7 +108,7 @@
108108
<StatusBar Height="32"
109109
Style="{DynamicResource MahApps.Styles.StatusBar}"
110110
DockPanel.Dock="Bottom"
111-
Visibility="{Binding Settings.DisplayDetails, Converter={StaticResource BoolToVisibilityCollapsedConverter}}"
111+
Visibility="{Binding DisplayDetails, Converter={StaticResource BoolToVisibilityCollapsedConverter}}"
112112
VerticalAlignment="Bottom"
113113
Margin="12,6,12,10"
114114
Background="{Binding StatusBarBackground, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
@@ -154,13 +154,14 @@
154154
ShowLineNumbers="True"
155155
WordWrap="True"
156156
FontSize="15"
157-
FontFamily="{Binding Settings.CurrentFont, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
157+
FontFamily="{Binding CurrentFont, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
158158
PreviewMouseWheel="tbxMultiLine_PreviewMouseWheel" />
159159
</DockPanel>
160+
160161
<tb:TaskbarIcon IconSource=".\Clipboard.ico"
161162
x:Name="TaskbarIcon"
162163
Grid.RowSpan="2"
163164
Grid.ColumnSpan="4"
164-
ContextMenu="{DynamicResource contextMenu}"></tb:TaskbarIcon>
165+
ContextMenu="{DynamicResource contextMenu}" />
165166
</Grid>
166167
</Window>

QuickNoteWidget/MainWindowViewModel.cs

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,37 @@ public class MainWindowViewModel : ViewModelBase
3434
private string _statusBarBackground;
3535
private double _transparencyValue;
3636
private int _transparencyInPercent;
37-
private Settings _settings;
3837
private ObservableCollection<string> _fonts;
3938
private ObservableCollection<string> _themes;
4039
private ObservableCollection<string> _accents;
4140
private string _selectedTheme;
4241
private string _selectedAccent;
42+
private bool _ontop;
43+
private bool _showintaskbar;
44+
private bool _displaydetails;
45+
private string _currentfont;
46+
47+
48+
public string CurrentFont
49+
{
50+
get { return _currentfont; }
51+
set { SetProperty(ref _currentfont, value, () => CurrentFont); }
52+
}
53+
public bool OnTop
54+
{
55+
get { return _ontop; }
56+
set { SetProperty(ref _ontop, value, () => OnTop); }
57+
}
58+
public bool DisplayDetails
59+
{
60+
get { return _displaydetails; }
61+
set { SetProperty(ref _displaydetails, value, () => DisplayDetails); }
62+
}
63+
public bool ShowInTaskbar
64+
{
65+
get { return _showintaskbar; }
66+
set { SetProperty(ref _showintaskbar, value, () => ShowInTaskbar); }
67+
}
4368
public string SelectedAccent
4469
{
4570
get => _selectedAccent;
@@ -58,7 +83,7 @@ public string SelectedTheme
5883
StatusBarBackground = this.SelectedTheme == ThemeManager.BaseColorLight ? WHITE : BLACK;
5984
DragAreaColor = this.SelectedTheme == ThemeManager.BaseColorLight ? LIGHT_GRAY : GRAY;
6085
ThemeChanger.ChangeTheme(this.SelectedAccent, this.SelectedTheme);
61-
ThemeSelectionChanged();
86+
UpdateFontColorOnThemeSelectionChanged();
6287
}
6388
}
6489
public ObservableCollection<string> Accents
@@ -76,11 +101,6 @@ public ObservableCollection<string> Fonts
76101
get { return _fonts; }
77102
set { SetProperty(ref _fonts, value, () => Fonts); }
78103
}
79-
public Settings Settings
80-
{
81-
get { return _settings; }
82-
set { SetProperty(ref _settings, value, () => Settings); }
83-
}
84104
public int TransparencyInPercent
85105
{
86106
get { return _transparencyInPercent; }
@@ -89,8 +109,8 @@ public int TransparencyInPercent
89109
public double TransparencyValue
90110
{
91111
get { return _transparencyValue; }
92-
set
93-
{
112+
set
113+
{
94114
SetProperty(ref _transparencyValue, value, () => TransparencyValue);
95115
TransparencyInPercent = (Int32)(TransparencyValue * 100);
96116
}
@@ -123,14 +143,17 @@ public string MultiLine
123143

124144
#endregion MVVM Properties
125145

146+
147+
public Settings Settings { get; set; }
126148
public ICommand ClearMultiLineCommand { get; set; }
127149
public ICommand ResetViewCommand { get; set; }
128150

129151

152+
130153
public MainWindowViewModel()
131154
{
132-
LoadAvailableThemes();
133-
LoadSettings();
155+
LoadAvailableThemesAndAccents();
156+
LoadSettings(SettingsLoadLocations.FromFile);
134157
Init();
135158
}
136159

@@ -145,29 +168,28 @@ private void Init()
145168

146169
private IEnumerable<string> LoadInstalledFonts()
147170
{
148-
using (var fonts = new InstalledFontCollection())
149-
foreach (FontFamily font in fonts.Families)
150-
yield return font.Name;
171+
using (var fonts = new InstalledFontCollection())
172+
foreach (FontFamily font in fonts.Families)
173+
yield return font.Name;
151174
}
152175

153176
private void ClearMultiLine() => this.MultiLine = String.Empty;
154177

155178
private void ResetView()
156179
{
157-
this.TransparencyValue = 1;
158-
this.SelectedTheme = Themes.First();
159-
this.SelectedAccent = Accents.First(f => f == CYAN);
160-
this.Settings.CurrentFont = Fonts.First(f => f == DEFAULT_FONT);
180+
var defaultSettings = SettingsLogic.GetDefaultSettings();
181+
LoadSettings(SettingsLoadLocations.Default);
161182
}
162183

163184

164185
#region Themes
165-
private void LoadAvailableThemes()
186+
private void LoadAvailableThemesAndAccents()
166187
{
167188
Themes = new ObservableCollection<string>() { ThemeManager.BaseColorLight, ThemeManager.BaseColorDark };
168189
Accents = new ObservableCollection<string>(ThemeManager.Current.ColorSchemes);
169190
}
170-
private void ThemeSelectionChanged()
191+
192+
private void UpdateFontColorOnThemeSelectionChanged()
171193
{
172194
if (!String.IsNullOrEmpty(SelectedTheme))
173195
MultiLineTextForegroundColor = SelectedTheme == ThemeManager.BaseColorLight ? BLACK : WHITE;
@@ -178,18 +200,46 @@ private void ThemeSelectionChanged()
178200

179201

180202
#region Settings
181-
private void LoadSettings()
203+
private void LoadSettings(SettingsLoadLocations location)
182204
{
183-
this.Settings = SettingsLogic.GetSettings();
184-
SelectedTheme = Themes.FirstOrDefault(f => f == this.Settings.SelectedThemeName);
185-
SelectedAccent = Accents.FirstOrDefault(f => f == this.Settings.SelectedAccentName);
186-
TransparencyValue = Settings.TransparencyValue;
205+
InitSettingsBeforePropertiesUpdate(location);
206+
UpdateProperties();
187207
}
208+
209+
private void InitSettingsBeforePropertiesUpdate(SettingsLoadLocations location)
210+
{
211+
switch (location)
212+
{
213+
case SettingsLoadLocations.FromFile:
214+
Settings = SettingsLogic.GetSettings();
215+
break;
216+
case SettingsLoadLocations.Default:
217+
Settings = SettingsLogic.GetDefaultSettings();
218+
break;
219+
}
220+
}
221+
222+
private void UpdateProperties()
223+
{
224+
this.SelectedTheme = Themes.FirstOrDefault(f => f == this.Settings.SelectedThemeName);
225+
this.SelectedAccent = Accents.FirstOrDefault(f => f == this.Settings.SelectedAccentName);
226+
this.TransparencyValue = Settings.TransparencyValue;
227+
this.OnTop = Settings.OnTop;
228+
this.CurrentFont = Settings.CurrentFont;
229+
this.DisplayDetails = Settings.DisplayDetails;
230+
this.ShowInTaskbar = Settings.ShowInTaskbar;
231+
}
232+
233+
188234
public void SaveSettings()
189235
{
190236
Settings.SelectedAccentName = this.SelectedAccent;
191237
Settings.SelectedThemeName = this.SelectedTheme;
192238
Settings.TransparencyValue = this.TransparencyValue;
239+
Settings.OnTop = this.OnTop;
240+
Settings.CurrentFont = this.CurrentFont;
241+
Settings.DisplayDetails = this.DisplayDetails;
242+
Settings.ShowInTaskbar = this.ShowInTaskbar;
193243
SettingsLogic.SaveSettings(this.Settings);
194244
}
195245

QuickNoteWidget/QuickNoteWidget.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
<DependentUpon>InfoWindow.xaml</DependentUpon>
106106
</Compile>
107107
<Compile Include="Settings.cs" />
108+
<Compile Include="SettingsLoadLocations.cs" />
109+
<Compile Include="SettingsLogic.cs" />
108110
<Compile Include="Theme\ThemeChanger.cs" />
109111
<Page Include="InfoWindow.xaml">
110112
<SubType>Designer</SubType>

QuickNoteWidget/Settings.cs

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.IO;
3-
using System.Xml.Serialization;
4-
using DevExpress.Mvvm;
1+
using DevExpress.Mvvm;
52

63
namespace QuickNoteWidget
74
{
@@ -13,77 +10,6 @@ public class Settings : BindableBase
1310
public bool DisplayDetails { get; set; }
1411
public bool ShowInTaskbar { get; set; }
1512
public double TransparencyValue { get; set; }
16-
17-
private string _currentFont;
18-
19-
public string CurrentFont
20-
{
21-
get { return _currentFont; }
22-
set { SetProperty(ref _currentFont, value, () => CurrentFont); }
23-
}
24-
}
25-
26-
public static class SettingsLogic
27-
{
28-
private const string CYAN = "Cyan";
29-
private const string LIGHT = "Light";
30-
private static readonly string SettingsPath = $"{AppDomain.CurrentDomain.BaseDirectory}settings.xml";
31-
32-
public static void SaveSettings(Settings settings)
33-
{
34-
if (DoesSettingFileExists())
35-
File.Delete(SettingsPath);
36-
37-
SerializeToFile(settings);
38-
}
39-
40-
41-
public static Settings GetSettings()
42-
{
43-
if (DoesSettingFileExists() == false)
44-
return SettingsLogic.GetDefaultSettings();
45-
46-
return DeserializeFromFile();
47-
}
48-
49-
50-
51-
private static void SerializeToFile(Settings settings)
52-
{
53-
using (FileStream stream = new FileStream(SettingsPath, FileMode.OpenOrCreate))
54-
{
55-
var serializer = new XmlSerializer(typeof(Settings));
56-
serializer.Serialize(stream, settings);
57-
}
58-
}
59-
60-
private static Settings DeserializeFromFile()
61-
{
62-
using (FileStream stream = new FileStream(SettingsPath, FileMode.OpenOrCreate))
63-
{
64-
var serializer = new XmlSerializer(typeof(Settings));
65-
return serializer.Deserialize(stream) as Settings;
66-
}
67-
}
68-
69-
70-
71-
72-
private static Settings GetDefaultSettings() => new Settings()
73-
{
74-
SelectedAccentName = CYAN,
75-
SelectedThemeName = LIGHT,
76-
OnTop = false,
77-
DisplayDetails = false,
78-
ShowInTaskbar = false,
79-
TransparencyValue = 0.3,
80-
};
81-
82-
83-
84-
private static bool DoesSettingFileExists()
85-
{
86-
return File.Exists(SettingsPath);
87-
}
13+
public string CurrentFont { get; set; }
8814
}
8915
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace QuickNoteWidget
8+
{
9+
public enum SettingsLoadLocations
10+
{
11+
FromFile,
12+
Default
13+
}
14+
}

0 commit comments

Comments
 (0)