gem-graph-client/gemgraph/main.py

109 lines
3.8 KiB
Python

# main.py
#
# Copyright (C) 2022 Libre en Communs <contact@a-lec.org>
# Copyright (C) 2022 Adrien Bourmault <neox@a-lec.org>
# Copyright (C) 2022 Jean Sirmai <jean@a-lec.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import gi
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Gio, Adw
from .window import GemGraphWindow, AboutDialog
class GemGraphApplication(Adw.Application):
"""The main application singleton class."""
def __init__(self):
super().__init__(application_id='org.example.App',
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.create_action('quit', self.quit, ['<primary>q'])
self.create_action('about', self.on_about_action)
self.create_action('preferences', self.on_preferences_action)
self.create_action('editmode', self.on_editmode_action, ['<primary>e'])
self.create_action('runmode', self.on_runmode_action, ['<primary>r'])
self.create_action('presentmode', self.on_presentmode_action, ['<primary>p'])
self.create_action('togglesidebar', self.on_togglesidebar_action)
def do_activate(self):
"""Called when the application is activated.
We raise the application's main window, creating it if
necessary.
"""
win = self.props.active_window
if not win:
win = GemGraphWindow(application=self)
win.present()
def on_about_action(self, widget, _):
"""Callback for the app.about action."""
about = AboutDialog(self.props.active_window)
about.present()
def on_preferences_action(self, widget, _):
"""Callback for the app.preferences action."""
print('app.preferences action activated')
def on_editmode_action(self, widget, _):
"""Callback for the app.editmode action."""
print('app.editmode action activated')
win = self.props.active_window
win.stack_switch_mode("edit")
def on_runmode_action(self, widget, _):
"""Callback for the app.runmode action."""
print('app.runmode action activated')
win = self.props.active_window
win.stack_switch_mode("run")
def on_presentmode_action(self, widget, _):
"""Callback for the app.presentmode action."""
print('app.presentmode action activated')
win = self.props.active_window
win.stack_switch_mode("presentation")
def on_togglesidebar_action(self, widget, _):
"""Callback for the app.togglesidebar action."""
print('app.togglesidebar action activated')
win = self.props.active_window
win.toggle_sidebar()
def create_action(self, name, callback, shortcuts=None):
"""Add an application action.
Args:
name: the name of the action
callback: the function to be called when the action is
activated
shortcuts: an optional list of accelerators
"""
action = Gio.SimpleAction.new(name, None)
action.connect("activate", callback)
self.add_action(action)
if shortcuts:
self.set_accels_for_action(f"app.{name}", shortcuts)
def main(version):
"""The application's entry point."""
app = GemGraphApplication()
return app.run(sys.argv)