XIVChat/XIVChat Desktop/MainWindow.xaml.cs

163 lines
5.1 KiB
C#
Raw Normal View History

2020-10-23 21:24:32 +00:00
using System;
2020-11-01 01:31:10 +00:00
using System.Collections.Concurrent;
2020-10-23 21:24:32 +00:00
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
2020-11-01 01:31:10 +00:00
using XIVChatCommon;
2020-10-23 21:24:32 +00:00
namespace XIVChat_Desktop {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
2020-11-01 01:31:10 +00:00
public partial class MainWindow {
public App App => (App)Application.Current;
public List<ServerMessage> Messages { get; } = new List<ServerMessage>();
2020-11-01 01:31:10 +00:00
2020-10-23 21:24:32 +00:00
public MainWindow() {
2020-11-01 01:31:10 +00:00
this.InitializeComponent();
this.DataContext = this;
}
private T? FindElementByName<T>(DependencyObject element, string sChildName) where T : FrameworkElement {
T? childElement = null;
var nChildCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < nChildCount; i++) {
if (!(VisualTreeHelper.GetChild(element, i) is FrameworkElement child)) {
continue;
}
if (child is T t && child.Name.Equals(sChildName)) {
childElement = t;
break;
}
childElement = this.FindElementByName<T>(child, sChildName);
if (childElement != null) {
break;
}
}
return childElement;
}
public void ClearAllMessages() {
this.Messages.Clear();
foreach (var tab in this.App.Config.Tabs) {
tab.ClearMessages();
}
}
public void AddSystemMessage(string content) {
var message = new ServerMessage(
DateTime.UtcNow,
0,
new byte[0],
Encoding.UTF8.GetBytes(content),
new List<Chunk> {
new TextChunk(content) {
2020-11-01 01:31:10 +00:00
Foreground = 0xb38cffff,
},
}
);
2020-11-01 01:31:10 +00:00
this.AddMessage(message);
}
private int lastSequence = -1;
private int insertAt;
public void AddReversedChunk(ServerMessage[] messages, int sequence) {
if (sequence != this.lastSequence) {
this.lastSequence = sequence;
this.insertAt = this.Messages.Count;
}
// detect if scroller is at the bottom
var scroller = this.FindElementByName<ScrollViewer>(this.Tabs, "scroller");
var wasAtBottom = Math.Abs(scroller!.VerticalOffset - scroller.ScrollableHeight) < .0001;
// add messages to main list
this.Messages.InsertRange(this.insertAt, messages);
// add message to each tab if the filter allows for it
foreach (var tab in this.App.Config.Tabs) {
tab.AddReversedChunk(messages, sequence);
}
// scroll to the bottom if previously at the bottom
if (wasAtBottom) {
scroller.ScrollToBottom();
}
}
2020-11-01 01:31:10 +00:00
public void AddMessage(ServerMessage message) {
// detect if scroller is at the bottom
var scroller = this.FindElementByName<ScrollViewer>(this.Tabs, "scroller");
var wasAtBottom = Math.Abs(scroller!.VerticalOffset - scroller.ScrollableHeight) < .0001;
// add message to main list
this.Messages.Add(message);
2020-11-01 01:31:10 +00:00
// add message to each tab if the filter allows for it
foreach (var tab in this.App.Config.Tabs) {
tab.AddMessage(message);
}
// scroll to the bottom if previously at the bottom
if (wasAtBottom) {
scroller.ScrollToBottom();
}
}
private void Connect_Click(object sender, RoutedEventArgs e) {
var dialog = new ConnectDialog {
Owner = this,
};
dialog.ShowDialog();
}
private void Disconnect_Click(object sender, RoutedEventArgs e) {
this.App.Disconnect();
}
private void Input_Submit(object sender, KeyEventArgs e) {
if (e.Key != Key.Return) {
return;
}
var conn = this.App.Connection;
if (conn == null) {
return;
}
if (!(sender is TextBox)) {
return;
}
var textBox = (TextBox)sender;
conn.SendMessage(textBox.Text);
textBox.Text = "";
}
private void Configuration_Click(object sender, RoutedEventArgs e) {
new ConfigWindow(this, this.App.Config).Show();
}
private void Tabs_Loaded(object sender, RoutedEventArgs e) {
this.Tabs.SelectedIndex = 0;
}
private void Tabs_SelectionChanged(object sender, SelectionChangedEventArgs e) {
var scroller = this.FindElementByName<ScrollViewer>(this.Tabs, "scroller");
scroller?.ScrollToBottom();
}
private void ManageTabs_Click(object sender, RoutedEventArgs e) {
new ManageTabs(this).Show();
2020-10-23 21:24:32 +00:00
}
}
}