Store settings in settings.xml instead of registry.

This commit is contained in:
Frank Skare
2021-05-24 03:09:23 +02:00
parent bba71c3782
commit 9d27465492
13 changed files with 137 additions and 107 deletions

View File

@@ -19,7 +19,7 @@ namespace mpvnet
public static string LightTheme { get; set; } = "light";
public static string StartSize { get; set; } = "height-session";
public static bool RememberPosition { get; set; }
public static bool RememberWindowPosition { get; set; }
public static bool DebugMode { get; set; }
public static bool IsStartedFromTerminal { get; } = Environment.GetEnvironmentVariable("_started_from_console") == "yes";
public static bool RememberVolume { get; set; } = true;
@@ -39,6 +39,17 @@ namespace mpvnet
get => (DarkMode == "system" && Sys.IsDarkTheme) || DarkMode == "always";
}
static AppSettings _Settings;
public static AppSettings Settings {
get {
if (_Settings == null)
_Settings = SettingsManager.Load();
return _Settings;
}
}
public static void Init()
{
string dummy = Core.ConfigFolder;
@@ -132,18 +143,17 @@ namespace mpvnet
{
if (RememberVolume)
{
Core.set_property_int("volume", RegistryHelp.GetInt("volume", 70));
Core.set_property_string("mute", RegistryHelp.GetString("mute", "no"));
Core.set_property_int("volume", Settings.Volume);
Core.set_property_string("mute", Settings.Mute);
}
}
static void Shutdown()
{
if (RememberVolume)
{
RegistryHelp.SetInt("volume", Core.get_property_int("volume"));
RegistryHelp.SetString("mute", Core.get_property_string("mute"));
}
Settings.Volume = Core.get_property_int("volume");
Settings.Mute = Core.get_property_string("mute");
SettingsManager.Save(Settings);
}
static Dictionary<string, string> _Conf;
@@ -167,7 +177,7 @@ namespace mpvnet
{
switch (name)
{
case "remember-position": RememberPosition = value == "yes"; return true;
case "remember-window-position": RememberWindowPosition = value == "yes"; return true;
case "debug-mode": DebugMode = value == "yes"; return true;
case "remember-volume": RememberVolume = value == "yes"; return true;
case "queue": Queue = value == "yes"; return true;
@@ -190,23 +200,5 @@ namespace mpvnet
return false;
}
}
public static void ShowSetup()
{
int value = RegistryHelp.GetInt("location: " + Folder.Startup);
if (value != 1)
{
if (Msg.ShowQuestion("Would you like to setup mpv.net?",
"The setup allows to create a start menu shortcut, file associations and " +
"adding mpv.net to the Path environment variable.") == DialogResult.OK)
Commands.Execute("show-setup-dialog");
else
Msg.ShowInfo("The setup dialog can be found at:\n\nContext Menu > Tools > Setup");
RegistryHelp.SetInt("location: " + Folder.Startup, 1);
}
}
}
}

View File

