Skip to content

Commit

Permalink
Add terminal model
Browse files Browse the repository at this point in the history
  • Loading branch information
gave92 committed May 11, 2024
1 parent 5da01eb commit fa51140
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
21 changes: 21 additions & 0 deletions src/Files.App/Data/Models/TerminalModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Files.App.Data.Models
{
public class TerminalModel : IDisposable
{
public string Id { get; init; }
public string Name { get; init; }
public Control Control { get; init; }

public void Dispose()
{
(Control as IDisposable)?.Dispose();
}
}
}
11 changes: 6 additions & 5 deletions src/Files.App/UserControls/StatusBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:converters="using:Files.App.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:data="using:Files.App.Data.Items"
xmlns:datamodels="using:Files.App.Data.Models"
xmlns:helpers="using:Files.App.Helpers"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:usercontrols="using:Files.App.UserControls"
Expand Down Expand Up @@ -490,27 +491,27 @@
Padding="4"
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
IsItemClickEnabled="True"
ItemsSource="{x:Bind MainPageViewModel.TerminalNames, Mode=OneWay}"
ItemsSource="{x:Bind MainPageViewModel.Terminals, Mode=OneWay}"
SelectedIndex="{x:Bind MainPageViewModel.SelectedTerminal, Mode=TwoWay}"
SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<DataTemplate x:DataType="datamodels:TerminalModel">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
VerticalAlignment="Center"
Text="{x:Bind}"
Text="{x:Bind Name, Mode=OneWay}"
TextTrimming="CharacterEllipsis" />
<Button
Grid.Column="1"
AutomationProperties.Name="{helpers:ResourceString Name=Close}"
Background="Transparent"
BorderBrush="Transparent"
Click="Button_Click"
Tag="{x:Bind}"
Click="TerminalCloseButton_Click"
Tag="{x:Bind Id}"
ToolTipService.ToolTip="{helpers:ResourceString Name=Close}">
<FontIcon FontSize="12" Glyph="&#xE74D;" />
</Button>
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/UserControls/StatusBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private async void DeleteBranch_Click(object sender, RoutedEventArgs e)
await DirectoryPropertiesViewModel.ExecuteDeleteBranch(((BranchItem)((Button)sender).DataContext).Name);
}

private void Button_Click(object sender, RoutedEventArgs e)
private void TerminalCloseButton_Click(object sender, RoutedEventArgs e)
{
MainPageViewModel.TerminalCloseCommand.Execute(((Button)sender).Tag.ToString());
}
Expand Down
17 changes: 8 additions & 9 deletions src/Files.App/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ public MainPageViewModel()
{
if (Terminals.IsEmpty())
IsTerminalViewOpen = true;
Terminals.Add(new TerminalView(e ?? TerminalSelectedProfile)
Terminals.Add(new TerminalModel()
{
Tag = $"Terminal {Terminals.Count}"
Name = (e ?? TerminalSelectedProfile).Name,
Id = Guid.NewGuid().ToString(),
Control = new TerminalView(e ?? TerminalSelectedProfile)
});
OnPropertyChanged(nameof(SelectedTerminal));
OnPropertyChanged(nameof(ActiveTerminal));
OnPropertyChanged(nameof(TerminalNames));
});
TerminalToggleCommand = new RelayCommand(() =>
{
Expand All @@ -122,14 +123,13 @@ public MainPageViewModel()
});
TerminalCloseCommand = new RelayCommand<string>((name) =>
{
var terminal = Terminals.First(x => x.Tag.ToString() == name);
(terminal as IDisposable)?.Dispose();
var terminal = Terminals.First(x => x.Id == name);
terminal.Dispose();
Terminals.Remove(terminal);
SelectedTerminal = int.Min(SelectedTerminal, Terminals.Count - 1);
if (Terminals.IsEmpty())
IsTerminalViewOpen = false;
OnPropertyChanged(nameof(ActiveTerminal));
OnPropertyChanged(nameof(TerminalNames));
});
TerminalSelectedProfile = TerminalProfiles[0];
GeneralSettingsService.PropertyChanged += GeneralSettingsService_PropertyChanged;
Expand Down Expand Up @@ -345,10 +345,9 @@ public bool IsTerminalViewOpen
set => SetProperty(ref _isTerminalViewOpen, value);
}

public Control? ActiveTerminal => SelectedTerminal >= 0 && SelectedTerminal < Terminals.Count ? Terminals[SelectedTerminal] : null;
public Control? ActiveTerminal => SelectedTerminal >= 0 && SelectedTerminal < Terminals.Count ? Terminals[SelectedTerminal].Control : null;

public List<Control> Terminals { get; } = new();
public List<string> TerminalNames => Terminals.Select(x => x.Tag.ToString()!).ToList();
public ObservableCollection<TerminalModel> Terminals { get; } = new();

private int _selectedTerminal;
public int SelectedTerminal
Expand Down

0 comments on commit fa51140

Please sign in to comment.