mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-08 20:34:05 +02:00
Initial Options dialog refactoring to use an event for restart requests
This commit is contained in:
parent
8de913172c
commit
d83d2660cf
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TweetDuck.Data.Serialization;
|
||||
|
||||
namespace TweetDuck.Configuration{
|
||||
@ -7,11 +8,22 @@ sealed class SystemConfig{
|
||||
|
||||
// CONFIGURATION DATA
|
||||
|
||||
public bool HardwareAcceleration { get; set; } = true;
|
||||
public bool _hardwareAcceleration = true;
|
||||
|
||||
public bool ClearCacheAutomatically { get; set; } = true;
|
||||
public int ClearCacheThreshold { get; set; } = 250;
|
||||
|
||||
// SPECIAL PROPERTIES
|
||||
|
||||
public bool HardwareAcceleration{
|
||||
get => _hardwareAcceleration;
|
||||
set => UpdatePropertyWithEvent(ref _hardwareAcceleration, value, ProgramRestartRequested);
|
||||
}
|
||||
|
||||
// EVENTS
|
||||
|
||||
public event EventHandler ProgramRestartRequested;
|
||||
|
||||
// END OF CONFIG
|
||||
|
||||
private readonly string file;
|
||||
@ -20,6 +32,13 @@ private SystemConfig(string file){
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
private void UpdatePropertyWithEvent<T>(ref T field, T value, EventHandler eventHandler){
|
||||
if (!EqualityComparer<T>.Default.Equals(field, value)){
|
||||
field = value;
|
||||
eventHandler?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public void Save(){
|
||||
try{
|
||||
Serializer.Write(file, this);
|
||||
|
@ -47,19 +47,21 @@ static UserConfig(){
|
||||
public bool BestImageQuality { get; set; } = true;
|
||||
public bool EnableAnimatedImages { get; set; } = true;
|
||||
|
||||
public bool IgnoreTrackingUrlWarning { get; set; } = false;
|
||||
public bool EnableSmoothScrolling { get; set; } = true;
|
||||
public bool EnableTouchAdjustment { get; set; } = false;
|
||||
public bool _enableSmoothScrolling = true;
|
||||
public bool _enableTouchAdjustment = false;
|
||||
public string _customCefArgs = null;
|
||||
|
||||
public string BrowserPath { get; set; } = null;
|
||||
public bool IgnoreTrackingUrlWarning { get; set; } = false;
|
||||
public string SearchEngineUrl { get; set; } = null;
|
||||
private int _zoomLevel = 100;
|
||||
private bool _muteNotifications;
|
||||
|
||||
public int VideoPlayerVolume { get; set; } = 50;
|
||||
|
||||
public bool EnableSpellCheck { get; set; } = false;
|
||||
public string SpellCheckLanguage { get; set; } = "en-US";
|
||||
public string TranslationTarget { get; set; } = "en";
|
||||
public bool EnableSpellCheck { get; set; } = false;
|
||||
private string _spellCheckLanguage = "en-US";
|
||||
|
||||
public string TranslationTarget { get; set; } = "en";
|
||||
|
||||
private TrayIcon.Behavior _trayBehavior = TrayIcon.Behavior.Disabled;
|
||||
public bool EnableTrayHighlight { get; set; } = true;
|
||||
@ -85,11 +87,12 @@ static UserConfig(){
|
||||
public TweetNotification.Size NotificationSize { get; set; } = TweetNotification.Size.Auto;
|
||||
public Size CustomNotificationSize { get; set; } = Size.Empty;
|
||||
public int NotificationScrollSpeed { get; set; } = 100;
|
||||
|
||||
|
||||
private string _notificationSoundPath;
|
||||
private int _notificationSoundVolume = 100;
|
||||
|
||||
public string CustomCefArgs { get; set; } = null;
|
||||
private bool _muteNotifications;
|
||||
|
||||
public string CustomBrowserCSS { get; set; } = null;
|
||||
public string CustomNotificationCSS { get; set; } = null;
|
||||
|
||||
@ -125,6 +128,26 @@ public TrayIcon.Behavior TrayBehavior{
|
||||
get => _trayBehavior;
|
||||
set => UpdatePropertyWithEvent(ref _trayBehavior, value, TrayBehaviorChanged);
|
||||
}
|
||||
|
||||
public bool EnableSmoothScrolling{
|
||||
get => _enableSmoothScrolling;
|
||||
set => UpdatePropertyWithEvent(ref _enableSmoothScrolling, value, ProgramRestartRequested);
|
||||
}
|
||||
|
||||
public bool EnableTouchAdjustment{
|
||||
get => _enableTouchAdjustment;
|
||||
set => UpdatePropertyWithEvent(ref _enableTouchAdjustment, value, ProgramRestartRequested);
|
||||
}
|
||||
|
||||
public string CustomCefArgs{
|
||||
get => _customCefArgs;
|
||||
set => UpdatePropertyWithEvent(ref _customCefArgs, value, ProgramRestartRequested);
|
||||
}
|
||||
|
||||
public string SpellCheckLanguage{
|
||||
get => _spellCheckLanguage;
|
||||
set => UpdatePropertyWithEvent(ref _spellCheckLanguage, value, ProgramRestartRequested);
|
||||
}
|
||||
|
||||
// EVENTS
|
||||
|
||||
@ -133,6 +156,8 @@ public TrayIcon.Behavior TrayBehavior{
|
||||
public event EventHandler TrayBehaviorChanged;
|
||||
public event EventHandler SoundNotificationChanged;
|
||||
|
||||
public event EventHandler ProgramRestartRequested;
|
||||
|
||||
// END OF CONFIG
|
||||
|
||||
private readonly string file;
|
||||
|
@ -14,16 +14,16 @@
|
||||
|
||||
namespace TweetDuck.Core.Other{
|
||||
sealed partial class FormSettings : Form, FormManager.IAppDialog{
|
||||
public bool ShouldReloadBrowser { get; private set; }
|
||||
|
||||
private readonly FormBrowser browser;
|
||||
private readonly PluginManager plugins;
|
||||
|
||||
private readonly int buttonHeight;
|
||||
|
||||
private readonly Dictionary<Type, SettingsTab> tabs = new Dictionary<Type, SettingsTab>(4);
|
||||
private readonly Dictionary<Type, SettingsTab> tabs = new Dictionary<Type, SettingsTab>(8);
|
||||
private SettingsTab currentTab;
|
||||
|
||||
public bool ShouldReloadBrowser { get; private set; }
|
||||
|
||||
public FormSettings(FormBrowser browser, PluginManager plugins, UpdateHandler updates, AnalyticsManager analytics, Type startTab){
|
||||
InitializeComponent();
|
||||
|
||||
@ -36,6 +36,8 @@ public FormSettings(FormBrowser browser, PluginManager plugins, UpdateHandler up
|
||||
|
||||
this.buttonHeight = BrowserUtils.Scale(39, this.GetDPIScale()) | 1;
|
||||
|
||||
PrepareLoad();
|
||||
|
||||
AddButton("General", () => new TabSettingsGeneral(this.browser.ReloadColumns, updates));
|
||||
AddButton("Locales", () => new TabSettingsLocales());
|
||||
AddButton("System Tray", () => new TabSettingsTray());
|
||||
@ -47,8 +49,29 @@ public FormSettings(FormBrowser browser, PluginManager plugins, UpdateHandler up
|
||||
SelectTab(tabs[startTab ?? typeof(TabSettingsGeneral)]);
|
||||
}
|
||||
|
||||
private void FormSettings_FormClosing(object sender, FormClosingEventArgs e){
|
||||
private void PrepareLoad(){
|
||||
Program.UserConfig.ProgramRestartRequested += Config_ProgramRestartRequested;
|
||||
Program.SystemConfig.ProgramRestartRequested += Config_ProgramRestartRequested;
|
||||
}
|
||||
|
||||
private void PrepareUnload(){ // TODO refactor this further later
|
||||
currentTab.Control.OnClosing();
|
||||
|
||||
Program.UserConfig.ProgramRestartRequested -= Config_ProgramRestartRequested;
|
||||
Program.SystemConfig.ProgramRestartRequested -= Config_ProgramRestartRequested;
|
||||
|
||||
Program.UserConfig.Save();
|
||||
Program.SystemConfig.Save();
|
||||
}
|
||||
|
||||
private void Config_ProgramRestartRequested(object sender, EventArgs e){
|
||||
if (FormMessage.Information("TweetDuck Options", "The application must restart for the option to take place. Do you want to restart now?", FormMessage.Yes, FormMessage.No)){
|
||||
Program.Restart();
|
||||
}
|
||||
}
|
||||
|
||||
private void FormSettings_FormClosing(object sender, FormClosingEventArgs e){
|
||||
PrepareUnload();
|
||||
|
||||
foreach(SettingsTab tab in tabs.Values){
|
||||
if (tab.IsInitialized){
|
||||
@ -56,16 +79,15 @@ private void FormSettings_FormClosing(object sender, FormClosingEventArgs e){
|
||||
}
|
||||
}
|
||||
|
||||
Program.UserConfig.Save();
|
||||
browser.ResumeNotification();
|
||||
}
|
||||
|
||||
private void btnManageOptions_Click(object sender, EventArgs e){
|
||||
currentTab.Control.OnClosing();
|
||||
PrepareUnload();
|
||||
|
||||
using(DialogSettingsManage dialog = new DialogSettingsManage(plugins)){
|
||||
FormClosing -= FormSettings_FormClosing;
|
||||
|
||||
|
||||
if (dialog.ShowDialog() == DialogResult.OK){
|
||||
if (!dialog.IsRestarting){
|
||||
browser.ResumeNotification();
|
||||
@ -78,6 +100,7 @@ private void btnManageOptions_Click(object sender, EventArgs e){
|
||||
}
|
||||
else{
|
||||
FormClosing += FormSettings_FormClosing;
|
||||
PrepareLoad();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,11 +31,5 @@ protected BaseTabSettings(){
|
||||
|
||||
public virtual void OnReady(){}
|
||||
public virtual void OnClosing(){}
|
||||
|
||||
protected static void PromptRestart(){
|
||||
if (FormMessage.Information("TweetDuck Options", "The application must restart for the option to take place. Do you want to restart now?", FormMessage.Yes, FormMessage.No)){
|
||||
Program.Restart();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -64,7 +64,6 @@ public override void OnReady(){
|
||||
public override void OnClosing(){
|
||||
SysConfig.ClearCacheAutomatically = checkClearCacheAuto.Checked;
|
||||
SysConfig.ClearCacheThreshold = (int)numClearCacheThreshold.Value;
|
||||
SysConfig.Save();
|
||||
}
|
||||
|
||||
private void btnClearCache_Click(object sender, EventArgs e){
|
||||
@ -79,7 +78,6 @@ private void checkClearCacheAuto_CheckedChanged(object sender, EventArgs e){
|
||||
|
||||
private void checkHardwareAcceleration_CheckedChanged(object sender, EventArgs e){
|
||||
SysConfig.HardwareAcceleration = checkHardwareAcceleration.Checked;
|
||||
PromptRestart(); // calls OnClosing
|
||||
}
|
||||
|
||||
private void btnEditCefArgs_Click(object sender, EventArgs e){
|
||||
@ -94,10 +92,9 @@ private void btnEditCefArgs_Click(object sender, EventArgs e){
|
||||
|
||||
if (form.DialogResult == DialogResult.OK){
|
||||
Config.CustomCefArgs = form.CefArgs;
|
||||
PromptRestart();
|
||||
form.Dispose();
|
||||
}
|
||||
else form.Dispose();
|
||||
|
||||
form.Dispose();
|
||||
};
|
||||
|
||||
form.Show(ParentForm);
|
||||
|
@ -120,12 +120,10 @@ private void checkAnimatedAvatars_CheckedChanged(object sender, EventArgs e){
|
||||
|
||||
private void checkSmoothScrolling_CheckedChanged(object sender, EventArgs e){
|
||||
Config.EnableSmoothScrolling = checkSmoothScrolling.Checked;
|
||||
PromptRestart();
|
||||
}
|
||||
|
||||
private void checkTouchAdjustment_CheckedChanged(object sender, EventArgs e){
|
||||
Config.EnableTouchAdjustment = checkTouchAdjustment.Checked;
|
||||
PromptRestart();
|
||||
}
|
||||
|
||||
private void UpdateBrowserPathSelection(){
|
||||
|
@ -43,7 +43,6 @@ private void checkSpellCheck_CheckedChanged(object sender, EventArgs e){
|
||||
|
||||
private void comboBoxSpellCheckLanguage_SelectedValueChanged(object sender, EventArgs e){
|
||||
Config.SpellCheckLanguage = (comboBoxSpellCheckLanguage.SelectedItem as LocaleUtils.Item)?.Code ?? "en-US";
|
||||
PromptRestart();
|
||||
}
|
||||
|
||||
private void comboBoxTranslationTarget_SelectedValueChanged(object sender, EventArgs e){
|
||||
|
Loading…
Reference in New Issue
Block a user