@@ -6,8 +6,6 @@ using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
using static mpvnet.Global;
namespace mpvnet
{
static class Program
@@ -23,9 +21,6 @@ namespace mpvnet
if (App.IsStartedFromTerminal)
Native.AttachConsole(-1 /*ATTACH_PARENT_PROCESS*/);
if (Core.ConfigFolder == "")
return;
string[] args = Environment.GetCommandLineArgs().Skip(1).ToArray();
if (args.Length >= 2 && args[0] == "--reg-file-assoc")

56
src/Misc/Settings.cs Normal file
View File

@@ -0,0 +1,56 @@

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using static mpvnet.Global;
namespace mpvnet
{
[Serializable()]
public class AppSettings
{
public int LastUpdateCheck;
public int Volume = 70;
public List<string> RecentFiles = new List<string>();
public Point WindowLocation;
public Point WindowPosition;
public Size WindowSize;
public string ConfigEditorSearch = "";
public string Mute = "no";
public string UpdateCheckVersion = "";
}
class SettingsManager
{
public static string SettingsFile {
get => Core.ConfigFolder + "settings.xml";
}
public static AppSettings Load()
{
if (!File.Exists(SettingsFile))
return new AppSettings();
XmlSerializer serializer = new XmlSerializer(typeof(AppSettings));
using (FileStream fs = new FileStream(SettingsFile, FileMode.Open))
return (AppSettings)serializer.Deserialize(fs);
}
public static void Save(object obj)
{
using (XmlTextWriter writer = new XmlTextWriter(SettingsFile, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(writer, obj);
}
}
}
}

View File

@@ -15,9 +15,7 @@ namespace mpvnet
{
public static void DailyCheck()
{
if (App.UpdateCheck && RegistryHelp.GetInt("last-update-check")
!= DateTime.Now.DayOfYear)
if (App.UpdateCheck && App.Settings.LastUpdateCheck != DateTime.Now.DayOfYear)
CheckOnline();
}
@@ -27,7 +25,7 @@ namespace mpvnet
{
using (HttpClient client = new HttpClient())
{
RegistryHelp.SetValue("last-update-check", DateTime.Now.DayOfYear);
App.Settings.LastUpdateCheck = DateTime.Now.DayOfYear;
client.DefaultRequestHeaders.Add("User-Agent", "mpv.net");
var response = await client.GetAsync("https://api.github.com/repos/stax76/mpv.net/releases/latest");
response.EnsureSuccessStatusCode();
@@ -51,8 +49,8 @@ namespace mpvnet
return;
}
if ((RegistryHelp.GetString("update-check-version")
!= onlineVersion.ToString() || showUpToDateMessage) && Msg.ShowQuestion(
if ((App.Settings.UpdateCheckVersion != onlineVersion.ToString() ||
showUpToDateMessage) && Msg.ShowQuestion(
$"New version {onlineVersion} is available, update now?") == DialogResult.OK)
{
string url = $"https://github.com/stax76/mpv.net/releases/download/{onlineVersion}/mpv.net-{onlineVersion}-portable.zip";
@@ -73,7 +71,7 @@ namespace mpvnet
Core.command("quit");
}
RegistryHelp.SetValue("update-check-version", onlineVersion.ToString());
App.Settings.UpdateCheckVersion = onlineVersion.ToString();
}
}
catch (Exception ex)

View File

@@ -362,7 +362,7 @@ filter = "Screen"
help = "<float> Minimum aspect ratio, if the AR is smaller than the defined value then the window AR is set to 16/9. This avoids a square window for Music with cover art. Default: 1.2 (mpv.net specific setting)"
[[settings]]
name = "remember-position"
name = "remember-window-position"
file = "mpvnet"
default = "no"
filter = "Screen"

View File

@@ -31,7 +31,7 @@ namespace mpvnet
LoadConf(App.ConfPath);
LoadSettings();
InitialContent = GetCompareString();
SearchControl.Text = RegistryHelp.GetString("config-editor-search");
SearchControl.Text = App.Settings.ConfigEditorSearch;
FilterListBox.SelectedItem = SearchControl.Text.TrimEnd(':');
}
@@ -68,7 +68,7 @@ namespace mpvnet
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
RegistryHelp.SetValue("config-editor-search", SearchControl.Text);
App.Settings.ConfigEditorSearch = SearchControl.Text;
if (InitialContent == GetCompareString())
return;

View File

@@ -28,7 +28,6 @@ namespace mpvnet
int ShownTickCount;
Taskbar Taskbar;
List<string> RecentFiles;
bool WasMaximized;
public MainForm()
@@ -38,9 +37,6 @@ namespace mpvnet
try
{
object recent = RegistryHelp.GetValue("recent");
RecentFiles = recent is string[] r ? new List<string>(r) : new List<string>();
Instance = this;
Hwnd = Handle;
Core.Init();
@@ -100,21 +96,19 @@ namespace mpvnet
if (!Core.Border)
FormBorderStyle = FormBorderStyle.None;
int posX = RegistryHelp.GetInt("position-x");
int posY = RegistryHelp.GetInt("position-y");
Point pos = App.Settings.WindowPosition;
if ((posX != 0 || posY != 0) && App.RememberPosition)
if ((pos.X != 0 || pos.Y != 0) && App.RememberWindowPosition)
{
Left = posX - Width / 2;
Top = posY - Height / 2;
Left = pos.X - Width / 2;
Top = pos.Y - Height / 2;
int horizontal = RegistryHelp.GetInt("location-horizontal");
int vertical = RegistryHelp.GetInt("location-vertical");
Point location = App.Settings.WindowLocation;
if (horizontal == -1) Left = posX;
if (horizontal == 1) Left = posX - Width;
if (vertical == -1) Top = posY;
if (vertical == 1) Top = posY - Height;
if (location.X == -1) Left = pos.X;
if (location.X == 1) Left = pos.X - Width;
if (location.Y == -1) Top = pos.Y;
if (location.Y == 1) Top = pos.Y - Height;
}
if (Core.WindowMaximized)
@@ -273,12 +267,12 @@ namespace mpvnet
{
recent.DropDownItems.Clear();
foreach (string path in RecentFiles)
foreach (string path in App.Settings.RecentFiles)
MenuItem.Add(recent.DropDownItems, path, () => Core.LoadFiles(new[] { path }, true, Control.ModifierKeys.HasFlag(Keys.Control)));
recent.DropDownItems.Add(new ToolStripSeparator());
MenuItem mi = new MenuItem("Clear List");
mi.Action = () => RecentFiles.Clear();
mi.Action = () => App.Settings.RecentFiles.Clear();
recent.DropDownItems.Add(mi);
}
@@ -377,12 +371,11 @@ namespace mpvnet
}
else
{
int savedHeight = RegistryHelp.GetInt("window-height");
int savedWidth = RegistryHelp.GetInt("window-width");
Size windowSize = App.Settings.WindowSize;
if (App.StartSize == "height-always" && savedHeight != 0)
if (App.StartSize == "height-always" && windowSize.Height != 0)
{
height = savedHeight;
height = windowSize.Height;
width = height * videoSize.Width / videoSize.Height;
}
else if (App.StartSize == "height-session")
@@ -390,9 +383,9 @@ namespace mpvnet
height = autoFitHeight;
width = height * videoSize.Width / videoSize.Height;
}
if (App.StartSize == "width-always" && savedHeight != 0)
if (App.StartSize == "width-always" && windowSize.Height != 0)
{
width = savedWidth;
width = windowSize.Width;
height = (int)Math.Ceiling(width * videoSize.Height / (double)videoSize.Width);
}
else if (App.StartSize == "width-session")
@@ -400,10 +393,10 @@ namespace mpvnet
width = autoFitHeight / 9 * 16;
height = (int)Math.Ceiling(width * videoSize.Height / (double)videoSize.Width);
}
else if (App.StartSize == "always" && savedHeight != 0)
else if (App.StartSize == "always" && windowSize.Height != 0)
{
height = savedHeight;
width = savedWidth;
height = windowSize.Height;
width = windowSize.Width;
}
Core.WasInitialSizeSet = true;
@@ -495,7 +488,7 @@ namespace mpvnet
Rectangle workingArea = screen.WorkingArea;
Rectangle rect = new Rectangle(Left - workingArea.X, Top - workingArea.Y, Width, Height);
if (workingArea.Width / (float)Width < 1.2)
if (workingArea.Width / (float)Width < 1.1)
return 0;
if (rect.X * 3 < workingArea.Width - rect.Right)
@@ -512,7 +505,7 @@ namespace mpvnet
Rectangle workingArea = screen.WorkingArea;
Rectangle rect = new Rectangle(Left - workingArea.X, Top - workingArea.Y, Width, Height);
if (workingArea.Height / (float)Height < 1.2)
if (workingArea.Height / (float)Height < 1.1)
return 0;
if (rect.Y * 3 < workingArea.Height - rect.Bottom)
@@ -619,13 +612,13 @@ namespace mpvnet
UpdateProgressBar();
}));
if (RecentFiles.Contains(path))
RecentFiles.Remove(path);
if (App.Settings.RecentFiles.Contains(path))
App.Settings.RecentFiles.Remove(path);
RecentFiles.Insert(0, path);
App.Settings.RecentFiles.Insert(0, path);
while (RecentFiles.Count > App.RecentCount)
RecentFiles.RemoveAt(App.RecentCount);
while (App.Settings.RecentFiles.Count > App.RecentCount)
App.Settings.RecentFiles.RemoveAt(App.RecentCount);
}
void SetTitle() => BeginInvoke(new Action(() => Text = Core.expand(Title)));
@@ -635,32 +628,25 @@ namespace mpvnet
if (WindowState == FormWindowState.Normal)
{
SavePosition();
RegistryHelp.SetInt("window-width", ClientSize.Width);
RegistryHelp.SetInt("window-height", ClientSize.Height);
App.Settings.WindowSize = ClientSize;
}
}
void SavePosition()
{
int posX = Left + Width / 2;
int posY = Top + Height / 2;
Point pos = new Point(Left + Width / 2, Top + Height / 2);
Screen screen = Screen.FromControl(this);
int x = GetHorizontalLocation(screen);
int y = GetVerticalLocation(screen);
if (x == -1) posX = Left;
if (x == 1) posX = Left + Width;
if (y == -1) posY = Top;
if (y == 1) posY = Top + Height;
if (x == -1) pos.X = Left;
if (x == 1) pos.X = Left + Width;
if (y == -1) pos.Y = Top;
if (y == 1) pos.Y = Top + Height;
RegistryHelp.SetInt("position-x", posX);
RegistryHelp.SetInt("position-y", posY);
RegistryHelp.SetInt("location-horizontal", x);
RegistryHelp.SetInt("location-vertical", y);
App.Settings.WindowPosition = pos;
App.Settings.WindowLocation = new Point(x, y);
}
protected override CreateParams CreateParams {
@@ -952,7 +938,6 @@ namespace mpvnet
App.RunTask(() => App.Extension = new Extension());
CSharpScriptHost.ExecuteScriptsInFolder(Core.ConfigFolder + "scripts-cs");
ShownTickCount = Environment.TickCount;
App.ShowSetup();
//if (Debugger.IsAttached)
//{
@@ -1005,7 +990,6 @@ namespace mpvnet
{
base.OnFormClosing(e);
SaveWindowProperties();
RegistryHelp.SetValue("recent", RecentFiles.ToArray());
if (Core.IsQuitNeeded)
Core.commandv("quit");

View File

@@ -189,6 +189,7 @@ public class ToolStripRendererEx : ToolStripSystemRenderer
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
rect = new Rectangle(rect.X + 2, rect.Y, rect.Width - 4, rect.Height - 1);
rect.Inflate(-1, -1);
using (SolidBrush b = new SolidBrush(SelectionColor))
e.Graphics.FillRectangle(b, rect);
}
@@ -205,12 +206,12 @@ public class ToolStripRendererEx : ToolStripSystemRenderer
float y3 = e.Item.Height * 0.75f;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
using (Brush b = new SolidBrush(ForegroundColor))
using (Brush brush = new SolidBrush(ForegroundColor))
{
using (Pen p = new Pen(b, Control.DefaultFont.Height / 20f))
using (Pen pen = new Pen(brush, Control.DefaultFont.Height / 20f))
{
e.Graphics.DrawLine(p, x1, y1, x2, y2);
e.Graphics.DrawLine(p, x2, y2, x3, y3);
e.Graphics.DrawLine(pen, x1, y1, x2, y2);
e.Graphics.DrawLine(pen, x2, y2, x3, y3);
}
}
}
@@ -221,7 +222,6 @@ public class ToolStripRendererEx : ToolStripSystemRenderer
return;
MenuItem item = e.Item as MenuItem;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if (!item.Checked)
@@ -250,6 +250,7 @@ public class ToolStripRendererEx : ToolStripSystemRenderer
int top = e.Item.Height / 2;
top -= 1;
int offset = Convert.ToInt32(e.Item.Font.Height * 0.7);
using (Pen p = new Pen(BorderColor))
e.Graphics.DrawLine(p,
new Point(offset, top),
@@ -298,6 +299,7 @@ public struct HSLColor
value = 0;
else if (value > 1)
value = 1;
return value;
}
@@ -388,9 +390,9 @@ public struct HSLColor
public void SetRGB(int red, int green, int blue)
{
HSLColor hc = HSLColor.Convert(Color.FromArgb(red, green, blue));
HSLColor hc = Convert(Color.FromArgb(red, green, blue));
_Hue = hc._Hue;
_Saturation = hc._Saturation;
_Luminosity = hc._Luminosity;
}
}
}

View File

@@ -114,6 +114,7 @@
<Compile Include="Misc\Help.cs" />
<Compile Include="Misc\GlobalHotkey.cs" />
<Compile Include="Misc\Msg.cs" />
<Compile Include="Misc\Settings.cs" />
<Compile Include="Misc\Terminal.cs" />
<Compile Include="Misc\UpdateCheck.cs" />
<Compile Include="Misc\Theme.cs" />
@@ -141,7 +142,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="mpv\libmpv.cs" />
<Compile Include="Native\libmpv.cs" />
<Compile Include="WinForms\MainForm.cs">
<SubType>Form</SubType>
</Compile>
@@ -149,7 +150,7 @@
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Misc\Misc.cs" />
<Compile Include="mpv\CorePlayer.cs" />
<Compile Include="Misc\CorePlayer.cs" />
<Compile Include="Misc\Commands.cs" />
<Compile Include="Native\Native.cs" />
<Compile Include="Misc\Program.cs" />