Skip to content

Commit 4d79542

Browse files
committed
dynamically adjust tab title length
1 parent ad1b9ae commit 4d79542

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

sources/RevitDBExplorer/Extensions/System/StringExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public static string RemovePrefix(this string input, string prefix)
7474
public static string Truncate(this string value, int maxChars)
7575
{
7676
if (value == null) return null;
77-
return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
77+
if (maxChars <= 3) return value;
78+
79+
return value.Length <= maxChars ? value : value.Substring(0, maxChars - 3) + "...";
7880
}
7981

8082
public static string ReplaceMany(this string input, IEnumerable<(string, string)> replacements)

sources/RevitDBExplorer/UIComponents/Workspaces/WorkspaceViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,8 @@ void IAmScriptOpener.Open(string scriptText)
201201
{
202202
openRDSWithGivenScript(scriptText);
203203
}
204+
205+
206+
public void RefreshTab() => OnPropertyChanged(nameof(InfoAboutSource));
204207
}
205208
}

sources/RevitDBExplorer/UIComponents/Workspaces/WorkspacesView.xaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
xmlns:componentCAndC="clr-namespace:RevitDBExplorer.UIComponents.CommandAndControl"
1414
xmlns:controls="clr-namespace:RevitDBExplorer.WPF.Controls"
1515
mc:Ignorable="d"
16-
d:DesignHeight="450" d:DesignWidth="800">
16+
d:DesignHeight="450"
17+
d:DesignWidth="800"
18+
SizeChanged="Grid_SizeChanged"
19+
>
1720
<UserControl.Resources>
1821
<ResourceDictionary>
1922
<ResourceDictionary.MergedDictionaries>
@@ -27,8 +30,8 @@
2730
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverterHidden" WhenFalse="Hidden"/>
2831
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverterHiddenInverted" WhenFalse="Visible" WhenTrue="Hidden"/>
2932
<converters:EnumMatchToVisibilityConverter x:Key="EnumMatchToVisibilityConverter"/>
30-
<converters:StringTruncateConverter x:Key="StringTruncateConverter" MaxChars="27"/>
31-
33+
<converters:StringTruncateConverter x:Key="StringTruncateConverter" MaxChars="{Binding TabTitleMaxLength}"/>
34+
3235

3336
<Style TargetType="{x:Type TabItem}">
3437
<Setter Property="Template">

sources/RevitDBExplorer/UIComponents/Workspaces/WorkspacesView.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,13 @@ public WorkspacesView()
1010
{
1111
InitializeComponent();
1212
}
13+
14+
private void Grid_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
15+
{
16+
if (this.DataContext is WorkspacesViewModel workspacesViewModel)
17+
{
18+
workspacesViewModel.Width = e.NewSize.Width;
19+
}
20+
}
1321
}
1422
}

sources/RevitDBExplorer/UIComponents/Workspaces/WorkspacesViewModel.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ internal class WorkspacesViewModel : BaseViewModel
2424
private readonly ObservableCollection<WorkspaceViewModel> workspaces = new ObservableCollection<WorkspaceViewModel>();
2525
private WorkspaceViewModel selectedWorkspace;
2626
private double firstColumnWidth;
27+
private int tabTitleMaxLength =27;
28+
private double width;
2729

2830
public event Action<SelectedItemChangedEventArgs> SelectedItemsChanged;
2931
public ObservableCollection<WorkspaceViewModel> Workspaces => workspaces;
@@ -40,6 +42,30 @@ public WorkspaceViewModel SelectedWorkspace
4042
OnPropertyChanged();
4143
}
4244
}
45+
public int TabTitleMaxLength
46+
{
47+
get
48+
{
49+
return tabTitleMaxLength;
50+
}
51+
set
52+
{
53+
tabTitleMaxLength = value;
54+
OnPropertyChanged();
55+
}
56+
}
57+
public double Width
58+
{
59+
get
60+
{
61+
return width;
62+
}
63+
set
64+
{
65+
width = value;
66+
AdjustTabTitleLength();
67+
}
68+
}
4369

4470

4571
public WorkspacesViewModel(IAmWindowOpener windowOpener, IAmQueryExecutor queryExecutor, Action<string> openRDSWithGivenScript)
@@ -94,6 +120,7 @@ private void SetSelectedWorkspace(WorkspaceViewModel workspaceViewModel)
94120
{
95121
selectedWorkspace = workspaceViewModel;
96122
OnPropertyChanged(nameof(SelectedWorkspace));
123+
AdjustTabTitleLength();
97124
}
98125
private WorkspaceViewModel GetFirstAvailableWorkspace()
99126
{
@@ -153,6 +180,20 @@ private void Workspace_ListSelectedItemChanged(ListSelectedItemChangedEventArgs
153180
{
154181
SelectedItemsChanged?.Invoke(new SelectedItemChangedEventArgs(null, eventArgs.NewOne));
155182
}
183+
184+
public void AdjustTabTitleLength()
185+
{
186+
if (!double.IsNaN(width))
187+
{
188+
var activeWorkspaces = Workspaces.Count(x => x.IsActive == true);
189+
var availableWidth = Math.Max(width - 222, 200);
190+
191+
192+
int magicValue = (int)Math.Floor((availableWidth / activeWorkspaces) / 5.55) - 3;
193+
TabTitleMaxLength = Math.Max(magicValue, 7);
194+
Workspaces.ForEach(x => x.RefreshTab());
195+
}
196+
}
156197
}
157198

158199
internal record class SelectedItemChangedEventArgs(TreeItem treeItem, IListItem listItem);

sources/RevitDBExplorer/WPF/Converters/StringTruncateConverter.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,27 @@
55

66
namespace RevitDBExplorer.WPF.Converters
77
{
8-
internal class StringTruncateConverter : DependencyObject, IValueConverter
8+
internal class StringTruncateConverter : Freezable, IValueConverter
99
{
10+
protected override Freezable CreateInstanceCore()
11+
{
12+
return new StringTruncateConverter();
13+
}
14+
15+
16+
1017
public int MaxChars
1118
{
1219
get => (int)GetValue(MaxCharsProperty);
1320
set => SetValue(MaxCharsProperty, value);
1421
}
15-
public static readonly DependencyProperty MaxCharsProperty = DependencyProperty.Register("MaxChars", typeof(int), typeof(StringTruncateConverter), new PropertyMetadata(0));
22+
public static readonly DependencyProperty MaxCharsProperty = DependencyProperty.Register("MaxChars", typeof(int), typeof(StringTruncateConverter), new PropertyMetadata(0, MaxCharsPropertyChanged));
23+
24+
25+
private static void MaxCharsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
26+
{
27+
28+
}
1629

1730
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1831
{

0 commit comments

Comments
 (0)