1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-29 20:34:05 +02:00

Initial Options dialog refactoring to use an event for restart requests

This commit is contained in:
chylex 2018-07-24 04:04:44 +02:00
parent 8de913172c
commit d83d2660cf
7 changed files with 86 additions and 31 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using TweetDuck.Data.Serialization; using TweetDuck.Data.Serialization;
namespace TweetDuck.Configuration{ namespace TweetDuck.Configuration{
@ -7,11 +8,22 @@ sealed class SystemConfig{
// CONFIGURATION DATA // CONFIGURATION DATA
public bool HardwareAcceleration { get; set; } = true; public bool _hardwareAcceleration = true;
public bool ClearCacheAutomatically { get; set; } = true; public bool ClearCacheAutomatically { get; set; } = true;
public int ClearCacheThreshold { get; set; } = 250; 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 // END OF CONFIG
private readonly string file; private readonly string file;
@ -20,6 +32,13 @@ private SystemConfig(string file){
this.file = 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(){ public void Save(){
try{ try{
Serializer.Write(file, this); Serializer.Write(file, this);

View File

@ -47,19 +47,21 @@ static UserConfig(){
public bool BestImageQuality { get; set; } = true; public bool BestImageQuality { get; set; } = true;
public bool EnableAnimatedImages { get; set; } = true; public bool EnableAnimatedImages { get; set; } = true;
public bool IgnoreTrackingUrlWarning { get; set; } = false; public bool _enableSmoothScrolling = true;
public bool EnableSmoothScrolling { get; set; } = true; public bool _enableTouchAdjustment = false;
public bool EnableTouchAdjustment { get; set; } = false; public string _customCefArgs = null;
public string BrowserPath { get; set; } = null; public string BrowserPath { get; set; } = null;
public bool IgnoreTrackingUrlWarning { get; set; } = false;
public string SearchEngineUrl { get; set; } = null; public string SearchEngineUrl { get; set; } = null;
private int _zoomLevel = 100; private int _zoomLevel = 100;
private bool _muteNotifications;
public int VideoPlayerVolume { get; set; } = 50; public int VideoPlayerVolume { get; set; } = 50;
public bool EnableSpellCheck { get; set; } = false; public bool EnableSpellCheck { get; set; } = false;
public string SpellCheckLanguage { get; set; } = "en-US"; private string _spellCheckLanguage = "en-US";
public string TranslationTarget { get; set; } = "en";
public string TranslationTarget { get; set; } = "en";
private TrayIcon.Behavior _trayBehavior = TrayIcon.Behavior.Disabled; private TrayIcon.Behavior _trayBehavior = TrayIcon.Behavior.Disabled;
public bool EnableTrayHighlight { get; set; } = true; public bool EnableTrayHighlight { get; set; } = true;
@ -85,11 +87,12 @@ static UserConfig(){
public TweetNotification.Size NotificationSize { get; set; } = TweetNotification.Size.Auto; public TweetNotification.Size NotificationSize { get; set; } = TweetNotification.Size.Auto;
public Size CustomNotificationSize { get; set; } = Size.Empty; public Size CustomNotificationSize { get; set; } = Size.Empty;
public int NotificationScrollSpeed { get; set; } = 100; public int NotificationScrollSpeed { get; set; } = 100;
private string _notificationSoundPath; private string _notificationSoundPath;
private int _notificationSoundVolume = 100; private int _notificationSoundVolume = 100;
public string CustomCefArgs { get; set; } = null; private bool _muteNotifications;
public string CustomBrowserCSS { get; set; } = null; public string CustomBrowserCSS { get; set; } = null;
public string CustomNotificationCSS { get; set; } = null; public string CustomNotificationCSS { get; set; } = null;
@ -125,6 +128,26 @@ public TrayIcon.Behavior TrayBehavior{
get => _trayBehavior; get => _trayBehavior;
set => UpdatePropertyWithEvent(ref _trayBehavior, value, TrayBehaviorChanged); 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 // EVENTS
@ -133,6 +156,8 @@ public TrayIcon.Behavior TrayBehavior{
public event EventHandler TrayBehaviorChanged; public event EventHandler TrayBehaviorChanged;
public event EventHandler SoundNotificationChanged; public event EventHandler SoundNotificationChanged;
public event EventHandler ProgramRestartRequested;
// END OF CONFIG // END OF CONFIG
private readonly string file; private readonly string file;

View File

@ -14,16 +14,16 @@
namespace TweetDuck.Core.Other{ namespace TweetDuck.Core.Other{
sealed partial class FormSettings : Form, FormManager.IAppDialog{ sealed partial class FormSettings : Form, FormManager.IAppDialog{
public bool ShouldReloadBrowser { get; private set; }
private readonly FormBrowser browser; private readonly FormBrowser browser;
private readonly PluginManager plugins; private readonly PluginManager plugins;
private readonly int buttonHeight; 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; private SettingsTab currentTab;
public bool ShouldReloadBrowser { get; private set; }
public FormSettings(FormBrowser browser, PluginManager plugins, UpdateHandler updates, AnalyticsManager analytics, Type startTab){ public FormSettings(FormBrowser browser, PluginManager plugins, UpdateHandler updates, AnalyticsManager analytics, Type startTab){
InitializeComponent(); InitializeComponent();
@ -36,6 +36,8 @@ public FormSettings(FormBrowser browser, PluginManager plugins, UpdateHandler up
this.buttonHeight = BrowserUtils.Scale(39, this.GetDPIScale()) | 1; this.buttonHeight = BrowserUtils.Scale(39, this.GetDPIScale()) | 1;
PrepareLoad();
AddButton("General", () => new TabSettingsGeneral(this.browser.ReloadColumns, updates)); AddButton("General", () => new TabSettingsGeneral(this.browser.ReloadColumns, updates));
AddButton("Locales", () => new TabSettingsLocales()); AddButton("Locales", () => new TabSettingsLocales());
AddButton("System Tray", () => new TabSettingsTray()); AddButton("System Tray", () => new TabSettingsTray());
@ -47,8 +49,29 @@ public FormSettings(FormBrowser browser, PluginManager plugins, UpdateHandler up
SelectTab(tabs[startTab ?? typeof(TabSettingsGeneral)]); 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(); 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){ foreach(SettingsTab tab in tabs.Values){
if (tab.IsInitialized){ if (tab.IsInitialized){
@ -56,16 +79,15 @@ private void FormSettings_FormClosing(object sender, FormClosingEventArgs e){
} }
} }
Program.UserConfig.Save();
browser.ResumeNotification(); browser.ResumeNotification();
} }
private void btnManageOptions_Click(object sender, EventArgs e){ private void btnManageOptions_Click(object sender, EventArgs e){
currentTab.Control.OnClosing(); PrepareUnload();
using(DialogSettingsManage dialog = new DialogSettingsManage(plugins)){ using(DialogSettingsManage dialog = new DialogSettingsManage(plugins)){
FormClosing -= FormSettings_FormClosing; FormClosing -= FormSettings_FormClosing;
if (dialog.ShowDialog() == DialogResult.OK){ if (dialog.ShowDialog() == DialogResult.OK){
if (!dialog.IsRestarting){ if (!dialog.IsRestarting){
browser.ResumeNotification(); browser.ResumeNotification();
@ -78,6 +100,7 @@ private void btnManageOptions_Click(object sender, EventArgs e){
} }
else{ else{
FormClosing += FormSettings_FormClosing; FormClosing += FormSettings_FormClosing;
PrepareLoad();
} }
} }
} }

View File

@ -31,11 +31,5 @@ protected BaseTabSettings(){
public virtual void OnReady(){} public virtual void OnReady(){}
public virtual void OnClosing(){} 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();
}
}
} }
} }

View File

@ -64,7 +64,6 @@ public override void OnReady(){
public override void OnClosing(){ public override void OnClosing(){
SysConfig.ClearCacheAutomatically = checkClearCacheAuto.Checked; SysConfig.ClearCacheAutomatically = checkClearCacheAuto.Checked;
SysConfig.ClearCacheThreshold = (int)numClearCacheThreshold.Value; SysConfig.ClearCacheThreshold = (int)numClearCacheThreshold.Value;
SysConfig.Save();
} }
private void btnClearCache_Click(object sender, EventArgs e){ 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){ private void checkHardwareAcceleration_CheckedChanged(object sender, EventArgs e){
SysConfig.HardwareAcceleration = checkHardwareAcceleration.Checked; SysConfig.HardwareAcceleration = checkHardwareAcceleration.Checked;
PromptRestart(); // calls OnClosing
} }
private void btnEditCefArgs_Click(object sender, EventArgs e){ 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){ if (form.DialogResult == DialogResult.OK){
Config.CustomCefArgs = form.CefArgs; Config.CustomCefArgs = form.CefArgs;
PromptRestart();
form.Dispose();
} }
else form.Dispose();
form.Dispose();
}; };
form.Show(ParentForm); form.Show(ParentForm);

View File

@ -120,12 +120,10 @@ private void checkAnimatedAvatars_CheckedChanged(object sender, EventArgs e){
private void checkSmoothScrolling_CheckedChanged(object sender, EventArgs e){ private void checkSmoothScrolling_CheckedChanged(object sender, EventArgs e){
Config.EnableSmoothScrolling = checkSmoothScrolling.Checked; Config.EnableSmoothScrolling = checkSmoothScrolling.Checked;
PromptRestart();
} }
private void checkTouchAdjustment_CheckedChanged(object sender, EventArgs e){ private void checkTouchAdjustment_CheckedChanged(object sender, EventArgs e){
Config.EnableTouchAdjustment = checkTouchAdjustment.Checked; Config.EnableTouchAdjustment = checkTouchAdjustment.Checked;
PromptRestart();
} }
private void UpdateBrowserPathSelection(){ private void UpdateBrowserPathSelection(){

View File

@ -43,7 +43,6 @@ private void checkSpellCheck_CheckedChanged(object sender, EventArgs e){
private void comboBoxSpellCheckLanguage_SelectedValueChanged(object sender, EventArgs e){ private void comboBoxSpellCheckLanguage_SelectedValueChanged(object sender, EventArgs e){
Config.SpellCheckLanguage = (comboBoxSpellCheckLanguage.SelectedItem as LocaleUtils.Item)?.Code ?? "en-US"; Config.SpellCheckLanguage = (comboBoxSpellCheckLanguage.SelectedItem as LocaleUtils.Item)?.Code ?? "en-US";
PromptRestart();
} }
private void comboBoxTranslationTarget_SelectedValueChanged(object sender, EventArgs e){ private void comboBoxTranslationTarget_SelectedValueChanged(object sender, EventArgs e){