181 lines
7.6 KiB
C#
181 lines
7.6 KiB
C#
using mbts.ui.shell;
|
|
using System;
|
|
using System.IO;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
|
|
namespace mbts.ui {
|
|
public static class ShellWindow {
|
|
private class AnonShellWindow : IShellWindow {
|
|
private ImageSource _icon;
|
|
public ImageSource Icon {
|
|
get { return _icon; }
|
|
set { _icon = value; IconChanged?.Invoke(); }
|
|
}
|
|
public IShellModule Owner { get; set; }
|
|
private string _title;
|
|
public string Title {
|
|
get { return _title; }
|
|
set { _title = value; TitleChanged?.Invoke(); }
|
|
}
|
|
public string window_type { get; set; }
|
|
|
|
public event Action IconChanged;
|
|
public event Action TitleChanged;
|
|
|
|
public IDisposable Disposable;
|
|
public void Dispose() {
|
|
mbts.Disposable.dispose(ref Disposable);
|
|
}
|
|
public void GetState(Stream save_here) {
|
|
get_state?.Invoke(Content.Value, save_here);
|
|
}
|
|
|
|
public Action<UIElement,Stream> get_state;
|
|
|
|
public ShellWindowUI GetContent() {
|
|
return new ShellWindowUI(Content.Value);
|
|
}
|
|
|
|
public Lazy<UIElement> Content { get; set; }
|
|
|
|
public Action<ShellWindowVM> on_shell_window_vm { get; set; }
|
|
|
|
void IShellWindow.on_shell_window_vm(ShellWindowVM shell_window_vm) =>
|
|
on_shell_window_vm?.Invoke(shell_window_vm);
|
|
}
|
|
|
|
class LazyAnonShellWindow : AnonShellWindow, ISharedWindow {
|
|
public string thread_id { get; set; }
|
|
|
|
public void thread_init() { }
|
|
}
|
|
|
|
public static IShellWindow create(IShellModule owner, string title, string window_type, Lazy<UIElement> content, Action<UIElement, Stream> get_state = null, ImageSource icon = null, string thread_id=null, Action<ShellWindowVM> on_shell_window_vm = null, Action<KeyEventArgs> on_key_down = null) {
|
|
IShellWindow rv = null;
|
|
if (string.IsNullOrEmpty(thread_id)) {
|
|
rv = create<AnonShellWindow>(owner, title, window_type, content, get_state, icon, on_shell_window_vm);
|
|
if (on_key_down != null)
|
|
rv = new KeyListenerWrapper(rv, on_key_down);
|
|
}
|
|
else {
|
|
var res = create<LazyAnonShellWindow>(owner, title, window_type, content, get_state, icon, on_shell_window_vm);
|
|
res.thread_id = thread_id;
|
|
rv = on_key_down == null ? (ISharedWindow)res : new SharedKeyListenerWrapper(res, on_key_down);
|
|
}
|
|
return rv;
|
|
}
|
|
static T create<T>(IShellModule owner, string title, string window_type, Lazy<UIElement> content, Action<UIElement,Stream> get_state=null, ImageSource icon=null, Action<ShellWindowVM> on_shell_window_vm = null) where T : AnonShellWindow, new() {
|
|
var result = new T() {
|
|
Title = title,
|
|
window_type = window_type,
|
|
Icon = icon ?? (owner != null ? owner.icon : Icons.Window),
|
|
Owner = owner,
|
|
get_state = get_state,
|
|
on_shell_window_vm = on_shell_window_vm,
|
|
Content = content,
|
|
Disposable = Disposable.Create(() => {
|
|
if (content.IsValueCreated) {
|
|
if (content.Value is IDisposable d) { d.Dispose(); }
|
|
if ((content.Value as FrameworkElement)?.DataContext is IDisposable dc) { dc.Dispose(); }
|
|
}
|
|
})
|
|
};
|
|
return result;
|
|
}
|
|
public static IShellWindow create(IShellModule owner, string title, FrameworkElement content, IDisposable dispose, Action<Stream> get_state, ImageSource icon=null) {
|
|
var result = new AnonShellWindow() {
|
|
Title = title,
|
|
Icon = icon ?? (owner != null ? owner.icon : Icons.Window),
|
|
Owner = owner,
|
|
get_state = (_,s) => get_state(s),
|
|
Content = new Lazy<UIElement>(() => content),
|
|
Disposable = dispose
|
|
};
|
|
return result;
|
|
}
|
|
|
|
public static Window dialog(IShellWindow owner, string title, FrameworkElement content,
|
|
bool allow_resize=false,
|
|
bool? show_close=null,
|
|
bool? show_minimize=null,
|
|
bool? show_maximize=null) {
|
|
|
|
var window = new Window() {
|
|
Title = title,
|
|
SizeToContent = SizeToContent.WidthAndHeight,
|
|
Content = content
|
|
};
|
|
window.Style = window.TryFindResource("BTSWindow") as Style;
|
|
WindowStyle.SetAllowResize(window, allow_resize);
|
|
if (show_close.HasValue) WindowStyle.SetShowCloseButton(window, show_close.Value);
|
|
if (show_minimize.HasValue) WindowStyle.SetShowMinimizeButton(window, show_minimize.Value);
|
|
if (show_maximize.HasValue) WindowStyle.SetShowMaximizeButton(window, show_maximize.Value);
|
|
|
|
window.CommandBindings.Add(new System.Windows.Input.CommandBinding(SystemCommands.CloseWindowCommand, (s,e) => window.Close()));
|
|
if (show_close == true) {
|
|
window.PreviewKeyDown += (s, e) => {
|
|
if (e.Key == System.Windows.Input.Key.Escape) {
|
|
window.Close();
|
|
}
|
|
};
|
|
}
|
|
|
|
IDisposable cleanup = content.DataContext as IDisposable;
|
|
if (cleanup != null) { window.Closed += (o, e) => cleanup.Dispose(); }
|
|
return window;
|
|
}
|
|
|
|
class KeyListenerWrapper<T> : IShellWindow, IKeyListener where T : class, IShellWindow {
|
|
readonly T _model;
|
|
readonly Action<KeyEventArgs> _key_down;
|
|
|
|
public KeyListenerWrapper(T model, Action<KeyEventArgs> key_down) {
|
|
_model = model ?? throw new ArgumentNullException(nameof(model));
|
|
_key_down = key_down ?? throw new ArgumentNullException(nameof(key_down));
|
|
}
|
|
|
|
public event Action TitleChanged {
|
|
add => _model.TitleChanged += value;
|
|
remove => _model.TitleChanged -= value;
|
|
}
|
|
|
|
public event Action IconChanged {
|
|
add => _model.IconChanged += value;
|
|
remove => _model.IconChanged -= value;
|
|
}
|
|
|
|
public string Title { get => _model.Title; set => _model.Title = value; }
|
|
public string window_type => _model.window_type;
|
|
public ImageSource Icon => _model.Icon;
|
|
public IShellModule Owner => _model.Owner;
|
|
|
|
public void Dispose() => _model.Dispose();
|
|
public ShellWindowUI GetContent() => _model.GetContent();
|
|
public void GetState(Stream save_here) => _model.GetState(save_here);
|
|
public void on_shell_window_vm(ShellWindowVM shell_window_vm) => _model.on_shell_window_vm(shell_window_vm);
|
|
public void key_down(KeyEventArgs e) =>
|
|
_key_down(e);
|
|
}
|
|
|
|
class KeyListenerWrapper : KeyListenerWrapper<IShellWindow> {
|
|
public KeyListenerWrapper(IShellWindow model, Action<KeyEventArgs> key_down)
|
|
: base(model, key_down) { }
|
|
}
|
|
|
|
class SharedKeyListenerWrapper : KeyListenerWrapper<ISharedWindow>, ISharedWindow {
|
|
readonly ISharedWindow _model;
|
|
readonly Action<KeyEventArgs> _key_down;
|
|
|
|
public SharedKeyListenerWrapper(ISharedWindow model, Action<KeyEventArgs> key_down)
|
|
: base(model, key_down) {
|
|
_model = model ?? throw new ArgumentNullException(nameof(model));
|
|
_key_down = key_down ?? throw new ArgumentNullException(nameof(key_down));
|
|
}
|
|
|
|
public string thread_id => _model.thread_id;
|
|
public void thread_init() => _model.thread_init();
|
|
}
|
|
}
|
|
} |