Skip to content

Commit 31e474b

Browse files
committed
lazy load nav button icon
1 parent 792a3a7 commit 31e474b

File tree

9 files changed

+68
-107
lines changed

9 files changed

+68
-107
lines changed

Source/Components/ImageGlass.Settings/Config.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,15 +1015,19 @@ public static void Load(IConfigurationRoot? items = null)
10151015
#endregion // Other types items
10161016

10171017

1018-
// initialize Magick.NET
1019-
PhotoCodec.InitMagickNET();
1020-
1021-
// listen to system events
1022-
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
1023-
10241018
// migrate user config file if config version is changed
10251019
MigrateUserConfigFile();
10261020

1021+
1022+
Task.Run(() =>
1023+
{
1024+
// initialize Magick.NET
1025+
PhotoCodec.InitMagickNET();
1026+
1027+
// listen to system events
1028+
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
1029+
});
1030+
10271031
#nullable enable
10281032
}
10291033

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

15761580
// load theme settings
1577-
BHelper.RunSync(th.LoadThemeSettingsAsync);
1581+
th.LoadThemeSettings();
15781582

15791583
// load theme colors
15801584
th.LoadThemeColors();

Source/Components/ImageGlass.Settings/Forms/ThemedForm.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public class ThemedForm : ModernForm
4848

