De-snaggle some of the snaggle-toothed save routine nonsense
This commit is contained in:
parent
3a570f000e
commit
fcf44f55a1
@ -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,117 +977,78 @@ 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),
|
||||||
Options =
|
DefName = filename:basename(Path),
|
||||||
[{message, J("Save Location")},
|
save_dialog(State, DefDir, DefName, Widget, Index, Page);
|
||||||
{defaultDir, DefaultDir},
|
|
||||||
{defaultFile, filename:basename(Path)},
|
|
||||||
{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,
|
|
||||||
NewPath = filename:join(Dir, File),
|
|
||||||
Source = gd_sophia_editor:get_text(Widget),
|
|
||||||
case filelib:ensure_dir(NewPath) of
|
|
||||||
ok ->
|
|
||||||
case file:write_file(NewPath, Source) of
|
|
||||||
ok ->
|
|
||||||
true = wxNotebook:setPageText(Codebook, Index, File),
|
|
||||||
NewPrefs = maps:put(dir, Dir, Prefs),
|
|
||||||
NewPage = Page#p{path = {file, NewPath}},
|
|
||||||
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;
|
|
||||||
#p{path = {hash, _}} ->
|
#p{path = {hash, _}} ->
|
||||||
save(State)
|
save(State)
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
save_dialog(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}},
|
||||||
|
DefDir, DefName, Widget, Index, Page) ->
|
||||||
|
Options =
|
||||||
|
[{message, J("Save Location")},
|
||||||
|
{defaultDir, DefDir},
|
||||||
|
{defaultFile, DefName},
|
||||||
|
{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,
|
||||||
|
NewPath = filename:join(Dir, File),
|
||||||
|
Source = gd_sophia_editor:get_text(Widget),
|
||||||
|
case filelib:ensure_dir(NewPath) of
|
||||||
|
ok ->
|
||||||
|
case file:write_file(NewPath, Source) of
|
||||||
|
ok ->
|
||||||
|
true = wxNotebook:setPageText(Codebook, Index, File),
|
||||||
|
NewPrefs = maps:put(dir, Dir, Prefs),
|
||||||
|
NewPage = Page#p{path = {file, NewPath}},
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
close_source(State = #s{code = {Codebook, Pages}}) ->
|
close_source(State = #s{code = {Codebook, Pages}}) ->
|
||||||
case wxNotebook:getSelection(Codebook) of
|
case wxNotebook:getSelection(Codebook) of
|
||||||
?wxNOT_FOUND ->
|
?wxNOT_FOUND ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user