~jaap.karssenberg/zim/pyzim-gtk3

« back to all changes in this revision

Viewing changes to zim/plugins/distractionfree.py

  • Committer: Jaap Karssenberg
  • Date: 2014-03-08 11:47:43 UTC
  • mfrom: (668.1.49 pyzim-refactor)
  • Revision ID: jaap.karssenberg@gmail.com-20140308114743-fero6uvy9zirbb4o
Merge branch with refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import gtk
6
6
import logging
7
7
 
8
 
from zim.plugins import PluginClass
 
8
from zim.plugins import PluginClass, WindowExtension, extends
9
9
 
10
10
from zim.gui import PATHBAR_NONE, PATHBAR_RECENT
11
11
 
43
43
                #('fgcolor', 'color', _('Screen foreground color'), '#eeeeec'),
44
44
        )
45
45
 
46
 
        def __init__(self, *a):
 
46
 
 
47
@extends('MainWindow')
 
48
class MainWindowExtension(WindowExtension):
 
49
 
 
50
        def __init__(self, plugin, window):
 
51
                WindowExtension.__init__(self, plugin, window)
47
52
                self._normal_colors = None
48
53
                self._show_panes = True
49
 
                PluginClass.__init__(self, *a)
50
 
 
51
 
        def finalize_ui(self, ui):
52
 
                self.connectto(self.ui.mainwindow.pageview.view, 'size-allocate')
53
 
                self.connectto(self.ui.mainwindow, 'fullscreen-changed')
54
 
                self.do_preferences_changed()
55
 
 
56
 
        def do_preferences_changed(self):
 
54
                self.preferences = plugin.preferences
 
55
 
 
56
                self.connectto(plugin.preferences, 'changed', self.on_preferences_changed)
 
57
                self.connectto(window, 'init-uistate', self.on_init_uistate)
 
58
 
 
59
                self.connectto(window, 'fullscreen-changed')
 
60
                self.connectto(window.pageview.view, 'size-allocate')
 
61
 
 
62
        def on_init_uistate(self, window):
 
63
                self.on_preferences_changed(self.plugin.preferences)
 
64
 
 
65
        def on_preferences_changed(self, preferences):
57
66
                # Set show menubar & Update margins
58
 
                show_menubar = not self.preferences['hide_menubar']
59
 
                show_toolbar = not self.preferences['hide_toolbar']
60
 
                show_pathbar = not self.preferences['hide_pathbar']
61
 
                show_statusbar = not self.preferences['hide_statusbar']
62
 
                if self.ui.mainwindow.isfullscreen:
63
 
                        self.ui.mainwindow.toggle_menubar(show_menubar)
64
 
                        self.ui.mainwindow.toggle_toolbar(show_toolbar)
65
 
                        self.ui.mainwindow.toggle_statusbar(show_statusbar)
 
67
                show_menubar = not preferences['hide_menubar']
 
68
                show_toolbar = not preferences['hide_toolbar']
 
69
                show_pathbar = not preferences['hide_pathbar']
 
70
                show_statusbar = not preferences['hide_statusbar']
 
71
                if self.window.isfullscreen:
 
72
                        self.window.toggle_menubar(show_menubar)
 
73
                        self.window.toggle_toolbar(show_toolbar)
 
74
                        self.window.toggle_statusbar(show_statusbar)
66
75
 
67
76
                        if show_pathbar \
68
 
                        and self.ui.uistate['MainWindow']['pathbar_type_fullscreen'] == PATHBAR_NONE:
69
 
                                self.ui.mainwindow.set_pathbar(PATHBAR_RECENT)
 
77
                        and self.window.uistate['pathbar_type_fullscreen'] == PATHBAR_NONE:
 
78
                                self.window.set_pathbar(PATHBAR_RECENT)
70
79
                        elif not show_pathbar:
71
 
                                self.ui.mainwindow.set_pathbar(PATHBAR_NONE)
 
80
                                self.window.set_pathbar(PATHBAR_NONE)
72
81
 
73
 
                        textview = self.ui.mainwindow.pageview.view
 
82
                        textview = self.window.pageview.view
74
83
                        self.on_size_allocate(textview, textview.get_allocation())
75
84
                else:
76
 
                        self.ui.uistate['MainWindow']['show_menubar_fullscreen'] = show_menubar
77
 
                        self.ui.uistate['MainWindow']['show_toolbar_fullscreen'] = show_toolbar
