| 1 |
|
| 2 |
|
| 3 |
module Diva::Model::PhotoInterface |
| 4 |
|
| 5 |
イルシステム上のものなら真 |
| 6 |
def local? |
| 7 |
uri.scheme == 'file'.freeze |
| 8 |
end |
| 9 |
|
| 10 |
を作成するDeferredを返す |
| 11 |
def download_pixbuf(width:, height:) |
| 12 |
increase_read_count |
| 13 |
cache_get_defer(width: width, height: height).trap do |err| |
| 14 |
error err if err |
| 15 |
gen_pixbuf_from_raw_data(width: width, height: height) |
| 16 |
end |
| 17 |
end |
| 18 |
|
| 19 |
シュされているGdkPixbuf::Pixbufを返す。 |
| 20 |
場合は ifnone を返す。 |
| 21 |
在しない場合、ロード中を示すPixbufを返し、 |
| 22 |
を開始する。 |
| 23 |
_&complete_callback_ が呼び出される。 |
| 24 |
|
| 25 |
f の幅(px) |
| 26 |
の高さ(px) |
| 27 |
plete_callback_ に渡す値 |
| 28 |
ダウンロードが行われた場合、ダウンロード完了時に呼ばれる |
| 29 |
|
| 30 |
|
| 31 |
def load_pixbuf(width:, height:, ifnone: Skin[:notfound].pixbuf(width: width, height: height), &complete_callback) |
| 32 |
result = pixbuf(width: width, height: height) rescue ifnone |
| 33 |
if result |
| 34 |
result |
| 35 |
else |
| 36 |
download_pixbuf(width: width, height: height).next(&complete_callback).trap{ |
| 37 |
complete_callback.(ifnone) |
| 38 |
} |
| 39 |
Skin[:loading].pixbuf(width: width, height: height) |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
Pixbufキャッシュから返す。 |
| 44 |
在しない場合は nil を返す。 |
| 45 |
上に見つかった場合は、その場でそれを読み込んで返す。 |
| 46 |
る場合は、このメソッドはnilを返さず、常に GdkPixbuf::Pixbuf を返す。 |
| 47 |
|
| 48 |
(px) |
| 49 |
さ(px) |
| 50 |
|
| 51 |
ファイルシステムから画像が見つかった場合 |
| 52 |
にキャッシュされていない場合 |
| 53 |
def pixbuf(width:, height:) |
| 54 |
result = pixbuf_cache[[width, height].hash] |
| 55 |
if result |
| 56 |
result |
| 57 |
elsif local? |
| 58 |
pixbuf_cache_set(GdkPixbuf::Pixbuf.new(file: uri.path, width: width, height: height), width: width, height: height) |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
private |
| 63 |
|
| 64 |
def gen_pixbuf_from_raw_data(width:, height:) |
| 65 |
download(width: width, height: height).next{|photo| |
| 66 |
pb = pixbuf(width: width, height: height) |
| 67 |
if pb |
| 68 |
pb |
| 69 |
else |
| 70 |
loader = GdkPixbuf::PixbufLoader.new |
| 71 |
loader.write photo.blob |
| 72 |
loader.close |
| 73 |
pb = loader.pixbuf |
| 74 |
pixbuf_cache_set(pb.scale(*calc_fitclop(pb, Gdk::Rectangle.new(0, 0, width, height))), |
| 75 |
width: width, |
| 76 |
height: height) |
| 77 |
end |
| 78 |
} |
| 79 |
end |
| 80 |
|
| 81 |
def cache_get_defer(width:, height:) |
| 82 |
Deferred.new.next do |
| 83 |
result = pixbuf(width: width, height: height) |
| 84 |
if result |
| 85 |
result |
| 86 |
else |
| 87 |
Deferred.fail(result) |
| 88 |
end |
| 89 |
end |
| 90 |
end |
| 91 |
|
| 92 |
def pixbuf_cache_set(pixbuf, width:, height:) |
| 93 |
if pixbuf_cache[[width, height].hash] |
| 94 |
error "cache already exists for #{uri} #{width}*#{height}" |
| 95 |
pixbuf(width: width, height: height) |
| 96 |
else |
| 97 |
pixbuf_cache[[width, height].hash] = pixbuf |
| 98 |
pixbuf |
| 99 |
end |
| 100 |
end |
| 101 |
|
| 102 |
def pixbuf_cache |
| 103 |
@pixbuf_cache ||= TimeLimitedStorage.new(Integer, GdkPixbuf::Pixbuf, 60) |
| 104 |
end |
| 105 |
|
| 106 |
た状態で内接するように縮小した場合のサイズを返す |
| 107 |
|
| 108 |
ctangle) |
| 109 |
(Gtk::Rectangle) |
| 110 |
|
| 111 |
さ(px) |
| 112 |
def calc_fitclop(src, dst) |
| 113 |
if (dst.width * src.height) > (dst.height * src.width) |
| 114 |
return src.width * dst.height / src.height, dst.height |
| 115 |
else |
| 116 |
return dst.width, src.height * dst.width / src.width |
| 117 |
end |
| 118 |
end |
| 119 |
end |