|
907 | 907 | catch(make_directory(Dir), E, (print_message(warning, E), fail)). |
908 | 908 |
|
909 | 909 | :- elif(current_prolog_flag(apple, true)). |
| 910 | +use_app_settings(true). % Indicate we need app settings |
910 | 911 |
|
911 | 912 | setup_app :- |
912 | 913 | apple_set_locale, |
|
917 | 918 | current_prolog_flag(executable, Exe), |
918 | 919 | file_base_name(Exe, 'SWI-Prolog'), |
919 | 920 | !, |
920 | | - setup_macos_app, |
921 | | - at_halt(save_working_directory). |
| 921 | + setup_macos_app. |
922 | 922 | setup_app. |
923 | 923 |
|
924 | 924 | apple_set_locale :- |
925 | 925 | ( getenv('LC_CTYPE', 'UTF-8'), |
926 | | - apple_current_locale_identifier(LocaleID), |
927 | | - atom_concat(LocaleID, '.UTF-8', Locale), |
928 | | - catch(setlocale(ctype, _Old, Locale), _, fail) |
| 926 | + apple_current_locale_identifier(LocaleID), |
| 927 | + atom_concat(LocaleID, '.UTF-8', Locale), |
| 928 | + catch(setlocale(ctype, _Old, Locale), _, fail) |
929 | 929 | -> setenv('LANG', Locale), |
930 | 930 | unsetenv('LC_CTYPE') |
931 | 931 | ; true |
932 | 932 | ). |
933 | 933 |
|
934 | 934 | setup_macos_app :- |
935 | | - app_settings(Settings), |
936 | | - WD = Settings.get(working_directory), |
937 | | - catch(working_directory(_, WD), _, fail), |
| 935 | + restore_working_directory, |
938 | 936 | !. |
939 | 937 | setup_macos_app :- |
940 | 938 | expand_file_name('~/Documents/Prolog', [PrologDir]), |
|
948 | 946 | !. |
949 | 947 | setup_macos_app. |
950 | 948 |
|
| 949 | +:- elif(current_prolog_flag(emscripten, true)). |
| 950 | +setup_app. |
| 951 | +:- else. |
| 952 | +use_app_settings(true). % Indicate we need app settings |
| 953 | + |
| 954 | +% Other (Unix-like) platforms. |
| 955 | +setup_app :- |
| 956 | + running_as_app, |
| 957 | + restore_working_directory, |
| 958 | + !. |
| 959 | +setup_app. |
| 960 | + |
| 961 | +%! running_as_app is semidet. |
| 962 | +% |
| 963 | +% True if we were started from the dock. |
| 964 | + |
| 965 | +running_as_app :- |
| 966 | +% getenv('FLATPAK_SANDBOX_DIR', _), |
| 967 | + current_prolog_flag(epilog, true), |
| 968 | + stream_property(In, file_no(0)), |
| 969 | + \+ stream_property(In, tty(true)), |
| 970 | + !. |
| 971 | + |
| 972 | +:- endif. |
| 973 | + |
| 974 | + |
| 975 | +:- if(use_app_settings(true)). |
| 976 | + |
| 977 | + /******************************* |
| 978 | + * APP WORKING DIRECTORY * |
| 979 | + *******************************/ |
| 980 | + |
951 | 981 | save_working_directory :- |
952 | 982 | working_directory(WD, WD), |
953 | 983 | app_settings(Settings), |
954 | 984 | ( Settings.get(working_directory) == WD |
955 | 985 | -> true |
956 | | - ; save_settings(Settings.put(working_directory, WD)) |
| 986 | + ; app_save_settings(Settings.put(working_directory, WD)) |
957 | 987 | ). |
958 | 988 |
|
| 989 | +restore_working_directory :- |
| 990 | + at_halt(save_working_directory), |
| 991 | + app_settings(Settings), |
| 992 | + WD = Settings.get(working_directory), |
| 993 | + catch(working_directory(_, WD), _, fail), |
| 994 | + !. |
| 995 | + |
| 996 | + /******************************* |
| 997 | + * SETTINGS * |
| 998 | + *******************************/ |
| 999 | + |
| 1000 | +%! app_settings(-Settings:dict) is det. |
| 1001 | +% |
| 1002 | +% Get a dict holding the persistent application settings. |
| 1003 | + |
959 | 1004 | app_settings(Settings) :- |
960 | 1005 | app_settings_file(File), |
961 | 1006 | access_file(File, read), |
|
968 | 1013 | !. |
969 | 1014 | app_settings(#{}). |
970 | 1015 |
|
971 | | -save_settings(Settings) :- |
| 1016 | +%! app_save_settings(+Settings:dict) is det. |
| 1017 | +% |
| 1018 | +% Save the given application settings dict. |
| 1019 | + |
| 1020 | +app_save_settings(Settings) :- |
972 | 1021 | app_settings_file(File), |
973 | 1022 | catch(setup_call_cleanup( |
974 | 1023 | open(File, write, Out, [encoding(utf8)]), |
|
988 | 1037 | [ access(write), |
989 | 1038 | file_errors(fail) |
990 | 1039 | ]). |
| 1040 | +:- endif.% app_settings |
991 | 1041 |
|
992 | | -:- else. |
993 | | -% Other platforms. |
994 | | -setup_app. |
995 | | -:- endif. |
| 1042 | + /******************************* |
| 1043 | + * TOPLEVEL * |
| 1044 | + *******************************/ |
996 | 1045 |
|
997 | 1046 | :- '$hide'('$toplevel'/0). % avoid in the GUI stacktrace |
998 | 1047 |
|
|
0 commit comments