Update titlebar filecount text

This commit is contained in:
Samuel Chapleau
2022-12-07 20:47:35 -08:00
parent b46b8d176f
commit e504653323
5 changed files with 79 additions and 44 deletions

View File

@@ -26,7 +26,11 @@ namespace Peek.UI
[ObservableProperty]
private List<File> files = new ();
public int CurrentItemIndex { get; private set; } = UninitializedItemIndex;
[ObservableProperty]
private bool isMultiSelection;
[ObservableProperty]
private int currentItemIndex = UninitializedItemIndex;
private CancellationTokenSource CancellationTokenSource { get; set; } = new CancellationTokenSource();
@@ -35,6 +39,7 @@ namespace Peek.UI
public void Clear()
{
CurrentFile = null;
IsMultiSelection = false;
if (InitializeFilesTask != null && InitializeFilesTask.Status == TaskStatus.Running)
{
@@ -89,6 +94,8 @@ namespace Peek.UI
return;
}
IsMultiSelection = selectedItems.Count > 1;
// Prioritize setting CurrentFile, which notifies UI
var firstSelectedItem = selectedItems.Item(0);
CurrentFile = new File(firstSelectedItem.Path);

View File

@@ -30,6 +30,8 @@
x:Name="TitleBarControl"
Grid.Row="0"
File="{x:Bind ViewModel.FolderItemsQuery.CurrentFile, Mode=OneWay}"
FileIndex="{x:Bind ViewModel.FolderItemsQuery.CurrentItemIndex, Mode=OneWay}"
IsMultiSelection="{x:Bind ViewModel.FolderItemsQuery.IsMultiSelection, Mode=OneWay}"
NumberOfFiles="{x:Bind ViewModel.FolderItemsQuery.Files.Count, Mode=OneWay}" />
<fp:FilePreview

View File

@@ -122,7 +122,7 @@
<comment>Name of application.</comment>
</data>
<data name="AppTitle_FileCounts_Text" xml:space="preserve">
<value>({0}/{1} files)</value>
<value>({0}/{1})</value>
<comment>Text for the file count in the titlebar. 0: the index of the current file. 1: the total number of files selected.</comment>
</data>
<data name="LaunchAppButton_OpenWith_Text" xml:space="preserve">

View File

@@ -42,7 +42,7 @@
FontWeight="Bold"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{x:Bind FileCountText, Mode=OneWay}"
Visibility="Collapsed" />
Visibility="{x:Bind IsMultiSelection, Mode=OneWay}" />
<TextBlock
x:Name="AppTitle_FileName"
@@ -95,7 +95,6 @@
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="AppTitle_FileName.MaxWidth" Value="560" />
<Setter Target="AppTitle_FileCount.Visibility" Value="Visible" />
<Setter Target="LaunchAppButton_Text.Visibility" Value="Visible" />
</VisualState.Setters>
</VisualState>
@@ -105,7 +104,6 @@
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="AppTitle_FileName.MaxWidth" Value="400" />
<Setter Target="AppTitle_FileCount.Visibility" Value="Visible" />
<Setter Target="LaunchAppButton_Text.Visibility" Value="Visible" />
</VisualState.Setters>
</VisualState>
@@ -115,7 +113,6 @@
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="AppTitle_FileName.MaxWidth" Value="320" />
<Setter Target="AppTitle_FileCount.Visibility" Value="Visible" />
<Setter Target="LaunchAppButton_Text.Visibility" Value="Collapsed" />
</VisualState.Setters>
</VisualState>

View File

@@ -23,18 +23,32 @@ namespace Peek.UI.Views
public sealed partial class TitleBar : UserControl
{
public static readonly DependencyProperty FileProperty =
DependencyProperty.Register(
nameof(File),
typeof(File),
typeof(TitleBar),
new PropertyMetadata(null, (d, e) => ((TitleBar)d).OnFilePropertyChanged()));
DependencyProperty.Register(
nameof(File),
typeof(File),
typeof(TitleBar),
new PropertyMetadata(null, (d, e) => ((TitleBar)d).OnFilePropertyChanged()));
public static readonly DependencyProperty FileIndexProperty =
DependencyProperty.Register(
nameof(FileIndex),
typeof(int),
typeof(TitleBar),
new PropertyMetadata(-1, (d, e) => ((TitleBar)d).OnFileIndexPropertyChanged()));
public static readonly DependencyProperty IsMultiSelectionProperty =
DependencyProperty.Register(
nameof(IsMultiSelection),
typeof(bool),
typeof(TitleBar),
new PropertyMetadata(false));
public static readonly DependencyProperty NumberOfFilesProperty =
DependencyProperty.Register(
nameof(NumberOfFiles),
typeof(int),
typeof(TitleBar),
new PropertyMetadata(null, null));
DependencyProperty.Register(
nameof(NumberOfFiles),
typeof(int),
typeof(TitleBar),
new PropertyMetadata(null, null));
private string? defaultAppName;
@@ -58,6 +72,18 @@ namespace Peek.UI.Views
set => SetValue(FileProperty, value);
}
public int FileIndex
{
get => (int)GetValue(FileIndexProperty);
set => SetValue(FileIndexProperty, value);
}
public bool IsMultiSelection
{
get => (bool)GetValue(IsMultiSelectionProperty);
set => SetValue(IsMultiSelectionProperty, value);
}
public int NumberOfFiles
{
get => (int)GetValue(NumberOfFilesProperty);
@@ -83,6 +109,31 @@ namespace Peek.UI.Views
}
}
[RelayCommand]
private async void LaunchDefaultAppButtonAsync()
{
StorageFile storageFile = await File.GetStorageFileAsync();
LauncherOptions options = new ();
if (string.IsNullOrEmpty(defaultAppName))
{
// If there's no default app found, open the App picker
options.DisplayApplicationPicker = true;
}
else
{
// Try to launch the default app for current file format
bool result = await Launcher.LaunchFileAsync(storageFile, options);
if (!result)
{
// If we couldn't successfully open the default app, open the App picker as a fallback
options.DisplayApplicationPicker = true;
await Launcher.LaunchFileAsync(storageFile, options);
}
}
}
private void UpdateTitleBarCustomization(MainWindow mainWindow)
{
if (AppWindowTitleBar.IsCustomizationSupported())
@@ -111,15 +162,18 @@ namespace Peek.UI.Views
UpdateDefaultAppToLaunch();
}
private void OnFileIndexPropertyChanged()
{
UpdateFileCountText();
}
private void UpdateFileCountText()
{
// Update file count
if (NumberOfFiles > 1)
{
// TODO: Update the hardcoded fileIndex when the NFQ PR gets merged
int currentFileIndex = 1;
string fileCountTextFormat = ResourceLoader.GetForViewIndependentUse().GetString("AppTitle_FileCounts_Text");
FileCountText = string.Format(fileCountTextFormat, currentFileIndex, NumberOfFiles);
FileCountText = string.Format(fileCountTextFormat, FileIndex + 1, NumberOfFiles);
}
}
@@ -134,30 +188,5 @@ namespace Peek.UI.Views
string openWithAppToolTipFormat = ResourceLoader.GetForViewIndependentUse().GetString("LaunchAppButton_OpenWithApp_ToolTip");
OpenWithAppToolTip = string.Format(openWithAppToolTipFormat, defaultAppName);
}
[RelayCommand]
private async void LaunchDefaultAppButtonAsync()
{
StorageFile storageFile = await File.GetStorageFileAsync();
LauncherOptions options = new ();
if (string.IsNullOrEmpty(defaultAppName))
{
// If there's no default app found, open the App picker
options.DisplayApplicationPicker = true;
}
else
{
// Try to launch the default app for current file format
bool result = await Launcher.LaunchFileAsync(storageFile, options);
if (!result)
{
// If we couldn't successfully open the default app, open the App picker as a fallback
options.DisplayApplicationPicker = true;
await Launcher.LaunchFileAsync(storageFile, options);
}
}
}
}
}