Pub.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. unit Pub;
  2. interface
  3. uses
  4. FormDocument, DocWinList, rjson, JsonDocument, System.JSON, System.Generics.Collections,
  5. System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  6. FMX.Types, FMX.Controls, System.IOUtils;
  7. const
  8. app_name = 'gsJSON';
  9. app_version = 'v0.7.1';
  10. url_github = 'https://github.com/higale/galeJSON';
  11. c_untitled_filename = 'untitled.json';
  12. type
  13. TDocumentWindow = TfmDocument;
  14. TPub = class
  15. private
  16. FIniPath: string;
  17. FLangIndex: integer;
  18. FDocumentWindowList: TDocumentWindowList;
  19. function GetIsDarkStyle: Boolean;
  20. procedure SetIsDarkStyle(AValue: Boolean);
  21. function GetStyleBook: TStyleBook;
  22. procedure SetLangIndex(AValue: integer);
  23. public
  24. Config: TRJSON;
  25. Languages: TRJSON;
  26. property LangIndex: integer read FLangIndex write SetLangIndex;
  27. property IniPath: string read FIniPath;
  28. property DocumentWindowList: TDocumentWindowList read FDocumentWindowList;
  29. property IsDarkStyle: Boolean read GetIsDarkStyle write SetIsDarkStyle;
  30. property StyleBook: TStyleBook read GetStyleBook;
  31. public
  32. constructor Create;
  33. destructor Destroy; override;
  34. procedure LoadConfig;
  35. procedure LoadLanguages;
  36. function LangStr(const AKey: string): string;
  37. procedure SaveConfig;
  38. procedure ResetConfig;
  39. end;
  40. var
  41. g_pub: TPub;
  42. implementation
  43. uses FormMain, FormLogger;
  44. constructor TPub.Create;
  45. begin
  46. inherited;
  47. {$IFDEF MSWINDOWS}
  48. // C:\Users\xxx\AppData\Roaming\gsJSON\
  49. //FIniPath := TPath.GetHomePath + TPath.DirectorySeparatorChar + TPath.GetFileNameWithoutExtension(ParamStr(0)) + TPath.DirectorySeparatorChar;
  50. FIniPath := ExtractFilePath(ParamStr(0));
  51. //ForceDirectories(FIniPath);
  52. {$ELSE}
  53. FIniPath := ExtractFilePath(TPath.GetDocumentsPath) + '.' + TPath.GetFileNameWithoutExtension(ParamStr(0)) + TPath.DirectorySeparatorChar;
  54. ForceDirectories(FIniPath);
  55. {$ENDIF}
  56. FDocumentWindowList := TDocumentWindowList.Create;
  57. FDocumentWindowList.OwnsObjects := False;
  58. end;
  59. destructor TPub.Destroy;
  60. begin
  61. FDocumentWindowList.Free;
  62. inherited;
  63. end;
  64. procedure TPub.LoadConfig;
  65. begin
  66. Config.LoadFromFile(FIniPath + 'config.json');
  67. if Config.IsNil then
  68. begin
  69. Config['appName'] := app_name;
  70. Config['appVer'] := app_version;
  71. Config['ui.lang'] := 0;
  72. SaveConfig;
  73. end;
  74. FLangIndex := Config['ui.lang']
  75. end;
  76. procedure TPub.LoadLanguages;
  77. begin
  78. {$IFDEF MSWINDOWS}
  79. Languages.LoadFromFile(FIniPath + 'lang.json');
  80. {$ELSE}
  81. var
  82. AppPath := System.IOUtils.TPath.GetDirectoryName(ParamStr(0));
  83. Languages.LoadFromFile(ExtractFilePath(AppPath) + 'Resources/lang.json');
  84. {$ENDIF}
  85. end;
  86. function TPub.LangStr(const AKey: string): string;
  87. begin
  88. Result := Languages[FLangIndex]['items.' + AKey];
  89. if Result = '' then
  90. Result := Languages[0]['items.' + AKey].ToStr('NoLangString');
  91. end;
  92. procedure TPub.SaveConfig;
  93. begin
  94. Config.SaveToFile(FIniPath + 'config.json', 4, True, False)
  95. end;
  96. procedure TPub.ResetConfig;
  97. begin
  98. Config.Reset;
  99. SaveConfig;
  100. end;
  101. function TPub.GetIsDarkStyle: Boolean;
  102. begin
  103. Result := Config['ui.isDarkStyle'].ToBool(True);
  104. end;
  105. procedure TPub.SetIsDarkStyle(AValue: Boolean);
  106. begin
  107. Config['ui.isDarkStyle'] := AValue;
  108. SaveConfig;
  109. for var item in g_pub.DocumentWindowList do
  110. item.SetStyleBook;
  111. if AValue then
  112. begin
  113. fmLogger.StyleBook := fmMain.StyleBookBlack;
  114. end
  115. else
  116. begin
  117. fmLogger.StyleBook := fmMain.StyleBookWhite;
  118. end;
  119. end;
  120. function TPub.GetStyleBook: TStyleBook;
  121. begin
  122. if IsDarkStyle then
  123. Result := fmMain.StyleBookBlack
  124. else
  125. Result := fmMain.StyleBookWhite;
  126. end;
  127. procedure TPub.SetLangIndex(AValue: integer);
  128. begin
  129. if FLangIndex = AValue then
  130. Exit;
  131. FLangIndex := AValue;
  132. Config['ui.lang'] := FLangIndex;
  133. end;
  134. initialization
  135. g_pub := TPub.Create;
  136. g_pub.LoadConfig;
  137. g_pub.LoadLanguages;
  138. finalization
  139. g_pub.SaveConfig;
  140. FreeAndNil(g_pub);
  141. end.