[corlib] Update ValueTuple implementation
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / BreadCrumbTextConverter.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.Activities.Presentation.View
6 {
7     using System.Windows.Data;
8     using System.Activities.Presentation.Model;
9
10     internal class BreadCrumbTextConverter : IMultiValueConverter
11     {
12         const int MaxDisplayNameLength = 20;
13         double pixelsPerChar = 6.5;
14
15         internal double PixelsPerChar
16         {
17             get { return this.pixelsPerChar; }
18             set { this.pixelsPerChar = Math.Max(5.0, value); }
19         }
20
21         public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
22         {
23             int actualDisplayNameLength = MaxDisplayNameLength;
24             ModelItem boundModelItem = values[0] as ModelItem;
25
26             // default to root item's typename
27             string breadCrumbText = (null != boundModelItem ? boundModelItem.ItemType.Name : "<null>");
28             // if there is a display name property on root use that as the file name.
29             if (values[1] is ModelItem)
30             {
31                 ModelItem displayNameProperty = (ModelItem)values[1];
32                 if (typeof(string) == displayNameProperty.ItemType)
33                 {
34                     values[1] = displayNameProperty.GetCurrentValue();
35                 }
36             }
37             if (values[1] is string)
38             {
39                 string displayName = (string)values[1];
40                 if (!displayName.Equals(string.Empty))
41                 {
42                     breadCrumbText = displayName;
43                 }
44             }
45             if (values.Length == 3 && values[2] is double)
46             {
47                 double actualControlWidth = (double)values[2];
48                 actualDisplayNameLength = (int)Math.Max(MaxDisplayNameLength, actualControlWidth / pixelsPerChar);
49
50             }
51             if (breadCrumbText.Length > actualDisplayNameLength)
52             {
53                 breadCrumbText = breadCrumbText.Substring(0, actualDisplayNameLength - 3) + "...";
54             }
55             return breadCrumbText;
56         }
57
58         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
59         {
60             throw FxTrace.Exception.AsError(new InvalidOperationException());
61         }
62     }
63 }