Skip to content

Commit 5260a3f

Browse files
library: Add Color Dialog entry (#232)
1 parent e8a2c23 commit 5260a3f

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Gtk 4.0;
2+
using Adw 1;
3+
4+
Adw.StatusPage {
5+
title: "Color Dialog";
6+
description: _("Show a dialog to select a color");
7+
8+
Box {
9+
orientation: vertical;
10+
halign: center;
11+
spacing:42;
12+
13+
Box{
14+
orientation:vertical;
15+
16+
Label {
17+
label: _("Dialog Button");
18+
margin-bottom:12;
19+
styles ["title-4"]
20+
}
21+
22+
ColorDialogButton color_dialog_button {
23+
halign: center;
24+
margin-bottom:12;
25+
}
26+
27+
28+
LinkButton {
29+
label: "API Reference";
30+
uri: "https://docs.gtk.org/gtk4/class.ColorDialogButton.html";
31+
}
32+
}
33+
34+
Box{
35+
orientation: vertical;
36+
37+
Label {
38+
label: _("Dialog with Custom Button");
39+
margin-bottom:12;
40+
styles ["title-4"]
41+
}
42+
43+
Button custom_button {
44+
label: _("Select Color…");
45+
margin-start:42;
46+
margin-end:42;
47+
margin-bottom:12;
48+
styles ["pill"]
49+
}
50+
51+
LinkButton {
52+
label: "API Reference";
53+
uri: "https://docs.gtk.org/gtk4/class.ColorDialog.html";
54+
}
55+
}
56+
}
57+
}
58+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Gtk from "gi://Gtk";
2+
import Gdk from "gi://Gdk";
3+
import Gio from "gi://Gio";
4+
5+
Gio._promisify(Gtk.ColorDialog.prototype, "choose_rgba", "choose_rgba_finish");
6+
7+
const color_dialog_button = workbench.builder.get_object("color_dialog_button");
8+
const custom_button = workbench.builder.get_object("custom_button");
9+
10+
const color = new Gdk.RGBA();
11+
color.parse("red");
12+
13+
const dialog_standard = new Gtk.ColorDialog({
14+
title: "Select a color",
15+
modal: true,
16+
with_alpha: true,
17+
});
18+
19+
color_dialog_button.dialog = dialog_standard;
20+
color_dialog_button.rgba = color;
21+
22+
color_dialog_button.connect("notify::rgba", () => {
23+
console.log(
24+
`Color Dialog Button: The color selected is ${color_dialog_button.rgba.to_string()}`,
25+
);
26+
});
27+
28+
const dialog_custom = new Gtk.ColorDialog({
29+
title: "Select a color",
30+
modal: true,
31+
with_alpha: false,
32+
});
33+
34+
custom_button.connect("clicked", async () => {
35+
try {
36+
const color = await dialog_custom.choose_rgba(workbench.window, null, null);
37+
console.log(`Custom Button: The color selected is ${color.to_string()}`);
38+
} catch (err) {
39+
logError(err);
40+
}
41+
});
42+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Color Dialog",
3+
"category": "user_interface",
4+
"description": "Show a dialog to select color",
5+
"panels": [
6+
"ui",
7+
"preview"
8+
],
9+
"autorun": true
10+
}

0 commit comments

Comments
 (0)