-
-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathSidebar.cs
More file actions
250 lines (221 loc) · 9.34 KB
/
Copy pathSidebar.cs
File metadata and controls
250 lines (221 loc) · 9.34 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System.Globalization;
using Eto.Forms;
using NAPS2.EtoForms.Layout;
using NAPS2.EtoForms.Ui;
using NAPS2.EtoForms.Widgets;
using NAPS2.Scan;
using NAPS2.Scan.Internal;
namespace NAPS2.EtoForms.Desktop;
public class Sidebar
{
private readonly IScanPerformer _scanPerformer;
private readonly DeviceCapsCache _deviceCapsCache;
private readonly IProfileManager _profileManager;
private readonly Naps2Config _config;
private readonly IIconProvider _iconProvider;
private readonly IFormFactory _formFactory;
private readonly IDesktopScanController _desktopScanController;
private readonly LayoutVisibility _sidebarVis = new(true);
private readonly LayoutVisibility _onboardingVis = new(false);
private readonly LayoutVisibility _predefinedVis = new(true);
private readonly DropDownWidget<ScanProfile> _profile = new();
private readonly EnumDropDownWidget<ScanSource> _paperSource = new();
private readonly EnumDropDownWidget<ScanBitDepth> _bitDepth = new();
private DeviceSelectorWidget? _deviceSelectorWidget;
private PageSizeDropDownWidget? _pageSize;
private ResolutionDropDownWidget? _resolution;
public Sidebar(IScanPerformer scanPerformer, DeviceCapsCache deviceCapsCache, IProfileManager profileManager,
Naps2Config config, IIconProvider iconProvider, IFormFactory formFactory,
IDesktopScanController desktopScanController)
{
_scanPerformer = scanPerformer;
_deviceCapsCache = deviceCapsCache;
_profileManager = profileManager;
_config = config;
_iconProvider = iconProvider;
_formFactory = formFactory;
_desktopScanController = desktopScanController;
_profile.Format = x => x.DisplayName;
_profileManager.ProfilesUpdated += (_, _) => UpdateProfilesDropdown();
UpdateProfilesDropdown();
EditProfileCommand = new ActionCommand(EditProfile)
{
ToolTip = UiStrings.Edit,
IconName = "pencil_small"
};
NewProfileCommand = new ActionCommand(NewProfile)
{
Text = UiStrings.NewProfile,
ToolTip = UiStrings.New,
IconName = "add_small"
};
ScanCommand = new ActionCommand(DoScan)
{
Text = UiStrings.Scan,
IconName = "control_play_blue_small"
};
}
private ActionCommand NewProfileCommand { get; }
private ActionCommand EditProfileCommand { get; }
private ActionCommand ScanCommand { get; }
private void UpdateProfilesDropdown()
{
_profile.Items = _profileManager.Profiles;
_onboardingVis.IsVisible = _profileManager.Profiles.Count == 0;
}
private void EditProfile()
{
var originalProfile = _profile.SelectedItem;
if (originalProfile != null)
{
var fedit = _formFactory.Create<EditProfileForm>();
fedit.ScanProfile = originalProfile;
fedit.ShowModal();
if (fedit.Result)
{
_profile.SelectedItem = fedit.ScanProfile;
_profileManager.Mutate(new ListMutation<ScanProfile>.ReplaceWith(fedit.ScanProfile),
ListSelection.Of(originalProfile));
}
}
}
private void NewProfile()
{
if (!(_config.Get(c => c.NoUserProfiles) && _profileManager.Profiles.Any(x => x.IsLocked)))
{
var fedit = _formFactory.Create<EditProfileForm>();
fedit.NewProfile = true;
fedit.ScanProfile = _config.DefaultProfileSettings();
fedit.ShowModal();
if (fedit.Result)
{
_profile.SelectedItem = fedit.ScanProfile;
_profileManager.Mutate(new ListMutation<ScanProfile>.Append(fedit.ScanProfile),
ListSelection.Empty<ScanProfile>());
}
}
}
private void DoScan()
{
var profile = _profile.SelectedItem!;
var pageSize = _pageSize!.SelectedItem!;
profile.PaperSource = _paperSource.SelectedItem;
profile.PageSize = pageSize.Type;
profile.CustomPageSizeName = pageSize.CustomName;
profile.CustomPageSize = pageSize.CustomDimens;
profile.Resolution = new ScanResolution { Dpi = _resolution?.SelectedItem?.Dpi ?? 0 };
profile.BitDepth = _bitDepth.SelectedItem;
_profileManager.Save();
_desktopScanController.ScanWithProfile(profile);
}
public LayoutElement CreateView(IFormBase parentWindow)
{
_sidebarVis.IsVisible = _config.Get(c => c.SidebarVisible);
_profile.SelectedItem = _profileManager.DefaultProfile ?? _profileManager.Profiles.FirstOrDefault();
_deviceSelectorWidget = new DeviceSelectorWidget(_scanPerformer, _deviceCapsCache, _iconProvider, parentWindow)
{
ShowChooseDevice = false,
ProfileFunc = () => _profile.SelectedItem!
};
_pageSize = new PageSizeDropDownWidget(parentWindow);
_resolution = new ResolutionDropDownWidget(parentWindow);
_profile.SelectedItemChanged += (_, _) =>
{
if (_config.Get(c => c.ScanChangesDefaultProfile))
{
_profileManager.DefaultProfile = _profile.SelectedItem;
}
UpdateUiForProfile();
};
_profileManager.ProfilesUpdated += (_, _) => UpdateUiForProfile();
UpdateUiForProfile();
return L.Column(
C.Filler().NaturalWidth(100),
L.Column(
C.Button(NewProfileCommand, ButtonImagePosition.Left).Height(30).AlignCenter()
).Visible(_onboardingVis),
L.Column(
L.Row(
C.Label(UiStrings.ProfileLabel).AlignTrailing(),
C.Filler(),
// On Mac we set an explicit height as for some reason it fixes the button style after hide+show
C.Button(EditProfileCommand, ButtonImagePosition.Overlay)
.Height(EtoPlatform.Current.IsMac ? 20 : null).Width(30),
C.Button(NewProfileCommand, ButtonImagePosition.Overlay)
.Height(EtoPlatform.Current.IsMac ? 20 : null).Width(30)
),
_profile.AsControl(),
C.Spacer(),
_deviceSelectorWidget,
L.Column(
C.Spacer(),
C.Label(UiStrings.PaperSourceLabel),
_paperSource,
C.Label(UiStrings.PageSizeLabel),
_pageSize,
C.Label(UiStrings.ResolutionLabel),
_resolution,
C.Label(UiStrings.BitDepthLabel),
_bitDepth
).Visible(_predefinedVis),
C.Spacer(),
C.Button(ScanCommand, ButtonImagePosition.Left).AlignCenter().Height(30)
).Visible(!_onboardingVis),
C.Filler()
).Padding(left: parentWindow.LayoutController.DefaultSpacing + 10, right: 10).Visible(_sidebarVis);
}
private void UpdateUiForProfile()
{
var profile = _profile.SelectedItem;
if (profile == null) return;
var deviceDriver = new ScanOptionsValidator().ValidateDriver(
Enum.TryParse<Driver>(profile.DriverName, true, out var driver)
? driver
: Driver.Default);
var device = profile.Device?.ToScanDevice(deviceDriver);
if (device != null)
{
_deviceSelectorWidget!.Choice = DeviceChoice.ForDevice(device);
}
else
{
_deviceSelectorWidget!.Choice = DeviceChoice.ForAlwaysAsk(deviceDriver);
}
_predefinedVis.IsVisible = !profile.UseNativeUI;
if (profile.PageSize == ScanPageSize.Custom && profile.CustomPageSize != null)
{
_pageSize!.SetCustom(profile.CustomPageSizeName, profile.CustomPageSize);
}
else
{
_pageSize!.SetPreset(profile.PageSize);
}
_paperSource.SelectedItem = profile.PaperSource;
_bitDepth.SelectedItem = profile.BitDepth;
_resolution!.SetDpi(profile.Resolution.Dpi);
_paperSource.Items = ScanSourceHelper.GetCompatibleScanSources(profile, deviceDriver);
var selectedSource = _paperSource.SelectedItem;
var perSource = selectedSource switch
{
ScanSource.Glass => profile.Caps?.Glass,
ScanSource.Feeder => profile.Caps?.Feeder,
ScanSource.Duplex => profile.Caps?.Duplex,
_ => null
};
var validResolutions = perSource?.Resolutions;
_resolution.VisiblePresets = validResolutions is [_, ..]
? validResolutions
: EnumDropDownWidget<ScanDpi>.DefaultItems.Select(x => x.ToIntDpi());
var scanArea = perSource?.ScanArea;
var sizeCaps = new PageSizeCaps { ScanArea = scanArea };
var allPresets = EnumDropDownWidget<ScanPageSize>.DefaultItems.SkipLast(2).ToList();
var conditionalPresets = new[] { ScanPageSize.A3, ScanPageSize.B4 };
_pageSize.VisiblePresets = allPresets.Where(preset =>
!conditionalPresets.Contains(preset) || sizeCaps.Fits(preset.PageDimensions()!.ToPageSize()));
}
public void ToggleVisibility()
{
_sidebarVis.IsVisible = !_sidebarVis.IsVisible;
_config.User.Set(c => c.SidebarVisible, _sidebarVis.IsVisible);
}
}