File: flowbox.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 (85 lines) | stat: -rwxr-xr-x 2,524 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env ruby
#
# Copyright (c)  2014  Gian Mario Tagliaretti
# Copyright (c)  2015-2020  Ruby-GNOME Project Team
#
# Permission is granted to copy, distribute and/or modify this document
# under the terms of the GNU Free Documentation License, Version 1.3
# or any later version published by the Free Software Foundation;
# with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
# A copy of the license is included in the section entitled "GNU
# Free Documentation License".
#
# Original code: https://github.com/sebp/PyGObject-Tutorial/blob/master/examples/layout_flowbox_example.py

require "gtk3"

unless Gtk::Version.or_later?(3, 12, 1)
  puts "This sample requires GTK+ 3.12.1 or later: #{Gtk::Version::STRING}"
  exit
end

COLORS = %w(AliceBlue AntiqueWhite AntiqueWhite1 AntiqueWhite2 AntiqueWhite3
            AntiqueWhite4 aqua aquamarine aquamarine1 aquamarine2 aquamarine3
            aquamarine4 azure azure1 azure2 azure3 azure4 beige bisque bisque1
            bisque2 bisque3 bisque4 black BlanchedAlmond blue blue1 blue2
            blue3 blue4 BlueViolet brown brown1 brown2 brown3 brown4 burlywood
            burlywood1 burlywood2 burlywood3 burlywood4 CadetBlue CadetBlue1
            CadetBlue2 CadetBlue3 CadetBlue4 chartreuse chartreuse1 chartreuse2
            chartreuse3 chartreuse4 chocolate chocolate1 chocolate2 chocolate3
            chocolate4 coral coral1 coral2 coral3 coral4)

class FlowBoxWindow < Gtk::Window
  def initialize
    super

    set_border_width(10)
    set_default_size(300, 250)

    header = Gtk::HeaderBar.new
    header.title = "Flow Box"
    header.subtitle = "Sample FlowBox app"
    header.show_close_button = true

    set_titlebar(header)

    scrolled = Gtk::ScrolledWindow.new
    scrolled.set_policy(:never, :automatic)

    flowbox = Gtk::FlowBox.new
    flowbox.valign = :start
    flowbox.max_children_per_line = 30
    flowbox.selection_mode = :none

    COLORS.each do |color|
      swatch = create_color_swatch(color)
      flowbox.add swatch
    end

    scrolled.add(flowbox)
    add(scrolled)

    signal_connect("destroy") { Gtk.main_quit }
  end

  private

  def create_color_swatch(color_name)
    color = Gdk::RGBA.parse(color_name)
    button = Gtk::Button.new

    area = Gtk::DrawingArea.new
    area.set_size_request(24, 24)
    area.signal_connect("draw") do |_, context|
      context.set_source_rgba(color)
      context.paint
    end

    button.add(area)
  end
end

win = FlowBoxWindow.new
win.show_all

Gtk.main