Skip to content

Commit

Permalink
Merge pull request #871 from cjee21/dark-mode
Browse files Browse the repository at this point in the history
Windows GUI: Implement theme preferences
  • Loading branch information
JeromeMartinez authored Jun 18, 2024
2 parents 9d78f50 + bf2574b commit f6b9e72
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 86 deletions.
1 change: 1 addition & 0 deletions Source/Common/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ int Preferences::Config_Save()
if (Config(__T("Donated")).empty()) Config(__T("Donated"))=__T("0");
if (Config(__T("Donate_Display")).empty()) Config(__T("Donate_Display"))=__T("1");
if (Config(__T("Sponsored")).empty()) Config(__T("Sponsored"))=__T("0");
if (Config(__T("Theme")).empty()) Config(__T("Theme"))=__T("0");

HANDLE Temp=CreateFile((BaseFolder+__T("MediaInfo.cfg")).c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (Temp==INVALID_HANDLE_VALUE)
Expand Down
193 changes: 112 additions & 81 deletions Source/GUI/VCL/GUI_Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,37 @@ bool __fastcall TMainF::WindowsDarkModeEnabled()
return DarkModeEnabled;
}

void __fastcall TMainF::ConfigTheme()
{
switch(Prefs->Config(__T("Theme")).To_int32s()) {
case 0:
M_Options_Theme_System->Checked = true;
if (WindowsDarkModeEnabled()) {
TStyleManager::TrySetStyle(DARK_MODE_STYLE, false);
} else {
TStyleManager::TrySetStyle(LIGHT_MODE_STYLE, false);
}
break;
case 1:
M_Options_Theme_Light->Checked = true;
TStyleManager::TrySetStyle(LIGHT_MODE_STYLE, false);
break;
case 2:
M_Options_Theme_Dark->Checked = true;
TStyleManager::TrySetStyle(DARK_MODE_STYLE, false);
break;
default:
Prefs->Config(__T("Theme")) = __T("0");
M_Options_Theme_System->Checked = true;
if (WindowsDarkModeEnabled()) {
TStyleManager::TrySetStyle(DARK_MODE_STYLE, false);
} else {
TStyleManager::TrySetStyle(LIGHT_MODE_STYLE, false);
}
break;
}
}

//Function to inject a CSS style into html documents to make their look match the dark mode style
std::wstring __fastcall TMainF::InjectDarkModeHTMLStyle(const wchar_t* HTMLDocument) {
const wchar_t* InsertionPoint = wcsstr(HTMLDocument, L"<head>");
Expand Down Expand Up @@ -214,12 +245,6 @@ __fastcall TMainF::TMainF(TComponent* Owner)
Page_Position=-1;
Caption=MEDIAINFO_TITLE;

//Set dark mode
if (WindowsDarkModeEnabled()) {
TStyleManager::TrySetStyle(DARK_MODE_STYLE, false);
M_Options_Darkmode->Checked=true;
}

//Opt-out from styling dialogs and use native Windows dialogs for dark mode as well
TStyleManager::SystemHooks = TStyleManager::SystemHooks -
(TStyleManager::TSystemHooks() << TStyleManager::TSystemHook::shDialogs);
Expand All @@ -233,6 +258,32 @@ __fastcall TMainF::TMainF(TComponent* Owner)
Page_Custom_Text->Font = monoFont;
Page_Sheet_Text->Font = monoFont;
}

//Configuration of MediaInfoLib
if (I == NULL)
I = new MediaInfoList;

//Load GUI preferences
GUI_Configure();

//File(s) in command line
#ifdef UNICODE
if (IsWin9X())
{
for (int I1 = 1; I1 <= ParamCount(); I1++)
I->Open(ParamStr(I1).c_str());
}
else
{
int argc;
MediaInfoNameSpace::Char** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
for (int I1 = 1; I1 < argc; I1++)
I->Open(argv[I1]);
}
#else
for (int I1 = 1; I1 < ParamCount(); I1++)
I->Open(Ztring().From_Local(ParamStr(I1).c_str()));
#endif
}

