Skip to content

Commit

Permalink
Support dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
HeHang0 committed Jun 23, 2023
1 parent 78c3025 commit 7845c8a
Show file tree
Hide file tree
Showing 19 changed files with 164 additions and 70 deletions.
6 changes: 5 additions & 1 deletion FileControl/App/AppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void OnColorChanged(Color color)

private string filePath;

public void OnFileChanged((string FilePath, FileExtension Ext) file)
public void ChangeFile((string FilePath, FileExtension Ext) file)
{
OnColorChanged(Color.FromRgb(0xA1, 0xD5, 0xD3));
GlobalNotify.OnSizeChange(450, 800);
Expand All @@ -61,6 +61,10 @@ public void OnFileChanged((string FilePath, FileExtension Ext) file)
bgWorker.RunWorkerAsync(file);
}

public void ChangeTheme(bool dark)
{
}

protected override void BgWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
Expand Down
6 changes: 5 additions & 1 deletion FileControl/Common/CommonModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void OnColorChanged(Color color)
GlobalNotify.OnColorChange(color);
}

public void OnFileChanged((string FilePath, FileExtension Ext) file)
public void ChangeFile((string FilePath, FileExtension Ext) file)
{
OnColorChanged(Color.FromRgb(0xA1, 0xD5, 0xD3));
GlobalNotify.OnSizeChange(450, 800);
Expand All @@ -75,6 +75,10 @@ public void OnFileChanged((string FilePath, FileExtension Ext) file)
bgWorker.RunWorkerAsync(file.FilePath);
}

public void ChangeTheme(bool dark)
{
}

protected override void BgWorker_DoWork(object sender, DoWorkEventArgs e)
{
var bgw = sender as BackgroundWorker;
Expand Down
9 changes: 7 additions & 2 deletions FileControl/FileControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ public FileControl()
{
}

public void OnFileChanged((string FilePath, FileExtension Ext) file)
public void ChangeFile((string FilePath, FileExtension Ext) file)
{
model.OnFileChanged(file);
model.ChangeFile(file);
}

public void ChangeTheme(bool dark)
{
model.ChangeTheme(dark);
}
}
}
32 changes: 29 additions & 3 deletions FileControl/FileViewControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
using FileViewer.FileControl.Video;
using FileViewer.FileHelper;
using FileViewer.Globle;
using FileViewer.Monitor;
using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

Expand All @@ -26,13 +30,29 @@ public partial class FileViewControl : UserControl

private bool cloudflareOK = false;

private bool isWebView2Installed = false;

public FileViewControl()
{
InitializeComponent();
DataContextChanged += OnDataContextChanged;
MyGrid.Children.Add(new HelloControl());
GlobalNotify.FileLoadFailed += OnFileLoadFailed;
cloudflareOK = Utils.CheckUrlOK("https://cdnjs.cloudflare.com");
new ThemeListener().ThemeChanged += OnThemeChanged;
CheckWebView2();
}

private async void CheckWebView2()
{
try
{
var webView2Environment = await CoreWebView2Environment.CreateAsync(userDataFolder: Path.Combine(Path.GetTempPath(), "WebView2"));
isWebView2Installed = webView2Environment != null;
}
catch (Exception)
{
}
}

private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
Expand Down Expand Up @@ -73,14 +93,14 @@ private void SetResource(string filePath, bool loadWithTypeNone = false)
controlInfo = (typeof(CommonControl), false);
}

if(typeInfo.Type == FileViewType.Text && cloudflareOK)
if(cloudflareOK && isWebView2Installed && typeInfo.Type == FileViewType.Text)
{
controlInfo.Type = typeof(PdfControl);
}

if(lastViewType == controlInfo.Type)
{
(MyGrid.Children[0] as FileControl).OnFileChanged((filePath, typeInfo.Ext));
(MyGrid.Children[0] as FileControl).ChangeFile((filePath, typeInfo.Ext));
return;
}
lastViewType = controlInfo.Type;
Expand All @@ -95,7 +115,13 @@ private void LoadFile(FileControl fc, string filePath, FileExtension ext)
{
MyGrid.Children.Add(fc);
fc.Margin = new Thickness(0);
fc.OnFileChanged((filePath, ext));
fc.ChangeFile((filePath, ext));
fc.ChangeTheme(ThemeListener.IsDarkMode());
}

private void OnThemeChanged(bool isDark)
{
(MyGrid.Children[0] as FileControl).ChangeTheme(isDark);
}
}
}
10 changes: 3 additions & 7 deletions FileControl/IFileModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@

