Skip to content

Commit

Permalink
Minor variable streamlining
Browse files Browse the repository at this point in the history
Adjustments to repository information
  • Loading branch information
Taicanium committed Dec 14, 2024
1 parent 9f0ab9b commit d0afe7b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 41 deletions.
7 changes: 5 additions & 2 deletions Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,23 @@ public enum UUIDType

public static void AddDatabase(Database db)
{
static object PanelLabel(TabItem item) => ((Label)((StackPanel)item.Header).Children[0]).Content;

var template = (DataTemplate)Application.Current.MainWindow.TryFindResource("DatabaseContentTemplate");
var control = (TabControl)Application.Current.MainWindow.FindName("DatabasesPanel");
var tabs = control.Items.Cast<TabItem>();

if ((db.Name ?? string.Empty).Equals(string.Empty))
db.Name = DefaultDatabase;

if (tabs.Where(item => ((Label)((StackPanel)item.Header).Children[0]).Content.Equals(db.Name)).Any())
if (tabs.Where(item => PanelLabel(item).Equals(db.Name)).Any())
{
var index = 1;
Match match;
if ((match = IndexDigits().Match(db.Name ?? string.Empty)).Success)
index = int.Parse(match.Groups[1].Value);

while (tabs.Where(item => ((Label)((StackPanel)item.Header).Children[0]).Content.Equals($"{db.Name} ({index})")).Any())
while (tabs.Where(item => PanelLabel(item).Equals($"{db.Name} ({index})")).Any())
index++;
db.Name = $"{db.Name} ({index})";
}
Expand Down Expand Up @@ -127,6 +129,7 @@ public static void AddDatabase(Database db)
public static SolidColorBrush? BrushFromBytes(string data)
{
var hex = NumberStyles.HexNumber;

if (data.Length == 6)
data = "FF" + data;

Expand Down
42 changes: 17 additions & 25 deletions ContextSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ public void Load()
if (!File.Exists(SettingsFile))
return;

string[] settings = File.ReadAllLines(SettingsFile);

for (int i = 0; i < settings.Length; i++)
foreach (string setting in File.ReadAllLines(SettingsFile))
{
var setting = settings[i].Trim();
var keyValue = setting.Split(':', 2);
var keyValue = setting.Trim().Split(':', 2);
switch (keyValue[0])
{
case "AccentBackground":
Expand Down Expand Up @@ -141,25 +138,20 @@ public void Load()

protected void OnPropertyChanged([CallerMemberName] string? name = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

public void Save()
{
string[] settings = [
$"AccentBackground:{BytesFromBrush(AccentBackground)}",
$"AccentForeground:{BytesFromBrush(AccentForeground)}",
$"FontFamily:{MainFontFamily?.Source}",
$"FontSize:{MainFontSize}",
$"LastDatabases:{string.Join(';', DatabaseFiles.Distinct().Where(File.Exists))}",
$"ListBackground:{BytesFromBrush(ListBackground)}",
$"ListForeground:{BytesFromBrush(ListForeground)}",
$"MenuBackground:{BytesFromBrush(MenuBackground)}",
$"MenuForeground:{BytesFromBrush(MenuForeground)}",
$"RecentNotesSortMode:{(int)RecentEntriesSortMode}",
$"RibbonDisplayMode:{(int)RibbonTabContent}",
$"SearchResultsOnTop:{SearchResultsOnTop}",
$"SnapSearchResults:{SnapSearchResults}",
];

File.WriteAllLines(SettingsFile, settings);
}
public void Save() => File.WriteAllLines(SettingsFile, [
$"AccentBackground:{BytesFromBrush(AccentBackground)}",
$"AccentForeground:{BytesFromBrush(AccentForeground)}",
$"FontFamily:{MainFontFamily?.Source}",
$"FontSize:{MainFontSize}",
$"LastDatabases:{string.Join(';', DatabaseFiles.Distinct().Where(File.Exists))}",
$"ListBackground:{BytesFromBrush(ListBackground)}",
$"ListForeground:{BytesFromBrush(ListForeground)}",
$"MenuBackground:{BytesFromBrush(MenuBackground)}",
$"MenuForeground:{BytesFromBrush(MenuForeground)}",
$"RecentNotesSortMode:{(int)RecentEntriesSortMode}",
$"RibbonDisplayMode:{(int)RibbonTabContent}",
$"SearchResultsOnTop:{SearchResultsOnTop}",
$"SnapSearchResults:{SnapSearchResults}",
]);
}
}
18 changes: 6 additions & 12 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private void MainWindow_Loaded(object sender, RoutedEventArgs e)
Directory.CreateDirectory(folder.Value);

if (FirstRun)
Database.Create(Path.Join(Subfolders["Databases"], $"{DefaultDatabase}", $"{DefaultDatabase}.sidb"));
Database.Create(Path.Join(Subfolders["Databases"], DefaultDatabase, $"{DefaultDatabase}.sidb"));

DatabasesPanel.SelectedIndex = 0;

Expand Down Expand Up @@ -276,17 +276,15 @@ protected override void OnClosed(EventArgs e)
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var helper = new WindowInteropHelper(this);
WindowSource = HwndSource.FromHwnd(helper.Handle);
WindowSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
WindowSource.AddHook(HwndHook);
RegisterHotKey();
}

private void RegisterHotKey()
{
var helper = new WindowInteropHelper(this);
if (!RegisterHotKey(helper.Handle, HotKeyID, 2, (uint)KeyInterop.VirtualKeyFromKey(Key.N)))
MessageBox.Show("Failed to register hotkey.", "Sylver Ink: Error", MessageBoxButton.OK, MessageBoxImage.Error);
if (!RegisterHotKey(new WindowInteropHelper(this).Handle, HotKeyID, 2, (uint)KeyInterop.VirtualKeyFromKey(Key.N)))
MessageBox.Show("Failed to register new note hotkey.", "Sylver Ink: Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

private void RenameClosed(object sender, EventArgs e)
Expand Down Expand Up @@ -364,7 +362,7 @@ private void TabChanged(object sender, SelectionChangedEventArgs e)
var control = (TabControl)sender;
if (control.Name.Equals("DatabasesPanel"))
{
var item = (TabItem)control.SelectedItem;
var item = (TabItem?)control.SelectedItem;
if (item is null)
return;

Expand All @@ -380,10 +378,6 @@ private void TabChanged(object sender, SelectionChangedEventArgs e)
DeferUpdateRecentNotes(true);
}

private void UnregisterHotKey()
{
var helper = new WindowInteropHelper(this);
UnregisterHotKey(helper.Handle, HotKeyID);
}
private void UnregisterHotKey() => UnregisterHotKey(new WindowInteropHelper(this).Handle, HotKeyID);
}
}
5 changes: 3 additions & 2 deletions Sylver Ink.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
<SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("yyyyMMdd"))</SourceRevisionId>
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<Version>1.1.2</Version>
<Version>1.1.3</Version>
<UseWindowsForms>False</UseWindowsForms>
<Title>Sylver Ink</Title>
<Authors>Taica</Authors>
<Authors>Taicanium,milesfarber,norvinsk.hunter</Authors>
<PackageIcon>glasses.png</PackageIcon>
<ApplicationIcon>glasses.ico</ApplicationIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Company />
<PackageProjectUrl>https://github.com/Taicanium/Sylver-Ink</PackageProjectUrl>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RepositoryUrl>https://github.com/Taicanium/Sylver-Ink</RepositoryUrl>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down

0 comments on commit d0afe7b

Please sign in to comment.