78
 
                        self.ui.uistate['MainWindow']['show_statusbar_fullscreen'] = show_statusbar
 
85
                        self.window.uistate['show_menubar_fullscreen'] = show_menubar
 
86
                        self.window.uistate['show_toolbar_fullscreen'] = show_toolbar
 
87
                        self.window.uistate['show_statusbar_fullscreen'] = show_statusbar
79
88
 
80
89
                        if show_pathbar \
81
 
                        and self.ui.uistate['MainWindow']['pathbar_type_fullscreen'] == PATHBAR_NONE:
82
 
                                self.ui.uistate['MainWindow']['pathbar_type_fullscreen'] = PATHBAR_RECENT
 
90
                        and self.window.uistate['pathbar_type_fullscreen'] == PATHBAR_NONE:
 
91
                                self.window.uistate['pathbar_type_fullscreen'] = PATHBAR_RECENT
83
92
                        elif not show_pathbar:
84
 
                                self.ui.uistate['MainWindow']['pathbar_type_fullscreen'] = PATHBAR_NONE
 
93
                                self.window.uistate['pathbar_type_fullscreen'] = PATHBAR_NONE
85
94
 
86
95
                # TODO - would be nice to be able to toggle hide/show for pathbar without need to set type
87
96
                #        allow hiding container or seperate widget from "model"
89
98
        def on_fullscreen_changed(self, window):
90
99
                if window.isfullscreen:
91
100
                        self._show_panes = bool(window.get_visible_panes())
92
 
                        self._save_colors()
 
101
                        self._save_colors(window)
93
102
                        self._set_colors(self._custom_colors)
94
103
                        window.toggle_panes(show=False)
95
104
                elif self._normal_colors:
104
113
        #       we can not set just the few colors in RcStyle, would need to
105
114
        #       switch the whole theme
106
115
 
107
 
        def _save_colors(self):
108
 
                style = self.ui.mainwindow.pageview.view.rc_get_style()
 
116
        def _save_colors(self, window):
 
117
                style = window.pageview.view.rc_get_style()
109
118
                self._normal_colors = []
110
119
                for state in (
111
120
                        gtk.STATE_NORMAL,
139
148
                #return [normal, normal, normal, selected, normal]
140
149
                return (normal,)
141
150
 
142
 
 
143
151
        def _set_colors(self, colors):
144
152
                # See gtk.RcStyle docs for all values in RC file
145
153
                rc = 'style "zim-colors"\n{\n'
167
175
        def on_size_allocate(self, textview, allocation):
168
176
                # Here we play with textview margin windows to position text
169
177
                # in center of screen with a maximum size
170
 
                if not self.ui.mainwindow.isfullscreen:
 
178
                if not self.window.isfullscreen:
171
179
                        self._set_margins(0, 0, 0, 0)
172
180
                        return
173
181
 
218
226
                self._set_margins(left, right, top, bottom)
219
227
 
220
228
        def _set_margins(self, left, right, top, bottom):
221
 
                self.ui.mainwindow.pageview.view.set_border_window_size(gtk.TEXT_WINDOW_LEFT, left)
222
 
                self.ui.mainwindow.pageview.view.set_border_window_size(gtk.TEXT_WINDOW_RIGHT, right)
223
 
                self.ui.mainwindow.pageview.view.set_border_window_size(gtk.TEXT_WINDOW_TOP, top)
224
 
                self.ui.mainwindow.pageview.view.set_border_window_size(gtk.TEXT_WINDOW_BOTTOM, bottom)
 
229
                textview = self.window.pageview.view
 
230
                textview.set_border_window_size(gtk.TEXT_WINDOW_LEFT, left)
 
231
                textview.set_border_window_size(gtk.TEXT_WINDOW_RIGHT, right)
 
232
                textview.set_border_window_size(gtk.TEXT_WINDOW_TOP, top)
 
233
                textview.set_border_window_size(gtk.TEXT_WINDOW_BOTTOM, bottom)
225
234
 
226
 
        def destroy(self):
 
235
        def teardown(self):
227
236
                # show at least menubar again, set margins to zero & restore colors
228
 
                self.ui.uistate['MainWindow']['show_menubar_fullscreen'] = True
 
237
                self.window.uistate['show_menubar_fullscreen'] = True
229
238
                self._set_margins(0, 0, 0, 0)
230
239
                if self._normal_colors:
231
240
                        self._set_colors(self._normal_colors)
232
 
                PluginClass.destroy(self)
233
241