JJServiceManagerEnt/Core/Ui.cs

115 lines
4.1 KiB
C#

using System;
using System.IO;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace JJServiceManager
{
/// <summary>Views 폴더의 XAML 리소스를 실행 시 읽어들임</summary>
public static class XamlLoader
{
public static object Load(string fileName)
{
var asm = typeof(XamlLoader).Assembly;
var resName = "JJServiceManager.Views." + fileName;
using var stream = asm.GetManifestResourceStream(resName)
?? throw new InvalidOperationException("화면 리소스를 찾을 수 없습니다: " + resName);
return XamlReader.Load(stream);
}
public static FrameworkElement LoadElement(string fileName) => (FrameworkElement)Load(fileName);
public static T Find<T>(this FrameworkElement root, string name) where T : class
=> root.FindName(name) as T ?? throw new InvalidOperationException($"'{name}' 요소를 찾을 수 없습니다.");
}
public static class AppIcon
{
private static ImageSource _image;
private static System.Drawing.Icon _icon;
/// <summary>트레이용 아이콘</summary>
public static System.Drawing.Icon Icon
{
get
{
if (_icon == null)
{
try { _icon = System.Drawing.Icon.ExtractAssociatedIcon(Environment.ProcessPath); }
catch { _icon = System.Drawing.SystemIcons.Application; }
}
return _icon;
}
}
/// <summary>창 제목줄용 아이콘</summary>
public static ImageSource Image
{
get
{
if (_image == null)
{
try
{
_image = Imaging.CreateBitmapSourceFromHIcon(Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
_image.Freeze();
}
catch { }
}
return _image;
}
}
public static void Apply(Window w)
{
var img = Image;
if (img != null) w.Icon = img;
}
}
public static class Brushes2
{
public static SolidColorBrush Make(string hex)
{
var b = new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString(hex));
b.Freeze();
return b;
}
public static readonly SolidColorBrush Running = Make("#2E7D32");
public static readonly SolidColorBrush Stopped = Make("#C62828");
public static readonly SolidColorBrush Pending = Make("#EF6C00");
public static readonly SolidColorBrush Unknown = Make("#757575");
}
public static class Dialogs
{
public static bool Confirm(Window owner, string message, string title = "확인")
{
var r = owner != null && owner.IsVisible
? MessageBox.Show(owner, message, title, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No)
: MessageBox.Show(message, title, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
return r == MessageBoxResult.Yes;
}
public static void Info(Window owner, string message, string title = "알림")
{
if (owner != null && owner.IsVisible)
MessageBox.Show(owner, message, title, MessageBoxButton.OK, MessageBoxImage.Information);
else
MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Information);
}
public static void Error(Window owner, string message, string title = "오류")
{
if (owner != null && owner.IsVisible)
MessageBox.Show(owner, message, title, MessageBoxButton.OK, MessageBoxImage.Warning);
else
MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
}