Skip to content

Commit

Permalink
added new API: IG_ApplySettings
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Nov 16, 2024
1 parent 8581a5d commit 0d37980
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 268 deletions.
4 changes: 3 additions & 1 deletion Source/Components/ImageGlass.Settings/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1622,8 +1622,10 @@ public static void TriggerRequestUpdatingLanguage()
/// <summary>
/// Updates form icon using theme setting.
/// </summary>
public static async Task UpdateFormIcon(Form frm)
public static async Task UpdateFormIcon(Form? frm)
{
if (frm is null) return;

// Icon theming
if (Config.Theme.Settings.AppLogo?.GetHicon() is not IntPtr hIcon) return;
frm.Icon = Icon.FromHandle(hIcon);
Expand Down
4 changes: 2 additions & 2 deletions Source/ImageGlass/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public partial class FrmMain : ThemedForm

// cancellation tokens of synchronious task
private CancellationTokenSource? _loadCancelTokenSrc = new();
private IProgress<ProgressReporterEventArgs> _uiReporter;
private readonly IProgress<ProgressReporterEventArgs> _uiReporter;
private MovableForm? _movableForm;
private bool _isShowingImagePreview;

Expand Down Expand Up @@ -2444,5 +2444,5 @@ private void MnuExit_Click(object sender, EventArgs e)
#endregion // Main Menu component



}
10 changes: 10 additions & 0 deletions Source/ImageGlass/FrmMain/FrmMain.IGMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,16 @@ public void IG_OpenSettings()
}


/// <summary>
/// Parses and applies settings from JSON
/// </summary>
/// <param name="json"></param>
public static void IG_ApplySettings(string json)
{
Local.ApplySettingsFromJson(json);
}


/// <summary>
/// Exits ImageGlass
/// </summary>
Expand Down
266 changes: 1 addition & 265 deletions Source/ImageGlass/FrmSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ You should have received a copy of the GNU General Public License
*/
using Cysharp.Text;
using ImageGlass.Base;
using ImageGlass.Base.PhotoBox;
using ImageGlass.Settings;
using System.Dynamic;

