diff --git a/src/gd_con.erl b/src/gd_con.erl index b68b319..6bb2e63 100644 --- a/src/gd_con.erl +++ b/src/gd_con.erl @@ -63,7 +63,7 @@ when Name :: ui_name(). show_ui(Name) -> - gen_server:cast(?MODULE, {show_ui, Name}). + gen_server:call(?MODULE, {show_ui, Name}). -spec open_wallet(Path, Phrase) -> Result @@ -329,6 +329,7 @@ init(none) -> T = erlang:send_after(tic(), self(), tic), State = #s{window = Window, timer = T, wallets = Wallets, prefs = Prefs}, NewState = do_show_ui(gd_v_wallman, State), + ok = gd_v_wallman:first_run(), {ok, NewState}. @@ -378,6 +379,9 @@ handle_call({save, Module, Prefs}, _, State) -> handle_call({mnemonic, ID}, _, State) -> Response = do_mnemonic(ID, State), {reply, Response, State}; +handle_call({show_ui, Name}, _, State) -> + NewState = do_show_ui(Name, State), + {reply, ok, NewState}; handle_call(Unexpected, From, State) -> ok = log(warning, "Unexpected call from ~tp: ~tp~n", [From, Unexpected]), {noreply, State}. @@ -391,13 +395,11 @@ handle_call(Unexpected, From, State) -> %% The gen_server:handle_cast/2 callback. %% See: http://erlang.org/doc/man/gen_server.html#Module:handle_cast-2 -handle_cast({show_ui, Name}, State) -> - NewState = do_show_ui(Name, State), - {noreply, NewState}; handle_cast(close_wallet, State) -> NextState = do_close_wallet(State), ok = gd_gui:show([]), NewState = do_show_ui(gd_v_wallman, NextState), + ok = gd_v_wallman:to_front(), {noreply, NewState}; handle_cast({new_wallet, Net, Name, Path, Password}, State) -> NewState = do_new_wallet(Net, Name, Path, Password, State), @@ -535,7 +537,6 @@ do_show_ui(Name, State = #s{tasks = Tasks, prefs = Prefs}) -> Win = Name:start_link({TaskPrefs, TaskData}), PID = wx_object:get_pid(Win), Mon = monitor(process, PID), -% ok = Name:to_front(Win), UI = #ui{name = Name, pid = PID, wx = Win, mon = Mon}, State#s{tasks = [UI | Tasks]} end. @@ -867,7 +868,9 @@ do_make_key(Name, Seed, base58, Transform, State) -> do_make_key2(_, _, _, State = #s{wallet = none}) -> ok = gd_gui:trouble("No wallet selected!"), - do_show_ui(gd_v_wallman, State); + NewState = do_show_ui(gd_v_wallman, State), + ok = gd_v_wallman:to_front(), + NewState; do_make_key2(Name, Bin, Transform, State = #s{wallet = Current, wallets = Wallets, pass = Pass}) -> #wallet{name = WalletName, poas = POAs, keys = Keys} = Current, diff --git a/src/gd_gui.erl b/src/gd_gui.erl index b7650b4..3b583aa 100644 --- a/src/gd_gui.erl +++ b/src/gd_gui.erl @@ -361,6 +361,7 @@ refresh(State) -> wallman(State) -> ok = gd_con:show_ui(gd_v_wallman), + ok = gd_v_wallman:to_front(), State. diff --git a/src/gd_v_wallman.erl b/src/gd_v_wallman.erl index e2e493c..4f44db0 100644 --- a/src/gd_v_wallman.erl +++ b/src/gd_v_wallman.erl @@ -7,7 +7,7 @@ -behavior(wx_object). %-behavior(gd_v). -include_lib("wx/include/wx.hrl"). --export([to_front/1, trouble/1]). +-export([to_front/0, to_front/1, first_run/0, trouble/1]). -export([show/2]). -export([start_link/1]). -export([init/1, terminate/2, code_change/3, @@ -35,6 +35,12 @@ %%% Interface +-spec to_front() -> ok. + +to_front() -> + wx_object:cast(?MODULE, to_front). + + -spec to_front(Win) -> ok when Win :: wx:wx_object(). @@ -42,6 +48,12 @@ to_front(Win) -> wx_object:cast(Win, to_front). +-spec first_run() -> ok. + +first_run() -> + wx_object:cast(?MODULE, first_run). + + -spec trouble(Info) -> ok when Info :: term(). @@ -116,19 +128,6 @@ init({Prefs, Manifest}) -> ok = wxFrame:connect(Frame, command_button_clicked), ok = wxFrame:connect(Frame, close_window), ok = wxListBox:connect(Picker, command_listbox_doubleclicked), - Count = length(Manifest), - ok = - if - Count =:= 0 -> - self() ! wiz, - ok; - Count =:= 1 -> - self() ! open, - ok; - Count > 1 -> - true = wxFrame:show(Frame), - wxFrame:raise(Frame) - end, State = #s{wx = Wx, frame = Frame, lang = Lang, j = J, prefs = Prefs, wallets = Manifest, picker = Picker, @@ -137,6 +136,7 @@ init({Prefs, Manifest}) -> + %%% wx_object handle_call(Unexpected, From, State) -> @@ -147,6 +147,9 @@ handle_cast(to_front, State = #s{frame = Frame}) -> ok = ensure_shown(Frame), ok = wxFrame:raise(Frame), {noreply, State}; +handle_cast(first_run, State) -> + NewState = do_first_run(State), + {noreply, NewState}; handle_cast({trouble, Info}, State) -> ok = handle_troubling(State, Info), {noreply, State}; @@ -158,12 +161,6 @@ handle_cast(Unexpected, State) -> {noreply, State}. -handle_info(wiz, State) -> - NewState = do_wiz(State), - {noreply, NewState}; -handle_info(open, State) -> - NewState = do_open(State), - {noreply, NewState}; handle_info(Unexpected, State) -> ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]), {noreply, State}. @@ -232,6 +229,20 @@ do_show(Manifest, State = #s{picker = Picker}) -> State#s{wallets = Manifest}. +do_first_run(State = #s{frame = Frame, wallets = Manifest}) -> + Count = length(Manifest), + if + Count =:= 0 -> + do_wiz(State); + Count =:= 1 -> + do_open(State); + Count > 1 -> + true = wxFrame:show(Frame), + wxFrame:raise(Frame), + State + end. + + close_wiz(State = #s{frame = Frame, wiz = {Wiz, _}}) -> ok = wxWindow:destroy(Wiz), true = wxFrame:show(Frame), @@ -274,8 +285,11 @@ do_open(State = #s{wallets = [#wr{pass = false, path = Path}], frame = Frame}) - State; do_open(State = #s{picker = Picker}) -> case wxListBox:getSelection(Picker) of - -1 -> State; - Selected -> do_open2(Selected + 1, State) + -1 -> + State; + Selected -> + ok = do_open2(Selected + 1, State), + State end. do_open2(Selected, State = #s{wallets = Wallets}) -> @@ -284,8 +298,7 @@ do_open2(Selected, State = #s{wallets = Wallets}) -> do_open3(Path, State); #wr{pass = false, path = Path} -> ok = gd_con:open_wallet(Path, none), - ok = do_close(State), - State + do_close(State) end. do_open3(Path, State = #s{frame = Frame, j = J}) -> @@ -310,24 +323,20 @@ do_open3(Path, State = #s{frame = Frame, j = J}) -> case wxTextCtrl:getValue(PassTx) of "" -> ok = wxDialog:destroy(Dialog), - ok = ensure_shown(Frame), - State; + ensure_shown(Frame); Phrase -> ok = wxDialog:destroy(Dialog), - ok = - case gd_con:open_wallet(Path, Phrase) of - ok -> - do_close(State); - Error -> - ok = ensure_shown(Frame), - trouble(Error) - end, - State + case gd_con:open_wallet(Path, Phrase) of + ok -> + do_close(State); + Error -> + ok = ensure_shown(Frame), + trouble(Error) + end end; ?wxID_CANCEL -> ok = wxDialog:destroy(Dialog), - ok = ensure_shown(Frame), - State + ensure_shown(Frame) end.