Skip to content

Commit b9c37f5

Browse files
committed
Support usernames and other improvements
1 parent fa924c2 commit b9c37f5

File tree

3 files changed

+116
-110
lines changed

3 files changed

+116
-110
lines changed

source/app.d

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ int main(string[] args) {
3636
}
3737
.acc-name {
3838
font-weight: bold;
39-
font-size: 1.2rem;
39+
font-size: 1.3rem;
40+
}
41+
.user-name {
42+
font-size: 1.0rem;
4043
}
4144
.title-label {
4245
font: 2.2rem raleway;

source/ui/account_view.d

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,36 @@ package final class AccountView : Frame {
2424
bgboxChild = new ListBoxRow();
2525
bgboxChild.setActivatable(false);
2626

27-
vbox = new VBox(false, 0);
27+
auto vbox = new VBox(false, 0);
2828

29-
name_lbl = new Label(account.name);
29+
auto name_lbl = new Label(account.name);
3030
name_lbl.getStyleContext().addClass("acc-name");
3131
name_lbl.setAlignment(0, 0);
3232
name_lbl.setPadding(6, 2);
3333

34+
auto hbox2 = new HBox(false, 0);
35+
hbox2.packStart(name_lbl, false, false, 0);
36+
37+
if (account.username.length > 0) {
38+
auto user_lbl = new Label(account.username);
39+
user_lbl.getStyleContext().addClass("user-name");
40+
user_lbl.setMarginTop(2);
41+
hbox2.packStart(user_lbl, false, false, 0);
42+
}
43+
3444
code_lbl = new Label(account.secret);
3545
code_lbl.getStyleContext().addClass("code-number");
3646
code_lbl.setAlignment(0, 0);
3747
code_lbl.setPadding(6, 2);
3848

39-
hbox = new HBox(false, 0);
49+
auto hbox = new HBox(false, 0);
4050
hbox.packStart(code_lbl, true, true, 0);
4151
auto tmv = new TimerView();
42-
import std.conv : to;
4352

4453
tmv.setTimerCallback({ generateCode(); });
4554
hbox.packStart(tmv, false, false, 10);
4655

47-
vbox.packStart(name_lbl, false, false, 0);
56+
vbox.packStart(hbox2, false, false, 0);
4857
vbox.packStart(hbox, false, false, 0);
4958

5059
bgboxChild.add(vbox);
@@ -75,8 +84,5 @@ private:
7584
Account account;
7685
ListBox bgbox;
7786
ListBoxRow bgboxChild;
78-
VBox vbox;
79-
HBox hbox;
80-
Label name_lbl;
8187
Label code_lbl;
8288
}

source/ui/window.d

Lines changed: 98 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -28,111 +28,108 @@ public final class Window : ApplicationWindow {
2828
icon.setFromGicon(new ThemedIcon("gtk-add"), GtkIconSize.LARGE_TOOLBAR);
2929
addBtn.setImage(icon);
3030

31-
addCallback = (Button) {
32-
auto d = new Dialog("Add a new account", this, GtkDialogFlags.MODAL,
33-
["Cancel", "Add"], [
34-
ResponseType.CANCEL, ResponseType.ACCEPT
35-
]);
36-
37-
d.getWidgetForResponse(ResponseType.ACCEPT).setSensitive(false);
38-
scope (exit)
39-
d.destroy();
40-
41-
d.setDefaultResponse(ResponseType.ACCEPT);
42-
d.setDefaultSize(400, -1);
43-
44-
auto box = new VBox(false, 0);
45-
auto hbox = new HBox(false, 0);
46-
47-
auto name_ent = new Entry();
48-
auto secret_ent = new Entry();
49-
auto username_ent = new Entry();
50-
51-
const void delegate(EditableIF) cb = (EditableIF) {
52-
d.getWidgetForResponse(ResponseType.ACCEPT).setSensitive(name_ent.getText()
53-
.length > 0 && secret_ent.getText().length > 0);
54-
};
55-
name_ent.addOnChanged(cb);
56-
secret_ent.addOnChanged(cb);
57-
58-
auto lbl1 = new Label("The name of this account:");
59-
auto lbl2 = new Label("The secret code that you got:");
60-
auto lbl3 = new Label("Your username (optional):");
61-
lbl1.setAlignment(0, 0);
62-
lbl2.setAlignment(0, 0);
63-
lbl3.setAlignment(0, 0);
64-
lbl2.setMarginTop(10);
65-
lbl3.setMarginTop(10);
66-
67-
box.packStart(lbl1, false, false, 5);
68-
box.packStart(name_ent, false, false, 0);
69-
box.packStart(lbl2, false, false, 5);
70-
box.packStart(secret_ent, false, false, 0);
71-
box.packStart(lbl3, false, false, 5);
72-
box.packStart(username_ent, false, false, 0);
73-
74-
box.setMarginStart(15);
75-
box.setMarginEnd(15);
76-
username_ent.setMarginBottom(15);
77-
d.getContentArea().add(box);
78-
d.showAll();
79-
const res = d.run();
80-
81-
if (res == ResponseType.ACCEPT) {
82-
Account acc = {
83-
name: name_ent.getText(), secret: secret_ent.getText()};
84-
storage.addAccount(acc);
85-
reloadAccountList();
86-
}
87-
};
88-
addBtn.addOnClicked(addCallback);
89-
header.add(addBtn);
90-
setTitlebar(header);
91-
92-
contents = new VBox(false, 0);
93-
reloadAccountList();
31+
addBtn.addOnClicked(&onAddClicked);
32+
header.add(addBtn);
33+
setTitlebar(header);
9434

95-
add(contents);
96-
}
35+
contents = new VBox(false, 0);
36+
reloadAccountList();
9737

98-
private:
99-
void reloadAccountList() {
100-
import std.conv : to;
38+
add(contents);
39+
}
10140

102-
header.setTitle(storage.countAccounts().to!string ~ " Accounts");
41+
private:
42+
43+
void onAddClicked(Button b) {
44+
auto d = new Dialog("Add a new account", this, GtkDialogFlags.MODAL,
45+
["Cancel", "Add"], [ResponseType.CANCEL, ResponseType.ACCEPT]);
46+
47+
d.getWidgetForResponse(ResponseType.ACCEPT).setSensitive(false);
48+
scope (exit)
49+
d.destroy();
50+
51+
d.setDefaultResponse(ResponseType.ACCEPT);
52+
d.setDefaultSize(400, -1);
53+
54+
auto box = new VBox(false, 0);
55+
auto hbox = new HBox(false, 0);
56+
57+
auto name_ent = new Entry();
58+
auto secret_ent = new Entry();
59+
auto username_ent = new Entry("@");
60+
61+
const void delegate(EditableIF) cb = (EditableIF) {
62+
d.getWidgetForResponse(ResponseType.ACCEPT).setSensitive(name_ent.getText()
63+
.length > 0 && secret_ent.getText().length > 0);
64+
};
65+
name_ent.addOnChanged(cb);
66+
secret_ent.addOnChanged(cb);
67+
68+
auto lbl1 = new Label("The name of this account:");
69+
auto lbl2 = new Label("The secret code that you got:");
70+
auto lbl3 = new Label("Your username (optional):");
71+
lbl1.setAlignment(0, 0);
72+
lbl2.setAlignment(0, 0);
73+
lbl3.setAlignment(0, 0);
74+
lbl2.setMarginTop(10);
75+
lbl3.setMarginTop(10);
76+
77+
box.packStart(lbl1, false, false, 5);
78+
box.packStart(name_ent, false, false, 0);
79+
box.packStart(lbl2, false, false, 5);
80+
box.packStart(secret_ent, false, false, 0);
81+
box.packStart(lbl3, false, false, 5);
82+
box.packStart(username_ent, false, false, 0);
83+
84+
box.setMarginStart(15);
85+
box.setMarginEnd(15);
86+
username_ent.setMarginBottom(15);
87+
d.getContentArea().add(box);
88+
d.showAll();
89+
const res = d.run();
90+
91+
if (res == ResponseType.ACCEPT) {
92+
storage.addAccount(Account(name_ent.getText(),
93+
secret_ent.getText(), username_ent.getText()));
94+
reloadAccountList();
95+
}
96+
}
10397

104-
if (auto t = contents.getChildren()) {
105-
foreach (ref w; t.toArray!Widget()) {
106-
contents.remove(w);
107-
w.destroy();
108-
}
109-
}
110-
if (storage.countAccounts() == 0) {
111-
auto welcome = new Welcome("Authomata",
112-
"You currently don't have any accounts. Use the button below to add the first one");
113-
auto ic = new Image();
114-
ic.setFromIconName("gtk-add", GtkIconSize.LARGE_TOOLBAR);
115-
welcome.addButton("Add an account",
116-
"Add a new 2-factor authentication account", ic, addCallback);
117-
contents.packStart(welcome, true, true, 0);
118-
} else {
119-
auto s = new ScrolledWindow();
120-
auto vb = new VBox(false, 0);
121-
122-
auto title = new Label("Authomata");
123-
title.getStyleContext().addClass("app-title");
124-
vb.packStart(title, false, false, 20);
125-
126-
foreach (acc; storage.getAccounts())
127-
vb.packStart(new AccountView(acc), false, false, 5);
128-
s.add(vb);
129-
contents.packStart(s, true, true, 0);
98+
void reloadAccountList() {
99+
import std.conv : to;
100+
101+
header.setTitle(storage.countAccounts().to!string ~ " Accounts");
102+
if (auto t = contents.getChildren()) {
103+
foreach (ref w; t.toArray!Widget()) {
104+
contents.remove(w);
105+
w.destroy();
130106
}
131-
contents.showAll();
132107
}
133-
134-
Storage storage;
135-
VBox contents;
136-
void delegate(Button) addCallback;
137-
HeaderBar header;
108+
if (storage.countAccounts() == 0) {
109+
auto welcome = new Welcome("Authomata",
110+
"You currently don't have any accounts. Use the button below to add the first one");
111+
auto ic = new Image();
112+
ic.setFromIconName("gtk-add", GtkIconSize.LARGE_TOOLBAR);
113+
welcome.addButton("Add an account",
114+
"Add a new 2-factor authentication account", ic, &onAddClicked);
115+
contents.packStart(welcome, true, true, 0);
116+
} else {
117+
auto s = new ScrolledWindow();
118+
auto vb = new VBox(false, 0);
119+
120+
auto title = new Label("Authomata");
121+
title.getStyleContext().addClass("app-title");
122+
vb.packStart(title, false, false, 20);
123+
124+
foreach (acc; storage.getAccounts())
125+
vb.packStart(new AccountView(acc), false, false, 5);
126+
s.add(vb);
127+
contents.packStart(s, true, true, 0);
128+
}
129+
contents.showAll();
138130
}
131+
132+
Storage storage;
133+
VBox contents;
134+
HeaderBar header;
135+
}

0 commit comments

Comments
 (0)