File tree Expand file tree Collapse file tree 5 files changed +56
-2
lines changed Expand file tree Collapse file tree 5 files changed +56
-2
lines changed Original file line number Diff line number Diff line change @@ -169,9 +169,9 @@ public static void SetTheme(string theme, string themeOverridesFile)
169
169
}
170
170
}
171
171
172
- public static void SetupTrayIcon ( bool enable )
172
+ public void SetupTrayIcon ( bool enable )
173
173
{
174
- if ( enable )
174
+ if ( enable && Native . OS . EnsureSingleInstance ( ) )
175
175
{
176
176
var icons = new TrayIcons {
177
177
new TrayIcon {
@@ -186,6 +186,7 @@ public static void SetupTrayIcon(bool enable)
186
186
} ;
187
187
icons [ 0 ] . Clicked += ( _ , _ ) => ToggleWindow ( ) ;
188
188
TrayIcon . SetIcons ( Current , icons ) ;
189
+ _createdSystemTrayIcon = true ;
189
190
}
190
191
}
191
192
@@ -591,5 +592,6 @@ private void ShowSelfUpdateResult(object data)
591
592
private ResourceDictionary _activeLocale = null ;
592
593
private ResourceDictionary _themeOverrides = null ;
593
594
private ResourceDictionary _fontsOverrides = null ;
595
+ private bool _createdSystemTrayIcon = false ;
594
596
}
595
597
}
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ namespace SourceGit.Native
11
11
[ SupportedOSPlatform ( "linux" ) ]
12
12
internal class Linux : OS . IBackend
13
13
{
14
+ private FileStream _fs = null ;
14
15
public void SetupApp ( AppBuilder builder )
15
16
{
16
17
builder . With ( new X11PlatformOptions ( ) { EnableIme = true } ) ;
@@ -97,6 +98,26 @@ public void OpenWithDefaultEditor(string file)
97
98
}
98
99
}
99
100
101
+ public bool EnsureSingleInstance ( )
102
+ {
103
+ var pidfile = Path . Combine ( Path . GetTempPath ( ) , "sourcegit.pid" ) ;
104
+ var pid = Process . GetCurrentProcess ( ) . Id . ToString ( ) ;
105
+ Console . WriteLine ( "pid " + pid ) ;
106
+
107
+ try
108
+ {
109
+ _fs = File . OpenWrite ( pidfile ) ;
110
+ _fs . Lock ( 0 , 1000 ) ;
111
+ new StreamWriter ( _fs ) . Write ( pid ) ;
112
+ return true ;
113
+ }
114
+ catch ( IOException )
115
+ {
116
+ Console . WriteLine ( "another SourceGit is running" ) ;
117
+ return false ;
118
+ }
119
+ }
120
+
100
121
private string FindExecutable ( string filename )
101
122
{
102
123
var pathVariable = Environment . GetEnvironmentVariable ( "PATH" ) ?? string . Empty ;
Original file line number Diff line number Diff line change @@ -86,5 +86,7 @@ public void OpenWithDefaultEditor(string file)
86
86
{
87
87
Process . Start ( "open" , $ "\" { file } \" ") ;
88
88
}
89
+
90
+ public bool EnsureSingleInstance ( ) { return true ; }
89
91
}
90
92
}
Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ public interface IBackend
23
23
void OpenInFileManager ( string path , bool select ) ;
24
24
void OpenBrowser ( string url ) ;
25
25
void OpenWithDefaultEditor ( string file ) ;
26
+
27
+ bool EnsureSingleInstance ( ) ;
26
28
}
27
29
28
30
public static string DataDir {
@@ -217,6 +219,11 @@ private static void UpdateGitVersion()
217
219
[ GeneratedRegex ( @"^git version[\s\w]*(\d+)\.(\d+)[\.\-](\d+).*$" ) ]
218
220
private static partial Regex REG_GIT_VERSION ( ) ;
219
221
222
+ public static bool EnsureSingleInstance ( )
223
+ {
224
+ return _backend . EnsureSingleInstance ( ) ;
225
+ }
226
+
220
227
private static IBackend _backend = null ;
221
228
private static string _gitExecutable = string . Empty ;
222
229
}
Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ namespace SourceGit.Native
14
14
[ SupportedOSPlatform ( "windows" ) ]
15
15
internal class Windows : OS . IBackend
16
16
{
17
+ private FileStream _fs = null ;
18
+
17
19
[ StructLayout ( LayoutKind . Sequential ) ]
18
20
internal struct RTL_OSVERSIONINFOEX
19
21
{
@@ -393,5 +395,25 @@ private string FindVSSolutionFile(DirectoryInfo dir, int leftDepth)
393
395
394
396
return null ;
395
397
}
398
+
399
+ public bool EnsureSingleInstance ( )
400
+ {
401
+ var pidfile = Path . Combine ( Path . GetTempPath ( ) , "sourcegit.pid" ) ;
402
+ var pid = Process . GetCurrentProcess ( ) . Id . ToString ( ) ;
403
+ Console . WriteLine ( "pid " + pid ) ;
404
+
405
+ try
406
+ {
407
+ _fs = File . OpenWrite ( pidfile ) ;
408
+ _fs . Lock ( 0 , 1000 ) ;
409
+ new StreamWriter ( _fs ) . Write ( pid ) ;
410
+ return true ;
411
+ }
412
+ catch ( IOException )
413
+ {
414
+ Console . WriteLine ( "another SourceGit is running" ) ;
415
+ return false ;
416
+ }
417
+ }
396
418
}
397
419
}
You can’t perform that action at this time.
0 commit comments