Expand Down Expand Up @@ -376,270 +375,7 @@ protected override async Task OnWeb2MessageReceivedAsync(Web2MessageReceivedEven

private void ApplySettings(string dataJson)
{
var dict = BHelper.ParseJson<ExpandoObject>(dataJson)
.ToDictionary(i => i.Key, i => i.Value.ToString() ?? string.Empty);


var requests = UpdateRequests.None;
var reloadImg = false;
var reloadImgList = false;
var updateSlideshow = false;
var updateToolbarAlignment = false;
var updateToolbarIcons = false;
var updateToolbarButtons = false;
var updateGallery = false;
var updateLanguage = false;
var updateAppearance = false;
var updateTheme = false;
var updateLayout = false;


// Tab General
#region Tab General

_ = Config.SetFromJson(dict, nameof(Config.ShowWelcomeImage));
_ = Config.SetFromJson(dict, nameof(Config.ShouldOpenLastSeenImage));

if (Config.SetFromJson(dict, nameof(Config.EnableRealTimeFileUpdate)).Done)
{
Local.FrmMain.IG_SetRealTimeFileUpdate(Config.EnableRealTimeFileUpdate);
}
_ = Config.SetFromJson(dict, nameof(Config.ShouldAutoOpenNewAddedImage));

_ = Config.SetFromJson(dict, nameof(Config.AutoUpdate));
_ = Config.SetFromJson(dict, nameof(Config.EnableMultiInstances));
if (Config.SetFromJson(dict, nameof(Config.ShowAppIcon)).Done)
{
_ = Config.UpdateFormIcon(this);
_ = Config.UpdateFormIcon(Local.FrmMain);
}
_ = Config.SetFromJson(dict, nameof(Config.InAppMessageDuration));

if (Config.SetFromJson(dict, nameof(Config.ImageInfoTags)).Done)
{
Local.FrmMain.LoadImageInfo();
}

#endregion // Tab General


// Tab Image
#region Tab Image

// Image loading
if (Config.SetFromJson(dict, nameof(Config.ImageLoadingOrder)).Done) { reloadImgList = true; }
if (Config.SetFromJson(dict, nameof(Config.ImageLoadingOrderType)).Done) { reloadImgList = true; }
if (Config.SetFromJson(dict, nameof(Config.ShouldUseExplorerSortOrder)).Done) { reloadImgList = true; }
if (Config.SetFromJson(dict, nameof(Config.EnableRecursiveLoading)).Done) { reloadImgList = true; }
if (Config.SetFromJson(dict, nameof(Config.ShouldGroupImagesByDirectory)).Done) { reloadImgList = true; }
if (Config.SetFromJson(dict, nameof(Config.ShouldLoadHiddenImages)).Done) { reloadImgList = true; }

_ = Config.SetFromJson(dict, nameof(Config.EnableLoopBackNavigation));
_ = Config.SetFromJson(dict, nameof(Config.ShowImagePreview));
_ = Config.SetFromJson(dict, nameof(Config.EnableImageTransition));
_ = Config.SetFromJson(dict, nameof(Config.EnableImageAsyncLoading));

if (Config.SetFromJson(dict, nameof(Config.UseEmbeddedThumbnailRawFormats)).Done) { reloadImg = true; }
if (Config.SetFromJson(dict, nameof(Config.UseEmbeddedThumbnailOtherFormats)).Done) { reloadImg = true; }
if (Config.SetFromJson(dict, nameof(Config.EmbeddedThumbnailMinWidth)).Done) { reloadImg = true; }
if (Config.SetFromJson(dict, nameof(Config.EmbeddedThumbnailMinHeight)).Done) { reloadImg = true; }


// Image booster
if (Config.SetFromJson(dict, nameof(Config.ImageBoosterCacheCount)).Done)
{
Local.Images.MaxQueue = Config.ImageBoosterCacheCount;
}
if (Config.SetFromJson(dict, nameof(Config.ImageBoosterCacheMaxDimension)).Done)
{
Local.Images.MaxImageDimensionToCache = Config.ImageBoosterCacheMaxDimension;
}
if (Config.SetFromJson(dict, nameof(Config.ImageBoosterCacheMaxFileSizeInMb)).Done)
{
Local.Images.MaxFileSizeInMbToCache = Config.ImageBoosterCacheMaxFileSizeInMb;
}


// Color manmagement
if (Config.SetFromJson(dict, nameof(Config.ShouldUseColorProfileForAll)).Done) { reloadImg = true; }
if (Config.SetFromJson(dict, nameof(Config.ColorProfile)).Done) { reloadImg = true; }

#endregion // Tab Image


// Tab Slideshow
#region Tab Slideshow

_ = Config.SetFromJson(dict, nameof(Config.HideMainWindowInSlideshow));
if (Config.SetFromJson(dict, nameof(Config.ShowSlideshowCountdown)).Done) { updateSlideshow = true; }
if (Config.SetFromJson(dict, nameof(Config.EnableLoopSlideshow)).Done) { updateSlideshow = true; }
if (Config.SetFromJson(dict, nameof(Config.EnableFullscreenSlideshow)).Done) { updateSlideshow = true; }
if (Config.SetFromJson(dict, nameof(Config.UseRandomIntervalForSlideshow)).Done) { updateSlideshow = true; }
if (Config.SetFromJson(dict, nameof(Config.SlideshowInterval)).Done) { updateSlideshow = true; }
if (Config.SetFromJson(dict, nameof(Config.SlideshowIntervalTo)).Done) { updateSlideshow = true; }
if (Config.SetFromJson(dict, nameof(Config.SlideshowBackgroundColor)).Done) { updateSlideshow = true; }

if (Config.SetFromJson(dict, nameof(Config.SlideshowImagesToNotifySound)).Done) { updateSlideshow = true; }

#endregion // Tab Slideshow


// Tab Edit
#region Tab Edit

_ = Config.SetFromJson(dict, nameof(Config.ShowDeleteConfirmation));
_ = Config.SetFromJson(dict, nameof(Config.ShowSaveOverrideConfirmation));
_ = Config.SetFromJson(dict, nameof(Config.ShouldPreserveModifiedDate));
_ = Config.SetFromJson(dict, nameof(Config.OpenSaveAsDialogInTheCurrentImageDir));
_ = Config.SetFromJson(dict, nameof(Config.ImageEditQuality));
_ = Config.SetFromJson(dict, nameof(Config.AfterEditingAction));
_ = Config.SetFromJson(dict, nameof(Config.EnableCopyMultipleFiles));
_ = Config.SetFromJson(dict, nameof(Config.EnableCutMultipleFiles));
_ = Config.SetFromJson(dict, nameof(Config.EditApps));

#endregion // Tab Edit


// Tab Viewer
#region Tab Viewer

if (Config.SetFromJson(dict, nameof(Config.CenterWindowFit)).Done)
{
Local.FrmMain.FitWindowToImage();
}

if (Config.SetFromJson(dict, nameof(Config.ShowCheckerboardOnlyImageRegion)).Done)
{
Local.FrmMain.IG_ToggleCheckerboard(Config.ShowCheckerboard);
}
if (Config.SetFromJson(dict, nameof(Config.EnableNavigationButtons)).Done)
{
Local.FrmMain.PicMain.NavDisplay = Config.EnableNavigationButtons
? NavButtonDisplay.Both
: NavButtonDisplay.None;
}
_ = Config.SetFromJson(dict, nameof(Config.UseWebview2ForSvg));
if (Config.SetFromJson(dict, nameof(Config.PanSpeed)).Done)
{
Local.FrmMain.PicMain.PanDistance = Config.PanSpeed;
}
if (Config.SetFromJson(dict, nameof(Config.ImageInterpolationScaleDown)).Done)
{
Local.FrmMain.PicMain.InterpolationScaleDown = Config.ImageInterpolationScaleDown;
}
if (Config.SetFromJson(dict, nameof(Config.ImageInterpolationScaleUp)).Done)
{
Local.FrmMain.PicMain.InterpolationScaleUp = Config.ImageInterpolationScaleUp;
}
if (Config.SetFromJson(dict, nameof(Config.ZoomSpeed)).Done)
{
Local.FrmMain.PicMain.ZoomSpeed = Config.ZoomSpeed;
}
if (Config.SetFromJson(dict, nameof(Config.ZoomLevels)).Done)
{
Local.FrmMain.PicMain.ZoomLevels = Config.ZoomLevels;
}

#endregion // Tab Viewer


// Tab Toolbar
#region Tab Toolbar

_ = Config.SetFromJson(dict, nameof(Config.HideToolbarInFullscreen));
if (Config.SetFromJson(dict, nameof(Config.EnableCenterToolbar)).Done) { updateToolbarAlignment = true; }
if (Config.SetFromJson(dict, nameof(Config.ToolbarIconHeight)).Done) { updateToolbarIcons = true; }
if (Config.SetFromJson(dict, nameof(Config.ToolbarButtons)).Done) { updateToolbarButtons = true; }

#endregion // Tab Toolbar


// Tab Gallery
#region Tab Gallery

_ = Config.SetFromJson(dict, nameof(Config.HideGalleryInFullscreen));
if (Config.SetFromJson(dict, nameof(Config.ShowGalleryScrollbars)).Done) { updateGallery = true; }
if (Config.SetFromJson(dict, nameof(Config.ShowGalleryFileName)).Done) { updateGallery = true; }
if (Config.SetFromJson(dict, nameof(Config.ThumbnailSize)).Done) { updateGallery = true; }
if (Config.SetFromJson(dict, nameof(Config.GalleryCacheSizeInMb)).Done) { updateGallery = true; }
if (Config.SetFromJson(dict, nameof(Config.GalleryColumns)).Done) { updateGallery = true; }

#endregion // Tab Gallery


// Tab Layout
#region Tab Layout
if (Config.SetFromJson(dict, nameof(Config.Layout)).Done) { updateLayout = true; }

#endregion // Tab Layout


// Tab Mouse & Keyboard
#region Tab Mouse & Keyboard

_ = Config.SetFromJson(dict, nameof(Config.MouseWheelActions));

#endregion // Tab Mouse & Keyboard


// Tab File type associations
#region Tab File type associations

_ = Config.SetFromJson(dict, nameof(Config.FileFormats));

#endregion // Tab File type associations


// Tab Tools
#region Tab Tools

if (Config.SetFromJson(dict, nameof(Config.Tools)).Done)
{
// update hotkeys list
FrmMain.CurrentMenuHotkeys = Config.GetAllHotkeys(FrmMain.CurrentMenuHotkeys);

// update extenal tools menu
Local.FrmMain.LoadExternalTools();
}

#endregion // Tab Tools


// Tab Language
#region Tab Language

if (Config.SetFromJson(dict, nameof(Config.Language)).Done)
{
updateLanguage = true;
}

#endregion // Tab Language


// Tab Appearance
#region Tab Appearance

if (Config.SetFromJson(dict, nameof(Config.WindowBackdrop)).Done) { updateAppearance = true; }
if (Config.SetFromJson(dict, nameof(Config.BackgroundColor)).Done) { updateAppearance = true; }
if (Config.SetFromJson(dict, nameof(Config.DarkTheme)).Done) { updateTheme = true; }
if (Config.SetFromJson(dict, nameof(Config.LightTheme)).Done) { updateTheme = true; }

#endregion // Tab Appearance


if (reloadImg) requests |= UpdateRequests.ReloadImage;
if (reloadImgList) requests |= UpdateRequests.ReloadImageList;
if (updateSlideshow) requests |= UpdateRequests.Slideshow;
if (updateToolbarAlignment) requests |= UpdateRequests.ToolbarAlignment;
if (updateToolbarIcons) requests |= UpdateRequests.ToolbarIcons;
if (updateToolbarButtons) requests |= UpdateRequests.ToolbarButtons;
if (updateGallery) requests |= UpdateRequests.Gallery;
if (updateLanguage) requests |= UpdateRequests.Language;
if (updateAppearance) requests |= UpdateRequests.Appearance;
if (updateTheme) requests |= UpdateRequests.Theme;
if (updateLayout) requests |= UpdateRequests.Layout;

Local.UpdateFrmMain(requests, (e) =>
Local.ApplySettingsFromJson(dataJson, (e) =>
{
if (e.HasFlag(UpdateRequests.Theme))
{
Expand Down
Loading

0 comments on commit 0d37980

Please sign in to comment.