-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWP.GitHub.Setting.pas
182 lines (161 loc) · 4.98 KB
/
WP.GitHub.Setting.pas
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
{ ***************************************************}
{ Auhtor: Ali Dehbansiahkarbon([email protected]) }
{ GitHub: https://github.com/AliDehbansiahkarbon }
{ ***************************************************}
unit WP.GitHub.Setting;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, System.Win.Registry, WP.GitHub.Constants, ToolsAPI,
Vcl.BaseImageCollection, Vcl.ImageCollection;
type
TSingletonSettings = class
private
FGitPath: string;
FStartupLoad: Boolean;
FDefaultPeriodIndex: Byte;
FDefaultLanguageIndex: Byte;
class var FInstance: TSingletonSettings;
class function GetInstance: TSingletonSettings; static;
constructor Create;
function LoadFromRegistry: TSingletonSettings;
public
class procedure RegisterFormClassForTheming(const AFormClass: TCustomFormClass; const Component: TComponent); static;
class property Instance: TSingletonSettings read GetInstance;
property GitPath: string read FGitPath write FGitPath;
property StartupLoad: Boolean read FStartupLoad write FStartupLoad;
property DefaultPeriodIndex: Byte read FDefaultPeriodIndex write FDefaultPeriodIndex;
property DefaultLanguageIndex: Byte read FDefaultLanguageIndex write FDefaultLanguageIndex;
end;
TFrm_Settings = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
edt_GitPath: TEdit;
Button1: TButton;
GroupBox2: TGroupBox;
Btn_Close: TButton;
Btn_Save: TButton;
Label2: TLabel;
cbb_Period: TComboBox;
Label3: TLabel;
cbb_Lang: TComboBox;
chk_StartupLoad: TCheckBox;
procedure Btn_CloseClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Btn_SaveClick(Sender: TObject);
private
procedure LoadSettings;
procedure SaveSettings;
public
{ Public declarations }
end;
var
Frm_Settings: TFrm_Settings;
implementation
{$R *.dfm}
procedure TFrm_Settings.Btn_CloseClick(Sender: TObject);
begin
Close;
end;
procedure TFrm_Settings.Btn_SaveClick(Sender: TObject);
begin
SaveSettings;
Close;
end;
procedure TFrm_Settings.FormCreate(Sender: TObject);
begin
LoadSettings;
end;
procedure TFrm_Settings.LoadSettings;
begin
var LvSetting := TSingletonSettings.Instance.LoadFromRegistry;
edt_GitPath.Text := LvSetting.GitPath;
chk_StartupLoad.Checked := LvSetting.StartupLoad;
cbb_Period.ItemIndex := LvSetting.DefaultPeriodIndex;
cbb_Lang.ItemIndex := LvSetting.DefaultLanguageIndex;
end;
procedure TFrm_Settings.SaveSettings;
var
LvRegistry: TRegistry;
begin
if not FileExists(edt_GitPath.Text) then
begin
ShowMessage('Git.exe could not be found!');
Exit;
end;
LvRegistry := TRegistry.Create;
try
LvRegistry.RootKey := HKEY_CURRENT_USER;
if LvRegistry.OpenKey(cBaseKey + cSettingsPath, True) then
begin
var LvSetting := TSingletonSettings.Instance;
LvSetting.GitPath := edt_GitPath.Text;
LvSetting.StartupLoad := chk_StartupLoad.Checked;
LvSetting.DefaultPeriodIndex := cbb_Period.ItemIndex;
LvSetting.DefaultLanguageIndex := cbb_Lang.ItemIndex;
LvRegistry.WriteString('GitPath', LvSetting.GitPath);
LvRegistry.WriteBool('StartupLoad', LvSetting.StartupLoad);
LvRegistry.WriteInteger('DefaultPeriod', LvSetting.DefaultPeriodIndex);
LvRegistry.WriteInteger('DefaultLanguage', LvSetting.DefaultLanguageIndex);
LvRegistry.CloseKey;
end;
finally
LvRegistry.Free;
end;
end;
{ TSingletonSettings }
constructor TSingletonSettings.Create;
begin
inherited;
LoadFromRegistry;
end;
class function TSingletonSettings.GetInstance: TSingletonSettings;
begin
if not Assigned(FInstance) then
FInstance := TSingletonSettings.Create;
Result := FInstance;
end;
function TSingletonSettings.LoadFromRegistry: TSingletonSettings;
var
LvRegistry: TRegistry;
begin
Result := FInstance;
LvRegistry := TRegistry.Create;
try
LvRegistry.RootKey := HKEY_CURRENT_USER;
if LvRegistry.OpenKeyReadOnly(cBaseKey + cSettingsPath) then
begin
FGitPath := LvRegistry.ReadString('GitPath');
FStartupLoad := LvRegistry.ReadBool('StartupLoad');
FDefaultPeriodIndex := LvRegistry.ReadInteger('DefaultPeriod');
FDefaultLanguageIndex := LvRegistry.ReadInteger('DefaultLanguage');
LvRegistry.CloseKey;
end
else
begin //Default settings
FGitPath := 'C:\Program Files\Git\bin\git.exe';
FStartupLoad := True;
FDefaultPeriodIndex := 0;
FDefaultLanguageIndex := 0;
end;
finally
LvRegistry.Free;
end;
end;
class procedure TSingletonSettings.RegisterFormClassForTheming(
const AFormClass: TCustomFormClass; const Component: TComponent);
var
ITS: IOTAIDEThemingServices;
begin
if Supports(BorlandIDEServices, IOTAIDEThemingServices, ITS) then
begin
if ITS.IDEThemingEnabled then
begin
ITS.RegisterFormClass(AFormClass);
if Assigned(Component) then
ITS.ApplyTheme(Component);
end;
end;
end;
end.