File: gtk_webicon.rb

package info (click to toggle)
mikutter 5.0.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,700 kB
  • sloc: ruby: 21,307; sh: 181; makefile: 19
file content (57 lines) | stat: -rw-r--r-- 1,664 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-

# /(^o^)\

require 'environment'
require 'mui/gtk_web_image_loader'
require 'serialthread'
require 'skin'

require 'gtk3'
require 'observer'

# Web上の画像をレンダリングできる。
# レンダリング中は読み込み中の代替イメージが表示され、ロードが終了したら指定された画像が表示される。
# メモリキャッシュ、ストレージキャッシュがついてる。
module Gtk
  class WebIcon < Image
    DEFAULT_RECTANGLE = Gdk::Rectangle.new(0, 0, 48, 48)

    include Observable

    # ==== Args
    # [url] 画像のURLもしくはパス(String)
    # [rect] 画像のサイズ(Gdk::Rectangle) または幅(px)
    # [height] 画像の高さ(px)
    def initialize(url, rect = DEFAULT_RECTANGLE, height = nil)
      rect = Gdk::Rectangle.new(0, 0, rect, height) if height
      case url
      when Diva::Model
        super(pixbuf: load_model(url, rect, set_loading_image: false))
      when GdkPixbuf::Pixbuf
        super(pixbuf: url)
      else
        photo = Plugin.collect(:photo_filter, url, Pluggaloid::COLLECT).first
        super(pixbuf: load_model(photo || Skin[:notfound], rect, set_loading_image: false))
      end
    end

    def load_model(photo, rect, set_loading_image: true)
      loading = photo.load_pixbuf(width: Gdk.scale(rect.width), height: Gdk.scale(rect.height)) do |pb|
        update_pixbuf(pb)
      end
      if set_loading_image
        self.pixbuf = loading
      end
      loading
    end

    def update_pixbuf(pixbuf)
      unless destroyed?
        self.pixbuf = pixbuf
        changed
        notify_observers
      end
    end
  end
end