Skip to content

Commit ea52bf8

Browse files
gave92Marco Gavelli
authored andcommitted
Add terminal model
1 parent 04c9ec0 commit ea52bf8

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.UI.Xaml.Controls;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Files.App.Data.Models
9+
{
10+
public class TerminalModel : IDisposable
11+
{
12+
public string Id { get; init; }
13+
public string Name { get; init; }
14+
public Control Control { get; init; }
15+
16+
public void Dispose()
17+
{
18+
(Control as IDisposable)?.Dispose();
19+
}
20+
}
21+
}

src/Files.App/UserControls/StatusBar.xaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
xmlns:converters="using:Files.App.Converters"
88
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
99
xmlns:data="using:Files.App.Data.Items"
10+
xmlns:datamodels="using:Files.App.Data.Models"
1011
xmlns:helpers="using:Files.App.Helpers"
1112
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1213
xmlns:toolkit="using:CommunityToolkit.WinUI.Controls"
@@ -407,27 +408,27 @@
407408
Padding="4"
408409
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
409410
IsItemClickEnabled="True"
410-
ItemsSource="{x:Bind MainPageViewModel.TerminalNames, Mode=OneWay}"
411+
ItemsSource="{x:Bind MainPageViewModel.Terminals, Mode=OneWay}"
411412
SelectedIndex="{x:Bind MainPageViewModel.SelectedTerminal, Mode=TwoWay}"
412413
SelectionMode="Single">
413414
<ListView.ItemTemplate>
414-
<DataTemplate x:DataType="x:String">
415+
<DataTemplate x:DataType="datamodels:TerminalModel">
415416
<Grid>
416417
<Grid.ColumnDefinitions>
417418
<ColumnDefinition Width="*" />
418419
<ColumnDefinition Width="Auto" />
419420
</Grid.ColumnDefinitions>
420421
<TextBlock
421422
VerticalAlignment="Center"
422-
Text="{x:Bind}"
423+
Text="{x:Bind Name, Mode=OneWay}"
423424
TextTrimming="CharacterEllipsis" />
424425
<Button
425426
Grid.Column="1"
426427
AutomationProperties.Name="{helpers:ResourceString Name=Close}"
427428
Background="Transparent"
428429
BorderBrush="Transparent"
429-
Click="Button_Click"
430-
Tag="{x:Bind}"
430+
Click="TerminalCloseButton_Click"
431+
Tag="{x:Bind Id}"
431432
ToolTipService.ToolTip="{helpers:ResourceString Name=Close}">
432433
<FontIcon FontSize="12" Glyph="&#xE74D;" />
433434
</Button>

src/Files.App/UserControls/StatusBar.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private async void DeleteBranch_Click(object sender, RoutedEventArgs e)
7979
await StatusBarViewModel.ExecuteDeleteBranch(((BranchItem)((Button)sender).DataContext).Name);
8080
}
8181

82-
private void Button_Click(object sender, RoutedEventArgs e)
82+
private void TerminalCloseButton_Click(object sender, RoutedEventArgs e)
8383
{
8484
MainPageViewModel.TerminalCloseCommand.Execute(((Button)sender).Tag.ToString());
8585
}

src/Files.App/UserControls/TerminalView.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ private async Task ResizeTask()
114114
private Terminal _terminal;
115115
private BufferedReader _reader;
116116
private ShellProfile _profile;
117+
private string _id;
117118

118-
public TerminalView(ShellProfile profile) : this()
119-
=> _profile = profile;
119+
public TerminalView(ShellProfile profile, string id) : this()
120+
=> (_profile, _id) = (profile, id);
120121

121122
public TerminalView()
122123
{
@@ -389,7 +390,7 @@ private void StartShellProcess(TerminalSize size, ShellProfile profile)
389390
{
390391
DispatcherQueue.EnqueueAsync(() =>
391392
{
392-
_mainPageModel.TerminalCloseCommand.Execute(Tag);
393+
_mainPageModel.TerminalCloseCommand.Execute(_id);
393394
});
394395
};
395396

src/Files.App/ViewModels/MainPageViewModel.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,15 @@ public MainPageViewModel()
176176
{
177177
if (Terminals.IsEmpty())
178178
IsTerminalViewOpen = true;
179-
Terminals.Add(new TerminalView(e ?? TerminalSelectedProfile)
179+
var termId = Guid.NewGuid().ToString();
180+
Terminals.Add(new TerminalModel()
180181
{
181-
Tag = $"Terminal {Terminals.Count}"
182+
Name = (e ?? TerminalSelectedProfile).Name,
183+
Id = termId,
184+
Control = new TerminalView(e ?? TerminalSelectedProfile, termId)
182185
});
183186
OnPropertyChanged(nameof(SelectedTerminal));
184187
OnPropertyChanged(nameof(ActiveTerminal));
185-
OnPropertyChanged(nameof(TerminalNames));
186188
});
187189
TerminalToggleCommand = new RelayCommand(() =>
188190
{
@@ -204,14 +206,13 @@ public MainPageViewModel()
204206
});
205207
TerminalCloseCommand = new RelayCommand<string>((name) =>
206208
{
207-
var terminal = Terminals.First(x => x.Tag.ToString() == name);
208-
(terminal as IDisposable)?.Dispose();
209+
var terminal = Terminals.First(x => x.Id == name);
210+
terminal.Dispose();
209211
Terminals.Remove(terminal);
210212
SelectedTerminal = int.Min(SelectedTerminal, Terminals.Count - 1);
211213
if (Terminals.IsEmpty())
212214
IsTerminalViewOpen = false;
213215
OnPropertyChanged(nameof(ActiveTerminal));
214-
OnPropertyChanged(nameof(TerminalNames));
215216
});
216217
TerminalSelectedProfile = TerminalProfiles[0];
217218

@@ -475,10 +476,9 @@ public bool IsTerminalViewOpen
475476
set => SetProperty(ref _isTerminalViewOpen, value);
476477
}
477478

478-
public Control? ActiveTerminal => SelectedTerminal >= 0 && SelectedTerminal < Terminals.Count ? Terminals[SelectedTerminal] : null;
479+
public Control? ActiveTerminal => SelectedTerminal >= 0 && SelectedTerminal < Terminals.Count ? Terminals[SelectedTerminal].Control : null;
479480

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

483483
private int _selectedTerminal;
484484
public int SelectedTerminal

0 commit comments

Comments
 (0)