namespace FileViewer.FileControl
{
interface IFileModel: IFileChanged, INotifyPropertyChanged, IColorChanged
interface IFileModel: IFileChanged, INotifyPropertyChanged
{
}


public interface IFileChanged
{
void OnFileChanged((string FilePath, FileHelper.FileExtension Ext) file);
}

public interface IColorChanged
{
void OnColorChanged(Color color);
void ChangeFile((string FilePath, FileHelper.FileExtension Ext) file);
void ChangeTheme(bool dark);
}
}
7 changes: 3 additions & 4 deletions FileControl/Image/ImageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ImageModel : BaseBackgroundWork, IFileModel
public event PropertyChangedEventHandler PropertyChanged;
public ImageSource ThumbnailImage { get; private set; }

public void OnFileChanged((string FilePath, FileExtension Ext) file)
public void ChangeFile((string FilePath, FileExtension Ext) file)
{
try
{
Expand All @@ -44,12 +44,11 @@ public void OnFileChanged((string FilePath, FileExtension Ext) file)
{
ShowThumbnail(GetDefaultThumbnail(file.FilePath));
}
OnColorChanged(Colors.White);
GlobalNotify.OnColorChange(Colors.White);
}

public void OnColorChanged(System.Windows.Media.Color color)
public void ChangeTheme(bool dark)
{
GlobalNotify.OnColorChange(color);
}

public bool IsGif { get; private set; }
Expand Down
3 changes: 0 additions & 3 deletions FileControl/Loading.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" x:Name="root"
mc:Ignorable="d" >
<UserControl.Background>
<SolidColorBrush Color="White" Opacity="0.4" ></SolidColorBrush>
</UserControl.Background>
<Viewbox Margin="{Binding LoadCirclesMargin,ElementName=root}"
HorizontalAlignment="Center"
VerticalAlignment="Center">
Expand Down
6 changes: 5 additions & 1 deletion FileControl/MobileProvision/MobileProvisionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MobileProvisionModel : BaseBackgroundWork, IFileModel
private readonly double width = SystemParameters.WorkArea.Width / 2;

private (string FilePath, FileExtension Ext) currentFilePath;
public void OnFileChanged((string FilePath, FileExtension Ext) file)
public void ChangeFile((string FilePath, FileExtension Ext) file)
{
currentFilePath = file;
InitBackGroundWork();
Expand All @@ -44,6 +44,10 @@ public void OnFileChanged((string FilePath, FileExtension Ext) file)
GlobalNotify.OnSizeChange(height, width);
}

public void ChangeTheme(bool dark)
{
}

protected override void BgWorker_DoWork(object sender, DoWorkEventArgs e)
{
byte[] bytes = null;
Expand Down
7 changes: 3 additions & 4 deletions FileControl/Music/MusicModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,18 @@ public MusicModel()
defaultThumbnail = Utils.GetBitmapSource(Properties.Resources.cloundmusic.ToBitmap());
}

public void OnFileChanged((string FilePath, FileExtension Ext) file)
public void ChangeFile((string FilePath, FileExtension Ext) file)
{
filePath = file.FilePath;
StopCommand.Execute(null);
GlobalNotify.OnLoadingChange(false);
GlobalNotify.OnSizeChange(450, 500);
SetThumbnailImage();
OnColorChanged(Colors.White);
GlobalNotify.OnColorChange(Colors.White);
}

public void OnColorChanged(System.Windows.Media.Color color)
public void ChangeTheme(bool dark)
{
GlobalNotify.OnColorChange(color);
}

private void SetThumbnailImage()
Expand Down
12 changes: 6 additions & 6 deletions FileControl/Office/OfficeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ internal class OfficeModel: BaseBackgroundWork, IFileModel
{
public event PropertyChangedEventHandler PropertyChanged;

public void OnFileChanged((string FilePath, FileExtension Ext) file)
public void ChangeFile((string FilePath, FileExtension Ext) file)
{
ShowBrowser = false;
InitBackGroundWork();
bgWorker.RunWorkerAsync(file.FilePath);
GlobalNotify.OnColorChange(Color.FromRgb(0xEE, 0xF5, 0xFD));
}

public void ChangeTheme(bool dark)
{

}

public IDocumentPaginatorSource OfficeContent { get; set; }
public bool ShowBrowser { get; private set; }

Expand Down Expand Up @@ -62,11 +67,6 @@ protected override void BgWorker_RunWorkerCompleted(object sender, RunWorkerComp
GlobalNotify.OnLoadingChange(false);
}

public void OnColorChanged(Color color)
{
GlobalNotify.OnColorChange(color);
}

private bool ConvertOfficeToXps(string officeFilePath, string xpsFilePath)
{
if (!IsOfficeInstalled()) return false;
Expand Down
19 changes: 0 additions & 19 deletions FileControl/Pdf/PdfControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ private async void InitWebView2(string userDataFolder)
{
}
webView2.WebMessageReceived += WebMessageReceived;
webView2.NavigationCompleted += InjectTheme;
}

private void TextFileChanged()
Expand Down Expand Up @@ -95,27 +94,9 @@ private void WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEve
case "loaded":
ShowTextWithWebView2(_model.RealFilePath);
break;
case "theme-dark":
_model.OnColorChanged(Color.FromRgb(0x1E, 0x1E, 0x1E));
break;
case "theme-light":
_model.OnColorChanged(Color.FromRgb(0xFF, 0xFF, 0xFE));
break;
}
}

