def cancel_debounce(self, obj): if log.query(log.DEBUG): Gedit.debug_plugin_message(log.format("%s", obj)) if obj in self._debounce_ids: GLib.source_remove(self._debounce_ids[obj]) del self._debounce_ids[obj]
def do_create_configure_widget(self): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("")) app = Gedit.App.get_default() is_primary = not (app.get_flags() & Gio.ApplicationFlags.NON_UNIQUE) settings = ExMortisSettings() if settings.can_save: widget = Gtk.CheckButton.new_with_label( _("Restore windows between sessions") ) if not is_primary: widget.set_sensitive(False) create_bindings( self, settings, widget, {'restore_between_sessions': 'active'}, GObject.BindingFlags.BIDIRECTIONAL ) widget.set_active(settings.restore_between_sessions) widget._settings = settings else: widget = Gtk.Label.new(_("Could not load settings schema")) box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0) box.set_border_width(5) box.add(widget) return box
def restore_windows(self, window_manager, window, tab): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s, %s", window, tab)) active_tab = window.get_active_tab() num_tabs = len(active_tab.get_parent().get_children()) is_single_empty_tab = (num_tabs == 1 and tab == active_tab and tab.get_document().is_untouched() and tab.get_state() == Gedit.TabState.STATE_NORMAL) # if there is only one empty tab, let gedit reuse it when opening files # otherwise, open a new tab to be (re)used # this protects the new tab that was added if gedit was run with # --new-document and one or more files to open if log.query(log.DEBUG): Gedit.debug_plugin_message( log.format("is_single_empty_tab=%s", is_single_empty_tab)) if not is_single_empty_tab: window.create_tab(True) state = self._restore_states.pop() window_manager.import_window_state(window, state) for state in self._restore_states: window_manager.open_new_window_with_window_state(state) self._restore_states = None if not is_single_empty_tab: window.set_active_tab(active_tab) window.present()
def debounce(self, obj, fn, *args): if log.query(log.DEBUG): Gedit.debug_plugin_message(log.format("%s", obj)) self.cancel_debounce(obj) self._debounce_ids[obj] = GLib.timeout_add(1000, fn, obj, *args)
def on_tab_notify_name(self, tab, pspec, window, state): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s, %s", window, tab)) state.save_uri(window, tab) self.emit('tab-updated', window, tab)
def apply_window(self, window, is_new_window=False): if log.query(log.INFO): Gedit.debug_plugin_message( log.format("%s, is_new_window=%s", window, is_new_window)) # need to unmaximize/unfullscreen to set size window.unmaximize() window.unfullscreen() self.apply_size(window, is_new_window) self.apply_window_state(window) self.apply_side_panel_page_name(window) self.apply_side_panel_visible(window) self.apply_bottom_panel_page_name(window) self.apply_bottom_panel_visible(window) if is_new_window: window.show() self.apply_side_panel_size(window) self.apply_bottom_panel_size(window) self.apply_uris(window) self.apply_active_uri(window) self.apply_notebook_widths(window)
def apply_uris(self, window): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) uris = self._restore_uris if log.query(log.DEBUG): Gedit.debug_plugin_message(log.format("applying uris=%s", uris)) if uris: documents = window.get_documents() create_notebook = False if documents: window.set_active_tab( Gedit.Tab.get_from_document(documents[-1])) for notebook_uris in uris: if create_notebook: window.activate_action('new-tab-group') locations = [ Gio.File.new_for_uri(uri) for uri in notebook_uris ] Gedit.commands_load_locations(window, locations, None, 0, 0) create_notebook = True
def on_paned_notify_position(self, paned, pspec, window, state, multi_notebook): if log.query(log.DEBUG): Gedit.debug_plugin_message(log.format("%s, %s", window, paned)) self.debounce(multi_notebook, self.debounce_save_notebook_widths, window, state)
def setup_window(self, window, is_existing=False): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s, is_existing=%s", window, is_existing)) window_manager = self._window_manager settings = self._settings if is_existing: info_bar, quit_response_id = self.add_existing(window) connect_handlers( self, info_bar, ['response'], 'existing_window_info_bar', quit_response_id ) self.show_existing_info_bar(window) connect_handlers( self, window, ['delete-event'], 'window', window_manager ) window_manager.track_window(window) self.setup_restore_window(window_manager, window) if self.is_saving_window_states(): self.bind_window_settings(window_manager, settings, window)
def save_window_state(self, window, window_state=None): if log.query(log.INFO): Gedit.debug_plugin_message( log.format("%s, window_state=%s", window, window_state)) if window_state is None: gdk_window = window.get_window() if not gdk_window: if log.query(log.DEBUG): Gedit.debug_plugin_message( log.format("window not yet realized")) return False window_state = gdk_window.get_state() maximized = bool(window_state & Gdk.WindowState.MAXIMIZED) fullscreen = bool(window_state & Gdk.WindowState.FULLSCREEN) results = [ self.save_property('maximized', maximized), self.save_property('fullscreen', fullscreen) ] return any(results)
def on_window_active_tab_changed(self, window, tab, state): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s, %s", window, tab)) state.save_active_uri(window, tab) self.emit('active-tab-changed', window, tab)
def on_window_tabs_reordered(self, window, state): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) state.update_structure(window) self.emit('tabs-reordered', window)
def on_app_shutdown(self, app): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("")) settings = self._settings self.end_quitting(settings, settings.restore_between_sessions)
def untrack_window(self, window): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) if window not in self._windows: if log.query(log.WARNING): Gedit.debug_plugin_message(log.format("unknown window")) return state, widgets = self._windows[window] multi_notebook = widgets['multi_notebook'] hpaned = widgets['hpaned'] vpaned = widgets['vpaned'] for document in window.get_documents(): self.untrack_tab(window, Gedit.Tab.get_from_document(document), state) for paned in self.find_paneds(multi_notebook): self.untrack_paned(window, paned, state, multi_notebook) self.cancel_debounce(window) self.cancel_debounce(multi_notebook) self.cancel_debounce(hpaned) self.cancel_debounce(vpaned) disconnect_handlers(self, window) disconnect_handlers(self, multi_notebook) disconnect_handlers(self, widgets['side_panel']) disconnect_handlers(self, widgets['bottom_panel']) disconnect_handlers(self, hpaned) disconnect_handlers(self, vpaned) del self._windows[window]
def save_to_window_state(self, window): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) state = self.get_window_state(window) if state: state.save_window(window)
def __init__(self): GObject.Object.__init__(self) if log.query(log.INFO): Gedit.debug_plugin_message(log.format("")) self._windows = {} self._debounce_ids = {}
def restore_from_window_state(self, window): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) state = self.get_window_state(window) if state: state.apply_window(window)
def on_window_manager_tabs_reordered(self, window_manager, window): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) if not self.is_existing(window): self.cancel_closing(window) self.cancel_quitting()
def save_side_panel_size(self, window): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) hpaned = window.get_template_child(Gedit.Window, 'hpaned') position = hpaned.get_position() return self.save_property('side-panel-size', position)
def on_window_manager_tab_removed(self, window_manager, window, tab): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s, %s", window, tab)) if not self.is_existing(window): self.update_closing(window, tab) self.update_quitting(window, tab)
def save_bottom_panel_visible(self, window): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) bottom_panel = window.get_bottom_panel() visible = bottom_panel.get_visible() return self.save_property('bottom-panel-visible', visible)
def notebook_widths_changed(self): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("")) zipped = zip(self._restore_filter, self._notebook_widths) self._restore_notebook_widths = [ width for can_restore, width in zipped if can_restore ]
def track_window(self, window): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) if window in self._windows: if log.query(log.WARNING): Gedit.debug_plugin_message( log.format("window already being tracked")) return state = ExMortisWindowState() state.save_window(window) multi_notebook = window.get_template_child(Gedit.Window, 'multi_notebook') side_panel = window.get_side_panel() bottom_panel = window.get_bottom_panel() hpaned = window.get_template_child(Gedit.Window, 'hpaned') vpaned = window.get_template_child(Gedit.Window, 'vpaned') connect_handlers(self, window, [ 'tab-added', 'tab-removed', 'tabs-reordered', 'active-tab-changed', 'configure-event', 'window-state-event', ], 'window', state) connect_handlers(self, multi_notebook, ['notebook-added', 'notebook-removed'], 'multi_notebook', window, state) connect_handlers(self, side_panel, ['notify::visible-child-name', 'notify::visible'], 'side_panel', window, state) connect_handlers(self, bottom_panel, ['notify::visible-child-name', 'notify::visible'], 'bottom_panel', window, state) connect_handlers(self, hpaned, ['notify::position'], 'hpaned', window, state) connect_handlers(self, vpaned, ['notify::position'], 'vpaned', window, state) self._windows[window] = (state, { 'multi_notebook': multi_notebook, 'side_panel': side_panel, 'bottom_panel': bottom_panel, 'hpaned': hpaned, 'vpaned': vpaned }) for paned in self.find_paneds(multi_notebook): self.track_paned(window, paned, state, multi_notebook) for document in window.get_documents(): self.track_tab(window, Gedit.Tab.get_from_document(document), state)
def on_window_tab_removed(self, window, tab, state): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s, %s", window, tab)) self.untrack_tab(window, tab, state) state.update_structure(window) self.emit('tab-removed', window, tab)
def on_multi_notebook_notebook_added(self, multi_notebook, notebook, window, state): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s, %s", window, notebook)) self.track_paned(window, notebook.get_parent(), state, multi_notebook) self.debounce(multi_notebook, self.debounce_save_notebook_widths, window, state)
def on_multi_notebook_notebook_removed(self, multi_notebook, notebook, window, state): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s, %s", window, notebook)) # can't untrack_paned() since the notebook is already disconnected and the paned gone self.debounce(multi_notebook, self.debounce_save_notebook_widths, window, state)
def cleanup(self): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("")) for window in list(self._windows.keys()): self.untrack_window(window) self._windows = None self._debounce_ids = None
def forget_tab(self, tab): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", tab)) if tab == self._active_tab: self._active_tab = None if tab in self._tab_map: del self._tab_map[tab]
def debounce_save_bottom_panel_size(self, vpaned, window, state): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) state.save_bottom_panel_size(window) self.done_debounce(vpaned) return False
def debounce_save_notebook_widths(self, multi_notebook, window, state): if log.query(log.INFO): Gedit.debug_plugin_message(log.format("%s", window)) state.save_notebook_widths(window) self.done_debounce(multi_notebook) return False
def _setup(self, tab): notebooks = self._notebooks multi = self._get_multi_notebook(tab) if multi: self._connect_handlers(multi, ('notebook-added', 'notebook-removed', 'tab-added', 'tab-removed'), 'multi_notebook', notebooks) self._multi = multi for doc in self.window.get_documents(): self.on_multi_notebook_notebook_added(multi, Gedit.Tab.get_from_document(doc).get_parent(), notebooks) self._connect_handlers(self.window, ('tabs-reordered', 'active-tab-changed', 'key-press-event', 'key-release-event', 'focus-out-event'), 'window', notebooks) elif hasattr(Gedit, 'debug_plugin_message'): Gedit.debug_plugin_message("cannot find multi notebook from %s", tab)