Skip to content

Commit 71a816e

Browse files
authored
Merge pull request #7 from ErikReider/pw-toggle-vis
Toggle password visibility
2 parents 3997ed7 + eff3cc4 commit 71a816e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

window.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ static void window_pwcheck(GtkWidget *widget, gpointer data) {
9393
g_thread_new(NULL, window_pwwait, ctx);
9494
}
9595

96+
static void toggle_pw_visibility(GtkEntry* entry, GtkEntryIconPosition icon_pos) {
97+
if (icon_pos != GTK_ENTRY_ICON_SECONDARY) return;
98+
gboolean state = !gtk_entry_get_visibility(entry);
99+
char *icon = state ? "view-conceal-symbolic" : "view-reveal-symbolic";
100+
gtk_entry_set_icon_from_icon_name(entry, GTK_ENTRY_ICON_SECONDARY, icon);
101+
gtk_entry_set_visibility(entry, state);
102+
}
103+
96104
static void window_setup_input(struct Window *ctx) {
97105
if(ctx->input_box != NULL) {
98106
gtk_widget_destroy(ctx->input_box);
@@ -112,6 +120,10 @@ static void window_setup_input(struct Window *ctx) {
112120
ctx->input_field = gtk_entry_new();
113121
gtk_entry_set_input_purpose((GtkEntry*)ctx->input_field, GTK_INPUT_PURPOSE_PASSWORD);
114122
gtk_entry_set_visibility((GtkEntry*)ctx->input_field, FALSE);
123+
gtk_entry_set_icon_from_icon_name((GtkEntry*)ctx->input_field,
124+
GTK_ENTRY_ICON_SECONDARY,
125+
"view-reveal-symbolic");
126+
g_signal_connect(ctx->input_field, "icon-release", G_CALLBACK(toggle_pw_visibility), NULL);
115127
g_signal_connect(ctx->input_field, "activate", G_CALLBACK(window_pwcheck), ctx);
116128
gtk_widget_set_size_request(ctx->input_field, 384, -1);
117129
gtk_widget_set_halign(ctx->input_field, GTK_ALIGN_END);

0 commit comments

Comments
 (0)