Exemple #1
0
 private void Update(ViewManager_LayoutStats stats)
 {
     this.textblockLayout.setText("(Planned layout in " + Math.Round(stats.ViewManager_getBestLayout_Duration.TotalSeconds, 1)
                                  + "s. Completed layout in " + Math.Round(stats.ViewManager_LayoutDuration.TotalSeconds, 1) + "s)");
 }
Exemple #2
0
        // redoes the layout
        private void DoLayout()
        {
            this.needsRelayout = false;

            // determine which views are currently focused so we can re-focus them after redoing the layout
            List <View> focusedViews = new List <View>();

            if (this.specificLayout != null)
            {
                foreach (SpecificLayout layout in this.specificLayout.GetDescendents())
                {
                    if (layout.View != null && layout.View.IsFocused && layout.GetParticipatingChildren().Count() < 1)
                    {
                        focusedViews.Add(layout.View);
                    }
                }
            }

            // check some data in preparation for computing stats
            int      num_grid_preComputations = GridLayout.NumComputations;
            DateTime startTime = DateTime.Now;

            // record the parent of each view before the relayout, to help us know which parents to disconnect
            Dictionary <View, SpecificLayout> preParents = this.findAncestors(this.specificLayout);

            // recompute the new desired layout
            // generally we expect the overall score to be positive, so we start by hypothesizing
            // that there exists a layout with positive score, and only checking negative-scoring layouts if no positive-scoring layout is found
            LayoutQuery query = new MaxScore_LayoutQuery(this.displaySize.Width, this.displaySize.Height, LayoutScore.Zero, this.visualDefaults.LayoutDefaults);
            DateTime    getBestLayout_startDate = DateTime.Now;

            this.specificLayout = this.GetSublayout().GetBestLayout(query);
            if (this.specificLayout == null)
            {
                query = query.WithScore(LayoutScore.Minimum);
                this.specificLayout = this.GetSublayout().GetBestLayout(query);
            }

            DateTime getBestLayout_endDate = DateTime.Now;

            // find the parent of each view after the relayout, to help us know which parents to disconnect
            Dictionary <View, SpecificLayout> postParents = this.findAncestors(this.specificLayout);

            // disconnect any parents that are no longer the same
            foreach (View view in preParents.Keys)
            {
                SpecificLayout preLayout = preParents[view];
                if (preLayout != null)
                {
                    SpecificLayout postLayout = this.DictionaryGet(postParents, view);
                    if (postLayout == null || preLayout.View != postLayout.View)
                    {
                        // The parent of <view> has changed.
                        // Disconnect it from the previous parent.
                        preLayout.Remove_VisualDescendent(view);
                    }
                }
            }

            // record that our layout is up-to-date (so any future updates will trigger a relayout)
            this.Reset_ChangeAnnouncement();

            // update our actual view
            this.mainView.Content = this.specificLayout.DoLayout(displaySize, this.visualDefaults.ViewDefaults);

            // Inform each layout whose view was reattached, in case they need to restore any state that can only be restored after being reattached (most likely because the view system would overwrite it)
            foreach (SpecificLayout layout in postParents.Values)
            {
                layout.AfterLayoutAttached();
            }

            // display stats
            DateTime endTime  = DateTime.Now;
            TimeSpan duration = endTime.Subtract(startTime);

            System.Diagnostics.Debug.WriteLine("ViewManager DoLayout finished in " + duration + " (" + query.Cost + ") queries");
            System.Diagnostics.Debug.WriteLine("Text formatting time = " + TextLayout.TextTime + " for " + TextLayout.NumMeasures + " measures");
            int num_grid_postComputations = GridLayout.NumComputations;

            System.Diagnostics.Debug.WriteLine("Num grid computations = " + (num_grid_postComputations - num_grid_preComputations));
            TextLayout.NumMeasures = 0;
            TextLayout.TextTime    = new TimeSpan();

            // refocus the previously focused views
            foreach (View view in focusedViews)
            {
                if (postParents.ContainsKey(view))
                {
                    view.Focus();
                }
            }

            System.Diagnostics.Debug.WriteLine("ViewManager completed layout at " + DateTime.Now);

            if (this.LayoutCompleted != null)
            {
                ViewManager_LayoutStats stats = new ViewManager_LayoutStats();
                stats.ViewManager_LayoutDuration         = duration;
                stats.ViewManager_getBestLayout_Duration = getBestLayout_endDate.Subtract(getBestLayout_startDate);
                this.LayoutCompleted.Invoke(stats);
            }
        }