Example #1
0
        public void OnLayoutLoaded(LayoutLoadedArgs args)
        {
            var matchingAnchorables = args.LayoutAnchorables.Where(anch => anch.Content.GetType() == Definition.View &&
                                                                   anch.Content.SafeCast <UserControl>().DataContext.GetType() == Definition.ViewModel &&
                                                                   anch.IsVisible);

            var rougeAnchorables = ActivePanels.Where(o => !matchingAnchorables.Select(anch => anch.Content.SafeCast <UserControl>().DataContext).
                                                      Contains(o.Content.SafeCast <UserControl>().DataContext));

            foreach (var anchorable in rougeAnchorables.ToList())
            {
                RemoveDynamicPanel(anchorable);
            }

            foreach (var invalidationSubscription in InvalidationSubscriptions)
            {
                invalidationSubscription.Event.Unsubscribe(invalidationSubscription.Token);
            }

            InvalidationSubscriptions.Clear();
            ActivePanels.Clear();
            foreach (var anchorable in matchingAnchorables)
            {
                ResolveDynamicPanelDependencies(anchorable);
                ActivePanels.Add(anchorable);
            }

            SyncSelection(ActivePanels.Select(o => o.Content.SafeCast <UserControl>().DataContext));
        }
Example #2
0
        public void OnLayoutLoaded(LayoutLoadedArgs args)
        {
            var layoutGroupData = new Dictionary <LayoutAnchorable, LayoutAnchorablePane>();

            var oldViews = anchorableDefinitions.Select(o => o.Key.Content as UserControl).ToList();

            anchorableDefinitions.Clear();
            var staticAnchorables = args.LayoutAnchorables.Where(o => PanelManager.StaticPanelDefinitions.Any(def => def.View == o.Content.GetType() &&
                                                                                                              def.ViewModel == ((UserControl)o.Content).DataContext.GetType()));

            foreach (var anchorable in staticAnchorables)
            {
                var associatedDefinition = PanelManager.StaticPanelDefinitions.Single(o => o.View.GetGuid() == anchorable.Content.GetType().GetGuid() &&
                                                                                      o.ViewModel.GetGuid() == anchorable.Content.SafeCast <UserControl>().DataContext.GetType().GetGuid());
                anchorableDefinitions.Add(anchorable, associatedDefinition);
                anchorable.Hiding += (sender, e) =>
                {
                    var source = sender.SafeCast <LayoutAnchorable>();
                    VisibilityManager.UpdateContainer(source);
                };

                LayoutAnchorablePane layoutGroup = null;
                if (anchorable.Parent != null && anchorable.Parent is LayoutAnchorablePane)
                {
                    layoutGroup = anchorable.Parent as LayoutAnchorablePane;
                }
                else
                {
                    layoutGroup = anchorable.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).
                                  Single(prop => prop.Name == "PreviousContainer").GetValue(anchorable) as LayoutAnchorablePane;
                }

                layoutGroupData.Add(anchorable, layoutGroup);
            }

            // Resolve static panel definitions that were not included in the layout.
            var resolvedDefinitions = anchorableDefinitions.Select(o => o.Value);
            var newDefinitions      = PanelManager.StaticPanelDefinitions.Where(o => !resolvedDefinitions.Contains(o)).ToList();

            foreach (var definition in newDefinitions)
            {
                var config = definition.OfType <StaticPanelConfiguration>().Single();

                var anchorable = new LayoutAnchorable();

                var view = oldViews.Single(o => o.GetType() == definition.View);

                anchorable.Content   = view;
                anchorable.ContentId = definition.View.GetGuid();
                anchorable.Title     = config.Title();

                LayoutAnchorablePane container = null;

                if (anchorableDefinitions.Any(o => o.Value.OfType <StaticPanelConfiguration>().Single().Placement == config.Placement))
                {
                    container = anchorableDefinitions.Where(o => o.Value.OfType <StaticPanelConfiguration>().Single().Placement == config.Placement).Select(o => o.Key).
                                GroupBy(o => layoutGroupData[o]).OrderBy(o => o.Count()).Last().Key;
                }
                else
                {
                    container = DockingView.DockingManager.Layout.Descendents().OfType <LayoutAnchorablePane>().FirstOrDefault();
                }

                container.Children.Add(anchorable);

                anchorable.CanAutoHide = true;
                anchorable.CanFloat    = config.CanFloat();



                anchorable.Hiding += (sender, e) => {
                    var source = sender.SafeCast <LayoutAnchorable>();
                    if (source.IsVisible)
                    {
                        VisibilityManager.UpdateContainer(source);
                    }
                };

                anchorableDefinitions.Add(anchorable, definition);
                layoutGroupData.Add(anchorable, container);
            }

            VisibilityManager.SetLayoutGroupData(layoutGroupData);

            foreach (var definition in newDefinitions)
            {
                var config = definition.OfType <StaticPanelConfiguration>().Single();

                var canOpen    = config.CanOpen();
                var canClose   = config.CanClose();
                var visibility = config.IsVisible();

                if (canOpen == false && canClose == false)
                {
                    throw new Exception($"Error in static panel {definition.ViewModel} metadata. CanOpen() and CanClose() defined " +
                                        $"in the panel definition's configuration metadata can't both return false at the same time.");
                }

                if (!visibility && canClose)
                {
                    var anchorable = anchorableDefinitions.GetKeysForValue(definition).Single();
                    VisibilityManager.SetVisibility(anchorable, false);
                }
            }
        }