File: action-namespace.rb

package info (click to toggle)
ruby-gnome 4.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,320 kB
  • sloc: ruby: 55,206; ansic: 29,012; xml: 333; sh: 229; cpp: 45; makefile: 42
file content (130 lines) | stat: -rwxr-xr-x 3,584 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
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
#!/usr/bin/env ruby
#
# Copyright (C) 2018  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

# example from https://github.com/GNOME/gtk/blob/master/examples/action-namespace.c

require_relative "utils"

require_gtk4

MENU_UI =
"<interface>
  <menu id='doc-menu'>
    <section>
      <item>
        <attribute name='label'>_Save</attribute>
        <attribute name='action'>save</attribute>
      </item>
      <item>
        <attribute name='label'>_Print</attribute>
        <attribute name='action'>print</attribute>
      </item>
      <item>
        <attribute name='label'>_Share</attribute>
        <attribute name='action'>share</attribute>
      </item>
    </section>
  </menu>
  <menu id='win-menu'>
    <section>
      <item>
        <attribute name='label'>_Fullscreen</attribute>
        <attribute name='action'>fullscreen</attribute>
      </item>
      <item>
        <attribute name='label'>_Close</attribute>
        <attribute name='action'>close</attribute>
      </item>
    </section>
  </menu>
</interface>"

def action(name, window)
  action = Gio::SimpleAction.new(name)
  action.signal_connect "activate" do |_simple_action, _parameter|
    dialog = Gtk::MessageDialog.new(:parent => window,
                                    :flags => :destroy_with_parent,
                                    :buttons => :close,
                                    :message => "Action #{name} activated.")
    dialog.signal_connect(:response) do
      dialog.destroy
    end
    dialog.show
  end
  action
end

def action_save(window)
  action("save", window)
end

def action_print(window)
  action("print", window)
end

def action_share(window)
  action("share", window)
end

def action_fullscreen(window)
  action("fullscreen", window)
end

def action_close(window)
  action("close", window)
end

application = Gtk::Application.new("org.gtk.example", :flags_none)

application.signal_connect "activate" do |app|
  win = Gtk::ApplicationWindow.new(app)
  win.set_default_size(200, 300)

  doc_actions = Gio::SimpleActionGroup.new
  doc_actions.add_action(action_save(win))
  doc_actions.add_action(action_share(win))
  doc_actions.add_action(action_print(win))

  win.add_action(action_close(win))
  win.add_action(action_fullscreen(win))

  builder = Gtk::Builder.new(:string => MENU_UI)
  docmenu = builder["doc-menu"]
  winmenu = builder["win-menu"]

  buttonmenu = Gio::Menu.new

  section = Gio::MenuItem.new(nil, docmenu)
  section.action_namespace = "doc"
  buttonmenu.append_item(section)

  section = Gio::MenuItem.new(nil, winmenu)
  section.action_namespace = "win"
  buttonmenu.append_item(section)

  button = Gtk::MenuButton.new
  button.label = "Menu"
  button.insert_action_group("doc", doc_actions)
  button.menu_model = buttonmenu
  button.halign = :center
  button.valign = :start
  win.set_child(button)
  win.show
end

application.run(ARGV)