De-snaggle some of the snaggle-toothed save routine nonsense

This commit is contained in:
Craig Everett 2026-02-13 15:07:22 +09:00
parent 3a570f000e
commit fcf44f55a1

View File

@ -945,15 +945,14 @@ open_hash3(State, Address, Source) ->
end. end.
% TODO: Break this down -- tons of things in here recur. save(State = #s{prefs = Prefs, code = {Codebook, Pages}}) ->
save(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}}) ->
case wxNotebook:getSelection(Codebook) of case wxNotebook:getSelection(Codebook) of
?wxNOT_FOUND -> ?wxNOT_FOUND ->
State; State;
Index -> Index ->
case lists:nth(Index + 1, Pages) of case lists:nth(Index + 1, Pages) of
#p{path = {file, Path}, code = Widget} -> #p{path = {file, Path}, code = Widget} ->
Source = wxTextCtrl:getValue(Widget), Source = wxStyledTextCtrl:getText(Widget),
case filelib:ensure_dir(Path) of case filelib:ensure_dir(Path) of
ok -> ok ->
case file:write_file(Path, Source) of case file:write_file(Path, Source) of
@ -968,7 +967,7 @@ save(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}})
State State
end; end;
Page = #p{path = {hash, Hash}, code = Widget} -> Page = #p{path = {hash, Hash}, code = Widget} ->
DefaultDir = DefDir =
case maps:find(dir, Prefs) of case maps:find(dir, Prefs) of
{ok, PrefDir} -> {ok, PrefDir} ->
PrefDir; PrefDir;
@ -978,69 +977,34 @@ save(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}})
D -> filename:basename(D) D -> filename:basename(D)
end end
end, end,
Options = DefName = unicode:characters_to_list([Hash, ".aes"]),
[{message, J("Save Location")}, save_dialog(State, DefDir, DefName, Widget, Index, Page)
{defaultDir, DefaultDir},
{defaultFile, unicode:characters_to_list([Hash, ".aes"])},
{wildCard, "*.aes"},
{style, ?wxFD_SAVE bor ?wxFD_OVERWRITE_PROMPT}],
Dialog = wxFileDialog:new(Frame, Options),
NewState =
case wxFileDialog:showModal(Dialog) of
?wxID_OK ->
Dir = wxFileDialog:getDirectory(Dialog),
case wxFileDialog:getFilename(Dialog) of
"" ->
State;
Name ->
File =
case filename:extension(Name) of
".aes" -> Name;
_ -> Name ++ ".aes"
end,
Path = filename:join(Dir, File),
Source = gd_sophia_editor:get_text(Widget),
case filelib:ensure_dir(Path) of
ok ->
case file:write_file(Path, Source) of
ok ->
true = wxNotebook:setPageText(Codebook, Index, File),
NewPrefs = maps:put(dir, Dir, Prefs),
NewPage = Page#p{path = {file, Path}},
NewPages = store_nth(Index + 1, NewPage, Pages),
NewCode = {Codebook, NewPages},
State#s{prefs = NewPrefs, code = NewCode};
Error ->
ok = handle_troubling(State, Error),
State
end;
Error ->
ok = handle_troubling(State, Error),
State
end
end;
?wxID_CANCEL ->
State
end,
ok = wxFileDialog:destroy(Dialog),
NewState
end end
end. end.
% TODO: Break this down -- tons of things in here recur. rename(State = #s{code = {Codebook, Pages}}) ->
rename(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}}) ->
case wxNotebook:getSelection(Codebook) of case wxNotebook:getSelection(Codebook) of
?wxNOT_FOUND -> ?wxNOT_FOUND ->
State; State;
Index -> Index ->
case lists:nth(Index + 1, Pages) of case lists:nth(Index + 1, Pages) of
Page = #p{path = {file, Path}, code = Widget} -> Page = #p{path = {file, Path}, code = Widget} ->
DefaultDir = filename:dirname(Path), DefDir = filename:dirname(Path),
DefName = filename:basename(Path),
save_dialog(State, DefDir, DefName, Widget, Index, Page);
#p{path = {hash, _}} ->
save(State)
end
end.
save_dialog(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}},
DefDir, DefName, Widget, Index, Page) ->
Options = Options =
[{message, J("Save Location")}, [{message, J("Save Location")},
{defaultDir, DefaultDir}, {defaultDir, DefDir},
{defaultFile, filename:basename(Path)}, {defaultFile, DefName},
{wildCard, "*.aes"}, {wildCard, "*.aes"},
{style, ?wxFD_SAVE bor ?wxFD_OVERWRITE_PROMPT}], {style, ?wxFD_SAVE bor ?wxFD_OVERWRITE_PROMPT}],
Dialog = wxFileDialog:new(Frame, Options), Dialog = wxFileDialog:new(Frame, Options),
@ -1082,11 +1046,7 @@ rename(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}}
State State
end, end,
ok = wxFileDialog:destroy(Dialog), ok = wxFileDialog:destroy(Dialog),
NewState; NewState.
#p{path = {hash, _}} ->
save(State)
end
end.
close_source(State = #s{code = {Codebook, Pages}}) -> close_source(State = #s{code = {Codebook, Pages}}) ->