4949
public ThemedForm() : base()
5050
{
51+
}
52+
53+
protected override void OnShown(EventArgs e)
54+
{
55+
base.OnShown(e);
56+
5157
Config.RequestUpdatingColorMode += Config_RequestUpdatingColorMode;
5258
Config.RequestUpdatingTheme += Config_RequestUpdatingTheme;
5359
Config.RequestUpdatingLanguage += Config_RequestUpdatingLanguage;

Source/Components/ImageGlass.Settings/Source.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
ImageGlass Project - Image viewer for Windows
33
Copyright (C) 2010 - 2024 DUONG DIEU PHAP
44
Project homepage: https://imageglass.org

Source/Components/ImageGlass.UI/Themes/IgTheme.cs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ You should have received a copy of the GNU General Public License
2020
using ImageGlass.Base.Photoing.Codecs;
2121
using ImageGlass.Base.WinApi;
2222
using ImageMagick;
23-
using WicNet;
2423

2524
namespace ImageGlass.UI;
2625

@@ -236,57 +235,52 @@ public bool ReadThemeConfig(string themeFolderPath)
236235
/// <summary>
237236
/// Loads theme <see cref="Settings"/> from <see cref="JsonModel"/>.
238237
/// </summary>
239-
public async Task LoadThemeSettingsAsync()
238+
public void LoadThemeSettings()
240239
{
241240
if (IsValid is false || JsonModel is null) return;
242241

243242
// dispose the current values
244243
Settings.Dispose();
244+
var navBtnSize = Const.TOOLBAR_ICON_HEIGHT * 4u;
245245

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

252-
var prop = Settings.GetType().GetProperty(item.Key);
252+
// PreviewImage
253+
if (JsonModel.Settings.TryGetValue(nameof(IgThemeSettings.PreviewImage), out var previewImageObject))
254+
{
255+
Settings.PreviewImage = previewImageObject?.ToString() ?? "";
256+
}
253257

254-
try
258+
_ = Task.Run(async () =>
259+
{
260+
// NavButtonLeft
261+
if (JsonModel.Settings.TryGetValue(nameof(IgThemeSettings.NavButtonLeft), out var navButtonLeftObject))
255262
{
256-
// property is WicBitmapSource
257-
if (prop?.PropertyType == typeof(WicBitmapSource))
258-
{
259-
var navBtnSize = Const.TOOLBAR_ICON_HEIGHT * 4u;
260-
using var bmp = await PhotoCodec.GetThumbnailAsync(Path.Combine(FolderPath, value), navBtnSize, navBtnSize);
261-
var wicBmp = BHelper.ToWicBitmapSource(bmp);
262-
263-
prop.SetValue(Settings, wicBmp);
264-
265-
return;
266-
}
263+
var iconPath = Path.Combine(FolderPath, navButtonLeftObject?.ToString());
264+
using var bmp = await PhotoCodec.GetThumbnailAsync(iconPath, navBtnSize, navBtnSize);
267265

268-
// property is Bitmap
269-
if (prop?.PropertyType == typeof(Bitmap))
270-
{
271-
var bmp = await PhotoCodec.GetThumbnailAsync(Path.Combine(FolderPath, value), 256, 256);
272-
273-
prop.SetValue(Settings, bmp);
274-
return;
275-
}
266+
Settings.NavButtonLeft = BHelper.ToWicBitmapSource(bmp);
267+
}
276268

277-
// property is String
278-
if (prop?.PropertyType == typeof(String))
279-
{
280-
prop.SetValue(Settings, value ?? string.Empty);
281-
return;
282-
}
269+
// NavButtonRight
270+
if (JsonModel.Settings.TryGetValue(nameof(IgThemeSettings.NavButtonRight), out var navButtonRightObject))
271+
{
272+
var iconPath = Path.Combine(FolderPath, navButtonRightObject?.ToString());
273+
using var bmp = await PhotoCodec.GetThumbnailAsync(iconPath, navBtnSize, navBtnSize);
283274

275+
Settings.NavButtonRight = BHelper.ToWicBitmapSource(bmp);
276+
}
284277

285-
// property is other types
286-
var typedValue = Convert.ChangeType(value, prop?.PropertyType ?? typeof(string));
287-
prop?.SetValue(Settings, typedValue);
278+
// AppLogo
279+
if (JsonModel.Settings.TryGetValue(nameof(IgThemeSettings.AppLogo), out var appLogoObject))
280+
{
281+
var iconPath = Path.Combine(FolderPath, appLogoObject?.ToString());
282+
Settings.AppLogo = await PhotoCodec.GetThumbnailAsync(iconPath, 256, 256);
288283
}
289-
catch { }
290284
});
291285

292286

Source/Components/ImageGlass.UI/Themes/IgThemeModels.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public virtual void Dispose()
157157
/// <summary>
158158
/// Sets, sets app logo
159159
/// </summary>
160-
public Bitmap? AppLogo { get; set; } = Properties.Resources.DefaultLogo;
160+
public Bitmap? AppLogo { get; set; } = null;
161161

162162
/// <summary>
163163
/// The preview image of the theme

Source/Components/ImageGlass.Views/ViewerCanvas.cs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public partial class ViewerCanvas : DXCanvas
102102

103103
// checkerboard
104104
private CheckerboardMode _checkerboardMode = CheckerboardMode.None;
105-
private float _checkerboardCellSize = 10f;
106105
private Color _checkerboardColor1 = Color.Black.WithAlpha(25);
107106
private Color _checkerboardColor2 = Color.White.WithAlpha(25);
108107

@@ -724,26 +723,6 @@ public CheckerboardMode CheckerboardMode
724723
}
725724

726725

727-
[Category("Checkerboard")]
728-
[DefaultValue(typeof(float), "10")]
729-
public float CheckerboardCellSize
730-
{
731-
get => _checkerboardCellSize;
732-
set
733-
{
734-
if (_checkerboardCellSize != value)
735-
{
736-
_checkerboardCellSize = value;
737-
738-
// reset checkerboard brush
739-
DisposeCheckerboardBrushes();
740-
741-
Invalidate();
742-
}
743-
}
744-
}
745-
746-
747726
[Category("Checkerboard")]
748727
[DefaultValue(typeof(Color), "25, 0, 0, 0")]
749728
public Color CheckerboardColor1
@@ -882,7 +861,7 @@ public Color NavButtonColor
882861
/// </summary>
883862
[Browsable(false)]
884863
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
885-
public SizeF NavButtonSize { get; set; } = new(60f, 60f);
864+
public SizeF NavButtonSize => this.ScaleToDpi(new SizeF(50f, 50f));
886865

887866

888867
/// <summary>
@@ -896,9 +875,7 @@ public WicBitmapSource? NavLeftImage
896875
{
897876
// _wicNavLeftImage is a ref, do not dispose here
898877
_wicNavLeftImage = value;
899-
900878
DXHelper.DisposeD2D1Bitmap(ref _d2dNavLeftImage);
901-
_d2dNavLeftImage = DXHelper.ToD2D1Bitmap(Device, _wicNavLeftImage);
902879
}
903880
}
904881

@@ -914,9 +891,7 @@ public WicBitmapSource? NavRightImage
914891
{
915892
// _wicNavRightImage is a ref, do not dispose here
916893
_wicNavRightImage = value;
917-
918894
DXHelper.DisposeD2D1Bitmap(ref _d2dNavRightImage);
919-
_d2dNavRightImage = DXHelper.ToD2D1Bitmap(Device, _wicNavRightImage);
920895
}
921896
}
922897

