Skip to content

Commit

Permalink
Merge branch 'main' into make-tiles-smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi authored Dec 9, 2024
2 parents 02700cc + 69b7193 commit c716607
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 17 deletions.
47 changes: 32 additions & 15 deletions AIDevGallery/Samples/Open Source Models/Language Models/Chat.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
Padding="-12,0,-12,24"
ItemTemplateSelector="{StaticResource ChatTemplateSelector}"
ItemsSource="{x:Bind Messages}"
SelectionMode="None">
SelectionMode="None"
Loaded="InvertedListView_Loaded">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel VerticalAlignment="Bottom" ItemsUpdatingScrollMode="KeepLastItemInView" />
Expand All @@ -92,21 +93,37 @@
Grid.Row="1"
AutomationProperties.Name="Prompt input"
KeyUp="TextBox_KeyUp"
PlaceholderText="Type your prompt" />
PlaceholderText="Enter your prompt (Press Shift + Enter to insert a newline)"
TextChanged="InputBox_TextChanged"
TextWrapping="Wrap"
AcceptsReturn="True"
MaxHeight="148"
ScrollViewer.VerticalScrollBarVisibility="Auto"/>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<Button
x:Name="SendBtn"
AutomationProperties.Name="Send Message"
Click="SendBtn_Click"
Content="Send"
Style="{StaticResource AccentButtonStyle}" />
<Button
x:Name="StopBtn"
AutomationProperties.Name="Stop"
Click="StopBtn_Click"
Content="Stop"
Style="{ThemeResource AccentButtonStyle}"
Visibility="Collapsed" />
<Button x:Name="SendBtn"
AutomationProperties.Name="Send Message"
Click="SendBtn_Click"
Style="{StaticResource AccentButtonStyle}"
IsEnabled="False">
<StackPanel Orientation="Horizontal"
Spacing="8">
<FontIcon FontSize="16"
Glyph="&#xE725;" />
<TextBlock Text="Send" />
</StackPanel>
</Button>
<Button x:Name="StopBtn"
AutomationProperties.Name="Stop"
Click="StopBtn_Click"
Style="{ThemeResource AccentButtonStyle}"
Visibility="Collapsed">
<StackPanel Orientation="Horizontal"
Spacing="8">
<FontIcon FontSize="16"
Glyph="&#xE73B;" />
<TextBlock Text="Stop" />
</StackPanel>
</Button>
</StackPanel>

</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using System;
using System.Collections.ObjectModel;
using System.Linq;
Expand Down Expand Up @@ -47,6 +48,8 @@ public Chat()
this.InitializeComponent();
}

private ScrollViewer? scrollViewer;

protected override async Task LoadModelAsync(SampleNavigationParameters sampleParams)
{
model = await sampleParams.GetIChatClientAsync();
Expand All @@ -71,15 +74,30 @@ private void CancelResponse()
{
StopBtn.Visibility = Visibility.Collapsed;
SendBtn.Visibility = Visibility.Visible;
EnableInputBoxWithPlaceholder();
cts?.Cancel();
cts?.Dispose();
cts = null;
}

private void TextBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter && sender is TextBox)
if (e.Key == Windows.System.VirtualKey.Enter &&
!Microsoft.UI.Input.InputKeyboardSource.GetKeyStateForCurrentThread(Windows.System.VirtualKey.Shift)
.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down) &&
sender is TextBox &&
!string.IsNullOrWhiteSpace(InputBox.Text))
{
var cursorPosition = InputBox.SelectionStart;
var text = InputBox.Text;
if (cursorPosition > 0 && (text[cursorPosition - 1] == '\n' || text[cursorPosition - 1] == '\r'))
{
text = text.Remove(cursorPosition - 1, 1);
InputBox.Text = text;
}

InputBox.SelectionStart = cursorPosition - 1;

SendMessage();
}
}
Expand All @@ -101,7 +119,7 @@ private void AddMessage(string text)
return;
}

Messages.Add(new Message(text, DateTime.Now, ChatRole.User));
Messages.Add(new Message(text.Trim(), DateTime.Now, ChatRole.User));
var contentStartedBeingGenerated = false; // <exclude-line>
NarratorHelper.Announce(InputBox, "Generating response, please wait.", "ChatWaitAnnouncementActivityId"); // <exclude-line>>

Expand All @@ -115,6 +133,8 @@ private void AddMessage(string text)
{
Messages.Add(responseMessage);
StopBtn.Visibility = Visibility.Visible;
InputBox.IsEnabled = false;
InputBox.PlaceholderText = "Please wait for the response to complete before entering a new prompt";
});

cts = new CancellationTokenSource();
Expand Down Expand Up @@ -147,6 +167,7 @@ private void AddMessage(string text)
NarratorHelper.Announce(InputBox, "Content has finished generating.", "ChatDoneAnnouncementActivityId"); // <exclude-line>
StopBtn.Visibility = Visibility.Collapsed;
SendBtn.Visibility = Visibility.Visible;
EnableInputBoxWithPlaceholder();
});
});
}
Expand All @@ -160,4 +181,64 @@ private void StopBtn_Click(object sender, RoutedEventArgs e)
{
CancelResponse();
}

private void InputBox_TextChanged(object sender, TextChangedEventArgs e)
{
SendBtn.IsEnabled = !string.IsNullOrWhiteSpace(InputBox.Text);
}

private void EnableInputBoxWithPlaceholder()
{
InputBox.IsEnabled = true;
InputBox.PlaceholderText = "Enter your prompt (Press Shift + Enter to insert a newline)";
}

private void InvertedListView_Loaded(object sender, RoutedEventArgs e)
{
scrollViewer = FindElement<ScrollViewer>(InvertedListView);

ItemsStackPanel? itemsStackPanel = FindElement<ItemsStackPanel>(InvertedListView);
if (itemsStackPanel != null)
{
itemsStackPanel.SizeChanged += ItemsStackPanel_SizeChanged;
}
}

private void ItemsStackPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (scrollViewer != null)
{
bool isScrollbarVisible = scrollViewer.ComputedVerticalScrollBarVisibility == Visibility.Visible;

if (isScrollbarVisible)
{
InvertedListView.Padding = new Thickness(-12, 0, 12, 24);
}
else
{
InvertedListView.Padding = new Thickness(-12, 0, -12, 24);
}
}
}

private T? FindElement<T>(DependencyObject element)
where T : DependencyObject
{
if (element is T targetElement)
{
return targetElement;
}

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
var child = VisualTreeHelper.GetChild(element, i);
var result = FindElement<T>(child);
if (result != null)
{
return result;
}
}

return null;
}
}

0 comments on commit c716607

Please sign in to comment.