forked from gufoe/text-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chars_counter.js
150 lines (120 loc) · 3.79 KB
/
chars_counter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const St = imports.gi.St;
const Tweener = imports.tweener.tweener;
var CharsCounter = class CharsCounter {
constructor() {
this.actor = new St.BoxLayout({
style_class: "translator-chars-counter-box",
visible: false
});
this._current_length = 0;
this._max_length = 0;
this._current_length_label = new St.Label({
style_class: "translator-chars-counter-text"
});
this._current_length_label.get_clutter_text().set_use_markup(true);
this._max_length_label = new St.Label({
style_class: "translator-chars-counter-text"
});
this._max_length_label.get_clutter_text().set_use_markup(true);
this._separator_label = new St.Label({
style_class: "translator-chars-counter-text",
text: "/"
});
this.actor.add_actor(this._current_length_label);
this.actor.add_actor(this._separator_label);
this.actor.add_actor(this._max_length_label);
}
_show() {
if (this.actor.visible) return;
this.actor.opacity = 0;
this.actor.show();
Tweener.addTween(this.actor, {
time: 0.3,
transition: "easeOutQuad",
opacity: 255
});
}
_hide() {
if (!this.actor.visible) return;
Tweener.addTween(this.actor, {
time: 0.3,
transition: "easeOutQuad",
opacity: 0,
onComplete: () => {
this.actor.hide();
this.actor.opacity = 255;
}
});
}
_maybe_show() {
if (this._max_length < 1 || this._current_length < 1) {
this._hide();
return;
}
if (this.actor.visible) return;
this._show();
}
_current_length_changed() {
this._maybe_show();
let markup;
if (this._current_length >= this._max_length) {
markup = '<span color="red"><b>%s</b></span>'.format(
this._current_length.toString()
);
} else {
markup = this._current_length.toString();
}
let clutter_text = this._current_length_label.get_clutter_text();
Tweener.addTween(this._current_length_label, {
time: 0.2,
transition: "easeOutQuad",
opacity: 100,
onComplete: () => {
clutter_text.set_markup(markup);
Tweener.addTween(this._current_length_label, {
time: 0.2,
transition: "easeOutQuad",
opacity: 255
});
}
});
clutter_text.set_markup(markup);
}
_max_length_changed() {
this._maybe_show();
let markup = "<b>%s</b>".format(this._max_length.toString());
let clutter_text = this._max_length_label.get_clutter_text();
Tweener.addTween(this._max_length_label, {
time: 0.2,
transition: "easeOutQuad",
opacity: 100,
onComplete: () => {
clutter_text.set_markup(markup);
Tweener.addTween(this._max_length_label, {
time: 0.2,
transition: "easeOutQuad",
opacity: 255
});
}
});
clutter_text.set_markup(markup);
this._current_length_changed();
}
destroy() {
this.actor.destroy();
}
get current_length() {
return this._current_length;
}
set current_length(length) {
this._current_length = length;
this._current_length_changed();
}
get max_length() {
return this._max_length;
}
set max_length(length) {
this._max_length = length;
this._max_length_changed();
}
};