Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fixed high CPU usage in the recent files widget #12169

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 57 additions & 57 deletions src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
ItemClick="RecentsView_ItemClick"
ItemContainerTransitions="{x:Null}"
ItemsSource="{x:Bind recentItemsCollection}"
RightTapped="ListView_RightTapped"
SelectionMode="None">
<ListView.ItemContainerStyle>
<Style BasedOn="{StaticResource DefaultListViewItemStyle}" TargetType="ListViewItem">
Expand All @@ -51,65 +52,64 @@
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="fs:RecentItem">
<ListViewItem RightTapped="ListViewItem_RightTapped" ToolTipService.ToolTip="{x:Bind RecentPath}">
Copy link
Member

@yaira2 yaira2 Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible that having a ListViewItem inside the ListViewItem template broke virtualization. In retrospect I should have caught this when the initial change was implemented. Anyways, the right click issue is back, but we can work around it by moving the right click event to the item container.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I did not check it enough. I have fixed it with reference to the implementation in the column layout.

<Grid
Padding="2.5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AutomationProperties.Name="{x:Bind Name}"
ColumnSpacing="14">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<FontIcon
x:Name="RecentFolderImg"
Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
x:Load="{x:Bind FolderImg}"
x:Phase="1"
FontSize="24"
Foreground="#ffe793"
Glyph="&#xE8B7;" />
<FontIcon
x:Name="EmptyImg"
Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
x:Load="{x:Bind EmptyImgVis}"
x:Phase="1"
FontSize="24"
Glyph="&#xE7C3;" />
<Image
x:Name="RecentFileImg"
Grid.Column="0"
Width="24"
Height="24"
HorizontalAlignment="Center"
VerticalAlignment="Center"
x:Load="{x:Bind FileIconVis}"
x:Phase="1"
Source="{x:Bind FileImg, Mode=OneWay}"
Stretch="Uniform" />
<Grid
Padding="2.5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AutomationProperties.Name="{x:Bind Name}"
ColumnSpacing="14"
ToolTipService.ToolTip="{x:Bind RecentPath}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<FontIcon
x:Name="RecentFolderImg"
Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
x:Load="{x:Bind FolderImg}"
x:Phase="1"
FontSize="24"
Foreground="#ffe793"
Glyph="&#xE8B7;" />
<FontIcon
x:Name="EmptyImg"
Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
x:Load="{x:Bind EmptyImgVis}"
x:Phase="1"
FontSize="24"
Glyph="&#xE7C3;" />
<Image
x:Name="RecentFileImg"
Grid.Column="0"
Width="24"
Height="24"
HorizontalAlignment="Center"
VerticalAlignment="Center"
x:Load="{x:Bind FileIconVis}"
x:Phase="1"
Source="{x:Bind FileImg, Mode=OneWay}"
Stretch="Uniform" />

<TextBlock
Grid.Column="1"
VerticalAlignment="Center"
Text="{x:Bind Name}"
TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap" />
<TextBlock
Grid.Column="1"
VerticalAlignment="Center"
Text="{x:Bind Name}"
TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap" />

<TextBlock
Grid.Column="2"
VerticalAlignment="Center"
FontSize="12"
Text="{x:Bind RecentPath}"
TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap" />
</Grid>
</ListViewItem>
<TextBlock
Grid.Column="2"
VerticalAlignment="Center"
FontSize="12"
Text="{x:Bind RecentPath}"
TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Expand Down
8 changes: 3 additions & 5 deletions src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ public RecentFilesWidget()
OpenPropertiesCommand = new RelayCommand<RecentItem>(OpenProperties);
}

private void ListViewItem_RightTapped(object sender, RightTappedRoutedEventArgs e)
private void ListView_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
ItemContextMenuFlyout = new CommandBarFlyout { Placement = FlyoutPlacementMode.Full };
ItemContextMenuFlyout.Opening += (sender, e) => App.LastOpenedFlyout = sender as CommandBarFlyout;
if (sender is not ListViewItem listViewItem || listViewItem.DataContext is not RecentItem item)
if (e.OriginalSource is not FrameworkElement element || element.DataContext is not RecentItem item)
return;

var menuItems = GetItemMenuItems(item, false);
Expand All @@ -132,11 +132,9 @@ private void ListViewItem_RightTapped(object sender, RightTappedRoutedEventArgs
.ForEach(i => i.MinWidth = Constants.UI.ContextMenuItemsMaxWidth);

secondaryElements.ForEach(i => ItemContextMenuFlyout.SecondaryCommands.Add(i));
ItemContextMenuFlyout.ShowAt(listViewItem, new FlyoutShowOptions { Position = e.GetPosition(listViewItem) });
ItemContextMenuFlyout.ShowAt(element, new FlyoutShowOptions { Position = e.GetPosition(element) });

_ = ShellContextmenuHelper.LoadShellMenuItems(item.Path, ItemContextMenuFlyout, showOpenWithMenu: true, showSendToMenu: true);

e.Handled = true;
}

public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCardItem item, bool isPinned, bool isFolder = false)
Expand Down