def do_activate(self): """Activates the application. """ self.window = self.props.active_window if not self.window: self.window = NorkaWindow(application=self, settings=self.settings) self.window.present()
def do_activate(self): """Activates the application. """ self.granite_settings = Granite.Settings.get_default() self.gtk_settings = Gtk.Settings.get_default() # Setup default theme mode if self.settings.get_boolean('prefer-dark-theme'): self.gtk_settings.props.gtk_application_prefer_dark_theme = True else: # Then, we check if the user's preference is for the dark style and set it if it is self.gtk_settings.props.gtk_application_prefer_dark_theme = \ self.granite_settings.props.prefers_color_scheme == Granite.SettingsColorScheme.DARK # Finally, we listen to changes in Granite.Settings and update our app if the user changes their preference self.granite_settings.connect("notify::prefers-color-scheme", self.color_scheme_changed) self.window = self.props.active_window if not self.window: self.window = NorkaWindow(application=self, settings=self.settings, storage=self.storage) self.window.present()
class Application(Gtk.Application): __gtype_name__ = 'NorkaApplication' def __init__(self, version: str = None): super().__init__(application_id=APP_ID, flags=Gio.ApplicationFlags.FLAGS_NONE) # Init GSettings self.settings = Settings.new() self.init_style() self.window = None # Init storage location and SQL structure try: storage.init() except Exception as e: sys.exit(e) action = Gio.SimpleAction.new("quit", None) action.connect("activate", self.on_quit) self.add_action(action) self.set_accels_for_action('app.quit', ('<Control>q',)) action = Gio.SimpleAction.new("about", None) action.connect("activate", self.on_about) self.add_action(action) action = Gio.SimpleAction.new("preferences", None) action.connect("activate", self.on_preferences) self.add_action(action) self.set_accels_for_action('app.preferences', ('<Control>comma',)) def init_style(self): css_provider = Gtk.CssProvider() css_provider.load_from_resource('/com/github/tenderowl/norka/css/application.css') screen = Gdk.Screen.get_default() style_context = Gtk.StyleContext() style_context.add_provider_for_screen( screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) def do_startup(self): Gtk.Application.do_startup(self) self.settings.connect("changed", self.on_settings_changed) def do_activate(self): self.window = self.props.active_window if not self.window: self.window = NorkaWindow(application=self, settings=self.settings) self.window.present() def on_settings_changed(self, settings, key): Logger.debug(f'SETTINGS: %s changed', key) if key == "autosave": self.window.autosave = settings.get_boolean(key) elif key == "spellcheck": self.window.toggle_spellcheck(settings.get_boolean(key)) elif key == 'stylescheme': self.window.set_style_scheme(settings.get_string(key)) elif key == 'autoindent': self.window.set_autoindent(settings.get_boolean('autoindent')) elif key == 'spaces-instead-of-tabs': self.window.set_tabs_spaces(settings.get_boolean('spaces-instead-of-tabs')) elif key == 'indent-width': self.window.set_indent_width(settings.get_int('indent-width')) elif key == 'font': self.window.editor.update_font(settings.get_string('font')) def on_preferences(self, sender: Gtk.Widget = None, event=None) -> None: preferences_dialog = PreferencesDialog(transient_for=self.window, settings=self.settings) preferences_dialog.show_all() preferences_dialog.present() def on_quit(self, action, param): self.props.active_window.on_window_delete_event() self.settings.sync() self.quit() def on_about(self, action, param): about_dialog = AboutDialog(transient_for=self.window, modal=True) about_dialog.present()
class Application(Gtk.Application): __gtype_name__ = 'NorkaApplication' def __init__(self, version: str = None): super().__init__(application_id=APP_ID, flags=Gio.ApplicationFlags.HANDLES_OPEN) self.version = version # Init GSettings self.settings = Settings.new() self.init_style() self.window: NorkaWindow = None # Init storage location and SQL structure try: storage.init() except Exception as e: sys.exit(e) quit_action = Gio.SimpleAction.new("quit", None) quit_action.connect("activate", self.on_quit) self.add_action(quit_action) self.set_accels_for_action('app.quit', ('<Control>q', )) about_action = Gio.SimpleAction.new("about", None) about_action.connect("activate", self.on_about) self.add_action(about_action) preferences_action = Gio.SimpleAction.new("preferences", None) preferences_action.connect("activate", self.on_preferences) self.add_action(preferences_action) self.set_accels_for_action('app.preferences', ('<Control>comma', )) shortcuts_action = Gio.SimpleAction.new("shortcuts", None) shortcuts_action.connect("activate", self.on_shortcuts) self.add_action(shortcuts_action) format_shortcuts_action = Gio.SimpleAction.new("format_shortcuts", None) format_shortcuts_action.connect("activate", self.on_format_shortcuts) self.add_action(format_shortcuts_action) def init_style(self): css_provider = Gtk.CssProvider() css_provider.load_from_resource( '/com/github/tenderowl/norka/css/application.css') screen = Gdk.Screen.get_default() style_context = Gtk.StyleContext() style_context.add_provider_for_screen( screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) def do_startup(self): Gtk.Application.do_startup(self) builder = Gtk.Builder.new_from_resource( '/com/github/tenderowl/norka/ui/app_menu.xml') self.set_app_menu(builder.get_object('app-menu')) self.settings.connect("changed", self.on_settings_changed) def do_activate(self): """Activates the application. """ self.window = self.props.active_window if not self.window: self.window = NorkaWindow(application=self, settings=self.settings) self.window.present() def do_open(self, files: [Gio.File], n_files: int, hint: str): """Opens the given files. :param files: an array of Gio.Files to open :param n_files: number of files in command line args :param hint: a hint (or “”), but never None """ if n_files and not self.window: self.do_activate() for gfile in files: path = gfile.get_path() if not path: continue self.window.import_document(file_path=path) def on_settings_changed(self, settings, key): Logger.debug(f'SETTINGS: %s changed', key) if key == "autosave": self.window.autosave = settings.get_boolean(key) elif key == "spellcheck": self.window.toggle_spellcheck(settings.get_boolean(key)) elif key == 'stylescheme': self.window.set_style_scheme(settings.get_string(key)) elif key == 'autoindent': self.window.set_autoindent(settings.get_boolean('autoindent')) elif key == 'spaces-instead-of-tabs': self.window.set_tabs_spaces( settings.get_boolean('spaces-instead-of-tabs')) elif key == 'indent-width': self.window.set_indent_width(settings.get_int('indent-width')) elif key == 'font': self.window.editor.update_font(settings.get_string('font')) def on_preferences(self, sender: Gtk.Widget = None, event=None) -> None: preferences_dialog = PreferencesDialog(transient_for=self.window, settings=self.settings) preferences_dialog.show_all() preferences_dialog.present() def on_quit(self, action, param): self.props.active_window.on_window_delete_event() self.settings.sync() self.quit() def on_about(self, action, param): about_dialog = AboutDialog( version=self.version, transient_for=self.window, modal=True, ) about_dialog.present() def on_shortcuts(self, action, param): builder = Gtk.Builder.new_from_resource( "/com/github/tenderowl/norka/ui/shortcuts.ui") dialog = builder.get_object("shortcuts") dialog.set_transient_for(self.window) dialog.show() def on_format_shortcuts(self, action, param): dialog = FormatShortcutsDialog() dialog.set_transient_for(self.window) dialog.show()
class Application(Gtk.Application): __gtype_name__ = 'NorkaApplication' granite_settings: Granite.Settings gtk_settings: Gtk.Settings def __init__(self, version: str = None): super().__init__(application_id=APP_ID, flags=Gio.ApplicationFlags.HANDLES_OPEN | Gio.ApplicationFlags.HANDLES_COMMAND_LINE) Handy.init() self.add_main_option('new', 110, GLib.OptionFlags.OPTIONAL_ARG, GLib.OptionArg.STRING, _('Open new document on start.')) self.version = version # Init GSettings self.settings = Settings.new() self.init_style() self.window: NorkaWindow = None # Init storage location and SQL structure self.base_path = os.path.join(GLib.get_user_data_dir(), APP_TITLE) storage_path = self.settings.get_string("storage-path") if not storage_path: storage_path = os.path.join(self.base_path, STORAGE_NAME) self.settings.set_string("storage-path", storage_path) self.storage = Storage(storage_path) try: self.storage.init() except Exception as e: sys.exit(e) quit_action = Gio.SimpleAction.new("quit", None) quit_action.connect("activate", self.on_quit) self.add_action(quit_action) self.set_accels_for_action('app.quit', ('<Control>q',)) about_action = Gio.SimpleAction.new("about", None) about_action.connect("activate", self.on_about) self.add_action(about_action) preferences_action = Gio.SimpleAction.new("preferences", None) preferences_action.connect("activate", self.on_preferences) self.add_action(preferences_action) self.set_accels_for_action('app.preferences', ('<Control>comma',)) shortcuts_action = Gio.SimpleAction.new("shortcuts", None) shortcuts_action.connect("activate", self.on_shortcuts) self.add_action(shortcuts_action) format_shortcuts_action = Gio.SimpleAction.new("format_shortcuts", None) format_shortcuts_action.connect("activate", self.on_format_shortcuts) self.add_action(format_shortcuts_action) def init_style(self): css_provider = Gtk.CssProvider() css_provider.load_from_resource(f"{RESOURCE_PREFIX}/css/application.css") screen = Gdk.Screen.get_default() style_context = Gtk.StyleContext() style_context.add_provider_for_screen( screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) if self.settings.get_boolean('prefer-dark-theme'): Gtk.Settings.get_default().props.gtk_application_prefer_dark_theme = True def do_startup(self): Gtk.Application.do_startup(self) # builder = Gtk.Builder.new_from_resource(f"{RESOURCE_PREFIX}/ui/app_menu.xml") # self.set_app_menu(builder.get_object('app-menu')) self.settings.connect("changed", self.on_settings_changed) def do_activate(self): """Activates the application. """ self.granite_settings = Granite.Settings.get_default() self.gtk_settings = Gtk.Settings.get_default() # Setup default theme mode if self.settings.get_boolean('prefer-dark-theme'): self.gtk_settings.props.gtk_application_prefer_dark_theme = True else: # Then, we check if the user's preference is for the dark style and set it if it is self.gtk_settings.props.gtk_application_prefer_dark_theme = \ self.granite_settings.props.prefers_color_scheme == Granite.SettingsColorScheme.DARK # Finally, we listen to changes in Granite.Settings and update our app if the user changes their preference self.granite_settings.connect("notify::prefers-color-scheme", self.color_scheme_changed) self.window = self.props.active_window if not self.window: self.window = NorkaWindow(application=self, settings=self.settings, storage=self.storage) self.window.present() def do_open(self, files: List[Gio.File], n_files: int, hint: str): """Opens the given files. :param files: an array of Gio.Files to open :param n_files: number of files in command line args :param hint: a hint (or “”), but never None """ if n_files and not self.window: self.do_activate() for gfile in files: path = gfile.get_path() if not path: continue self.window.import_document(file_path=path) def do_command_line(self, command_line): self.activate() options = command_line.get_options_dict() options = options.end().unpack() if 'new' in options: new_arg_value = options['new'] if new_arg_value and not self.window.is_document_editing: self.window.on_document_create_activated(title=new_arg_value) return 1 return 0 def on_settings_changed(self, settings, key): Logger.debug(f'SETTINGS: %s changed', key) if key == "autosave": self.window.autosave = settings.get_boolean(key) if key == "spellcheck": self.window.toggle_spellcheck(settings.get_boolean(key)) if key == "spellcheck-language": self.window.set_spellcheck_language(settings.get_string(key)) if key == 'stylescheme': self.window.set_style_scheme(settings.get_string(key)) if key == 'autoindent': self.window.set_autoindent(settings.get_boolean('autoindent')) if key == 'spaces-instead-of-tabs': self.window.set_tabs_spaces(settings.get_boolean('spaces-instead-of-tabs')) if key == 'indent-width': self.window.set_indent_width(settings.get_int('indent-width')) if key == 'font': self.window.editor.update_font(settings.get_string('font')) if key == 'prefer-dark-theme': Gtk.Settings.get_default().props.gtk_application_prefer_dark_theme = \ settings.get_boolean('prefer-dark-theme') def on_preferences(self, sender: Gtk.Widget = None, event=None) -> None: preferences_dialog = PreferencesDialog(transient_for=self.window, settings=self.settings) preferences_dialog.show_all() preferences_dialog.present() def on_quit(self, action, param): self.props.active_window.on_window_delete_event() self.settings.sync() self.quit() def on_about(self, action, param): about_dialog = AboutDialog(version=self.version, transient_for=self.window, modal=True, ) about_dialog.present() def color_scheme_changed(self, _old, _new): dark_mode = self.settings.get_boolean('prefer-dark-theme') if not dark_mode: self.gtk_settings.props.gtk_application_prefer_dark_theme = \ self.granite_settings.props.prefers_color_scheme == Granite.SettingsColorScheme.DARK def on_shortcuts(self, action, param): builder = Gtk.Builder.new_from_resource(f"{RESOURCE_PREFIX}/ui/shortcuts.ui") dialog = builder.get_object("shortcuts") dialog.set_transient_for(self.window) dialog.show() def on_format_shortcuts(self, action, param): dialog = FormatShortcutsDialog() dialog.set_transient_for(self.window) dialog.show()