2
2
// The Microsoft Corporation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using System . Globalization ;
5
6
using GitHubExtension . DeveloperId ;
6
7
using GitHubExtension . Helpers ;
7
8
using Microsoft . CommandPalette . Extensions ;
8
9
using Microsoft . CommandPalette . Extensions . Toolkit ;
9
- using Windows . Foundation ;
10
10
11
11
namespace GitHubExtension . Controls . Forms ;
12
12
@@ -21,10 +21,43 @@ public partial class SignInForm : FormContent, IGitHubForm
21
21
private readonly IDeveloperIdProvider _developerIdProvider ;
22
22
private readonly IResources _resources ;
23
23
24
+ private bool _isButtonEnabled = true ;
25
+
26
+ private string IsButtonEnabled =>
27
+ _isButtonEnabled . ToString ( CultureInfo . InvariantCulture ) . ToLower ( CultureInfo . InvariantCulture ) ;
28
+
24
29
public SignInForm ( IDeveloperIdProvider developerIdProvider , IResources resources )
25
30
{
26
31
_resources = resources ;
27
32
_developerIdProvider = developerIdProvider ;
33
+ _developerIdProvider . OAuthRedirected += DeveloperIdProvider_OAuthRedirected ;
34
+ SignOutForm . SignOutAction += SignOutForm_SignOutAction ;
35
+ }
36
+
37
+ private void SignOutForm_SignOutAction ( object ? sender , SignInStatusChangedEventArgs e )
38
+ {
39
+ _isButtonEnabled = ! e . IsSignedIn ;
40
+ }
41
+
42
+ private void DeveloperIdProvider_OAuthRedirected ( object ? sender , Exception ? e )
43
+ {
44
+ if ( e is not null )
45
+ {
46
+ SetButtonEnabled ( true ) ;
47
+ LoadingStateChanged ? . Invoke ( this , false ) ;
48
+ SignInAction ? . Invoke ( this , new SignInStatusChangedEventArgs ( false , e ) ) ;
49
+ FormSubmitted ? . Invoke ( this , new FormSubmitEventArgs ( false , e ) ) ;
50
+ return ;
51
+ }
52
+
53
+ SetButtonEnabled ( false ) ;
54
+ }
55
+
56
+ private void SetButtonEnabled ( bool isEnabled )
57
+ {
58
+ _isButtonEnabled = isEnabled ;
59
+ TemplateJson = TemplateHelper . LoadTemplateJsonFromTemplateName ( "AuthTemplate" , TemplateSubstitutions ) ;
60
+ OnPropertyChanged ( nameof ( TemplateJson ) ) ;
28
61
}
29
62
30
63
public Dictionary < string , string > TemplateSubstitutions => new ( )
@@ -33,6 +66,7 @@ public SignInForm(IDeveloperIdProvider developerIdProvider, IResources resources
33
66
{ "{{AuthButtonTitle}}" , _resources . GetResource ( "Forms_Sign_In" ) } ,
34
67
{ "{{AuthIcon}}" , $ "data:image/png;base64,{ GitHubIcon . GetBase64Icon ( "logo" ) } " } ,
35
68
{ "{{AuthButtonTooltip}}" , _resources . GetResource ( "Forms_Sign_In_Tooltip" ) } ,
69
+ { "{{ButtonIsEnabled}}" , IsButtonEnabled } ,
36
70
} ;
37
71
38
72
public override string TemplateJson => TemplateHelper . LoadTemplateJsonFromTemplateName ( "AuthTemplate" , TemplateSubstitutions ) ;
@@ -52,6 +86,7 @@ public override ICommandResult SubmitForm(string inputs, string data)
52
86
catch ( Exception ex )
53
87
{
54
88
LoadingStateChanged ? . Invoke ( this , false ) ;
89
+ SetButtonEnabled ( true ) ;
55
90
SignInAction ? . Invoke ( this , new SignInStatusChangedEventArgs ( false , ex ) ) ;
56
91
FormSubmitted ? . Invoke ( this , new FormSubmitEventArgs ( false , ex ) ) ;
57
92
}
0 commit comments