Skip to content

Instantly share code, notes, and snippets.

@Miqueas
Last active March 17, 2021 04:41
Show Gist options
  • Select an option

  • Save Miqueas/fa95326ef1d1b8f2273b0aacba0875b0 to your computer and use it in GitHub Desktop.

Select an option

Save Miqueas/fa95326ef1d1b8f2273b0aacba0875b0 to your computer and use it in GitHub Desktop.
[Vala + Gtk 3] Generic password window
// From my other gist: https://gist.github.com/Miqueas/c52a7f6684036030572a66d1f58ba574
Gtk.Grid build_grid() {
var grid = new Gtk.Grid() {
visible = true,
column_spacing = 6,
row_spacing = 6,
halign = Gtk.Align.CENTER,
valign = Gtk.Align.CENTER
};
grid.attach(new Gtk.Label("Usuario") { visible = true, xalign = 1 }, 0, 0);
grid.attach(new Gtk.Entry() { visible = true }, 1, 0);
grid.attach(new Gtk.Label("Contraseña") { visible = true, xalign = 1 }, 0, 1);
grid.attach(new Gtk.Entry() {
visible = true,
input_purpose = Gtk.InputPurpose.PASSWORD,
visibility = false
}, 1, 1);
grid.attach(new Gtk.Label("Repita la contraseña") { visible = true, xalign = 1 }, 0, 2);
grid.attach(new Gtk.Entry() {
visible = true,
input_purpose = Gtk.InputPurpose.PASSWORD,
visibility = false
}, 1, 2);
grid.attach(new Gtk.CheckButton() { visible = true, label = "Mostrar contraseña" }, 1, 3);
grid.attach(new Gtk.Button() { visible = true, label = "Limpiar" }, 1, 4);
return grid;
}
static int main(string[] args) {
Regex patt = /^([a-z]+[a-z0-9]*)$/;
var app = new Gtk.Application("org.example.gtk.password", ApplicationFlags.FLAGS_NONE);
app.activate.connect(() => {
var win = new Gtk.ApplicationWindow(app) { border_width = 10 };
var grid = build_grid();
var user = grid.get_child_at(1, 0) as Gtk.Entry;
var pass = grid.get_child_at(1, 1) as Gtk.Entry;
var confirm = grid.get_child_at(1, 2) as Gtk.Entry;
var check = grid.get_child_at(1, 3) as Gtk.CheckButton;
var clear = grid.get_child_at(1, 4) as Gtk.Button;
user.notify["text"].connect(() => {
if ( patt.match(user.text) )
user.secondary_icon_name = "process-completed-symbolic";
else user.secondary_icon_name = "process-error-symbolic";
});
confirm.notify["text"].connect(() => {
if ( confirm.text == pass.text )
confirm.secondary_icon_name = "process-completed-symbolic";
else confirm.secondary_icon_name = "process-error-symbolic";
});
check.toggled.connect(() => {
if (check.active) {
pass.visibility = true;
confirm.visibility = true;
} else {
pass.visibility = false;
confirm.visibility = false;
}
});
clear.clicked.connect(() => {
user.text = "";
pass.text = "";
confirm.text = "";
});
win.add(grid);
win.present();
});
return app.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment