-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamma.py
executable file
·204 lines (148 loc) · 5.95 KB
/
gamma.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#
#### Author: Hamad Al Marri <[email protected]>
#### Date: Feb 11th, 2020
#
#
# 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 <https://www.gnu.org/licenses/>.
#
#
#
#
# This is the entry point to Gamma editor. The gamma.py will load
# config parameters and load the builder (UI structure of the main window).
# This Application instance is the main root of gamma. It holds
# references to everything needed for other plugins such as config,
# window, builder, and plugins_manager.
# Also it loads the eager plugins in self.plugins_manager.load_plugins()
# which call activate for each plugin and store plugins references in
# plugins_manager.plugins
import sys
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GtkSource', "4")
from gi.repository import Gio, Gtk, Gdk, GtkSource, GObject
import config
import signal_handler
from plugins.plugins_manager import PluginsManager
class Application(Gtk.Application):
def __init__(self, *args, **kwargs):
# make the package name as "io.gitlab.hamadmarri.gamma"
# FLAGS_NONE means no passing arguments from command line, this
# might be changed later to support new window, new file, or open a file
super().__init__(*args, application_id=f"io.gitlab.hamadmarri.gamma",
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, **kwargs)
# this line is important to mak gtk object(newer version of pygtk) to
# include gtk sourceview.
GObject.type_register(GtkSource.View)
self.name = "GammaApplication"
self.window = None
self.builder = None
self.is_debugging = False
# config contains important paths and settings for ui, styles, plugins
self.config = config.config_paths_and_settings
# builder is the object responsible of
# translating .ui xml files (widgets design/layout. see glade) to
# be in the gtk objects form
self.load_builder()
# plugins_manager for anything related to plugins (eager plugins)
self.plugins_manager = PluginsManager(self)
# signal_handler is for handling general signals such as
# key press, and basic window resizing paned
# SignalHandler also makes it easier for other plugins to
# process key bindings. It loop through all plugins and
# call key_bindings function passing (event, keyval_name, ctrl, alt, shift)
# which is an easy design for plugins to set there key bindings
self.signal_handler = signal_handler.SignalHandler(self)
def load_builder(self):
self.builder = Gtk.Builder()
# load .ui file, its path is in config file
self.builder.add_from_file(self.config["ui-path"])
def set_handlers(self):
# this line connects signals in handlers object to
# some functions. "handlers" is set by SignalHandler and
# plugins that need to bind signals to functions
self.builder.connect_signals(self.signal_handler.handlers)
def do_startup(self):
Gtk.Application.do_startup(self)
def do_command_line(self, command_line):
args = command_line.get_arguments()
self.signal_handler.emit("log", self, "do_command_line:" + str(args))
if len(args) == 1:
self.do_activate()
elif args[1] == "--new-window":
self.do_activate(new_window=True)
elif args[1] == "--verbose":
self.is_debugging = True
# make sure at least the main window is open
self.do_activate()
if len(args) > 2:
# open files
self.open_files(args[2:])
else:
# make sure at least the main window is open
self.do_activate()
# open files
self.open_files(args[1:])
return 0
def do_activate(self, new_window=False):
if not self.window:
self.show_first_window()
elif new_window:
self.show_new_window()
# I think there is a bug
# eventhough header_left_side is set to visible
# but it doesn't show up unless window.show_all() which
# it changed to show() to make sourcemap (mini source view)
# able to be invisible
self.builder.get_object("header_left_side").show_all()
def show_first_window(self):
# get id=window (ui element in .ui) from builder
self.window = self.builder.get_object("window")
# bind this builder to this window
self.window.builder = self.builder
# loading plugins calls their activate functions.
# in plugins_manager.py, you can comment out plugins in
# plugin_list array
self.plugins_manager.load_plugins()
self.set_handlers()
# must set the parent application of
# window to this app(self)
self.window.props.application = self
self.window.set_icon_name("io.gitlab.hamadmarri.gamma")
self.window.show()
self.window.connect("focus_in_event", self.window_event)
def show_new_window(self):
self.load_builder()
# get id=window (ui element in .ui) from builder
self.window = self.builder.get_object("window")
# bind this builder to this window
self.window.builder = self.builder
self.plugins_manager.activate_plugins()
self.set_handlers()
# must set the parent application of
# window to this app(self)
self.window.props.application = self
self.window.set_icon_name("io.gitlab.hamadmarri.gamma")
self.window.show()
self.window.connect("focus_in_event", self.window_event)
def window_event(self, w, e=None):
self.window = w
self.builder = w.builder
self.signal_handler.emit("windo-focus-in", w)
def open_files(self, filenames):
self.signal_handler.emit("log", self, "do_open:" + str(filenames))
self.plugins_manager.THE("files_manager", "open_files", {"filenames": filenames})
if __name__ == "__main__":
app = Application()
app.run(sys.argv)