Skip to content
This repository has been archived by the owner on Dec 22, 2024. It is now read-only.

Commit

Permalink
Ver 2.2.0-Alpha.2: Show Announcement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiroiame-Kusu committed Jul 29, 2024
1 parent 5655ebe commit 16ac3e0
Show file tree
Hide file tree
Showing 11 changed files with 385 additions and 96 deletions.
17 changes: 16 additions & 1 deletion LoCyanFrpDesktop/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
using System.Runtime.InteropServices;
using System.Threading;
using System.Security;
using CefSharp.Wpf;
using System.Runtime.ConstrainedExecution;
using CefSharp;

namespace LoCyanFrpDesktop
{
Expand Down Expand Up @@ -60,8 +63,15 @@ protected override void OnStartup(StartupEventArgs e)
});*/
base.OnStartup(e);
//MainWindow mainWindow = new();
var settings = new CefSettings()
{
//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache"),
LogSeverity = LogSeverity.Verbose,
LogFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs\\CEF.log")
};


Cef.Initialize(settings);
if (args.Length > 0)
{
int i = 0;

Check warning on line 77 in LoCyanFrpDesktop/App.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows

The variable 'i' is assigned but its value is never used

Check warning on line 77 in LoCyanFrpDesktop/App.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows

The variable 'i' is assigned but its value is never used

Check warning on line 77 in LoCyanFrpDesktop/App.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows

The variable 'i' is assigned but its value is never used
Expand Down Expand Up @@ -118,6 +128,11 @@ protected override void OnStartup(StartupEventArgs e)
}

}
protected override void OnExit(ExitEventArgs e)
{
Cef.Shutdown();
base.OnExit(e);
}
private static void CurrentDomain_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = true;
Expand Down
14 changes: 10 additions & 4 deletions LoCyanFrpDesktop/DashBoard.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@
SelectedPageIndex="0">
<ui:NavigationStore.Items>
<ui:NavigationItem
Content="开始"
Icon="Home24"
PageTag="ProxyList"
PageType="{x:Type Dashboard:ProxyList}" />
Content="开始"
Icon="Home24"
PageTag="Home"
PageType="{x:Type Dashboard:Home}" />
<ui:NavigationSeparator />
<ui:NavigationItem
Content="隧道列表"
Icon="List24"
PageTag="ProxyList"
PageType="{x:Type Dashboard:ProxyList}"
/>
<ui:NavigationItem
Content="运行状态"
Icon="WindowConsole20"
Expand Down
71 changes: 71 additions & 0 deletions LoCyanFrpDesktop/Dashboard/Home.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<ui:UiPage xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
x:Class="LoCyanFrpDesktop.Dashboard.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:LoCyanFrpDesktop.Dashboard"
xmlns:cefSharpCore="clr-namespace:CefSharp;assembly=CefSharp.Core"
xmlns:cefSharpWpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Home">
<Grid Margin="0,0,20,0">
<Grid.Resources>
<SolidColorBrush x:Key="BorderColor"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" HorizontalAlignment="Left">
<DockPanel VerticalAlignment="Stretch">
<Border CornerRadius="20" Height="40" Width="40" BorderThickness="1" BorderBrush="LightBlue" Margin="10,0,10,0" Name="Avatar">
<Border.Background>
<ImageBrush Stretch="UniformToFill" ImageSource="/Resource/favicon.ico"/>
</Border.Background>
</Border>
<TextBlock VerticalAlignment="Center" Name="title_username" Text="欢迎回来," FontSize="25" Margin="0,0,10,0" />

</DockPanel>

</Grid>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="70"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="2">

<Border
Grid.Column="1"
Margin="10,0"
Padding="10,5"
CornerRadius="8"
Background="{DynamicResource ControlFillColorDefaultBrush}"
>
<cefSharpWpf:ChromiumWebBrowser x:Name="Browser">

