|
1 | 1 | import GLib from "gi://GLib"; |
2 | 2 | import Gdk from "gi://Gdk"; |
3 | | -import GdkPixbuf from "gi://GdkPixbuf"; |
4 | 3 | import Gio from "gi://Gio"; |
5 | 4 | import Soup from "gi://Soup"; |
6 | 5 |
|
7 | 6 | // https://picsum.photos/ |
8 | 7 | const IMAGE_URL = "https://picsum.photos/800"; |
9 | 8 |
|
10 | | -Gio._promisify(Soup.Session.prototype, "send_async", "send_finish"); |
11 | 9 | Gio._promisify( |
12 | | - GdkPixbuf.Pixbuf, |
13 | | - "new_from_stream_async", |
14 | | - "new_from_stream_finish", |
| 10 | + Soup.Session.prototype, |
| 11 | + "send_and_read_async", |
| 12 | + "send_and_read_finish", |
15 | 13 | ); |
16 | 14 |
|
17 | | -const input_stream = await getInputStream(IMAGE_URL); |
18 | | -const pixbuf = await GdkPixbuf.Pixbuf.new_from_stream_async(input_stream, null); |
19 | | -const texture = Gdk.Texture.new_for_pixbuf(pixbuf); |
| 15 | +const image_bytes = await getImageBytes(IMAGE_URL); |
| 16 | +const texture = Gdk.Texture.new_from_bytes(image_bytes); |
20 | 17 | workbench.builder.get_object("picture").set_paintable(texture); |
21 | 18 |
|
22 | | -async function getInputStream(url) { |
| 19 | +async function getImageBytes(url) { |
23 | 20 | const session = new Soup.Session(); |
24 | 21 | const message = new Soup.Message({ |
25 | 22 | method: "GET", |
26 | 23 | uri: GLib.Uri.parse(url, GLib.UriFlags.NONE), |
27 | 24 | }); |
28 | | - const input_stream = await session.send_async(message, null, null); |
| 25 | + const bytes = await session.send_and_read_async(message, null, null); |
29 | 26 | const { status_code, reason_phrase } = message; |
30 | 27 | if (status_code !== 200) { |
31 | 28 | throw new Error(`Got ${status_code}, ${reason_phrase}`); |
32 | 29 | } |
33 | | - return input_stream; |
| 30 | + return bytes; |
34 | 31 | } |
0 commit comments