Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit f58ffab

Browse files
committed
fixes #105
I know this is more of a patch than a proper solution, I should have looked up better what's causing the race condition of something not loading before calling editor.Focus(), but this should do, and rarely would break.
1 parent 8f7373e commit f58ffab

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

UI/MainWindow/MainWindow.xaml.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public partial class MainWindow
3636
private readonly Storyboard EnableServerAnim;
3737
public readonly List<MenuItem> MenuItems;
3838

39+
private EditorElement EditorToFocus;
40+
private readonly DispatcherTimer SelectDocumentTimer;
41+
3942
private bool ClosingBuffer;
4043
private readonly bool FullyInitialized;
4144

@@ -68,33 +71,53 @@ public MainWindow(SplashScreen sc)
6871
ThemeManager.GetAppTheme(Program.OptionsObject.Program_Theme));
6972
}
7073

71-
ObjectBrowserColumn.Width =
72-
new GridLength(Program.OptionsObject.Program_ObjectbrowserWidth, GridUnitType.Pixel);
73-
var heightDescriptor =
74-
DependencyPropertyDescriptor.FromProperty(ColumnDefinition.WidthProperty, typeof(ItemsControl));
75-
heightDescriptor.AddValueChanged(EditorObjectBrowserGrid.ColumnDefinitions[1],
76-
EditorObjectBrowserGridRow_WidthChanged);
74+
// Timer to select the newly opened editor 200ms after it has been opened
75+
SelectDocumentTimer = new DispatcherTimer()
76+
{
77+
Interval = TimeSpan.FromMilliseconds(200),
78+
};
79+
80+
SelectDocumentTimer.Tick += (s, e) =>
81+
{
82+
SelectDocumentTimer.Stop();
83+
EditorToFocus.editor.Focus();
84+
};
85+
86+
// Restore sizes of panels and separators
87+
ObjectBrowserColumn.Width = new GridLength(Program.OptionsObject.Program_ObjectbrowserWidth, GridUnitType.Pixel);
88+
var heightDescriptor = DependencyPropertyDescriptor.FromProperty(ColumnDefinition.WidthProperty, typeof(ItemsControl));
89+
heightDescriptor.AddValueChanged(EditorObjectBrowserGrid.ColumnDefinitions[1], EditorObjectBrowserGridRow_WidthChanged);
90+
91+
// Fill the configs menu and some toolbar combobox items
7792
FillConfigMenu();
7893
CompileButton.ItemsSource = CompileButtonDict;
7994
CompileButton.SelectedIndex = 0;
8095
CActionButton.ItemsSource = ActionButtonDict;
8196
CActionButton.SelectedIndex = 0;
8297

98+
// Enable/disable toolbar on startup
8399
if (Program.OptionsObject.UI_ShowToolBar)
84100
{
85101
Win_ToolBar.Height = double.NaN;
86102
}
87103

104+
// Fill OB scripting directories combobox from the bottom
88105
OBDirList.ItemsSource = Program.Configs[Program.SelectedConfig].SMDirectories;
89106
OBDirList.SelectedIndex = 0;
90107

108+
// Set some visual effects
91109
MetroDialogOptions.AnimateHide = MetroDialogOptions.AnimateShow = false;
92110
BlendOverEffect = (Storyboard)Resources["BlendOverEffect"];
93111
EnableServerAnim = (Storyboard)Resources["EnableServerAnim"];
94112
DisableServerAnim = (Storyboard)Resources["DisableServerAnim"];
113+
114+
// Start OB
95115
ChangeObjectBrowserToDirectory(Program.OptionsObject.Program_ObjectBrowserDirectory);
116+
117+
// Translate
96118
Language_Translate(true);
97119

120+
// Load previously opened files
98121
if (Program.OptionsObject.LastOpenFiles != null)
99122
{
100123
foreach (var file in Program.OptionsObject.LastOpenFiles)
@@ -103,6 +126,7 @@ public MainWindow(SplashScreen sc)
103126
}
104127
}
105128

129+
// Take startup commands in consideration
106130
var args = Environment.GetCommandLineArgs();
107131
for (var i = 0; i < args.Length; ++i)
108132
{
@@ -112,9 +136,11 @@ public MainWindow(SplashScreen sc)
112136
}
113137
}
114138

139+
// Close SplashScreen
115140
sc.Close(TimeSpan.FromMilliseconds(500.0));
116141
FullyInitialized = true;
117142

143+
// Enclose menuitems in an accesible list to set their InputGestureTexts easier
118144
MenuItems = new()
119145
{
120146
MenuI_File,
@@ -127,16 +153,24 @@ public MainWindow(SplashScreen sc)
127153
};
128154

129155
LoadInputGestureTexts();
156+
157+
// Load the commands dictionary in memory
130158
LoadCommandsDictionary();
159+
160+
// Load the recent files list
131161
LoadRecentsList();
132162

163+
// Disable the Reopen last closed tab button on startup for obvious reasons
133164
MenuI_ReopenLastClosedTab.IsEnabled = false;
134165

166+
// Updates the status of the File tab of the OB
135167
UpdateOBFileButton();
136168

169+
// Sets up the OB search cooldown timer
137170
SearchCooldownTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(300) };
138171
SearchCooldownTimer.Tick += OnSearchCooldownTimerTick;
139172

173+
// Passes the Logging Box to the LoggingControl class
140174
LoggingControl.LogBox = LogTextbox;
141175
}
142176
#endregion
@@ -390,6 +424,8 @@ private void AddEditorElement(FileInfo fInfo, string editorTitle, bool SelectMe,
390424
{
391425
layoutDocument.IsSelected = true;
392426
editor.editor.TextArea.Caret.Show();
427+
EditorToFocus = editor;
428+
SelectDocumentTimer.Start();
393429
}
394430
}
395431

0 commit comments

Comments
 (0)