Skip to content

Commit

Permalink
✨ 给自定义主页面增加了一个Java新闻的预设(超级卡顿)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaiYu-233 committed Aug 31, 2024
1 parent 7221806 commit b3009c0
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 25 deletions.
1 change: 1 addition & 0 deletions YMCL.Main/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
<StyleInclude Source="avares://Semi.Avalonia/Themes/Index.axaml" />
<semi:SemiTheme Locale="zh-cn" />
<u-semi:SemiTheme Locale="zh-CN" />
<StyleInclude Source="avares://AsyncImageLoader.Avalonia/AdvancedImage.axaml" />
</Application.Styles>
</Application>
69 changes: 69 additions & 0 deletions YMCL.Main/Public/Classes/Sundry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

namespace YMCL.Main.Public.Classes;

public class AggregateSearch()
{
public string Tag { get; set; }
public string Type { get; set; }
public string Summary { get; set; }
public string Text { get; set; }
}
public class FolderInfo()
{
public string Name { get; set; }
Expand Down Expand Up @@ -296,3 +303,65 @@ public class UrlImageDataListEntry()
public string Url { get; set; }
public Bitmap Bitmap { get; set; }
}

public class MojangJavaNews()
{
public class Image
{
/// <summary>
///
/// </summary>
public string url { get; set; }
/// <summary>
///
/// </summary>
public string title { get; set; }
}

public class EntriesItem
{
public string BitmapBase64 { get; set; }

/// <summary>
///
/// </summary>
public string title { get; set; }
/// <summary>
///
/// </summary>
public string type { get; set; }
/// <summary>
///
/// </summary>
public string version { get; set; }
/// <summary>
///
/// </summary>
public Image image { get; set; }
/// <summary>
///
/// </summary>
public string body { get; set; }
/// <summary>
///
/// </summary>
public string id { get; set; }
/// <summary>
///
/// </summary>
public string contentPath { get; set; }
}

public class Root
{
/// <summary>
///
/// </summary>
public int version { get; set; }
/// <summary>
///
/// </summary>
public List <EntriesItem > entries { get; set; }
}

}
1 change: 1 addition & 0 deletions YMCL.Main/Public/Const.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class String
Path.Combine(UserDataRootPath, "YMCL.MinecraftFolder.DaiYu");

public static string JavaDataPath { get; } = Path.Combine(UserDataRootPath, "YMCL.Java.DaiYu");
public static string JavaNewsDataPath { get; } = Path.Combine(UserDataRootPath, "YMCL.JavaNews.DaiYu");
public static string AppPathDataPath { get; } = Path.Combine(UserDataRootPath, "YMCL.AppPath.DaiYu");
public static string PlayerDataPath { get; } = Path.Combine(UserDataRootPath, "YMCL.Player.DaiYu");

Expand Down
29 changes: 29 additions & 0 deletions YMCL.Main/Public/Controls/JavaNewsEntry/JavaNewsEntry.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<UserControl
x:Class="YMCL.Main.Public.Controls.JavaNewsEntry"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:asyncImageLoader="clr-namespace:AsyncImageLoader;assembly=AsyncImageLoader.Avalonia"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Border
Padding="10"
Background="{DynamicResource 1x}"
ClipToBounds="True"
CornerRadius="5">
<DockPanel>
<Border
Width="180"
Height="180"
Margin="5"
VerticalAlignment="Top"
ClipToBounds="True"
CornerRadius="5">
<asyncImageLoader:AdvancedImage Name="ImageC" />
</Border>
<WrapPanel Name="WrapPanelC" Margin="10" />
</DockPanel>
</Border>
</UserControl>
94 changes: 94 additions & 0 deletions YMCL.Main/Public/Controls/JavaNewsEntry/JavaNewsEntry.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using HtmlAgilityPack;

namespace YMCL.Main.Public.Controls;

public partial class JavaNewsEntry : UserControl
{
public JavaNewsEntry(string img, string text)
{
InitializeComponent();
ImageC.Source = "https://launchercontent.mojang.com" + img;

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(text);

var wrapPanel = WrapPanelC;

// 解析并添加段落
var paragraphs = htmlDoc.DocumentNode.SelectNodes("//p");
if (paragraphs != null)
{
foreach (var paragraph in paragraphs)
{
var textBlock = new TextBlock
{
Text = paragraph.InnerText, FontFamily = (FontFamily)Application.Current.Resources["Font"]!,
TextWrapping = TextWrapping.Wrap
};
wrapPanel.Children.Add(textBlock);
}
}

// 解析并添加标题
var h1 = htmlDoc.DocumentNode.SelectSingleNode("//h1");
if (h1 != null)
{
var textBlock = new TextBlock
{
Text = h1.InnerText, FontWeight = FontWeight.Bold,
FontFamily = (FontFamily)Application.Current.Resources["Font"]!,
TextWrapping = TextWrapping.Wrap
};
wrapPanel.Children.Add(textBlock);
}

// 解析并添加实验特性
var h2 = htmlDoc.DocumentNode.SelectSingleNode("//h2");
if (h2 != null)
{
var textBlock = new TextBlock
{
Text = h2.InnerText, FontWeight = FontWeight.Bold,
FontFamily = (FontFamily)Application.Current.Resources["Font"]!,
TextWrapping = TextWrapping.Wrap
};
wrapPanel.Children.Add(textBlock);
}

// 解析并添加列表项
var listItems = htmlDoc.DocumentNode.SelectNodes("//ul/li");
if (listItems != null)
{
foreach (var item in listItems)
{
var textBlock = new TextBlock
{ Text = item.InnerText, FontFamily = (FontFamily)Application.Current.Resources["Font"]!,
TextWrapping = TextWrapping.Wrap };
wrapPanel.Children.Add(textBlock);
wrapPanel.Children.Add(new TextBlock { Text = "\n" }); // 添加换行
}
}

// 解析并添加超链接
var links = htmlDoc.DocumentNode.SelectNodes("//a[@href]");
if (links != null)
{
foreach (var link in links)
{
var hyperlink = new HyperlinkButton()
{
Content = link.InnerText, FontFamily = (FontFamily)Application.Current.Resources["Font"]!,
NavigateUri = new Uri(link.Attributes["href"].Value)
};
wrapPanel.Children.Add(hyperlink);
}
}
}
}
3 changes: 2 additions & 1 deletion YMCL.Main/Public/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public enum CustomHomePageWay
{
None,
Local,
Network
Network,
Presetting_JavaNews
}

public enum DaiYuLoaderType
Expand Down
36 changes: 36 additions & 0 deletions YMCL.Main/Public/Langs/MainLang.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions YMCL.Main/Public/Langs/MainLang.resx
Original file line number Diff line number Diff line change
Expand Up @@ -924,4 +924,16 @@ win10 以下系统(不包括10)不支持自动更新
<data name="OpenBrowser" xml:space="preserve">
<value>打开浏览器</value>
</data>
<data name="CustomHomePageWay_Presetting_JavaNews" xml:space="preserve">
<value>预设-Java新闻</value>
</data>
<data name="NetworkSource" xml:space="preserve">
<value>网络源</value>
</data>
<data name="NoSupportNow" xml:space="preserve">
<value>现不支持</value>
</data>
<data name="AggregateSearchTip" xml:space="preserve">
<value>输入关键词以在YMCL中搜索 </value>
</data>
</root>
Loading

0 comments on commit b3009c0

Please sign in to comment.