private void InjectTheme(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
string themeScript = @"
const themeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const message = dark => (dark ? 'theme-dark' : 'theme-light');
window.chrome.webview?.postMessage(message(themeMediaQuery.matches));
themeMediaQuery.addEventListener('change', function (ev) {
window.chrome.webview?.postMessage(message(ev.matches));
});";
webView2.ExecuteScriptAsync(themeScript);
}

private void SetLoaderDllFolderPath(string webView2Path)
{
var loaderPath = Path.Combine(webView2Path, "WebView2Loader.dll");
Expand Down
30 changes: 27 additions & 3 deletions FileControl/Pdf/PdfModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ public class PdfModel : IFileModel

public string RealFilePath;

public void OnFileChanged((string FilePath, FileExtension Ext) file)
private FileExtension extension = FileExtension.None;

public void ChangeFile((string FilePath, FileExtension Ext) file)
{
if (RealFilePath != file.FilePath)
{
RealFilePath = file.FilePath;
extension = file.Ext;
if (file.Ext == FileExtension.PDF)
{
PdfFilePath = file.FilePath;
Expand All @@ -32,9 +35,30 @@ public void OnFileChanged((string FilePath, FileExtension Ext) file)
GlobalNotify.OnLoadingChange(false);
}

public void OnColorChanged(Color color)
public void ChangeTheme(bool dark)
{
GlobalNotify.OnColorChange(color);
if (dark)
{
if (extension == FileExtension.PDF)
{
GlobalNotify.OnColorChange(Color.FromRgb(0x33, 0x33, 0x33));
}
else
{
GlobalNotify.OnColorChange(Color.FromRgb(0x1E, 0x1E, 0x1E));
}
}
else
{
if (extension == FileExtension.PDF)
{
GlobalNotify.OnColorChange(Color.FromRgb(0xF7, 0xF7, 0xF7));
}
else
{
GlobalNotify.OnColorChange(Color.FromRgb(0xFF, 0xFF, 0xFE));
}
}
}
}
}
15 changes: 7 additions & 8 deletions FileControl/Text/TextModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Encoding EncodeSelected
set
{
encodingSelected = value;
OnFileChanged(currentFilePath);
ChangeFile(currentFilePath);
}
}

Expand All @@ -88,7 +88,7 @@ public Encoding EncodeSelected
};

private (string FilePath, FileExtension Ext) currentFilePath;
public void OnFileChanged((string FilePath, FileExtension Ext) file)
public void ChangeFile((string FilePath, FileExtension Ext) file)
{
currentFilePath = file;
if (file.Ext == FileExtension.TS || file.Ext == FileExtension.VUE)
Expand All @@ -103,7 +103,11 @@ public void OnFileChanged((string FilePath, FileExtension Ext) file)
InitBackGroundWork();
bgWorker.RunWorkerAsync(file.FilePath);
GlobalNotify.OnSizeChange(height, width);
OnColorChanged(Colors.White);
GlobalNotify.OnColorChange(Colors.White);
}

public void ChangeTheme(bool dark)
{
}

protected override void BgWorker_DoWork(object sender, DoWorkEventArgs e)
Expand Down Expand Up @@ -153,10 +157,5 @@ protected override void BgWorker_RunWorkerCompleted(object sender, RunWorkerComp
SetText((string)e.Result);
GlobalNotify.OnLoadingChange(false);
}

public void OnColorChanged(Color color)
{
GlobalNotify.OnColorChange(color);
}
}
}
Loading

0 comments on commit 7845c8a

Please sign in to comment.