Skip to content

Commit a2a778d

Browse files
committed
<feat>: Add ConsoleBox
1 parent 76cf464 commit a2a778d

File tree

8 files changed

+249
-18
lines changed

8 files changed

+249
-18
lines changed

RszTool.App/Controls/ConsoleBox.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Windows;
2+
using System.Windows.Controls;
3+
using System.Windows.Media;
4+
using RszTool.App.Common;
5+
using RszTool.App.Resources;
6+
using RszTool.App.ViewModels;
7+
8+
namespace RszTool.App.Controls
9+
{
10+
public class ConsoleBox : UserControl
11+
{
12+
public RichTextBox RichTextBox { get; }
13+
14+
public ConsoleBox()
15+
{
16+
RichTextBox = new RichTextBox
17+
{
18+
IsReadOnly = true,
19+
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
20+
Padding = new Thickness(2),
21+
ContextMenu = new ContextMenu
22+
{
23+
Items =
24+
{
25+
new MenuItem { Header = Texts.Clear, Command = new RelayCommand(Clear) }
26+
}
27+
}
28+
};
29+
RichTextBox.Document.LineHeight = 1;
30+
Content = RichTextBox;
31+
Console.SetOut(new RichTextBoxWriter(RichTextBox)
32+
{
33+
SelectionColor = ThemeManager.Instance.LightForeground,
34+
DarkSelectionColor = ThemeManager.Instance.DarkForeground,
35+
});
36+
Console.SetError(new RichTextBoxWriter(RichTextBox)
37+
{
38+
SelectionColor = ThemeManager.Instance.LightErrorBrush,
39+
DarkSelectionColor = ThemeManager.Instance.DarkErrorBrush,
40+
});
41+
}
42+
43+
private void Clear(object arg)
44+
{
45+
RichTextBox.Document.Blocks.Clear();
46+
}
47+
}
48+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.IO;
2+
using System.Text;
3+
using System.Windows.Controls;
4+
using System.Windows.Controls.Primitives;
5+
using System.Windows.Documents;
6+
using System.Windows.Media;
7+
using RszTool.App.ViewModels;
8+
9+
namespace RszTool.App.Controls
10+
{
11+
public class TextBoxWriter : TextWriter
12+
{
13+
protected TextBoxBase textBox;
14+
public delegate void WriteFunc(string value);
15+
16+
public TextBoxWriter(TextBoxBase textBox)
17+
{
18+
this.textBox = textBox;
19+
}
20+
21+
/// <summary>
22+
/// 使用UTF-16避免不必要的编码转换
23+
/// </summary>
24+
public override Encoding Encoding
25+
{
26+
get { return Encoding.Unicode; }
27+
}
28+
29+
public virtual void DoWrite(string? value)
30+
{
31+
textBox.AppendText(value);
32+
}
33+
34+
/// <summary>
35+
/// 最低限度需要重写的方法
36+
/// </summary>
37+
/// <param name="value"></param>
38+
public override void Write(string? value)
39+
{
40+
if (!textBox.CheckAccess())
41+
{
42+
textBox.Dispatcher.BeginInvoke((WriteFunc)Write, value);
43+
}
44+
else
45+
{
46+
DoWrite(value);
47+
}
48+
}
49+
50+
/// <summary>
51+
/// 其他直接调用WriteLine(value)的情况会调用此函数
52+
/// </summary>
53+
/// <param name="value"></param>
54+
public override void WriteLine()
55+
{
56+
Write(NewLine);
57+
}
58+
59+
/// <summary>
60+
/// 为提高效率直接处理一行的输出
61+
/// </summary>
62+
/// <param name="value"></param>
63+
public override void WriteLine(string? value)
64+
{
65+
if (!textBox.CheckAccess())
66+
{
67+
textBox.Dispatcher.BeginInvoke((WriteFunc)WriteLine, value);
68+
}
69+
else
70+
{
71+
DoWrite(value);
72+
DoWrite(NewLine);
73+
}
74+
}
75+
}
76+
77+
78+
public class RichTextBoxWriter : TextBoxWriter
79+
{
80+
public RichTextBox RichTextBox => (textBox as RichTextBox)!;
81+
public Brush? SelectionColor { get; set; }
82+
public Brush? DarkSelectionColor { get; set; }
83+
84+
public RichTextBoxWriter(RichTextBox textBox) : base(textBox)
85+
{
86+
}
87+
88+
public override void DoWrite(string? value)
89+
{
90+
if (value == NewLine)
91+
{
92+
RichTextBox.AppendText(value);
93+
}
94+
else
95+
{
96+
TextRange tr = new(RichTextBox.Document.ContentEnd, RichTextBox.Document.ContentEnd)
97+
{
98+
Text = value
99+
};
100+
tr.ApplyPropertyValue(TextElement.ForegroundProperty, ThemeManager.Instance.IsDarkTheme ? DarkSelectionColor : SelectionColor);
101+
}
102+
}
103+
}
104+
}

RszTool.App/MainWindow.xaml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:RszTool.App"
77
xmlns:views="clr-namespace:RszTool.App.Views"
8+
xmlns:controls="clr-namespace:RszTool.App.Controls"
89
xmlns:viewmodels="clr-namespace:RszTool.App.ViewModels"
910
xmlns:res="clr-namespace:RszTool.App.Resources"
1011
xmlns:rsztool="clr-namespace:RszTool;assembly=RszTool"
@@ -135,18 +136,24 @@
135136
Theme="{Binding DockingTheme}">
136137

137138
<LayoutRoot PropertyChanged="OnLayoutRootPropertyChanged">
138-
<LayoutPanel Orientation="Horizontal">
139-
<LayoutAnchorablePaneGroup DockWidth="300">
140-
<LayoutAnchorablePane>
139+
<LayoutPanel Orientation="Vertical">
140+
<LayoutPanel Orientation="Horizontal">
141+
<LayoutAnchorablePane DockWidth="300">
141142
<LayoutAnchorable Title="File Explorer" ContentId="FileExplorerWindow">
142143
<views:FileExplorerTree DataContext="{Binding FileExplorerViewModel}" HorizontalAlignment="Stretch" />
143144
</LayoutAnchorable>
144145
</LayoutAnchorablePane>
145-
</LayoutAnchorablePaneGroup>
146146

147-
<LayoutDocumentPaneGroup>
148-
<LayoutDocumentPane />
149-
</LayoutDocumentPaneGroup>
147+
<LayoutDocumentPaneGroup>
148+
<LayoutDocumentPane />
149+
</LayoutDocumentPaneGroup>
150+
</LayoutPanel>
151+
152+
<LayoutAnchorablePane DockHeight="150">
153+
<LayoutAnchorable Title="{res:Text ConsoleLog}" ContentId="ConsoleBox">
154+
<controls:ConsoleBox />
155+
</LayoutAnchorable>
156+
</LayoutAnchorablePane>
150157
</LayoutPanel>
151158
</LayoutRoot>
152159
</DockingManager>

RszTool.App/Resources/Texts.Designer.cs

Lines changed: 26 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RszTool.App/Resources/Texts.resx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->
62-
<xsd:schema id="root"
63-
xmlns=""
64-
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
65-
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
6663
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
6764
<xsd:element name="root" msdata:IsDataSet="true">
6865
<xsd:complexType>
@@ -394,4 +391,10 @@ vec:x y z w</value>
394391
<data name="Cancel" xml:space="preserve">
395392
<value>Cancel</value>
396393
</data>
394+
<data name="Clear" xml:space="preserve">
395+
<value>Clear</value>
396+
</data>
397+
<data name="ConsoleLog" xml:space="preserve">
398+
<value>Log</value>
399+
</data>
397400
</root>

RszTool.App/Resources/Texts.zh-CN.resx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->
62-
<xsd:schema id="root"
63-
xmlns=""
64-
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
65-
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
6663
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
6764
<xsd:element name="root" msdata:IsDataSet="true">
6865
<xsd:complexType>
@@ -392,4 +389,10 @@ vec:x y z w</value>
392389
<data name="Cancel" xml:space="preserve">
393390
<value>取消</value>
394391
</data>
392+
<data name="Clear" xml:space="preserve">
393+
<value>清空</value>
394+
</data>
395+
<data name="ConsoleLog" xml:space="preserve">
396+
<value>日志</value>
397+
</data>
395398
</root>

RszTool.App/ViewModels/ThemeManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public class ThemeManager : INotifyPropertyChanged
1313
Application.Current.Resources["LightForeground"] as SolidColorBrush ?? Brushes.Black;
1414
public readonly SolidColorBrush DarkForeground =
1515
Application.Current.Resources["DarkForeground"] as SolidColorBrush ?? Brushes.White;
16+
public readonly SolidColorBrush LightErrorBrush =
17+
Application.Current.Resources["LightErrorBrush"] as SolidColorBrush ?? Brushes.Red;
18+
public readonly SolidColorBrush DarkErrorBrush =
19+
Application.Current.Resources["DarkErrorBrush"] as SolidColorBrush ?? Brushes.Red;
1620

1721
public static ThemeManager Instance
1822
{

RszTool/Common/Log.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace RszTool.Common
2+
{
3+
public static class Log
4+
{
5+
private static string DateTimeString => DateTime.Now.ToString("MM-dd HH:mm:ss"); // .fff
6+
7+
public static void Info(string? message)
8+
{
9+
Console.WriteLine($"{DateTimeString} [I] {message}");
10+
}
11+
12+
public static void Debug(string? message)
13+
{
14+
Console.WriteLine($"{DateTimeString} [D] {message}");
15+
}
16+
17+
public static void Warn(string? message)
18+
{
19+
Console.WriteLine($"{DateTimeString} [W] {message}");
20+
}
21+
22+
public static void WarnIf(bool condition, string? message)
23+
{
24+
if (condition) {
25+
Warn(message);
26+
}
27+
}
28+
29+
public static void Error(string? message)
30+
{
31+
Console.Error.WriteLine($"{DateTimeString} [E] {message}");
32+
}
33+
34+
public static void Error(Exception e)
35+
{
36+
Error(e.Message);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)