123 lines
5.0 KiB
C#
123 lines
5.0 KiB
C#
namespace mbts.ui.shell {
|
|
using bts;
|
|
|
|
using mbts.config;
|
|
using mbts.ui.extensions;
|
|
|
|
using Spark.Core.Disposables;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Threading;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
|
|
/// <summary>
|
|
/// This is the "application" object for the bts_shell UI. It is responsible
|
|
/// for creating the main objects which make the up interface (the launcher
|
|
/// and the collection of open windows) and displaying them.
|
|
/// </summary>
|
|
class ShellBootstrapper : BTSApplicationBootstrapper {
|
|
Window _login;
|
|
|
|
public ShellBootstrapper(
|
|
string user = null,
|
|
string password = null,
|
|
string config_source = null,
|
|
bool? restore_workspace = null,
|
|
LogLevel config_console_log_level = LogLevel.Warn,
|
|
LogLevel config_file_log_level = LogLevel.Info
|
|
) : base(config_source) {
|
|
// Get the LoginModel instance on this thread to make sure it performs syncronous config initialization.
|
|
var login_model = LoginModel.init(
|
|
config_source, config_console_log_level, config_file_log_level);
|
|
// Show the dialog on a background thread so animations aren't blocked during the main application startup
|
|
var thread = new Thread(thread_start) {
|
|
IsBackground = true,
|
|
Name = "Login"
|
|
};
|
|
thread.SetApartmentState(ApartmentState.STA);
|
|
thread.Start();
|
|
|
|
// If there was an error in config we will know by now
|
|
if (login_model.HasFatalError) {
|
|
// Block further startup but keep the LoginView responsive since it is displaying the error message.
|
|
Dispatcher.PushFrame(new());
|
|
// Should never get here but if we do bail quickly.
|
|
Environment.Exit(-1);
|
|
}
|
|
|
|
void thread_start() {
|
|
var workspace = restore_workspace ?? false ? WorkspacesModel.ACTIVE_WORKSPACE_NAME : null;
|
|
var login_vm = new LoginViewModel(login_model, Dispatcher.CurrentDispatcher);
|
|
_login = new LoginView(login_vm);
|
|
init_auto_login();
|
|
_login.ShowDialog();
|
|
Dispatcher.CurrentDispatcher.InvokeShutdown();
|
|
|
|
void init_auto_login() {
|
|
if (user is null) {
|
|
return;
|
|
}
|
|
var login_hooks = new ImmutableDisposable();
|
|
login_vm.begin_login += on_begin_login;
|
|
login_hooks += () => login_vm.begin_login -= on_begin_login;
|
|
/// Attempt to login as soon as `login_vm.KnownUsers` contains `user`
|
|
login_hooks += login_vm.KnownUsers.Subscribe((s, e) => auto_login());
|
|
if (restore_workspace.HasValue) {
|
|
login_vm.resume_workspace = restore_workspace.Value;
|
|
login_hooks += login_vm.Workspaces.Subscribe((s, e) => auto_login());
|
|
}
|
|
login_hooks += _login.SubscribeClosed((s, e) => Disposable.dispose(ref login_hooks));
|
|
|
|
void on_begin_login() =>
|
|
Disposable.dispose(ref login_hooks);
|
|
|
|
void auto_login() {
|
|
if (!login_vm.KnownUsers.Contains(user) || (workspace is not null && !login_vm.Workspaces.Contains(workspace))) {
|
|
return;
|
|
}
|
|
Disposable.dispose(ref login_hooks);
|
|
|
|
login_vm.Name = user;
|
|
if (workspace is not null) {
|
|
login_vm.SelectedWorkspace = workspace;
|
|
}
|
|
login_vm.Password = password ?? string.Empty;
|
|
|
|
// Must dispose `login_hooks` before setting `login_vm.Name` to
|
|
// avoid re-entrancy, as this method is called from a callback
|
|
// to `login_vm.Workspaces.CollectionChanged`, and the
|
|
// `login_vm.Name` setter causes `login_vm.Workspaces` to
|
|
// mutate, and thus trigger the CollectionChanged event.
|
|
if (workspace is not null) {
|
|
login_vm.SelectedWorkspace = workspace;
|
|
}
|
|
login_vm.LoginCommand.TryExecuteIfCan(null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void CreateShell(ShellModel model) {
|
|
if (Configuration.get<bts.config.ShellRenderMode>().SoftwareOnly) {
|
|
System.Windows.Media.RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;
|
|
}
|
|
|
|
var launcher = model.create<ILauncher>();
|
|
if (_login != null) {
|
|
launcher.shown += () => _login.Dispatcher.InvokeAsync(destroy_login);
|
|
|
|
void destroy_login() {
|
|
_login.Close();
|
|
_login = null;
|
|
}
|
|
}
|
|
|
|
launcher.show();
|
|
model.notify_delayed_load();
|
|
}
|
|
}
|
|
}
|