-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathShell.cs
More file actions
585 lines (514 loc) · 23.7 KB
/
Copy pathShell.cs
File metadata and controls
585 lines (514 loc) · 23.7 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Bloom.Api;
using Bloom.Collection;
using Bloom.Edit;
using Bloom.Properties;
using Bloom.ToPalaso;
using Bloom.Utils;
using Bloom.web.controllers;
using Bloom.Workspace;
using SIL.Extensions;
using SIL.Reporting;
using SIL.Windows.Forms.PortableSettingsProvider;
namespace Bloom
{
public partial class Shell : SIL.Windows.Forms.Miscellaneous.FormForUsingPortableClipboard
{
public static Form GetShellOrOtherOpenForm()
{
var form = Application.OpenForms.Cast<Form>().Where(x => x is Shell).FirstOrDefault();
if (form == null)
form = Application.OpenForms.Cast<Form>().LastOrDefault();
return form;
}
private readonly CollectionSettings _collectionSettings;
private readonly CollectionClosing _collectionClosingEvent;
private readonly ControlKeyEvent _controlKeyEvent;
private readonly WorkspaceView _workspaceView;
private AudioRecording _audioRecording;
private bool _sentInactiveBecauseMinimized;
private bool _mostRecentAppIsActive = true;
// This is needed because on Linux the ResizeEnd event is firing before the Load event handler is
// finished, overwriting the saved RestoreBounds before they are applied.
private bool _finishedLoading;
public Shell(
Func<WorkspaceView> projectViewFactory,
CollectionSettings collectionSettings,
BookDownloadStartingEvent bookDownloadStartingEvent,
CollectionClosing collectionClosingEvent,
QueueRenameOfCollection queueRenameOfCollection,
ControlKeyEvent controlKeyEvent,
SignLanguageApi signLanguageApi,
AudioRecording audioRecording
)
{
queueRenameOfCollection.Subscribe(newName =>
_nameToChangeCollectionUponClosing = newName.Trim().SanitizeFilename('-')
);
_collectionSettings = collectionSettings;
_collectionClosingEvent = collectionClosingEvent;
_controlKeyEvent = controlKeyEvent;
_audioRecording = audioRecording;
InitializeComponent();
Activated += (sender, args) =>
{
// In at least one case (BL-15060) we seem to have gotten activated
// while Bloom was shutting down to switch to an updated version. It's dangerous
// to do the usual activation process in such a state, because we may be in
// the process of disposing the ProjectContext, which can lead to ObjectDisposed
// exceptions, and if we're really unlucky, trying to report that can lock things up.
// So if we know we're in the process of shutting down, ignore being activated.
if (AppIsShuttingDown)
return;
// Some of the stuff we do to update things depends on a current editing view and model.
// So just don't try if the user is for some reason editing the videos while not editing
// the book. Hopefuly in that case he hasn't opened the book and none of its old state
// is cached.
if (_workspaceView.InEditMode)
signLanguageApi.CheckForChangedVideoOnActivate(sender, args);
if (_workspaceView.InCollectionTab)
_workspaceView.CheckForCollectionUpdates();
};
Deactivate += (sender, args) => signLanguageApi.DeactivateTime = DateTime.Now;
//bring the application to the front (will normally be behind the user's web browser)
bookDownloadStartingEvent.Subscribe(
(x) =>
{
try
{
this.Invoke((Action)this.Activate);
}
catch (Exception e)
{
Debug.Fail(
"(Debug Only) Can't bring to front in the current state: " + e.Message
);
//swallow... so we were in some state that we couldn't come to the front... that's ok.
}
}
);
WindowState = FormWindowState.Normal;
Size = new Size(1024, 720);
_contextMenu.Opening += _contextMenu_Opening;
_workspaceView = projectViewFactory();
_workspaceView.ReopenCurrentProject += (
(x, y) =>
{
UserWantsToOpeReopenProject = true;
Close();
}
);
_workspaceView.BackColor = Bloom.Palette.GeneralBackground;
_workspaceView.Dock = System.Windows.Forms.DockStyle.Fill;
this.Controls.Add(this._workspaceView);
SetWindowText(null);
}
public void CheckForInvalidBranding()
{
_workspaceView.CheckForInvalidBranding();
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
// BL-552, BL-779: a bug in Mono requires us to wait to set Icon until handle created.
this.Icon = global::Bloom.Properties.Resources.BloomIcon;
}
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
_audioRecording.ResumeMonitoringAudio();
if (!AppIsShuttingDown)
SendBloomAppActivationChanged(true);
}
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
_audioRecording.PauseMonitoringAudio(true);
SendBloomAppActivationChanged(false);
}
private void SendBloomAppActivationChanged(bool bloomIsActive)
{
if (_mostRecentAppIsActive == bloomIsActive)
return;
_mostRecentAppIsActive = bloomIsActive;
var websocketServer = BloomWebSocketServer.Instance;
if (websocketServer == null)
return;
dynamic eventBundle = new DynamicJson();
eventBundle.active = bloomIsActive;
websocketServer.SendBundle("window", "appActivationChanged", eventBundle);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
// Minimizing does not reliably trigger OnDeactivate (e.g., if no other window is activated).
// But we still want to release resources like camera/mic while Bloom is minimized.
if (WindowState == FormWindowState.Minimized)
{
if (!_sentInactiveBecauseMinimized)
{
_sentInactiveBecauseMinimized = true;
SendBloomAppActivationChanged(false);
}
}
else
{
// On some systems, minimizing doesn't reliably trigger OnDeactivate.
// If we sent an inactive notification while minimized and we are now restored and focused,
// make sure subscribers can resume.
if (_sentInactiveBecauseMinimized && (Focused || ContainsFocus))
{
SendBloomAppActivationChanged(true);
}
_sentInactiveBecauseMinimized = false;
}
}
public bool AppIsShuttingDown => _startedClosingEvent || _finishedClosingEvent;
private bool _startedClosingEvent;
private bool _finishedClosingEvent;
protected override void OnClosing(CancelEventArgs e)
{
// We want to get everything saved (under the old collection name, if we are changing the name and restarting).
// This is tricky because we may need to save current changes to a book we are editing, and this
// involves an inherently asynchronous process (thanks to WebView2). We tried endless ways to
// wait for the data we need from the page we're editing, and nothing worked reliably.
// If we go ahead and close the Shell, the message we eventually get on our API with the data to save
// tries to use Invoke on the Shell to get on the UI thread, but the Shell is already disposed.
// So, the first time OnClosing is called, we raise an event that will do the saving, and cancel
// the close. In case the user manages to click the Close button again before the saving is done,
// we set a flag to say it is in progress, so that we can ignore any subsequent OnClosing events
// until we are done saving. When we ARE done saving, we set a flag to say so, and then call Close()
// to actually get the window closed.
if (_finishedClosingEvent)
{
base.OnClosing(e);
return;
}
if (_startedClosingEvent)
{
e.Cancel = true;
return;
}
Logger.WriteMinorEvent("starting to shut Bloom down");
_startedClosingEvent = true;
_collectionClosingEvent.Raise(
new CollectionClosingArgs()
{
PostponedWork = () =>
{
if (
!string.IsNullOrEmpty(_nameToChangeCollectionUponClosing)
&& _nameToChangeCollectionUponClosing
!= _collectionSettings.CollectionName
&& UserWantsToOpeReopenProject
)
{
// Without checking and resetting this flag, Linux endlessly spawns new instances. Apparently the Mono runtime
// calls OnClosing again as a result of calling Program.RestartBloom() which calls Application.Exit().
UserWantsToOpeReopenProject = false;
//Actually restart Bloom with a parameter requesting this name change. It's way more likely to succeed
//when this run isn't holding onto anything.
try
{
var existingDirectoryPath = Path.GetDirectoryName(
_collectionSettings.SettingsFilePath
);
var parentDirectory = Path.GetDirectoryName(existingDirectoryPath);
var newDirectoryPath = Path.Combine(
parentDirectory,
_nameToChangeCollectionUponClosing
);
Program.RestartBloom(
true,
string.Format(
"--rename \"{0}\" \"{1}\" ",
existingDirectoryPath,
newDirectoryPath
)
);
}
catch (Exception error)
{
SIL.Reporting.ErrorReport.NotifyUserOfProblem(
error,
"Sorry, Bloom failed to even prepare for the rename of the project to '{0}'",
_nameToChangeCollectionUponClosing
);
}
}
_finishedClosingEvent = true;
Logger.WriteMinorEvent("closing the Shell");
Close();
},
FailureAction = () =>
{
// We didn't want a second attempt at saving if the user clicks the close box while we are
// still trying to save after the first click on Close. But if the first attempt fails,
// we don't want to stay in a state where all attempts to close the program are ignored.
_startedClosingEvent = false;
},
}
);
e.Cancel = true;
base.OnClosing(e);
}
public void SetWindowText(string bookName)
{
// Let's only mark the window text for Alpha and Beta releases. It looks odd to have that in
// release builds, and doesn't add much since we can treat Release builds as the unmarked case.
// Note that developer builds now have a special "channel" marking as well to differentiate them
// from true Release builds in screen shots.
var formattedText = string.Format(
"{0} - Bloom {1}",
_workspaceView.Text,
GetShortVersionInfo()
);
var channel = ApplicationUpdateSupport.ChannelName;
if (channel.ToLowerInvariant() != "release")
formattedText = string.Format("{0} {1}", formattedText, channel);
if (bookName != null)
{
formattedText = string.Format("{0} - {1}", bookName, formattedText);
}
Text = formattedText;
}
public static string GetShortVersionInfo()
{
var asm = Assembly.GetExecutingAssembly();
var ver = asm.GetName().Version;
return string.Format("{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build);
}
public bool UserWantsToOpenADifferentProject { get; set; }
public bool UserWantsToOpeReopenProject;
/// <summary>
/// used when the user does an in-app installer download; after we close down, Program will read this and return control to Sparkle
/// </summary>
public bool QuitForVersionUpdate;
public bool QuitForSystemShutdown;
private string _nameToChangeCollectionUponClosing;
private void Shell_Activated(object sender, EventArgs e) { }
private void Shell_Deactivate(object sender, EventArgs e)
{
Debug.WriteLine("Shell Deactivated");
}
/// <summary>
/// Prevent the window sizes text/debug context menu from displaying anywhere but along the
/// top tab strip of the workspace window.
/// </summary>
/// <remarks>
/// See https://issues.bloomlibrary.org/youtrack/issue/BL-8668. I never figured out why this
/// menu started appearing more and more aggressively in the Edit tab where it wasn't wanted,
/// but not in the other tabs. After close to a day spent on this issue, I decided that this
/// fix is good enough.
/// </remarks>
private void _contextMenu_Opening(object sender, CancelEventArgs e)
{
// In some ways, it would make more sense to have this context menu be attached
// to the TabStrip object, but since all of the menu actions affect the Shell
// object, it's just as easy to check its location before actually displaying
// the menu.
if (!_workspaceView.IsInTabStrip(this.PointToClient(_contextMenu.Location)))
e.Cancel = true;
}
private void On800x600Click(object sender, EventArgs e)
{
Size = new Size(800, 600);
}
private void On1024x600Click(object sender, EventArgs e)
{
Size = new Size(1024, 600);
}
private void On1024x768(object sender, EventArgs e)
{
Size = new Size(1024, 768);
}
private void On1024x586(object sender, EventArgs e)
{
Size = new Size(1024, 586);
}
public static void ComeToFront()
{
if (GetShellOrOtherOpenForm() is Shell shell)
{
shell.Invoke(
(Action)(
() =>
{
shell.ReallyComeToFront();
}
)
);
}
}
/// <summary>
/// we let the Program call this after it closes the splash screen
/// </summary>
public void ReallyComeToFront()
{
//try really hard to become top most. See http://stackoverflow.com/questions/5282588/how-can-i-bring-my-application-window-to-the-front
TopMost = true;
Focus();
BringToFront();
TopMost = false;
_finishedLoading = true;
}
private void Shell_Load(object sender, EventArgs e)
{
//Handle window sizing/location. Normally, we just Maximize the window.
//The exceptions to this are if we are in a DEBUG build or the settings have a MaximizeWindow=='False", which at this time
//must be done by hand (no user UI is provided).
try
{
SuspendLayout();
if (Settings.Default.WindowSizeAndLocation == null)
{
StartPosition = FormStartPosition.WindowsDefaultLocation;
WindowState = FormWindowState.Maximized;
Settings.Default.WindowSizeAndLocation = FormSettings.Create(this);
Settings.Default.Save();
}
// This feature is not yet a normal part of Bloom, since we think just maximizing is more rice-farmer-friendly.
// However, we added the ability to remember this stuff at the request of the person making videos, who needs
// Bloom to open in the same place / size each time.
if (Settings.Default.MaximizeWindow == false)
{
Settings.Default.WindowSizeAndLocation.InitializeForm(this);
}
else
{
// BL-1036: save and restore un-maximized settings
var savedBounds = Settings.Default.RestoreBounds;
if (
(savedBounds.Width > 200)
&& (savedBounds.Height > 200)
&& (IsOnScreen(savedBounds))
)
{
StartPosition = FormStartPosition.Manual;
WindowState = FormWindowState.Normal;
Bounds = savedBounds;
}
else
{
StartPosition = FormStartPosition.CenterScreen;
}
WindowState = FormWindowState.Maximized;
UpdatePerformanceMeasurementStatus();
}
// We may be opening on a different collection. Meddle with that collection if
// file meddling is enabled. (Don't stop meddling with the previous collection.)
if (FileMeddlerManager.IsMeddling)
{
FileMeddlerManager.Start(_collectionSettings?.FolderPath);
this.meddleWithNewFilesToolStripMenuItem.Text = "Stop Meddling with New Files";
}
}
catch (Exception error)
{
Debug.Fail(error.Message);
// ReSharper disable HeuristicUnreachableCode
//Not worth bothering the user. Just reset the values to something reasonable.
StartPosition = FormStartPosition.WindowsDefaultLocation;
WindowState = FormWindowState.Maximized;
// ReSharper restore HeuristicUnreachableCode
}
finally
{
ResumeLayout();
}
}
private void Shell_ResizeEnd(object sender, EventArgs e)
{
// BL-1036: save and restore un-maximized settings
if (!_finishedLoading)
return;
if (WindowState != FormWindowState.Normal)
return;
Settings.Default.RestoreBounds = new Rectangle(Left, Top, Width, Height);
Settings.Default.Save();
}
/// <summary>
/// Is a significant (100 x 100) portion of the form on-screen?
/// </summary>
/// <returns></returns>
private static bool IsOnScreen(Rectangle rect)
{
var screens = Screen.AllScreens;
var formTopLeft = new Rectangle(rect.Left, rect.Top, 100, 100);
return screens.Any(screen => screen.WorkingArea.Contains(formTopLeft));
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (Control.ModifierKeys == Keys.Control)
{
_controlKeyEvent.Raise(keyData);
//this event system doesn't actually give us a return value,, so we don't know if it was handled or not
//so we'll always just let it bubble. If that becomes a problem, we'll need a different design.
//return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void startMeasuringPerformanceToolStripMenuItem_Click(object sender, EventArgs e)
{
PerformanceMeasurement.Global.StartMeasuring();
UpdatePerformanceMeasurementStatus();
// open in a browser
this.showPerformancePageToolStripMenuItem_Click(sender, e);
}
private void showPerformancePageToolStripMenuItem_Click(object sender, EventArgs e)
{
ProcessExtra.SafeStartInFront(
BloomServer.ServerUrlWithBloomPrefixEndingInSlash
+ "performance/PerformanceLogPage.htm"
);
}
private void alwaysMeasureToolStripMenuItem_Click(object sender, EventArgs e)
{
Settings.Default.AlwaysMeasurePerformance = !Settings.Default.AlwaysMeasurePerformance;
UpdatePerformanceMeasurementStatus();
}
private void meddleWithNewFilesToolStripMenuItem_Click(object sender, EventArgs e)
{
if (FileMeddlerManager.IsMeddling)
{
FileMeddlerManager.Stop();
meddleWithNewFilesToolStripMenuItem.Text = "Meddle with New Files";
}
else
{
FileMeddlerManager.Start(_collectionSettings?.FolderPath);
meddleWithNewFilesToolStripMenuItem.Text = "Stop Meddling with New Files";
}
}
private void UpdatePerformanceMeasurementStatus()
{
alwaysMeasureToolStripMenuItem.Checked = Settings.Default.AlwaysMeasurePerformance;
if (
Settings.Default.AlwaysMeasurePerformance
&& !PerformanceMeasurement.Global.CurrentlyMeasuring
)
{
PerformanceMeasurement.Global.StartMeasuring();
}
this.startMeasuringPerformanceToolStripMenuItem.Enabled = !PerformanceMeasurement
.Global
.CurrentlyMeasuring;
this.showPerformancePageToolStripMenuItem.Enabled = PerformanceMeasurement
.Global
.CurrentlyMeasuring;
if (PerformanceMeasurement.Global.CurrentlyMeasuring)
{
startMeasuringPerformanceToolStripMenuItem.Text = "Currently Measuring Performance";
}
// if we're always measuring, don't offer to start/stop
//this.startMeasuringPerformanceToolStripMenuItem.Enabled = !Settings.Default.AlwaysMeasurePerformance;
}
}
}