77using System . IO . Pipes ;
88using System . Threading ;
99using System . Threading . Tasks ;
10+ using System . Windows . Forms ;
1011
1112namespace WinDynamicDesktop
1213{
@@ -30,7 +31,7 @@ public void Dispose()
3031 namedPipeServer ? . Dispose ( ) ;
3132 }
3233
33- public void ListenForArgs ( Action < string [ ] > listener )
34+ public void ListenForArgs ( ApplicationContext app )
3435 {
3536 Task . Factory . StartNew ( ( ) =>
3637 {
@@ -44,7 +45,8 @@ public void ListenForArgs(Action<string[]> listener)
4445 namedPipeServer . WaitForConnection ( ) ;
4546 }
4647
47- listener ( reader . ReadToEnd ( ) . Split ( Environment . NewLine , StringSplitOptions . RemoveEmptyEntries ) ) ;
48+ ProcessArgs ( app , reader . ReadToEnd ( ) . Split ( Environment . NewLine ,
49+ StringSplitOptions . RemoveEmptyEntries ) ) ;
4850 namedPipeServer . Disconnect ( ) ;
4951 }
5052 } , TaskCreationOptions . LongRunning ) ;
@@ -59,5 +61,115 @@ public void SendArgsToFirstInstance(string[] args)
5961 writer . Write ( string . Join ( Environment . NewLine , args ) ) ;
6062 writer . Flush ( ) ;
6163 }
64+
65+ public void ProcessArgs ( string [ ] initialArgs )
66+ {
67+ foreach ( string arg in initialArgs )
68+ {
69+ if ( arg . ToLower ( ) . StartsWith ( "/theme" ) && arg . IndexOf ( '=' ) != - 1 )
70+ {
71+ ProcessThemeArg ( arg . ToLower ( ) ) ;
72+ }
73+ else if ( arg . StartsWith ( '/' ) )
74+ {
75+ switch ( arg . ToLower ( ) )
76+ {
77+ case "/refresh" :
78+ break ;
79+ case "/theme:auto" :
80+ case "/theme:light" :
81+ case "/theme:dark" :
82+ AppearanceMode mode = ( AppearanceMode ) Enum . Parse ( typeof ( AppearanceMode ) ,
83+ arg . Substring ( 7 ) , true ) ;
84+ JsonConfig . settings . appearanceMode = ( int ) mode ;
85+ break ;
86+ default :
87+ Console . WriteLine ( "Unrecognized command line option: " + arg ) ;
88+ break ;
89+ }
90+ }
91+ else if ( File . Exists ( arg ) )
92+ {
93+ ThemeManager . importPaths . Add ( arg ) ;
94+ }
95+ }
96+ }
97+
98+ private void ProcessArgs ( ApplicationContext app , string [ ] args )
99+ {
100+ if ( JsonConfig . settings . hideTrayIcon )
101+ {
102+ app . MainForm . BeginInvoke ( AppContext . ToggleTrayIcon ) ;
103+ }
104+
105+ foreach ( string arg in args )
106+ {
107+ if ( arg . ToLower ( ) . StartsWith ( "/theme" ) && arg . IndexOf ( '=' ) != - 1 )
108+ {
109+ string themeId = ProcessThemeArg ( arg . ToLower ( ) ) ;
110+ if ( themeId != null )
111+ {
112+ ThemeShuffler . AddThemeToHistory ( themeId ) ;
113+ AppContext . scheduler . Run ( true ) ;
114+ }
115+ }
116+ else if ( arg . StartsWith ( '/' ) )
117+ {
118+ switch ( arg . ToLower ( ) )
119+ {
120+ case "/refresh" :
121+ AppContext . scheduler . RunAndUpdateLocation ( true ) ;
122+ break ;
123+ case "/theme:auto" :
124+ case "/theme:light" :
125+ case "/theme:dark" :
126+ AppearanceMode mode = ( AppearanceMode ) Enum . Parse ( typeof ( AppearanceMode ) ,
127+ arg . Substring ( 7 ) , true ) ;
128+ app . MainForm . BeginInvoke ( SolarScheduler . SetAppearanceMode , mode ) ;
129+ break ;
130+ default :
131+ Console . WriteLine ( "Unrecognized command line option: " + arg ) ;
132+ break ;
133+ }
134+ }
135+ else if ( File . Exists ( arg ) )
136+ {
137+ ThemeManager . importPaths . Add ( arg ) ;
138+ }
139+ }
140+
141+ if ( ThemeManager . importPaths . Count > 0 && ! ThemeManager . importMode )
142+ {
143+ app . MainForm . BeginInvoke ( ThemeManager . SelectTheme ) ;
144+ }
145+ }
146+
147+ private string ProcessThemeArg ( string arg )
148+ {
149+ string themeId = arg . Substring ( arg . IndexOf ( '=' ) + 1 ) ;
150+ if ( ThemeManager . themeSettings . Find ( ( theme ) => theme . themeId == themeId ) == null )
151+ {
152+ Console . WriteLine ( "Failed to set theme - unknown theme ID: " + themeId ) ;
153+ return null ;
154+ }
155+
156+ if ( arg . StartsWith ( "/theme=" ) )
157+ {
158+ JsonConfig . settings . activeThemes [ 0 ] = themeId ;
159+ }
160+ else if ( arg . StartsWith ( "/theme:L=" ) )
161+ {
162+ JsonConfig . settings . lockScreenTheme = themeId ;
163+ JsonConfig . settings . lockScreenDisplayIndex = - 1 ;
164+ }
165+ else
166+ {
167+ int displayNumber = int . Parse ( arg [ 7 ] . ToString ( ) ) ;
168+ JsonConfig . settings . activeThemes [ displayNumber ] = themeId ;
169+ JsonConfig . settings . activeThemes [ 0 ] = null ;
170+ }
171+
172+ return themeId ;
173+ }
62174 }
63175}
0 commit comments