Skip to content

Commit

Permalink
Simplify the style
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosifne committed Apr 1, 2021
1 parent 8c5abe6 commit 2d4edce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,6 @@
</Setter>
</Style>

<!-- The key is referenced from the code behind -->
<!-- Style used to display a single member's information, if click the button, it pops up a menu contains targets' information (one level menu)-->
<Style x:Key="SingleMemberContextMenuStyle" TargetType="{x:Type ContextMenu}">
<Setter Property="ItemContainerStyle" Value="{StaticResource TargetMenuItemStyle}"/>
<Setter Property="ItemsSource" Value="{Binding MenuItemViewModels}"/>
<Setter Property="MaxHeight" Value="400"/>
<Setter Property="StaysOpen" Value="True"/>
<Setter Property="Background" Value="{DynamicResource VsBrush.CommandBarMenuBackgroundGradient}"/>
<Setter Property="platformUi:ImageThemingUtilities.ImageBackgroundColor" Value="{Binding Path=Background, RelativeSource={RelativeSource Self}, Converter={StaticResource BrushToColorConverter}}" />
<Setter Property="FontFamily" Value="{DynamicResource VsFont.EnvironmentFontFamily}" />
<Setter Property="FontSize" Value="{DynamicResource VsFont.EnvironmentFontSize}" />
<Setter Property="Template" Value="{StaticResource ContextMenuTemplate}"/>
<EventSetter Event="Closed" Handler="ContextMenu_OnClose" />
<EventSetter Event="Opened" Handler="TargetsMenu_OnOpen" />
</Style>

<!-- Style used to display one member's information and pops up a submenu show all the targets -->
<Style x:Key="MemberMenuItemStyle" TargetType="{x:Type MenuItem}">
<Setter Property="Icon" Value="{StaticResource NonSharedIcon}"/>
Expand Down Expand Up @@ -188,12 +172,9 @@
</Setter>
</Style>


<!-- The key is referenced from the code behind -->
<!-- Style to show multiple members, when click the button it first pops a menu for all the members on this line (first level menu),
then each member has a second level of context menu, show its targets -->
<Style x:Key="MultipleMembersContextMenuStyle" TargetType="ContextMenu">
<Setter Property="ItemContainerStyle" Value="{StaticResource MemberMenuItemStyle}"/>
<!-- Style used to display context menu -->
<Style x:Key="ContextMenuStyle" TargetType="{x:Type ContextMenu}">
<Setter Property="ItemContainerStyle" Value="{StaticResource TargetMenuItemStyle}"/>
<Setter Property="ItemsSource" Value="{Binding MenuItemViewModels}"/>
<Setter Property="MaxHeight" Value="400"/>
<Setter Property="StaysOpen" Value="True"/>
Expand All @@ -203,12 +184,19 @@
<Setter Property="FontSize" Value="{DynamicResource VsFont.EnvironmentFontSize}" />
<Setter Property="Template" Value="{StaticResource ContextMenuTemplate}"/>
<EventSetter Event="Closed" Handler="ContextMenu_OnClose" />
<EventSetter Event="Opened" Handler="ContextMenu_OnOpen" />
<Style.Triggers>
<!-- For multiple members on the same line, when click the button it first pops a menu for all the members on this line (first level menu) -->
<DataTrigger Binding="{Binding HasMultipleMembers}" Value="True">
<Setter Property="ItemContainerStyle" Value="{StaticResource MemberMenuItemStyle}"/>
</DataTrigger>
</Style.Triggers>
</Style>

<!-- The key is referenced from the code behind -->
<Style x:Key="ToolTipStyle" TargetType="ToolTip">
<Setter Property="Background" Value="{DynamicResource {x:Static vsui:EnvironmentColors.ToolTipBrushKey }}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static vsui:EnvironmentColors.ToolTipBorderBrushKey }}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static vsui:EnvironmentColors.ToolTipBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static vsui:EnvironmentColors.ToolTipBorderBrushKey}}"/>
</Style>
</Button.Resources>

