Skip to content

Commit ceb5662

Browse files
library: Add Level Bars entry (#238)
1 parent 8e2c8df commit ceb5662

4 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Gtk 4.0;
2+
using Adw 1;
3+
4+
Adw.StatusPage {
5+
title: _("Level Bars");
6+
description: _("A bar widget used as a level indicator");
7+
8+
Box {
9+
orientation: vertical;
10+
margin-top: 24;
11+
halign: center;
12+
13+
Box {
14+
orientation: vertical;
15+
spacing: 18;
16+
margin-start: 18;
17+
margin-end: 18;
18+
19+
Label {
20+
label: _("Continuous");
21+
}
22+
23+
Box {
24+
orientation: vertical;
25+
spacing: 12;
26+
27+
Label {
28+
halign: start;
29+
margin-start: 12;
30+
margin-top: 12;
31+
label: _("Battery");
32+
}
33+
34+
LevelBar bar_continuous{
35+
mode: continuous;
36+
margin-start:12;
37+
margin-end:12;
38+
margin-bottom: 24;
39+
min-value:0;
40+
max-value:100;
41+
value: 50;
42+
}
43+
styles ["card"]
44+
margin-bottom: 24;
45+
}
46+
47+
48+
Label {
49+
label: _("Discrete");
50+
}
51+
52+
PasswordEntry entry {
53+
placeholder-text: "Choose a Password";
54+
show-peek-icon: true;
55+
}
56+
LevelBar bar_discrete {
57+
mode: discrete;
58+
min-value: 0;
59+
max-value: 6;
60+
}
61+
Label label_strength {
62+
63+
}
64+
}
65+
66+
Box {
67+
orientation: vertical;
68+
margin-bottom: 24;
69+
70+
LinkButton {
71+
label: "Tutorial";
72+
uri: "https://developer.gnome.org/documentation/tutorials/beginners/components/level_bar.html";
73+
}
74+
75+
LinkButton {
76+
label: "API Reference";
77+
uri: "https://docs.gtk.org/gtk4/class.LevelBar.html";
78+
}
79+
}
80+
}
81+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
levelbar block.full {
2+
background-color: @success_color;
3+
}
4+
5+
levelbar block.half {
6+
background-color: @warning_color;
7+
}
8+
9+
levelbar block.low {
10+
background-color: @error_color;
11+
}
12+
13+
levelbar block.very-weak {
14+
background-color: @red_4;
15+
}
16+
17+
levelbar block.weak {
18+
background-color: @orange_4;
19+
}
20+
21+
levelbar block.moderate {
22+
background-color: @yellow_4;
23+
}
24+
25+
levelbar block.strong {
26+
background-color: @green_4;
27+
}
28+
29+
.very-weak-label {
30+
color: @red_4;
31+
}
32+
33+
.weak-label {
34+
color: @orange_4;
35+
}
36+
37+
.moderate-label {
38+
color: @yellow_4;
39+
}
40+
41+
.strong-label {
42+
color: @green_4;
43+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const bar_continuous = workbench.builder.get_object("bar_continuous");
2+
3+
bar_continuous.add_offset_value("full", 100);
4+
bar_continuous.add_offset_value("half", 50);
5+
bar_continuous.add_offset_value("low", 25);
6+
7+
const bar_discrete = workbench.builder.get_object("bar_discrete");
8+
9+
bar_discrete.add_offset_value("very-weak", 1);
10+
bar_discrete.add_offset_value("weak", 2);
11+
bar_discrete.add_offset_value("moderate", 4);
12+
bar_discrete.add_offset_value("strong", 6);
13+
14+
const entry = workbench.builder.get_object("entry");
15+
const label_strength = workbench.builder.get_object("label_strength");
16+
17+
entry.connect("notify::text", () => {
18+
estimatePasswordStrength();
19+
});
20+
21+
// This is not a secure way to estimate password strength
22+
// Use appropriate solutions instead
23+
// such as https://github.com/dropbox/zxcvbn
24+
function estimatePasswordStrength() {
25+
const level = Math.min(Math.ceil(entry.text.length / 2), 6);
26+
27+
switch (level) {
28+
case 1:
29+
label_strength.label = "Very Weak";
30+
label_strength.css_classes = ["very-weak-label"];
31+
break;
32+
case 2:
33+
label_strength.label = "Weak";
34+
label_strength.css_classes = ["weak-label"];
35+
break;
36+
case 3:
37+
case 4:
38+
label_strength.label = "Moderate";
39+
label_strength.css_classes = ["moderate-label"];
40+
break;
41+
case 5:
42+
case 6:
43+
label_strength.label = "Strong";
44+
label_strength.css_classes = ["strong-label"];
45+
break;
46+
default:
47+
label_strength.label = "";
48+
label_strength.css_classes = [];
49+
}
50+
51+
bar_discrete.value = level;
52+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Level Bars",
3+
"category": "user_interface",
4+
"description": "A bar widget used as a level indicator",
5+
"panels": [
6+
"ui",
7+
"preview"
8+
],
9+
"autorun": true
10+
}

0 commit comments

Comments
 (0)