-
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
-
151
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
namespace PlacGtkAdwaita {
class Artwork : Gtk.Box {
private uint16 _width = 100;
public uint16 width {
get { return _width; }
construct set {
_width = value;
width_request = (int) value;
}
}
private uint16 _height = 100;
public uint16 height {
get { return _height; }
construct set {
_height = value;
height_request = (int) value;
}
}
private Plac.AsyncConnection? _conn = null;
public Plac.AsyncConnection? conn {
get { return _conn; }
set {
if (_conn != value) {
_conn = value;
render();
}
}
}
private string? _image_key = null;
public string? image_key {
get { return _image_key; }
set {
if (_image_key != value) {
_image_key = value;
render();
}
}
}
public Plac.Image.ScalingMethod scaling { get; set; default = FIT; }
private Gtk.Picture picture = new Gtk.Picture();
private Adw.Spinner spinner = new Adw.Spinner();
private Gtk.Image error_icon = new Gtk.Image.from_icon_name("image-missing-symbolic");
public Artwork() {
Object();
}
public Artwork.new_with_size(uint16 width, uint16 height) {
Object(width: width, height: height);
}
construct {
picture.set_parent(this);
picture.accessible_role = PRESENTATION;
picture.halign = CENTER;
picture.valign = CENTER;
picture.width_request = _width;
picture.height_request = _height;
picture.content_fit = SCALE_DOWN;
picture.add_css_class("plac-playback-artwork");
picture.visible = false;
spinner.set_parent(this);
spinner.halign = CENTER;
spinner.valign = CENTER;
spinner.width_request = _width;
spinner.height_request = _height;
spinner.visible = true;
error_icon.set_parent(this);
error_icon.halign = CENTER;
error_icon.valign = CENTER;
error_icon.width_request = _width;
error_icon.height_request = _height;
error_icon.pixel_size = _width / 2;
error_icon.visible = false;
render();
}
public void render() {
picture.paintable = null;
if (_conn == null || _image_key == null) {
return;
}
picture.visible = false;
spinner.visible = true;
error_icon.visible = false;
var opts = new Plac.Image.GetOptions();
opts.set_size(scaling, _width, _height);
opts.set_content_type(JPEG);
_conn.get_image.begin(_image_key, opts, (obj, res) => {
var result = _conn.get_image.end(res);
if (result == null) {
GLib.log("Plac", LEVEL_WARNING, "Failed to download image: Out of memory");
error_icon.visible = true;
spinner.visible = false;
return;
}
if (result.code != OK) {
GLib.log("Plac", LEVEL_WARNING, "Failed to download image: %s", result.code.to_string());
error_icon.visible = true;
spinner.visible = false;
return;
}
if (result.image == null) {
GLib.log("Plac", LEVEL_WARNING, "Failed to download image: image_field_missing");
error_icon.visible = true;
spinner.visible = false;
return;
}
var bytes = GLib.Bytes.new_with_owner(result.image.data, result.image);
try {
var texture = Gdk.Texture.from_bytes(bytes);
picture.paintable = texture;
spinner.visible = false;
picture.visible = true;
} catch (GLib.Error error) {
GLib.log("Plac", LEVEL_WARNING, "Failed to generate artwork texture: %s", error.message);
}
});
}
}
}