@@ -1114,16 +1089,10 @@ protected override void OnDeviceCreated(DeviceCreatedReason reason)
11141089
{
11151090
base.OnDeviceCreated(reason);
11161091

1117-
// re-create Direct2D left nav icon
1092+
// dispose the Direct2D resource of the old device
11181093
DXHelper.DisposeD2D1Bitmap(ref _d2dNavLeftImage);
1119-
_d2dNavLeftImage = DXHelper.ToD2D1Bitmap(Device, _wicNavLeftImage);
1120-
1121-
// re-create Direct2D right nav icon
11221094
DXHelper.DisposeD2D1Bitmap(ref _d2dNavRightImage);
1123-
_d2dNavRightImage = DXHelper.ToD2D1Bitmap(Device, _wicNavRightImage);
1124-
11251095

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

18091778

18101779
// create bitmap brush
1811-
_checkerboardBrushD2D ??= VHelper.CreateCheckerBoxTileD2D(Device, CheckerboardCellSize, CheckerboardColor1, CheckerboardColor2);
1780+
_checkerboardBrushD2D ??= VHelper.CreateCheckerBoxTileD2D(Device, this.ScaleToDpi(Const.VIEWER_GRID_SIZE), CheckerboardColor1, CheckerboardColor2);
18121781

18131782
// draw checkerboard
18141783
Device.FillRectangle(DXHelper.ToD2DRectF(region), _checkerboardBrushD2D);
@@ -2119,6 +2088,8 @@ protected virtual void DrawNavigationLayer(DXGraphics g)
21192088
{
21202089
if (_navLeftState != DXButtonStates.Normal)
21212090
{
2091+
_d2dNavLeftImage ??= DXHelper.ToD2D1Bitmap(Device, _wicNavLeftImage);
2092+
21222093
// draw background
21232094
VHelper.DrawDXButton(g,
21242095
new RectangleF()
@@ -2143,6 +2114,9 @@ protected virtual void DrawNavigationLayer(DXGraphics g)
21432114
{
21442115
if (_navRightState != DXButtonStates.Normal)
21452116
{
2117+
_d2dNavRightImage ??= DXHelper.ToD2D1Bitmap(Device, _wicNavRightImage);
2118+
2119+
21462120
// draw background
21472121
VHelper.DrawDXButton(g,
21482122
new RectangleF()

Source/ImageGlass/FrmMain.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ protected override void OnDpiChanged()
8989

9090
UpdateGallerySize();
9191

92-
// update picmain scaling
93-
PicMain.NavButtonSize = this.ScaleToDpi(new SizeF(50f, 50f));
94-
PicMain.CheckerboardCellSize = this.ScaleToDpi(Const.VIEWER_GRID_SIZE);
95-
9692
ResumeLayout(true);
9793
}
9894

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

104+
108105
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
109106
{
110107
// to fix arrow keys sometimes does not regconize

Source/ImageGlass/Program.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,20 @@ public static bool CheckAndRunQuickSetup()
130130
/// </summary>
131131
private static void CheckAndRunAutoUpdate()
132132
{
133-
if (Config.AutoUpdate != "0")
133+
if (Config.AutoUpdate == "0") return;
134+
135+
if (DateTime.TryParse(Config.AutoUpdate, out var lastUpdate))
134136
{
135-
if (DateTime.TryParse(Config.AutoUpdate, out var lastUpdate))
136-
{
137-
// Check for update every 5 days
138-
if (DateTime.UtcNow.Subtract(lastUpdate).TotalDays > 5)
139-
{
140-
CheckForUpdate(false);
141-
}
142-
}
143-
else
137+
// Check for update every 5 days
138+
if (DateTime.UtcNow.Subtract(lastUpdate).TotalDays > 5)
144139
{
145140
CheckForUpdate(false);
146141
}
147142
}
143+
else
144+
{
145+
CheckForUpdate(false);
146+
}
148147
}
149148

150149

Source/igcmd/Tools/FrmSlideshow.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,6 @@ protected override void OnRequestUpdatingColorMode(SystemColorModeChangedEventAr
301301
}
302302

303303

304-
protected override void OnDpiChanged()
305-
{
306-
base.OnDpiChanged();
307-
SuspendLayout();
308-
309-
// update picmain scaling
310-
PicMain.NavButtonSize = this.ScaleToDpi(new SizeF(50f, 50f));
311-
PicMain.CheckerboardCellSize = this.ScaleToDpi(Const.VIEWER_GRID_SIZE);
312-
313-
ResumeLayout(false);
314-
}
315-
316-
317304
protected override void OnDpiChanged(DpiChangedEventArgs e)
318305
{
319306
base.OnDpiChanged(e);

0 commit comments

Comments
 (0)