Expand All @@ -225,6 +213,6 @@

<Button.ContextMenu>
<!-- Ensure the context menu is not null -->
<ContextMenu/>
<ContextMenu Style="{StaticResource ContextMenuStyle}"/>
</Button.ContextMenu>
</Button>
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ public InheritanceMargin(
DataContext = viewModel;
ContextMenu.DataContext = viewModel;
ToolTip = new ToolTip { Content = viewModel.ToolTipTextBlock, Style = (Style)FindResource("ToolTipStyle") };

if (tag.MembersOnLine.Length == 1)
{
ContextMenu.Style = (Style)FindResource(SingleMemberContextMenuStyle);
}
else
{
ContextMenu.Style = (Style)FindResource(MultipleMembersContextMenuStyle);
}
}

private void InheritanceMargin_OnClick(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -115,6 +106,15 @@ private void ContextMenu_OnClose(object sender, RoutedEventArgs e)
ResetBorderToInitialColor();
}

private void ContextMenu_OnOpen(object sender, RoutedEventArgs e)
{
// If this context menu just has one member, then if the context menu open, it means all inheritance targets are shown.
if (e.OriginalSource is ContextMenu { DataContext: InheritanceMarginViewModel { HasMultipleMembers: false }})
{
Logger.Log(FunctionId.InheritanceMargin_TargetsMenuOpen);
}
}

private void TargetsMenu_OnOpen(object sender, RoutedEventArgs e)
{
Logger.Log(FunctionId.InheritanceMargin_TargetsMenuOpen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ internal class InheritanceMarginViewModel
/// </summary>
public ImmutableArray<InheritanceContextMenuItemViewModel> MenuItemViewModels { get; }

public bool HasMultipleMembers { get; }

private InheritanceMarginViewModel(
ImageMoniker imageMoniker,
TextBlock toolTipTextBlock,
string automationName,
ImmutableArray<InheritanceContextMenuItemViewModel> menuItemViewModels)
ImmutableArray<InheritanceContextMenuItemViewModel> menuItemViewModels,
bool hasMultipleMembers)
{
ImageMoniker = imageMoniker;
ToolTipTextBlock = toolTipTextBlock;
AutomationName = automationName;
MenuItemViewModels = menuItemViewModels;
HasMultipleMembers = hasMultipleMembers;
}

public static InheritanceMarginViewModel Create(
Expand All @@ -65,7 +69,7 @@ public static InheritanceMarginViewModel Create(
var automationName = member.DisplayTexts.JoinText() + textAppended;
var menuItemViewModels = member.TargetItems
.SelectAsArray(TargetMenuItemViewModel.Create).CastArray<InheritanceContextMenuItemViewModel>();
return new InheritanceMarginViewModel(tag.Moniker, toolTipTextBlock, automationName, menuItemViewModels);
return new InheritanceMarginViewModel(tag.Moniker, toolTipTextBlock, automationName, menuItemViewModels, false);
}
else
{
Expand All @@ -74,14 +78,12 @@ public static InheritanceMarginViewModel Create(
Text = EditorFeaturesWpfResources.Multiple_members_are_inherited
};

var allMemberDisplayNames = members.SelectAsArray(member => member.DisplayTexts.JoinText());

// Use a different automation name for accessiblity purpose.
// Same automation name can't be set for control. So add the line number info.
var automationName = string.Format(EditorFeaturesWpfResources.Multiple_members_are_inherited_on_line_0, tag.LineNumber);
var menuItemViewModels = tag.MembersOnLine
.SelectAsArray(MemberMenuItemViewModel.Create)
.CastArray<InheritanceContextMenuItemViewModel>();
return new InheritanceMarginViewModel(tag.Moniker, textBlock, automationName, menuItemViewModels);
return new InheritanceMarginViewModel(tag.Moniker, textBlock, automationName, menuItemViewModels, true);
}
}

Expand Down

0 comments on commit 2d4edce

Please sign in to comment.