Skip to content

Commit d7d11c3

Browse files
committed
Add terminal model
1 parent 5da01eb commit d7d11c3

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
@@ -6,6 +6,7 @@
66
xmlns:converters="using:Files.App.Converters"
77
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
88
xmlns:data="using:Files.App.Data.Items"
9+
xmlns:datamodels="using:Files.App.Data.Models"
910
xmlns:helpers="using:Files.App.Helpers"
1011
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1112
xmlns:usercontrols="using:Files.App.UserControls"
@@ -490,27 +491,27 @@
490491
Padding="4"
491492
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
492493
IsItemClickEnabled="True"
493-
ItemsSource="{x:Bind MainPageViewModel.TerminalNames, Mode=OneWay}"
494+
ItemsSource="{x:Bind MainPageViewModel.Terminals, Mode=OneWay}"
494495
SelectedIndex="{x:Bind MainPageViewModel.SelectedTerminal, Mode=TwoWay}"
495496
SelectionMode="Single">
496497
<ListView.ItemTemplate>
497-
<DataTemplate x:DataType="x:String">
498+
<DataTemplate x:DataType="datamodels:TerminalModel">
498499
<Grid>
499500
<Grid.ColumnDefinitions>
500501
<ColumnDefinition Width="*" />
501502
<ColumnDefinition Width="Auto" />
502503
</Grid.ColumnDefinitions>
503504
<TextBlock
504505
VerticalAlignment="Center"
505-
Text="{x:Bind}"
506+
Text="{x:Bind Name, Mode=OneWay}"
506507
TextTrimming="CharacterEllipsis" />
507508
<Button
508509
Grid.Column="1"
509510
AutomationProperties.Name="{helpers:ResourceString Name=Close}"
510511
Background="Transparent"
511512
BorderBrush="Transparent"
512-
Click="Button_Click"
513-
Tag="{x:Bind}"
513+
Click="TerminalCloseButton_Click"
514+
Tag="{x:Bind Id}"
514515
ToolTipService.ToolTip="{helpers:ResourceString Name=Close}">
515516
<FontIcon FontSize="12" Glyph="&#xE74D;" />
516517
</Button>

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

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

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

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
@@ -94,13 +94,15 @@ public MainPageViewModel()
9494
{
9595
if (Terminals.IsEmpty())
9696
IsTerminalViewOpen = true;
97-
Terminals.Add(new TerminalView(e ?? TerminalSelectedProfile)
97+
var termId = Guid.NewGuid().ToString();
98+
Terminals.Add(new TerminalModel()
9899
{
99-
Tag = $"Terminal {Terminals.Count}"
100+
Name = (e ?? TerminalSelectedProfile).Name,
101+
Id = termId,
102+
Control = new TerminalView(e ?? TerminalSelectedProfile, termId)
100103
});
101104
OnPropertyChanged(nameof(SelectedTerminal));
102105
OnPropertyChanged(nameof(ActiveTerminal));
103-
OnPropertyChanged(nameof(TerminalNames));
104106
});
105107
TerminalToggleCommand = new RelayCommand(() =>
106108
{
@@ -122,14 +124,13 @@ public MainPageViewModel()
122124
});
123125
TerminalCloseCommand = new RelayCommand<string>((name) =>
124126
{
125-
var terminal = Terminals.First(x => x.Tag.ToString() == name);
126-
(terminal as IDisposable)?.Dispose();
127+
var terminal = Terminals.First(x => x.Id == name);
128+
terminal.Dispose();
127129
Terminals.Remove(terminal);
128130
SelectedTerminal = int.Min(SelectedTerminal, Terminals.Count - 1);
129131
if (Terminals.IsEmpty())
130132
IsTerminalViewOpen = false;
131133
OnPropertyChanged(nameof(ActiveTerminal));
132-
OnPropertyChanged(nameof(TerminalNames));
133134
});
134135
TerminalSelectedProfile = TerminalProfiles[0];
135136
GeneralSettingsService.PropertyChanged += GeneralSettingsService_PropertyChanged;
@@ -345,10 +346,9 @@ public bool IsTerminalViewOpen
345346
set => SetProperty(ref _isTerminalViewOpen, value);
346347
}
347348

348-
public Control? ActiveTerminal => SelectedTerminal >= 0 && SelectedTerminal < Terminals.Count ? Terminals[SelectedTerminal] : null;
349+
public Control? ActiveTerminal => SelectedTerminal >= 0 && SelectedTerminal < Terminals.Count ? Terminals[SelectedTerminal].Control : null;
349350

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

353353
private int _selectedTerminal;
354354
public int SelectedTerminal

0 commit comments

Comments
 (0)