Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
96 lines (79 loc) · 3.44 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
96 lines (79 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using PPObjectSearch.Services;
using PPObjectSearch.ViewModels;
namespace PPObjectSearch;
public partial class MainWindow : Window
{
private readonly ShellViewModel _shell = new();
private Point? _dragStart;
public MainWindow()
{
// Screens opened as windows - by the setting, or popped out of a tab - need somewhere to go.
ScreenHost.Current.Attach(new Views.ScreenWindows(ScreenHost.Current));
InitializeComponent();
DataContext = _shell;
Loaded += (_, _) => WarnAboutSettings();
}
/// <summary>
/// Said once, at startup: the app carries on with defaults, and nothing typed into it from
/// now on will be saved until the file is fixed.
/// </summary>
private void WarnAboutSettings()
{
if (_shell.SettingsProblem is { } problem)
{
MessageBox.Show(this, problem, "Settings not loaded", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
protected override void OnClosing(CancelEventArgs e)
{
_shell.Shutdown();
base.OnClosing(e);
}
// Sidebar drag-to-reorder. The drag only starts once the mouse has moved past the system
// threshold, so plain clicks (including the close button) behave as before.
private void Tab_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_dragStart = e.GetPosition(this);
// Clicking the environment already selected brings it back in front of any screen tab.
_shell.Screens.ShowHome();
}
private void Tab_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (_dragStart is not { } start || e.LeftButton != MouseButtonState.Pressed) return;
var delta = e.GetPosition(this) - start;
if (Math.Abs(delta.X) < SystemParameters.MinimumHorizontalDragDistance &&
Math.Abs(delta.Y) < SystemParameters.MinimumVerticalDragDistance) return;
_dragStart = null;
if (sender is ListBoxItem { DataContext: EnvironmentSessionViewModel session } item)
DragDrop.DoDragDrop(item, session, DragDropEffects.Move);
}
private void Tab_DragOver(object sender, DragEventArgs e)
{
e.Effects = e.Data.GetDataPresent(typeof(EnvironmentSessionViewModel))
? DragDropEffects.Move
: DragDropEffects.None;
e.Handled = true;
}
private void Tab_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetData(typeof(EnvironmentSessionViewModel)) is not EnvironmentSessionViewModel dragged) return;
if (sender is not ListBoxItem { DataContext: EnvironmentSessionViewModel target }) return;
_shell.MoveTab(dragged, _shell.Sessions.IndexOf(target));
e.Handled = true;
}
/// <summary>Browser profiles can come and go between visits, so the menu reads them as it opens.</summary>
private void EnvironmentMenu_Opened(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement { DataContext: EnvironmentSessionViewModel session }) session.RaiseBrowserProfile();
}
/// <summary>The context menu's own DataContext is the row's session, so it closes that one.</summary>
private void CloseTab_Click(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement { DataContext: EnvironmentSessionViewModel session })
_shell.CloseTabCommand.Execute(session);
}
}