File: window-test2.rb

package info (click to toggle)
ruby-gnome 4.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,648 kB
  • sloc: ruby: 67,701; ansic: 67,431; xml: 350; sh: 201; cpp: 45; makefile: 42
file content (132 lines) | stat: -rwxr-xr-x 4,417 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env ruby
#
# This sample code is a port of clutter-gtk/examples/gtk-clutter-window-test2.c.
# It is licensed under the terms of the GNU Lesser General Public
# License, version 2.1 or (at your option) later.
#
# The original header:
#   (c) 2009, Collabora Ltd.
#
#   Written by Davyd Madeley <davyd.madeley@collabora.co.uk>
#
# Copyright (C) 2013  Ruby-GNOME2 Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

require "clutter-gtk"
require "gdk_pixbuf2"

window = ClutterGtk::Window.new

table = Gtk::Grid.new

table.hexpand = true
table.vexpand = true

add_button = lambda do |stock_id, row|
  button = Gtk::Button.new(:stock_id => stock_id)
  table.insert_row(row)
  table.insert_column(row)
  table.attach(button, row, row, 1, 1)

  button.signal_connect("clicked") do |_button|
    puts("button clicked: #{stock_id}")

    toplevel = _button.toplevel
    if toplevel.is_a?(ClutterGtk::Window)
      # create a texture from the button image
      texture = ClutterGtk::Texture.new
      image = _button.image
      size = image.icon_size
      texture.set_from_stock(_button, stock_id.to_s, size)

      # position the texture on top of the existing icon
      stage = toplevel.stage
      stage.add_actor(texture)

      allocation = image.allocation

      # replace the icon itself
      blank = GdkPixbuf::Pixbuf.new(:colorspace => :rgb,
                              :has_alpha =>  true,
                              :bits_per_sample => 8,
                              :width => allocation.width,
                              :height => allocation.height)
      blank.fill!(0x00000000)
      image.pixbuf = blank

      # animate a fall due to gravity
      toplevel_allocation = toplevel.allocation
      texture.save_easing_state do
        texture.easing_mode = :ease_in_quad
        texture.easing_duration = 200
        texture.set_final_state("x", allocation.x.to_f)
        texture.set_final_state("y", allocation.y.to_f)
        texture.y = toplevel_allocation.height.to_f
      end

      first_completed_id = texture.signal_connect_after("transitions-completed") do
        image_allocation = image.allocation

        # do the second animation, have the icon grow out from the middle of the
        # button
        texture.save_easing_state do
          texture.easing_mode = :ease_out_sine
          texture.easing_duration = 100
          texture.set_final_state("x", image_allocation.x.to_f)
          texture.set_final_state("y", image_allocation.y.to_f)
          texture.set_final_state("scale-x", 0.0)
          texture.set_final_state("scale-y", 0.0)
          texture.scale_x = 1.0
          texture.scale_y = 1.0
          texture.set_final_state("scale-gravity", Clutter::Gravity::CENTER)
        end

        texture.signal_handler_disconnect(first_completed_id)

        second_completed_id = texture.signal_connect_after("transitions-completed") do
          # undo our changes
          puts("set stock = #{stock_id}, size = #{size}")
          image.icon_size = size
          image.stock = stock_id
          texture.signal_handler_disconnect(second_completed_id)
          texture.destroy
        end
      end
    end
  end
end

add_button.call(Gtk::Stock::OK,     0)
add_button.call(Gtk::Stock::CANCEL, 1)
add_button.call(Gtk::Stock::CLOSE,  2)
add_button.call(Gtk::Stock::ABOUT,  3)
add_button.call(Gtk::Stock::BOLD,   4)
add_button.call(Gtk::Stock::ITALIC, 5)

window.add(table)
window.show_all

# override the gtk-button-images setting, since we kind
# of rely on this to be true to actually show the stock
# icon falling off the button
settings = window.settings
settings.gtk_button_images = true

window.signal_connect("destroy") do
  Gtk.main_quit
end

Gtk.main