Skip to content

Commit

Permalink
Add examples of stateful actions
Browse files Browse the repository at this point in the history
#77

Fixes #68
and #44
  • Loading branch information
TingPing authored and sebp committed Mar 3, 2016
1 parent 402f670 commit b13d270
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions examples/application_example.py
Expand Up @@ -9,6 +9,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id="app-menu">
<section>
<attribute name="label" translatable="yes">Change label</attribute>
<item>
<attribute name="action">win.change_label</attribute>
<attribute name="target">String 1</attribute>
<attribute name="label" translatable="yes">String 1</attribute>
</item>
<item>
<attribute name="action">win.change_label</attribute>
<attribute name="target">String 2</attribute>
<attribute name="label" translatable="yes">String 2</attribute>
</item>
<item>
<attribute name="action">win.change_label</attribute>
<attribute name="target">String 3</attribute>
<attribute name="label" translatable="yes">String 3</attribute>
</item>
</section>
<section>
<item>
<attribute name="action">win.maximize</attribute>
<attribute name="label" translatable="yes">Maximize</attribute>
</item>
</section>
<section>
<item>
<attribute name="action">app.about</attribute>
Expand All @@ -24,6 +48,44 @@
</interface>
"""

class AppWindow(Gtk.ApplicationWindow):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# This will be in the windows group and have the "win" prefix
max_action = Gio.SimpleAction.new_stateful("maximize", None,
GLib.Variant.new_boolean(False))
max_action.connect("change-state", self.on_maximize_toggle)
self.add_action(max_action)

# Keep it in sync with the actual state
self.connect("notify::is-maximized",
lambda obj, pspec: max_action.set_state(
GLib.Variant.new_boolean(obj.props.is_maximized)))

lbl_variant = GLib.Variant.new_string("String 1")
lbl_action = Gio.SimpleAction.new_stateful("change_label", lbl_variant.get_type(),
lbl_variant)
lbl_action.connect("change-state", self.on_change_label_state)
self.add_action(lbl_action)

self.label = Gtk.Label(label=lbl_variant.get_string(),
margin=30)
self.add(self.label)
self.label.show()

def on_change_label_state(self, action, value):
action.set_state(value)
self.label.set_text(value.get_string())

def on_maximize_toggle(self, action, value):
action.set_state(value)
if value.get_boolean():
self.maximize()
else:
self.unmaximize()

class Application(Gtk.Application):

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -54,8 +116,8 @@ def do_activate(self):
if not self.window:
# Windows are associated with the application
# when the last one is closed the application shuts down
self.window = Gtk.ApplicationWindow(application=self,
title="Main Window")
self.window = AppWindow(application=self, title="Main Window")

self.window.present()

def do_command_line(self, command_line):
Expand Down

0 comments on commit b13d270

Please sign in to comment.