//***************************************************************************
Expand All @@ -242,34 +293,6 @@ __fastcall TMainF::TMainF(TComponent* Owner)
//---------------------------------------------------------------------------
void __fastcall TMainF::GUI_Configure()
{
//Hard coded
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
int DPI;
if (osvi.dwMajorVersion >= 10 && (osvi.dwMajorVersion > 10 || osvi.dwMinorVersion > 0 || osvi.dwBuildNumber >= 14939))
DPI=GetDpiForWindow(WindowHandle);
else
DPI=GetDeviceCaps(GetDC(NULL), LOGPIXELSX);
float DPIScale=static_cast<float>(DPI)/96;
float ScaledScreenWidth=Screen->Width/DPIScale;
float ScaledScreenHeight=Screen->Height/DPIScale;
Width=500;
Height=400;
if (ScaledScreenWidth>=1024)
Width=700;
if (ScaledScreenWidth>=1280)
Width=830;
if (ScaledScreenHeight>=768)
Height=500;
if (ScaledScreenHeight>=1024)
Height=600;
Width*=DPIScale;
Height*=DPIScale;
Left=(Screen->Width-Width)/2;
Top=(Screen->Height-Height)/2;

//Load Configuration
if (Prefs->Config_Load()==2) //Showing options if no config
{
Expand Down Expand Up @@ -348,42 +371,44 @@ void __fastcall TMainF::GUI_Configure()
//Translation
Translate();

//Refresh global
FormResize(NULL);
//Configure theme
ConfigTheme();
}

//---------------------------------------------------------------------------
void __fastcall TMainF::FormShow(TObject *Sender)
{
//Configuration of MediaInfoLib
if (I == NULL)
{
I = new MediaInfoList;

//Load GUI preferences
GUI_Configure();

//File(s) in command line
#ifdef UNICODE
if (IsWin9X())
{
for (int I1 = 1; I1 <= ParamCount(); I1++)
I->Open(ParamStr(I1).c_str());
}
else
{
int argc;
MediaInfoNameSpace::Char** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
for (int I1 = 1; I1 < argc; I1++)
I->Open(argv[I1]);
}
#else
for (int I1 = 1; I1 < ParamCount(); I1++)
I->Open(Ztring().From_Local(ParamStr(I1).c_str()));
#endif
//Set window size and position
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
int DPI;
if (osvi.dwMajorVersion >= 10 && (osvi.dwMajorVersion > 10 || osvi.dwMinorVersion > 0 || osvi.dwBuildNumber >= 14939))
DPI=GetDpiForWindow(WindowHandle);
else
DPI=GetDeviceCaps(GetDC(NULL), LOGPIXELSX);
float DPIScale=static_cast<float>(DPI)/96;
float ScaledScreenWidth=Screen->Width/DPIScale;
float ScaledScreenHeight=Screen->Height/DPIScale;
Width=500;
Height=400;
if (ScaledScreenWidth>=1024)
Width=700;
if (ScaledScreenWidth>=1280)
Width=830;
if (ScaledScreenHeight>=768)
Height=500;
if (ScaledScreenHeight>=1024)
Height=600;
Width*=DPIScale;
Height*=DPIScale;
Left=(Screen->Width-Width)/2;
Top=(Screen->Height-Height)/2;

Refresh();
}
//Refresh global
FormResize(NULL);
Refresh();
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -940,7 +965,7 @@ void __fastcall TMainF::Refresh(TTabSheet *Page)
//Creating file
Ztring S1=I->Inform().c_str();
File F;
if (M_Options_Darkmode->Checked) {
if (TStyleManager::ActiveStyle == TStyleManager::Style[DARK_MODE_STYLE]) {
S1=InjectDarkModeHTMLStyle(I->Inform().c_str());
}
if (FileName_Temp==__T(""))
Expand All @@ -962,7 +987,7 @@ void __fastcall TMainF::Refresh(TTabSheet *Page)
Temp+=L"about:<html><head></head><body>";
Temp+=TempA.To_Unicode();
Temp+=L"</body></html>";
if (M_Options_Darkmode->Checked) {
if (TStyleManager::ActiveStyle == TStyleManager::Style[DARK_MODE_STYLE]) {
Temp=InjectDarkModeHTMLStyle(Temp.c_str());
}
Page_HTML_HTML->Navigate((MediaInfoNameSpace::Char*)Temp.c_str());
Expand Down Expand Up @@ -2090,29 +2115,35 @@ void __fastcall TMainF::Footer_ButtonClick(TObject *Sender)
}

//---------------------------------------------------------------------------
void __fastcall TMainF::M_Options_DarkmodeClick(TObject* Sender)
void __fastcall TMainF::M_Options_Theme_SystemClick(TObject *Sender)
{
if (M_Options_Darkmode->Checked) {
TStyleManager::TrySetStyle(LIGHT_MODE_STYLE, false);
M_Options_Darkmode->Checked = false;
} else {
TStyleManager::TrySetStyle(DARK_MODE_STYLE, false);
M_Options_Darkmode->Checked = true;
}
Prefs->Config(__T("Theme")) = __T("0");
Prefs->Config.Save();
ConfigTheme();
}

//---------------------------------------------------------------------------
void __fastcall TMainF::M_Options_Theme_LightClick(TObject *Sender)
{
Prefs->Config(__T("Theme")) = __T("1");
Prefs->Config.Save();
ConfigTheme();
}

//---------------------------------------------------------------------------
void __fastcall TMainF::M_Options_Theme_DarkClick(TObject *Sender)
{
Prefs->Config(__T("Theme")) = __T("2");
Prefs->Config.Save();
ConfigTheme();
}

//---------------------------------------------------------------------------
void __fastcall TMainF::ApplicationEvents1OnSettingChange(
TObject* Sender, int Flag, const UnicodeString Section, int &Result)
{
if (Section == "ImmersiveColorSet") {
if (WindowsDarkModeEnabled()) {
TStyleManager::TrySetStyle(DARK_MODE_STYLE, false);
M_Options_Darkmode->Checked = true;
} else {
TStyleManager::TrySetStyle(LIGHT_MODE_STYLE, false);
M_Options_Darkmode->Checked = false;
}
ConfigTheme();
}
}

Expand Down
21 changes: 18 additions & 3 deletions Source/GUI/VCL/GUI_Main.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -2339,9 +2339,24 @@ object MainF: TMainF
Hint = 'Show Menu'
OnClick = M_Options_ShowMenuClick
end
object M_Options_Darkmode: TMenuItem
Caption = 'Dark mode'
OnClick = M_Options_DarkmodeClick
object M_Options_Theme: TMenuItem
Caption = 'Theme'
object M_Options_Theme_System: TMenuItem
Caption = 'System'
Checked = True
RadioItem = True
OnClick = M_Options_Theme_SystemClick
end
object M_Options_Theme_Light: TMenuItem
Caption = 'Light'
RadioItem = True
OnClick = M_Options_Theme_LightClick
end
object M_Options_Theme_Dark: TMenuItem
Caption = 'Dark'
RadioItem = True
OnClick = M_Options_Theme_DarkClick
end
end
object N5: TMenuItem
Caption = '-'
Expand Down
10 changes: 8 additions & 2 deletions Source/GUI/VCL/GUI_Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,15 @@ class TMainF : public TForm
TImageCollection* ImageCollection1;
TVirtualImageList* Menu_Image;
TVirtualImageList* Toolbar_Image;
TMenuItem *M_Options_Darkmode;
TApplicationEvents *ApplicationEvents1;
TFileOpenDialog *FolderOpenDialog1;
TPanel *Page_Sheet_Panel1;
TSplitter *Page_Sheet_Splitter1;
TPanel *Page_Sheet_Panel2;
TMenuItem *M_Options_Theme;
TMenuItem *M_Options_Theme_System;
TMenuItem *M_Options_Theme_Light;
TMenuItem *M_Options_Theme_Dark;
void __fastcall FormResize(TObject *Sender);
void __fastcall M_Help_AboutClick(TObject *Sender);
void __fastcall M_Options_PreferencesClick(TObject *Sender);
Expand Down Expand Up @@ -309,10 +312,12 @@ class TMainF : public TForm
void __fastcall M_View_NISO_Z39_87Click(TObject *Sender);
void __fastcall M_View_Graph_SvgClick(TObject *Sender);
void __fastcall M_Options_FullParsingClick(TObject *Sender);
void __fastcall M_Options_DarkmodeClick(TObject *Sender);
void __fastcall ApplicationEvents1OnSettingChange(TObject *Sender, int Flag, const UnicodeString Section,
int &Result);
void __fastcall Page_Sheet_Splitter1Moved(TObject *Sender);
void __fastcall M_Options_Theme_SystemClick(TObject *Sender);
void __fastcall M_Options_Theme_LightClick(TObject *Sender);
void __fastcall M_Options_Theme_DarkClick(TObject *Sender);
protected:
virtual void __fastcall CreateWnd();
virtual void __fastcall DestroyWnd();
Expand All @@ -321,6 +326,7 @@ class TMainF : public TForm
const UnicodeString LIGHT_MODE_STYLE = "Windows"; // Name of style for light mode;
const UnicodeString DARK_MODE_STYLE = "Windows11 Modern Dark"; // Name of style for dark mode
bool __fastcall WindowsDarkModeEnabled();
void __fastcall ConfigTheme();
std::wstring __fastcall InjectDarkModeHTMLStyle(const wchar_t* HTMLDocument);
public: // User declarations
MESSAGE void __fastcall HandleDropFiles (TMessage&);
Expand Down

0 comments on commit f6b9e72

Please sign in to comment.