</cefSharpWpf:ChromiumWebBrowser>
</Border>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ui:Card Grid.Column="1" Padding="10" Margin="10,10,0,15" VerticalAlignment="Bottom">
<StackPanel>
<!--<TextBlock Name="Inbound" Margin="0,0,0,20" Text="上行流量:"/>
<TextBlock Name="OutBound" Text="下行流量:"/>-->
<TextBlock Name="Traffic" Text="剩余流量: "/>
</StackPanel>
</ui:Card>


</Grid>
</Grid>

</Grid>
</ui:UiPage>
189 changes: 189 additions & 0 deletions LoCyanFrpDesktop/Dashboard/Home.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Wpf.Ui.Controls;
using LoCyanFrpDesktop.Utils;
using LoCyanFrpDesktop.Dashboard;
using System.Windows.Threading;
using System.Text.RegularExpressions;
using System.IO;
using LoCyanFrpDesktop.Extensions;
using System.Security.Cryptography;
using Markdig;
using System.Windows.Controls;
using CefSharp;
using CefSharp.Wpf;
using System.Text;
using HtmlAgilityPack;


namespace LoCyanFrpDesktop.Dashboard
{
/// <summary>
/// Interaction logic for ProxyList.xaml
/// </summary>
public partial class Home : UiPage
{
public static ImageBrush AvatarImage;
public Home()
{
InitializeComponent();
//Wait For Rewrite.
//InitializeAutoLaunch();
DataContext = this;
title_username.Text += Global.Config.Username;
Resources["BorderColor"] = MainWindow.DarkThemeEnabled ? Colors.White : Colors.LightGray;
//BackgroundColor = Resources["ControlFillColorDefaultBrush"];
//Inbound.Text += MainWindow.Inbound;
//OutBound.Text += MainWindow.Outbound;
Traffic.Text += (MainWindow.Traffic / 1024) + "GB";

RefreshAvatar();
FetchAnnouncement();
}
private async void FetchAnnouncement()
{
try
{
using (HttpClient client = new())
{
client.BaseAddress = new Uri("https://api.locyanfrp.cn/App/GetBroadCast");
var result = await client.GetAsync(client.BaseAddress).Await().Content.ReadAsStringAsync();
var result2 = JObject.Parse(result);
if (result2 != null && (bool)result2["status"]) {

Check warning on line 57 in LoCyanFrpDesktop/Dashboard/Home.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows

Possible null reference argument for parameter 'value' in 'JToken.explicit operator bool(JToken value)'.
var html = Markdown.ToHtml(result2["broadcast"].ToString());

Check warning on line 58 in LoCyanFrpDesktop/Dashboard/Home.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows

Dereference of a possibly null reference.
var htmlDoc = new HtmlDocument();
if (MainWindow.DarkThemeEnabled)
{
htmlDoc.LoadHtml(html);
var cssContent = "* { color: white; } a { color: aqua}";
var styleNode = HtmlNode.CreateNode($"<style>{cssContent}</style>");
var scriptNode = HtmlNode.CreateNode("<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/smooth-scrollbar.js'></script>");
var newHeadNode = htmlDoc.CreateElement("head");
newHeadNode.AppendChild(styleNode);
newHeadNode.AppendChild(scriptNode);
htmlDoc.DocumentNode.PrependChild(newHeadNode);
}
Browser.LoadHtml(MainWindow.DarkThemeEnabled ? htmlDoc.DocumentNode.OuterHtml: html, "http://localhost",Encoding.UTF8);
Browser.LoadingStateChanged += OnLoadingStateChanged;

Check warning on line 72 in LoCyanFrpDesktop/Dashboard/Home.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows

Nullability of reference types in type of parameter 'sender' of 'void Home.OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)' doesn't match the target delegate 'EventHandler<LoadingStateChangedEventArgs>' (possibly because of nullability attributes).

}

}
}
catch (Exception _) {

Check warning on line 78 in LoCyanFrpDesktop/Dashboard/Home.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows

The variable '_' is declared but never used

}
}
private void OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
string css = @"
html {
scroll-behavior: smooth;
}
::-webkit-scrollbar {
width: 8px;
opacity: 0;
transition: opacity 0.5s;
}
::-webkit-scrollbar-track {
background: #555;
border-radius: 10px; /* Rounded corners for the track */
}
::-webkit-scrollbar-thumb {
background: #f1f1f1;
border-radius: 10px; /* Rounded corners for the track */
}
::-webkit-scrollbar-thumb:hover {
background: #888;
}
.show-scrollbar ::-webkit-scrollbar {
opacity: 1;";
string script = $"var style = document.createElement('style'); style.innerHTML = `{css}`; document.head.appendChild(style);";
string script2 = "let timeout; document.addEventListener('scroll', function() { document.documentElement.classList.add('show-scrollbar'); clearTimeout(timeout); timeout = setTimeout(() => { document.documentElement.classList.remove('show-scrollbar'); }, 1000); });";
Browser.ExecuteScriptAsync(script);
Browser.ExecuteScriptAsync(script2);

}
}
private async void RefreshAvatar()
{
try
{
using (var client = new HttpClient())
{

client.BaseAddress = new Uri(MainWindow.Avatar);
var Avatar = await client.GetAsync(client.BaseAddress).Await().Content.ReadAsStreamAsync();
var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Avatar.png");
var ApplyAvatar = () =>
{
BitmapImage bitmap = new BitmapImage();

// 设置 BitmapImage 的 UriSource 属性为图片文件的路径
bitmap.BeginInit();
bitmap.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
bitmap.EndInit();
AvatarImage = new ImageBrush(bitmap)
{
Stretch = Stretch.UniformToFill,

};

Dispatcher.Invoke(() =>
{
this.Avatar.Background = AvatarImage;
});
};

if (File.Exists(path)){
MD5 md5 = MD5.Create();
if (md5.ComputeHash(Avatar).Equals(md5.ComputeHash(File.ReadAllBytes("Avatar.png"))))
{
ApplyAvatar();
return;
}

File.Delete(path);
using (FileStream fileStream = new(path, FileMode.Create))
{
byte[] bytes = new byte[Avatar.Length];
Avatar.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
Avatar.Seek(0, SeekOrigin.Begin);

// 把 byte[] 写入文件

BinaryWriter bw = new BinaryWriter(fileStream);
bw.Write(bytes);
bw.Close();
fileStream.Close();
fileStream.Dispose();
}
ApplyAvatar();
}


}

}
catch (Exception ex)
{
Logger.MsgBox("无法获取您的头像, 请稍后重试", "LocyanFrp", 0, 48, 1);
}
}


}
}


//BreakAutoCompileBecauseTheRewriteIsNOTFinished
23 changes: 2 additions & 21 deletions LoCyanFrpDesktop/Dashboard/ProxyList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="100"/>
<RowDefinition Height="70"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="2">

Expand Down Expand Up @@ -78,30 +78,11 @@
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ui:Card Grid.Column="1" Padding="10" Margin="10,10,0,10" VerticalAlignment="Bottom">

<StackPanel>
<!--<TextBlock Name="Inbound" Margin="0,0,0,20" Text="上行流量:"/>
<TextBlock Name="OutBound" Text="下行流量:"/>-->
<TextBlock Name="Traffic" Text="剩余流量: "/>
</StackPanel>
</ui:Card>
<ui:Card Grid.Column="0" Padding="10" Margin="10,0,0,0">

<StackPanel>
<DockPanel>
<TextBlock Name="title_username" Text="欢迎回来,"/>

</DockPanel>
</StackPanel>

<ui:Card.Footer>
<TextBlock Text="PS:右键隧道可以打开菜单哦喵~" />
</ui:Card.Footer>
<TextBlock Text="PS:右键隧道可以打开菜单哦喵~" />
</ui:Card>


Expand Down
Loading

0 comments on commit 16ac3e0

Please sign in to comment.