Skip to content

Instantly share code, notes, and snippets.

@thiagojedi
Created October 1, 2019 03:13
Show Gist options
  • Save thiagojedi/39858db3345904249505a41ccf408bd8 to your computer and use it in GitHub Desktop.
Save thiagojedi/39858db3345904249505a41ccf408bd8 to your computer and use it in GitHub Desktop.
Crystal Gtk Application
require "gobject/gtk"
require "gobject/gio"
class MyApplication < Gtk::Application
include Gio::ActionMap
def self.new
super "org.example.application", :flags_none
end
def initialize(ptr)
super(ptr)
on_activate do
setup_actions
build_ui
end
end
def setup_actions
action_entries = [
LibGio::ActionEntry.new(name: "quit", activate: ->{ puts "quit" }), # works
# LibGio::ActionEntry.new(name: "quit", activate: ->{ self.quit }), # doesn't work
]
add_action_entries action_entries, action_entries.size, nil
end
def build_ui
title = "Hello World"
show_menu = !prefers_app_menu()
headerbar = Gtk::HeaderBar.new
headerbar.title = title
headerbar.subtitle = "This is a subtitle"
headerbar.show_close_button = true
menu_template = "
<interface>
<menu id='app_menu'>
<submenu>
<attribute name='label'>_File</attribute>
<section>
<item>
<attribute name='label'>_Quit</attribute>
<attribute name='action'>app.quit</attribute>
<attribute name='accel'>&lt;Primary&gt;q</attribute>
</item>
</section>
</submenu>
</menu>
</interface>
"
menuBuilder = Gtk::Builder.new_from_string menu_template, menu_template.size
menuModel = menuBuilder["app_menu"]
self.app_menu = menuModel unless show_menu
self.menubar = menuModel
window = Gtk::ApplicationWindow.new self
window.title = title
window.connect "destroy", &->self.quit
window.titlebar = headerbar
window.show_menubar = show_menu
window.set_default_size 600, 400
window.icon_name = "applications-development"
window.show_all
end
end
app = MyApplication.new
app.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment