Skip to content

Commit 76170db

Browse files
committed
feat: 稍微优化了布局, 以及使用体验
1 parent 2b95fab commit 76170db

File tree

9 files changed

+56
-73
lines changed

9 files changed

+56
-73
lines changed

OpenGptChat/Controls/MarkdownViewer.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,6 @@
1717

1818
namespace OpenGptChat.Controls
1919
{
20-
/// <summary>
21-
/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
22-
///
23-
/// Step 1a) Using this custom control in a XAML file that exists in the current project.
24-
/// Add this XmlNamespace attribute to the root element of the markup file where it is
25-
/// to be used:
26-
///
27-
/// xmlns:MyNamespace="clr-namespace:OpenGptChat.Controls"
28-
///
29-
///
30-
/// Step 1b) Using this custom control in a XAML file that exists in a different project.
31-
/// Add this XmlNamespace attribute to the root element of the markup file where it is
32-
/// to be used:
33-
///
34-
/// xmlns:MyNamespace="clr-namespace:OpenGptChat.Controls;assembly=OpenGptChat.Controls"
35-
///
36-
/// You will also need to add a project reference from the project where the XAML file lives
37-
/// to this project and Rebuild to avoid compilation errors:
38-
///
39-
/// Right click on the target project in the Solution Explorer and
40-
/// "Add Reference"->"Projects"->[Browse to and select this project]
41-
///
42-
///
43-
/// Step 2)
44-
/// Go ahead and use your control in the XAML file.
45-
///
46-
/// <MyNamespace:MarkdownViewer/>
47-
///
48-
/// </summary>
4920
public class MarkdownViewer : Control
5021
{
5122
static MarkdownViewer()

OpenGptChat/Converters/SingleLineTextConverter.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:local="clr-namespace:OpenGptChat.Converters">
4-
<local:SingleLineTextConverter x:Key="SingleLineTextConverter"/>
54
<local:OnlyTakeOneMultiValueConverter x:Key="OnlyTakeOneMultiValueConverter"/>
65
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
76
</ResourceDictionary>

OpenGptChat/Markdown/MarkdownWpfRenderer.cs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using ColorCode.Parsing;
1212
using ColorCode.Styling;
1313
using Markdig.Extensions.Tables;
14+
using Markdig.Extensions.TaskLists;
1415
using Markdig.Syntax;
1516
using Markdig.Syntax.Inlines;
1617
using OpenGptChat.Common.Models;
@@ -123,7 +124,10 @@ public FrameworkElement RenderBlock(Block block)
123124

124125
private FrameworkElement RenderContainerBlock(ContainerBlock containerBlock)
125126
{
126-
StackPanel documentElement = new StackPanel();
127+
StackPanel documentElement = new StackPanel()
128+
{
129+
Margin = new Thickness(0, 0, 0, NormalSize)
130+
};
127131

128132
foreach (var renderedBlock in RenderBlocks(containerBlock))
129133
documentElement.Children.Add(renderedBlock);
@@ -136,6 +140,7 @@ public FrameworkElement RenderTable(Table table)
136140
Border tableElement = new Border()
137141
{
138142
BorderThickness = new Thickness(0, 0, 1, 1),
143+
Margin = new Thickness(0, 0, 0, NormalSize)
139144
};
140145

141146
Grid tableContentElement = new Grid();
@@ -183,6 +188,8 @@ public FrameworkElement RenderTable(Table table)
183188
cellElement
184189
.BindTableBorder();
185190

191+
cellContentElement.Margin = new Thickness(0);
192+
186193
if (rowIndex % 2 == 1)
187194
cellElement.BindTableStripe();
188195

@@ -210,7 +217,7 @@ public FrameworkElement RenderListBlock(ListBlock listBlock)
210217

211218
Border listElement = new Border()
212219
{
213-
Margin = new Thickness(NormalSize / 2, 0, 0, NormalSize / 2)
220+
Margin = new Thickness(NormalSize / 2, 0, 0, NormalSize)
214221
};
215222

216223
Grid listContentElement = new Grid();
@@ -237,10 +244,17 @@ public FrameworkElement RenderListBlock(ListBlock listBlock)
237244
}
238245

239246
int index = 0;
247+
FrameworkElement? lastRenderedItemBlock = null;
240248
foreach (var itemBlock in listBlock)
241249
{
242250
if (RenderBlock(itemBlock) is FrameworkElement renderedItemBlock)
243251
{
252+
lastRenderedItemBlock = renderedItemBlock;
253+
renderedItemBlock.Margin = renderedItemBlock.Margin with
254+
{
255+
Bottom = renderedItemBlock.Margin.Bottom / 4
256+
};
257+
244258
TextBlock marker = new TextBlock();
245259
Grid.SetRow(marker, index);
246260
Grid.SetColumn(marker, 0);
@@ -257,6 +271,12 @@ public FrameworkElement RenderListBlock(ListBlock listBlock)
257271
}
258272
}
259273

274+
if (lastRenderedItemBlock != null)
275+
lastRenderedItemBlock.Margin = lastRenderedItemBlock.Margin with
276+
{
277+
Bottom = 0
278+
};
279+
260280
return listElement;
261281
}
262282

@@ -266,7 +286,7 @@ public FrameworkElement RenderThematicBreakBlock(ThematicBreakBlock thematicBrea
266286
{
267287
HorizontalAlignment = HorizontalAlignment.Stretch,
268288
Height = 1,
269-
Margin = new Thickness(0, 0, 0, 6)
289+
Margin = new Thickness(0, 0, 0, NormalSize)
270290
};
271291

272292
thematicBreakElement
@@ -288,7 +308,7 @@ public FrameworkElement RenderFencedCodeBlock(FencedCodeBlock fencedCodeBlock)
288308
Border codeElement = new Border()
289309
{
290310
CornerRadius = new CornerRadius(3),
291-
Margin = new Thickness(0, 0, 0, NormalSize / 2)
311+
Margin = new Thickness(0, 0, 0, NormalSize)
292312
};
293313

294314
TextBlock codeContentElement = new TextBlock()
@@ -335,7 +355,7 @@ public FrameworkElement RenderCodeBlock(CodeBlock codeBlock)
335355
Border codeElement = new Border()
336356
{
337357
CornerRadius = new CornerRadius(3),
338-
Margin = new Thickness(0, 0, 0, NormalSize / 2)
358+
Margin = new Thickness(0, 0, 0, NormalSize)
339359
};
340360

341361
TextBlock codeContentElement = new TextBlock()
@@ -373,7 +393,7 @@ public FrameworkElement RenderQuoteBlock(QuoteBlock quoteBlock)
373393
BorderThickness = new Thickness(NormalSize / 3, 0, 0, 0),
374394
CornerRadius = new CornerRadius(NormalSize / 4),
375395
Padding = new Thickness(NormalSize / 2, 0, 0, 0),
376-
Margin = new Thickness(0, 0, 0, NormalSize / 2),
396+
Margin = new Thickness(0, 0, 0, NormalSize),
377397
};
378398

379399
StackPanel quoteContentPanel = new StackPanel();
@@ -392,35 +412,26 @@ public FrameworkElement RenderQuoteBlock(QuoteBlock quoteBlock)
392412

393413
public FrameworkElement RenderHeadingBlock(HeadingBlock headingBlock)
394414
{
395-
double? fontSize = headingBlock.Level switch
415+
double fontSize = headingBlock.Level switch
396416
{
397417
1 => Heading1Size,
398418
2 => Heading2Size,
399419
3 => Heading3Size,
400420
4 => Heading4Size,
401-
_ => null
421+
_ => NormalSize
402422
};
403423

404424
TextBlock headingElement = new TextBlock()
405425
{
426+
FontSize = fontSize,
406427
FontWeight = FontWeights.Medium,
407-
// Margin 后面计算
428+
Margin = new Thickness(0, 0, 0, NormalSize)
408429
};
409430

410431
headingElement
411432
.BindMainForeground()
412433
.BindMainBackground();
413434

414-
if (fontSize.HasValue)
415-
{
416-
headingElement.FontSize = fontSize.Value;
417-
headingElement.Margin = new Thickness(0, 0, 0, fontSize.Value / 2);
418-
}
419-
else
420-
{
421-
headingElement.Margin = new Thickness(0, 0, 0, headingElement.FontSize / 2);
422-
}
423-
424435
if (headingBlock.Inline != null)
425436
headingElement.Inlines.AddRange(
426437
RenderInlines(headingBlock.Inline));
@@ -433,7 +444,7 @@ public FrameworkElement RenderParagraphBlock(ParagraphBlock paragraphBlock)
433444
TextBlock paragraphElement = new TextBlock()
434445
{
435446
TextWrapping = TextWrapping.Wrap,
436-
Margin = new Thickness(0, 0, 0, NormalSize / 2),
447+
Margin = new Thickness(0, 0, 0, NormalSize),
437448

438449
FontSize = NormalSize,
439450
};
@@ -507,12 +518,25 @@ public WpfDocs.Inline RenderInline(Inline inline)
507518
{
508519
return RenderContainerInline(containerInline);
509520
}
521+
else if (inline is TaskList taskListInline)
522+
{
523+
return RenderTaskListInline(taskListInline);
524+
}
510525
else
511526
{
512527
return new WpfDocs.Run();
513528
}
514529
}
515530

531+
private WpfDocs.Inline RenderTaskListInline(TaskList taskListInline)
532+
{
533+
return new CheckBox()
534+
{
535+
IsChecked = taskListInline.Checked,
536+
IsEnabled = false,
537+
}.WrapWithContainer();
538+
}
539+
516540
private WpfDocs.Inline RenderAutolinkInline(AutolinkInline autolinkInline)
517541
{
518542
return new WpfDocs.Run(autolinkInline.Url);

OpenGptChat/Models/AppConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public partial class AppConfig : ObservableObject
1818
private int _apiTimeout = 5000;
1919

2020
[ObservableProperty]
21-
private double _temerature = 1;
21+
private double _temerature = .5;
2222

2323
[ObservableProperty]
2424
private bool _enableChatContext = true;

OpenGptChat/Services/ChatService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public Task<ChatDialogue> ChatAsync(Guid sessionId, string message, Action<strin
7878
return ChatCoreAsync(sessionId, message, messageHandler, cancellation.Token);
7979
}
8080

81+
public Task<string> GetTitleAsync(Guid sessionId, CancellationToken token)
82+
{
83+
throw new NotImplementedException();
84+
}
85+
8186
public void Cancel()
8287
{
8388
cancellation?.Cancel();

OpenGptChat/ViewModels/ChatMessageModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ public ChatMessageModel(ChatMessage storage)
3030
private string _role = "user";
3131

3232
[ObservableProperty]
33+
[NotifyPropertyChangedFor(
34+
nameof(SingleLineContent))]
3335
private string _content = string.Empty;
3436

37+
public string SingleLineContent => Content.Replace('\n', ' ');
38+
3539
[ObservableProperty]
3640
[NotifyPropertyChangedFor(
3741
nameof(IsReadOnly))]

OpenGptChat/Views/Pages/ChatPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
xmlns:controls="clr-namespace:OpenGptChat.Controls"
88
xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
99
xmlns:utilities="clr-namespace:OpenGptChat.Utilities"
10-
mc:Ignorable="d"
10+
mc:Ignorable="d" FocusVisualStyle="{x:Null}"
1111
d:DesignHeight="600" d:DesignWidth="730"
1212
d:Background="White" d:DataContext="{d:DesignInstance Type=local:ChatPage}"
1313
Title="ChatPage">

OpenGptChat/Views/Pages/MainPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
<s:String x:Key="SessionMessageNameFormat">{0}:</s:String>
150150
</TextBlock.Resources>
151151
<Run Text="{Binding PageModel.LastMessage.DisplayName,Mode=OneWay,StringFormat={StaticResource SessionMessageNameFormat}}"/>
152-
<Run Text="{Binding PageModel.LastMessage.Content,Mode=OneWay,Converter={StaticResource SingleLineTextConverter},FallbackValue=Empty}"/>
152+
<Run Text="{Binding PageModel.LastMessage.SingleLineContent,Mode=OneWay,FallbackValue='Empty'}"/>
153153
</TextBlock>
154154
</StackPanel>
155155
</DataTemplate>

0 commit comments

Comments
 (0)