Skip to content

Commit

Permalink
lazy load nav button icon
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Jan 31, 2025
1 parent 792a3a7 commit 31e474b
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 107 deletions.
18 changes: 11 additions & 7 deletions Source/Components/ImageGlass.Settings/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,15 +1015,19 @@ public static void Load(IConfigurationRoot? items = null)
#endregion // Other types items


// initialize Magick.NET
PhotoCodec.InitMagickNET();

// listen to system events
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;

// migrate user config file if config version is changed
MigrateUserConfigFile();


Task.Run(() =>
{
// initialize Magick.NET
PhotoCodec.InitMagickNET();

// listen to system events
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
});

#nullable enable
}

Expand Down Expand Up @@ -1574,7 +1578,7 @@ public static void LoadThemePack(bool darkMode, bool useFallBackTheme, bool thro
else LightTheme = th.FolderName;

// load theme settings
BHelper.RunSync(th.LoadThemeSettingsAsync);
th.LoadThemeSettings();

// load theme colors
th.LoadThemeColors();
Expand Down
6 changes: 6 additions & 0 deletions Source/Components/ImageGlass.Settings/Forms/ThemedForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public class ThemedForm : ModernForm

public ThemedForm() : base()
{
}

protected override void OnShown(EventArgs e)
{
base.OnShown(e);

Config.RequestUpdatingColorMode += Config_RequestUpdatingColorMode;
Config.RequestUpdatingTheme += Config_RequestUpdatingTheme;
Config.RequestUpdatingLanguage += Config_RequestUpdatingLanguage;
Expand Down
2 changes: 1 addition & 1 deletion Source/Components/ImageGlass.Settings/Source.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
ImageGlass Project - Image viewer for Windows
Copyright (C) 2010 - 2024 DUONG DIEU PHAP
Project homepage: https://imageglass.org
Expand Down
68 changes: 31 additions & 37 deletions Source/Components/ImageGlass.UI/Themes/IgTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ You should have received a copy of the GNU General Public License
using ImageGlass.Base.Photoing.Codecs;
using ImageGlass.Base.WinApi;
using ImageMagick;
using WicNet;

namespace ImageGlass.UI;

Expand Down Expand Up @@ -236,57 +235,52 @@ public bool ReadThemeConfig(string themeFolderPath)
/// <summary>
/// Loads theme <see cref="Settings"/> from <see cref="JsonModel"/>.
/// </summary>
public async Task LoadThemeSettingsAsync()
public void LoadThemeSettings()
{
if (IsValid is false || JsonModel is null) return;

// dispose the current values
Settings.Dispose();
var navBtnSize = Const.TOOLBAR_ICON_HEIGHT * 4u;


await Parallel.ForEachAsync(JsonModel.Settings, async (item, _) =>
// IsDarkMode
if (JsonModel.Settings.TryGetValue(nameof(IgThemeSettings.IsDarkMode), out var darkModeObj))
{
var value = (item.Value ?? "")?.ToString()?.Trim();
if (string.IsNullOrEmpty(value)) return;
Settings.IsDarkMode = bool.TryParse(darkModeObj?.ToString(), out var darkMode) ? darkMode : false;
}

var prop = Settings.GetType().GetProperty(item.Key);
// PreviewImage
if (JsonModel.Settings.TryGetValue(nameof(IgThemeSettings.PreviewImage), out var previewImageObject))
{
Settings.PreviewImage = previewImageObject?.ToString() ?? "";
}

try
_ = Task.Run(async () =>
{
// NavButtonLeft
if (JsonModel.Settings.TryGetValue(nameof(IgThemeSettings.NavButtonLeft), out var navButtonLeftObject))
{
// property is WicBitmapSource
if (prop?.PropertyType == typeof(WicBitmapSource))
{
var navBtnSize = Const.TOOLBAR_ICON_HEIGHT * 4u;
using var bmp = await PhotoCodec.GetThumbnailAsync(Path.Combine(FolderPath, value), navBtnSize, navBtnSize);
var wicBmp = BHelper.ToWicBitmapSource(bmp);

prop.SetValue(Settings, wicBmp);

return;
}
var iconPath = Path.Combine(FolderPath, navButtonLeftObject?.ToString());
using var bmp = await PhotoCodec.GetThumbnailAsync(iconPath, navBtnSize, navBtnSize);

// property is Bitmap
if (prop?.PropertyType == typeof(Bitmap))
{
var bmp = await PhotoCodec.GetThumbnailAsync(Path.Combine(FolderPath, value), 256, 256);

prop.SetValue(Settings, bmp);
return;
}
Settings.NavButtonLeft = BHelper.ToWicBitmapSource(bmp);
}

// property is String
if (prop?.PropertyType == typeof(String))
{
prop.SetValue(Settings, value ?? string.Empty);
return;
}
// NavButtonRight
if (JsonModel.Settings.TryGetValue(nameof(IgThemeSettings.NavButtonRight), out var navButtonRightObject))
{
var iconPath = Path.Combine(FolderPath, navButtonRightObject?.ToString());
using var bmp = await PhotoCodec.GetThumbnailAsync(iconPath, navBtnSize, navBtnSize);

Settings.NavButtonRight = BHelper.ToWicBitmapSource(bmp);
}

// property is other types
var typedValue = Convert.ChangeType(value, prop?.PropertyType ?? typeof(string));
prop?.SetValue(Settings, typedValue);
// AppLogo
if (JsonModel.Settings.TryGetValue(nameof(IgThemeSettings.AppLogo), out var appLogoObject))
{
var iconPath = Path.Combine(FolderPath, appLogoObject?.ToString());
Settings.AppLogo = await PhotoCodec.GetThumbnailAsync(iconPath, 256, 256);
}
catch { }
});


Expand Down
2 changes: 1 addition & 1 deletion Source/Components/ImageGlass.UI/Themes/IgThemeModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public virtual void Dispose()
/// <summary>
/// Sets, sets app logo
/// </summary>
public Bitmap? AppLogo { get; set; } = Properties.Resources.DefaultLogo;
public Bitmap? AppLogo { get; set; } = null;

/// <summary>
/// The preview image of the theme
Expand Down
42 changes: 8 additions & 34 deletions Source/Components/ImageGlass.Views/ViewerCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public partial class ViewerCanvas : DXCanvas

// checkerboard
private CheckerboardMode _checkerboardMode = CheckerboardMode.None;
private float _checkerboardCellSize = 10f;
private Color _checkerboardColor1 = Color.Black.WithAlpha(25);
private Color _checkerboardColor2 = Color.White.WithAlpha(25);

Expand Down Expand Up @@ -724,26 +723,6 @@ public CheckerboardMode CheckerboardMode
}


[Category("Checkerboard")]
[DefaultValue(typeof(float), "10")]
public float CheckerboardCellSize
{
get => _checkerboardCellSize;
set
{
if (_checkerboardCellSize != value)
{
_checkerboardCellSize = value;

// reset checkerboard brush
DisposeCheckerboardBrushes();

Invalidate();
}
}
}


[Category("Checkerboard")]
[DefaultValue(typeof(Color), "25, 0, 0, 0")]
public Color CheckerboardColor1
Expand Down Expand Up @@ -882,7 +861,7 @@ public Color NavButtonColor
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public SizeF NavButtonSize { get; set; } = new(60f, 60f);
public SizeF NavButtonSize => this.ScaleToDpi(new SizeF(50f, 50f));


/// <summary>
Expand All @@ -896,9 +875,7 @@ public WicBitmapSource? NavLeftImage
{
// _wicNavLeftImage is a ref, do not dispose here
_wicNavLeftImage = value;

DXHelper.DisposeD2D1Bitmap(ref _d2dNavLeftImage);
_d2dNavLeftImage = DXHelper.ToD2D1Bitmap(Device, _wicNavLeftImage);
}
}

Expand All @@ -914,9 +891,7 @@ public WicBitmapSource? NavRightImage
{
// _wicNavRightImage is a ref, do not dispose here
_wicNavRightImage = value;

DXHelper.DisposeD2D1Bitmap(ref _d2dNavRightImage);
_d2dNavRightImage = DXHelper.ToD2D1Bitmap(Device, _wicNavRightImage);
}
}

Expand Down Expand Up @@ -1114,16 +1089,10 @@ protected override void OnDeviceCreated(DeviceCreatedReason reason)
{
base.OnDeviceCreated(reason);

// re-create Direct2D left nav icon
// dispose the Direct2D resource of the old device
DXHelper.DisposeD2D1Bitmap(ref _d2dNavLeftImage);
_d2dNavLeftImage = DXHelper.ToD2D1Bitmap(Device, _wicNavLeftImage);

// re-create Direct2D right nav icon
DXHelper.DisposeD2D1Bitmap(ref _d2dNavRightImage);
_d2dNavRightImage = DXHelper.ToD2D1Bitmap(Device, _wicNavRightImage);


// dispose the Direct2D resource of the old device
if (reason != DeviceCreatedReason.FirstTime)
{
// dispose checkerboard
Expand Down Expand Up @@ -1808,7 +1777,7 @@ protected virtual void DrawCheckerboardLayer(DXGraphics g)


// create bitmap brush
_checkerboardBrushD2D ??= VHelper.CreateCheckerBoxTileD2D(Device, CheckerboardCellSize, CheckerboardColor1, CheckerboardColor2);
_checkerboardBrushD2D ??= VHelper.CreateCheckerBoxTileD2D(Device, this.ScaleToDpi(Const.VIEWER_GRID_SIZE), CheckerboardColor1, CheckerboardColor2);

// draw checkerboard
Device.FillRectangle(DXHelper.ToD2DRectF(region), _checkerboardBrushD2D);
Expand Down Expand Up @@ -2119,6 +2088,8 @@ protected virtual void DrawNavigationLayer(DXGraphics g)
{
if (_navLeftState != DXButtonStates.Normal)
{
_d2dNavLeftImage ??= DXHelper.ToD2D1Bitmap(Device, _wicNavLeftImage);

// draw background
VHelper.DrawDXButton(g,
new RectangleF()
Expand All @@ -2143,6 +2114,9 @@ protected virtual void DrawNavigationLayer(DXGraphics g)
{
if (_navRightState != DXButtonStates.Normal)
{
_d2dNavRightImage ??= DXHelper.ToD2D1Bitmap(Device, _wicNavRightImage);


// draw background
VHelper.DrawDXButton(g,
new RectangleF()
Expand Down
5 changes: 1 addition & 4 deletions Source/ImageGlass/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ protected override void OnDpiChanged()

UpdateGallerySize();

// update picmain scaling
PicMain.NavButtonSize = this.ScaleToDpi(new SizeF(50f, 50f));
PicMain.CheckerboardCellSize = this.ScaleToDpi(Const.VIEWER_GRID_SIZE);

ResumeLayout(true);
}

Expand All @@ -105,6 +101,7 @@ protected override void OnDpiChanged(DpiChangedEventArgs e)
MnuSubMenu.CurrentDpi = e.DeviceDpiNew;
}


protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// to fix arrow keys sometimes does not regconize
Expand Down
19 changes: 9 additions & 10 deletions Source/ImageGlass/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,20 @@ public static bool CheckAndRunQuickSetup()
/// </summary>
private static void CheckAndRunAutoUpdate()
{
if (Config.AutoUpdate != "0")
if (Config.AutoUpdate == "0") return;

if (DateTime.TryParse(Config.AutoUpdate, out var lastUpdate))
{
if (DateTime.TryParse(Config.AutoUpdate, out var lastUpdate))
{
// Check for update every 5 days
if (DateTime.UtcNow.Subtract(lastUpdate).TotalDays > 5)
{
CheckForUpdate(false);
}
}
else
// Check for update every 5 days
if (DateTime.UtcNow.Subtract(lastUpdate).TotalDays > 5)
{
CheckForUpdate(false);
}
}
else
{
CheckForUpdate(false);
}
}


Expand Down
13 changes: 0 additions & 13 deletions Source/igcmd/Tools/FrmSlideshow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,6 @@ protected override void OnRequestUpdatingColorMode(SystemColorModeChangedEventAr
}


protected override void OnDpiChanged()
{
base.OnDpiChanged();
SuspendLayout();

// update picmain scaling
PicMain.NavButtonSize = this.ScaleToDpi(new SizeF(50f, 50f));
PicMain.CheckerboardCellSize = this.ScaleToDpi(Const.VIEWER_GRID_SIZE);

ResumeLayout(false);
}


protected override void OnDpiChanged(DpiChangedEventArgs e)
{
base.OnDpiChanged(e);
Expand Down

0 comments on commit 31e474b

Please sign in to comment.