Skip to content

Commit

Permalink
Fix enabled state bug with readme when theme changes while disabled
Browse files Browse the repository at this point in the history
If theme changed to dark mode, readme would go into light mode disabled state and wouldn't come out of it.
  • Loading branch information
FenPhoenix committed Mar 10, 2024
1 parent 1ef588f commit 81e16d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal void SetFontType(bool useFixed)
SaveZoom();
this.SuspendDrawing();

Font = useFixed ? MonospaceFont : DefaultFont;
SetFontProperty(useFixed);

string savedText = Text;

Expand All @@ -128,6 +128,8 @@ internal void SetFontType(bool useFixed)
}
}

private void SetFontProperty(bool useFixed) => Font = useFixed ? MonospaceFont : DefaultFont;

private void SuspendState(bool toggleReadOnly)
{
SaveZoom();
Expand Down Expand Up @@ -462,9 +464,34 @@ should be indication enough that something failed.

protected override void OnEnabledChanged(EventArgs e)
{
/*
Slightly more complicated system to handle the theme changing while disabled (can happen when following
system theme). Otherwise, in this scenario, the dark mode readme turns light-mode-disabled and doesn't
re-enable again.
*/
if (Enabled || _changedThemeWhileDisabled)
{
if (Enabled && _changedThemeWhileDisabled && _currentReadmeType == ReadmeType.PlainText)
{
Native.SCROLLINFO si = ControlUtils.GetCurrentScrollInfo(Handle, Native.SB_VERT);
try
{
// Force the disabled colors back to normal (otherwise you have to select an RTF readme to
// get the normal disabled colors back)
ResetFont();
SetFontProperty(_useFixedFont);
}
finally
{
ControlUtils.RepositionScroll(Handle, si, Native.SB_VERT);
}
}
_changedThemeWhileDisabled = false;
base.OnEnabledChanged(e);
return;
}

if (!_darkModeEnabled) base.OnEnabledChanged(e);
// Just suppress the base method call and done, no recoloring. Argh! I totally didn't make a huge
// ridiculous system for getting around it! I totally knew all along!
}

protected override void OnVScroll(EventArgs e)
Expand Down
9 changes: 9 additions & 0 deletions AngelLoader/Forms/CustomControls/RichTextBoxCustom/Theming.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using AngelLoader.DataClasses;
using AngelLoader.Forms.WinFormsNative;
Expand All @@ -11,6 +12,8 @@ namespace AngelLoader.Forms.CustomControls;

internal sealed partial class RichTextBoxCustom
{
private bool _changedThemeWhileDisabled;

private bool _darkModeEnabled;
[PublicAPI]
[Browsable(false)]
Expand All @@ -26,6 +29,12 @@ public bool DarkModeEnabled
// Perf: Don't load readme twice on startup, and don't load it again if we're on HTML or no FM
// selected or whatever
if (Visible) RefreshDarkModeState(preProcessedRtf: null, skipSuspend: false);

if (Visible && !Enabled)
{
_changedThemeWhileDisabled = true;
Native.EnableWindow(new HandleRef(this, Handle), _darkModeEnabled);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions AngelLoader/Forms/WinFormsNative/WinFormsNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,4 +956,7 @@ internal struct ICONINFO
internal static extern bool DeleteObject(IntPtr handle);

#endregion

[DllImport("user32.dll")]
internal static extern bool EnableWindow(HandleRef hWnd, bool enable);
}

0 comments on commit 81e16d6

Please sign in to comment.