add label truncate property

This commit is contained in:
Aylur
2023-08-19 23:02:12 +02:00
parent 79535cee07
commit e078336620
+22 -1
View File
@@ -1,7 +1,9 @@
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk?version=3.0';
import Pango from 'gi://Pango';
const justification = ['left', 'center', 'right', 'fill'];
const truncate = ['none', 'start', 'middle', 'end'];
export default class AgsLabel extends Gtk.Label {
static {
@@ -13,6 +15,11 @@ export default class AgsLabel extends Gtk.Label {
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
'',
),
'truncate': GObject.ParamSpec.string(
'truncate', 'Truncate', 'Truncate',
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
'',
),
},
}, this);
}
@@ -21,8 +28,22 @@ export default class AgsLabel extends Gtk.Label {
super(typeof params === 'string' ? { label: params } : params);
}
get truncate() { return truncate[this.ellipsize]; }
set truncate(truncate: string) {
if (!truncate)
return;
if (!truncate.includes(truncate)) {
console.error('wrong truncate value for Label');
return;
}
// @ts-ignore
this.ellipsize = Pango.EllipsizeMode[truncate.toUpperCase()];
}
get justification() { return justification[this.justify]; }
set justification(justify) {
set justification(justify: string) {
if (!justify)
return;