protected override async Task Execute() { // if not modal if (!Workitem.IsModal) { // focus next workitem IWorkItem toFocus = null; if (Workitem.IsFocused && CurrentContextService.Collection.Count > 1) { int index = CurrentContextService.Collection.IndexOf(Workitem); if (index > 0) { toFocus = CurrentContextService.Collection[index - 1]; } else if (index == 0) { toFocus = CurrentContextService.Collection[index + 1]; } } else if (Workitem.IsFocused && CurrentContextService.Collection.Count <= 1) { await CurrentContextService.FocusWorkitem(null); } if (toFocus != null) { await CurrentContextService.FocusWorkitem(toFocus); } } }
protected override async Task Execute() { // if not modal if (!(Workitem.IsModal)) { await CurrentContextService.FocusWorkitem((IWorkItem)Workitem.Parent); } }
/// <summary> /// Internal Launch implementation /// </summary> /// <param name="modalMetadata">Modal metadata</param> /// <returns></returns> private async Task <IObservable <WorkitemEventArgs> > LaunchInternal(ModalOptions modalMetadata = null) { try { //Logger.LogWithWorkitemData(String.Format("Opening workitem {0}{1}.", Workitem.WorkItemName, ShouldOpenModal ? " in modal state" : ""), LogLevel.Informative, Workitem); // Trick to let ui have some time to update its state await TaskManager.Run(() => Thread.Sleep(50)).ConfigureAwait(false); // Get the workitem type Type type = Workitem.GetType(); // If workitem is single instance and there is alredy a workitem // with the same type open focus the opened otherwise continue launching SingleInstanceWorkitemAttribute attribute = type.GetCustomAttributes(typeof(SingleInstanceWorkitemAttribute), false).FirstOrDefault() as SingleInstanceWorkitemAttribute; if (attribute != null) { IWorkItem exists = CurrentContextService.Collection.Where(w => w.GetType().Equals(type)).FirstOrDefault(); if (exists != null) { await CurrentContextService.FocusWorkitem(exists).ConfigureAwait(false); return(null); } } var impl = Workitem.GetType().GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ISupportsInitialization <>)); // If workitem supports initialization than do initialize it if (impl != null) { //Logger.LogWithWorkitemData("Initializing workitem", LogLevel.Informative, Workitem); try { var initType = impl.GetGenericArguments()[0]; // initialize if (Data == null || initType.IsAssignableFrom(Data.GetType())) { var methodInfo = Workitem.GetType().GetMethod("Initialize", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); methodInfo.Invoke(Workitem, new object[] { Data }); } else { throw new ArgumentException($"Workitem supports initialization only by {initType}"); } } catch (Exception e) { // Log error with workitem data //Logger.LogWithWorkitemData("Workitem initialization failed", LogLevel.Exception, Workitem, e); // Show error to the UI UIManager.Error("Failed to Initialize Workitem"); return(null); } } // Log //Logger.LogWithWorkitemData("Configuring Workitem", LogLevel.Informative, Workitem); // Configure Workitem Workitem.Configure(); // if should open modal set the workitem window if (ShouldOpenModal) { // if no custom modalMetadata is provided get the workitem default if (modalMetadata == null) { ModalMetadata = Workitem.Configuration.GetOption <ModalOptions>(); } else { ModalMetadata = modalMetadata; } // Set the workitem window in UI thread Application.Current.Dispatcher.InvokeIfNeeded(() => Workitem.Window = CurrentContextService.InitModalWindow(Project.GetOption <RegionOptions>().CreateModalWindow(), Workitem, ModalMetadata)); } // Execute isnatce specific launching await Execute().ConfigureAwait(false); // If Workitem is NullWorkitem set the WorkitemCollection.Null // otherwise add to the collection if (Workitem is NullWorkitem) { CurrentContextService.Collection.Null = Workitem; } else if (!ShouldOpenModal || !ModalMetadata.IsDialog) { CurrentContextService.Collection.Add(Workitem); } // If should open modal open the window if (ShouldOpenModal) { //// Log //Logger.LogWithWorkitemData("Showing modal window", LogLevel.Informative, Workitem); // If should open in dialog mode call Workitem.Window.ShowDialog if (ModalMetadata.IsDialog) { //// end loading now because after show dialog is execution will be blocked //CurrentContextService.EndLoading(); // Show dialog Application.Current.Dispatcher.InvokeIfNeeded(() => Workitem.Window.ShowDialog()); } else { Application.Current.Dispatcher.InvokeIfNeeded(() => Workitem.Window.Show()); } } if (!ShouldOpenModal || !ModalMetadata.IsDialog) { await CurrentContextService.FocusWorkitem(Workitem).ConfigureAwait(false); } // return the communication channel return(Channel); } catch (Exception e) { // Try and fix the broken parts CurrentContextService.Collection.Remove(Workitem); Workitem.Dispose(); //// Log workitem exception //Logger.LogWithWorkitemData("Failed to open workitem", LogLevel.Exception, Workitem, e); // Show error in the UI UIManager.Error("Failed to open workitem"); // return an empty channel return(Observable.Empty <WorkitemEventArgs>()); } finally { //// finally end loding for modal //if (ShouldOpenModal) // CurrentContextService.EndLoading(); } }