2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-06-30 16:23:41 -04:00

ready for testing

This commit is contained in:
Xjph
2024-01-21 13:35:03 -03:30
parent d99a190869
commit 97e981bae2
92 changed files with 3061 additions and 1186 deletions

View File

@ -1,7 +1,14 @@
using Observatory.Framework.Interfaces;
using Observatory.Framework;
using System.Collections;
using Observatory.PluginManagement;
using Observatory.Utils;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using System.Data.Common;
using System.ComponentModel.Design.Serialization;
namespace Observatory.UI
{
@ -39,6 +46,7 @@ namespace Observatory.UI
Font = menu.Items[0].Font,
TextAlign = menu.Items[0].TextAlign
};
ThemeManager.GetInstance.RegisterControl(newItem);
menu.Items.Add(newItem);
if (plugin.PluginUI.PluginUIType == Framework.PluginUI.UIType.Basic)
@ -49,7 +57,10 @@ namespace Observatory.UI
private static Panel CreateBasicUI(IObservatoryPlugin plugin)
{
Panel panel = new();
Panel panel = new()
{
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top
};
IObservatoryComparer columnSorter;
if (plugin.ColumnSorter != null)
@ -57,7 +68,7 @@ namespace Observatory.UI
else
columnSorter = new DefaultSorter();
ListView listView = new()
PluginListView listView = new()
{
View = View.Details,
Location = new Point(0, 0),
@ -65,16 +76,120 @@ namespace Observatory.UI
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top,
BackColor = Color.FromArgb(64, 64, 64),
ForeColor = Color.LightGray,
GridLines = true,
ListViewItemSorter = columnSorter,
Font = new Font(new FontFamily("Segoe UI"), 10, FontStyle.Regular)
};
foreach (var property in plugin.PluginUI.DataGrid.First().GetType().GetProperties())
string colSize = Properties.Core.Default.ColumnSizing;
List<ColumnSizing>? columnSizing = null;
if (!string.IsNullOrWhiteSpace(colSize))
{
listView.Columns.Add(property.Name);
try
{
columnSizing = JsonSerializer.Deserialize<List<ColumnSizing>>(colSize);
}
catch
{
// Failed deserialization means bad value, blow it away.
Properties.Core.Default.ColumnSizing = string.Empty;
Properties.Core.Default.Save();
}
}
columnSizing ??= new List<ColumnSizing>();
// Is losing column sizes between versions acceptable?
ColumnSizing pluginColumnSizing = columnSizing
.Where(c => c.PluginName == plugin.Name && c.PluginVersion == plugin.Version)
.FirstOrDefault(new ColumnSizing() { PluginName = plugin.Name, PluginVersion = plugin.Version });
if (!columnSizing.Contains(pluginColumnSizing))
{
columnSizing.Add(pluginColumnSizing);
}
foreach (var property in plugin.PluginUI.DataGrid.First().GetType().GetProperties())
{
// https://stackoverflow.com/questions/5796383/insert-spaces-between-words-on-a-camel-cased-token
string columnLabel = Regex.Replace(
Regex.Replace(
property.Name,
@"(\P{Ll})(\P{Ll}\p{Ll})",
"$1 $2"
),
@"(\p{Ll})(\P{Ll})",
"$1 $2"
);
int width;
if (pluginColumnSizing.ColumnWidth.ContainsKey(columnLabel))
{
width = pluginColumnSizing.ColumnWidth[columnLabel];
}
else
{
var widthAttrib = property.GetCustomAttribute<ColumnSuggestedWidth>();
width = widthAttrib == null
// Rough approximation of width by label length if none specified.
? columnLabel.Length * 10
: widthAttrib.Width;
pluginColumnSizing.ColumnWidth.Add(columnLabel, width);
}
listView.Columns.Add(columnLabel, width);
}
Properties.Core.Default.ColumnSizing = JsonSerializer.Serialize(columnSizing);
Properties.Core.Default.Save();
// Oddly, the listview resize event often fires after the column size change but
// with stale (default?!) column width values.
// Still need a resize handler to avoid the ugliness of the rightmost column
// leaving gaps, but preventing saving the width changes there should stop the
// stale resize event from overwriting with bad data.
// Using a higher-order function here to create two different versions of the
// event handler for these purposes.
var handleColSize = (bool saveProps) =>
(object? sender, EventArgs e) =>
{
int colTotalWidth = 0;
ColumnHeader? rightmost = null;
foreach (ColumnHeader column in listView.Columns)
{
colTotalWidth += column.Width;
if (rightmost == null || column.DisplayIndex > rightmost.DisplayIndex)
rightmost = column;
if (saveProps)
{
if (pluginColumnSizing.ColumnWidth.ContainsKey(column.Text))
pluginColumnSizing.ColumnWidth[column.Text] = column.Width;
else
pluginColumnSizing.ColumnWidth.Add(column.Text, column.Width);
}
}
if (rightmost != null && colTotalWidth < listView.Width)
{
rightmost.Width = listView.Width - (colTotalWidth - rightmost.Width);
if (saveProps)
pluginColumnSizing.ColumnWidth[rightmost.Text] = rightmost.Width;
}
if (saveProps)
{
Properties.Core.Default.ColumnSizing = JsonSerializer.Serialize(columnSizing);
Properties.Core.Default.Save();
}
};
listView.ColumnWidthChanged += handleColSize(true).Invoke;
listView.Resize += handleColSize(false).Invoke;
listView.ColumnClick += (sender, e) =>
{
if (e.Column == columnSorter.SortColumn)
@ -99,10 +214,10 @@ namespace Observatory.UI
};
panel.Controls.Add(listView);
plugin.PluginUI.DataGrid.CollectionChanged += (sender, e) =>
{
listView.Invoke(() =>
var updateGrid = () =>
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add &&
e.NewItems != null)
@ -153,7 +268,16 @@ namespace Observatory.UI
listView.Items.Add(listItem);
}
}
});
};
if (listView.Created)
{
listView.Invoke(updateGrid);
}
else
{
updateGrid();
}
};
return panel;