123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- unit Pub;
- interface
- uses
- FormDocument, DocWinList, rjson, JsonDocument, System.JSON, System.Generics.Collections,
- System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
- FMX.Types, FMX.Controls, System.IOUtils;
- const
- app_name = 'gsJSON';
- app_version = 'v0.7.1';
- url_github = 'https://github.com/higale/galeJSON';
- c_untitled_filename = 'untitled.json';
- type
- TDocumentWindow = TfmDocument;
- TPub = class
- private
- FIniPath: string;
- FLangIndex: integer;
- FDocumentWindowList: TDocumentWindowList;
- function GetIsDarkStyle: Boolean;
- procedure SetIsDarkStyle(AValue: Boolean);
- function GetStyleBook: TStyleBook;
- procedure SetLangIndex(AValue: integer);
- public
- Config: TRJSON;
- Languages: TRJSON;
- property LangIndex: integer read FLangIndex write SetLangIndex;
- property IniPath: string read FIniPath;
- property DocumentWindowList: TDocumentWindowList read FDocumentWindowList;
- property IsDarkStyle: Boolean read GetIsDarkStyle write SetIsDarkStyle;
- property StyleBook: TStyleBook read GetStyleBook;
- public
- constructor Create;
- destructor Destroy; override;
- procedure LoadConfig;
- procedure LoadLanguages;
- function LangStr(const AKey: string): string;
- procedure SaveConfig;
- procedure ResetConfig;
- end;
- var
- g_pub: TPub;
- implementation
- uses FormMain, FormLogger;
- constructor TPub.Create;
- begin
- inherited;
- {$IFDEF MSWINDOWS}
- // C:\Users\xxx\AppData\Roaming\gsJSON\
- //FIniPath := TPath.GetHomePath + TPath.DirectorySeparatorChar + TPath.GetFileNameWithoutExtension(ParamStr(0)) + TPath.DirectorySeparatorChar;
- FIniPath := ExtractFilePath(ParamStr(0));
- //ForceDirectories(FIniPath);
- {$ELSE}
- FIniPath := ExtractFilePath(TPath.GetDocumentsPath) + '.' + TPath.GetFileNameWithoutExtension(ParamStr(0)) + TPath.DirectorySeparatorChar;
- ForceDirectories(FIniPath);
- {$ENDIF}
- FDocumentWindowList := TDocumentWindowList.Create;
- FDocumentWindowList.OwnsObjects := False;
- end;
- destructor TPub.Destroy;
- begin
- FDocumentWindowList.Free;
- inherited;
- end;
- procedure TPub.LoadConfig;
- begin
- Config.LoadFromFile(FIniPath + 'config.json');
- if Config.IsNil then
- begin
- Config['appName'] := app_name;
- Config['appVer'] := app_version;
- Config['ui.lang'] := 0;
- SaveConfig;
- end;
- FLangIndex := Config['ui.lang']
- end;
- procedure TPub.LoadLanguages;
- begin
- {$IFDEF MSWINDOWS}
- Languages.LoadFromFile(FIniPath + 'lang.json');
- {$ELSE}
- var
- AppPath := System.IOUtils.TPath.GetDirectoryName(ParamStr(0));
- Languages.LoadFromFile(ExtractFilePath(AppPath) + 'Resources/lang.json');
- {$ENDIF}
- end;
- function TPub.LangStr(const AKey: string): string;
- begin
- Result := Languages[FLangIndex]['items.' + AKey];
- if Result = '' then
- Result := Languages[0]['items.' + AKey].ToStr('NoLangString');
- end;
- procedure TPub.SaveConfig;
- begin
- Config.SaveToFile(FIniPath + 'config.json', 4, True, False)
- end;
- procedure TPub.ResetConfig;
- begin
- Config.Reset;
- SaveConfig;
- end;
- function TPub.GetIsDarkStyle: Boolean;
- begin
- Result := Config['ui.isDarkStyle'].ToBool(True);
- end;
- procedure TPub.SetIsDarkStyle(AValue: Boolean);
- begin
- Config['ui.isDarkStyle'] := AValue;
- SaveConfig;
- for var item in g_pub.DocumentWindowList do
- item.SetStyleBook;
- if AValue then
- begin
- fmLogger.StyleBook := fmMain.StyleBookBlack;
- end
- else
- begin
- fmLogger.StyleBook := fmMain.StyleBookWhite;
- end;
- end;
- function TPub.GetStyleBook: TStyleBook;
- begin
- if IsDarkStyle then
- Result := fmMain.StyleBookBlack
- else
- Result := fmMain.StyleBookWhite;
- end;
- procedure TPub.SetLangIndex(AValue: integer);
- begin
- if FLangIndex = AValue then
- Exit;
- FLangIndex := AValue;
- Config['ui.lang'] := FLangIndex;
- end;
- initialization
- g_pub := TPub.Create;
- g_pub.LoadConfig;
- g_pub.LoadLanguages;
- finalization
- g_pub.SaveConfig;
- FreeAndNil(g_pub);
- end.
|