You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
using Sln.Wcs.UI.ViewModels;
namespace Sln.Wcs.UI.Views;
public partial class MainWindow : Window
{
private readonly NavigationViewModel _navVm;
private readonly List<Popup> _openPopups = new();
public MainWindow(NavigationViewModel navigationVm)
{
InitializeComponent();
_navVm = navigationVm;
DataContext = _navVm;
_navVm.PageChanged += OnPageChanged;
BuildMenu();
_navVm.LoadDefaultPage();
// 时钟
var timer = new System.Timers.Timer(1000);
timer.Elapsed += (_, _) =>
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
ClockText.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
timer.Start();
ClockText.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
// 退出按钮
LogoutBtn.Click += (_, _) => Close();
}
private void BuildMenu()
{
MenuContainer.Children.Clear();
foreach (var item in _navVm.TopMenuItems)
{
var btn = new Button
{
Content = item.Label,
Background = Brushes.Transparent,
Foreground = Brush.Parse("#8B9BB5"),
FontSize = 13,
Padding = new Thickness(14, 12),
Cursor = new Cursor(StandardCursorType.Hand),
BorderThickness = new Thickness(0),
};
if (item.Action != null)
{
btn.Click += (_, _) => item.Action();
}
if (item.Children != null && item.Children.Count > 0)
{
var popup = new Popup
{
PlacementTarget = btn,
Placement = PlacementMode.Bottom,
IsLightDismissEnabled = false,
};
var popupBorder = new Border
{
Background = Brush.Parse("#0F1620"),
BorderBrush = Brush.Parse("#1A2F4A"),
BorderThickness = new Thickness(1),
CornerRadius = new CornerRadius(4),
MinWidth = 160,
};
var stack = new StackPanel { Margin = new Thickness(4) };
foreach (var sub in item.Children)
{
var subBtn = new Button
{
Content = sub.Label,
Background = Brushes.Transparent,
Foreground = Brush.Parse("#8B9BB5"),
FontSize = 12,
HorizontalAlignment = HorizontalAlignment.Stretch,
HorizontalContentAlignment = HorizontalAlignment.Left,
Padding = new Thickness(12, 8),
BorderThickness = new Thickness(0),
Cursor = new Cursor(StandardCursorType.Hand),
};
subBtn.Click += (_, _) =>
{
popup.Close();
sub.Action();
};
subBtn.PointerEntered += (_, _) =>
subBtn.Background = Brush.Parse("#1B3A5C");
subBtn.PointerExited += (_, _) =>
subBtn.Background = Brushes.Transparent;
stack.Children.Add(subBtn);
}
popupBorder.Child = stack;
popup.Child = popupBorder;
// Hover 展开,离开按钮或 popup 时关闭
var closeTimer = new System.Timers.Timer(200) { AutoReset = false };
bool mouseInPopup = false;
bool mouseInButton = false;
popupBorder.PointerEntered += (_, _) => { mouseInPopup = true; closeTimer.Stop(); };
popupBorder.PointerExited += (_, _) => { mouseInPopup = false; TryClose(); };
btn.PointerEntered += (_, _) =>
{
mouseInButton = true;
closeTimer.Stop();
btn.Background = Brush.Parse("#1B3A5C");
btn.Foreground = Brush.Parse("#4FC3F7");
CloseAllPopups();
popup.Open();
};
btn.PointerExited += (_, _) =>
{
mouseInButton = false;
TryClose();
};
void TryClose()
{
closeTimer.Stop();
closeTimer.Start();
closeTimer.Elapsed += (_, _) =>
{
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
{
if (!mouseInButton && !mouseInPopup)
{
popup.Close();
}
});
};
}
popup.Closed += (_, _) =>
{
btn.Background = Brushes.Transparent;
btn.Foreground = Brush.Parse("#8B9BB5");
};
_openPopups.Add(popup);
}
else
{
btn.PointerEntered += (_, _) =>
{
btn.Background = Brush.Parse("#1B3A5C");
btn.Foreground = Brush.Parse("#4FC3F7");
};
btn.PointerExited += (_, _) =>
{
btn.Background = Brushes.Transparent;
btn.Foreground = Brush.Parse("#8B9BB5");
};
}
MenuContainer.Children.Add(btn);
}
}
private void CloseAllPopups()
{
foreach (var p in _openPopups)
p.Close();
}
private void OnPageChanged(Control? view)
{
if (view != null)
{
view.HorizontalAlignment = HorizontalAlignment.Stretch;
view.VerticalAlignment = VerticalAlignment.Stretch;
ContentArea.Child = view;
}
}
}