Skip to content

Commit df4cf97

Browse files
committed
Remove tab list from menu, replacing with keybinding actions
This commit creates a new MeldNotebook class for handling some contained notebook behaviours and keybindings. Since we're looking to reduce the overall UI handled by UIManager, and because it's no longer standard to keep a list of open tabs as menu items, we're dropping that bit of the UI and accompanying handling. This reimplements only the Alt+number keybindings for quick tab switching.
1 parent 8088f5e commit df4cf97

5 files changed

Lines changed: 55 additions & 62 deletions

File tree

data/ui/meldapp-ui.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
<separator/>
5858
<menuitem action="MoveTabPrev" />
5959
<menuitem action="MoveTabNext" />
60-
<separator/>
61-
<placeholder name="TabPlaceholder" />
6260
</menu>
6361
</menubar>
6462

data/ui/meldapp.ui

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@
2424
</packing>
2525
</child>
2626
<child>
27-
<object class="GtkNotebook" id="notebook">
27+
<object class="MeldNotebook" id="notebook">
2828
<property name="visible">True</property>
2929
<property name="can_focus">True</property>
3030
<property name="scrollable">True</property>
3131
<property name="enable_popup">True</property>
32-
<signal name="page-reordered" handler="_update_notebook_menu" swapped="no"/>
3332
<signal name="page-reordered" handler="after_page_reordered" after="yes" swapped="no"/>
34-
<signal name="page-added" handler="_update_notebook_menu" swapped="no"/>
3533
<signal name="switch-page" handler="on_switch_page" swapped="no"/>
3634
<signal name="switch-page" handler="after_switch_page" after="yes" swapped="no"/>
37-
<signal name="page-removed" handler="_update_notebook_menu" swapped="no"/>
3835
</object>
3936
<packing>
4037
<property name="expand">True</property>

meld/meldwindow.py

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ def app_action(*args):
184184
self.ui.add_ui_from_file(ui_file)
185185
self.widget.set_show_menubar(False)
186186

187-
self.tab_switch_actiongroup = None
188-
self.tab_switch_merge_id = None
189-
190187
for menuitem in ("Save", "Undo"):
191188
self.actiongroup.get_action(menuitem).props.is_important = True
192189
self.widget.add_accel_group(self.ui.get_accel_group())
@@ -410,10 +407,6 @@ def on_switch_page(self, notebook, page, which):
410407

411408
def after_switch_page(self, notebook, page, which):
412409
self._update_page_action_sensitivity()
413-
actiongroup = self.tab_switch_actiongroup
414-
if actiongroup:
415-
action_name = "SwitchTab%d" % which
416-
actiongroup.get_action(action_name).set_active(True)
417410

418411
def after_page_reordered(self, notebook, page, page_num):
419412
self._update_page_action_sensitivity()
@@ -431,13 +424,6 @@ def on_notebook_label_changed(self, component, text, tooltip):
431424
text = text.encode('utf8')
432425
self.notebook.child_set_property(page, "menu-label", text)
433426

434-
actiongroup = self.tab_switch_actiongroup
435-
if actiongroup:
436-
idx = self.notebook.page_num(page)
437-
action_name = "SwitchTab%d" % idx
438-
label = text.replace("_", "__")
439-
actiongroup.get_action(action_name).set_label(label)
440-
441427
def on_can_undo(self, undosequence, can):
442428
self.actiongroup.get_action("Undo").set_sensitive(can)
443429

@@ -563,48 +549,6 @@ def on_move_tab_next(self, *args):
563549
child = self.notebook.get_nth_page(page_num)
564550
self.notebook.reorder_child(child, page_num + 1)
565551

566-
def _update_notebook_menu(self, *args):
567-
if self.tab_switch_merge_id:
568-
self.ui.remove_ui(self.tab_switch_merge_id)
569-
self.ui.remove_action_group(self.tab_switch_actiongroup)
570-
self.ui.ensure_update()
571-
self.tab_switch_merge_id = None
572-
self.tab_switch_actiongroup = None
573-
574-
if not self.notebook.get_n_pages():
575-
return
576-
577-
self.tab_switch_merge_id = self.ui.new_merge_id()
578-
self.tab_switch_actiongroup = Gtk.ActionGroup(name="TabSwitchActions")
579-
self.ui.insert_action_group(self.tab_switch_actiongroup)
580-
group = None
581-
current_page = self.notebook.get_current_page()
582-
for i in range(self.notebook.get_n_pages()):
583-
page = self.notebook.get_nth_page(i)
584-
label = self.notebook.get_menu_label_text(page) or ""
585-
label = label.replace("_", "__")
586-
name = "SwitchTab%d" % i
587-
tooltip = _("Switch to this tab")
588-
action = Gtk.RadioAction(
589-
name=name, label=label, tooltip=tooltip,
590-
stock_id=None, value=i)
591-
action.join_group(group)
592-
group = action
593-
action.set_active(current_page == i)
594-
595-
def current_tab_changed_cb(action, current):
596-
if action == current:
597-
self.notebook.set_current_page(action.get_current_value())
598-
action.connect("changed", current_tab_changed_cb)
599-
if i < 10:
600-
accel = "<Alt>%d" % ((i + 1) % 10)
601-
else:
602-
accel = None
603-
self.tab_switch_actiongroup.add_action_with_accel(action, accel)
604-
self.ui.add_ui(self.tab_switch_merge_id,
605-
"/Menubar/TabMenu/TabPlaceholder",
606-
name, name, Gtk.UIManagerItemType.MENUITEM, False)
607-
608552
def page_removed(self, page, status):
609553
if hasattr(page, 'scheduler'):
610554
self.scheduler.remove_scheduler(page.scheduler)

meld/ui/gladesupport.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
from meld.ui import emblemcellrenderer
88
from meld.ui import historyentry
99
from meld.ui import msgarea
10+
from meld.ui import notebook
1011
from meld.ui import statusbar

meld/ui/notebook.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (C) 2015 Kai Willadsen <kai.willadsen@gmail.com>
2+
#
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 2 of the License, or (at
6+
# your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful, but
9+
# WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
# General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
from gi.repository import Gdk
17+
from gi.repository import GObject
18+
from gi.repository import Gtk
19+
20+
21+
class MeldNotebook(Gtk.Notebook):
22+
23+
__gtype_name__ = "MeldNotebook"
24+
25+
__gsignals__ = {
26+
'tab-switch': (GObject.SignalFlags.ACTION, None, (int,)),
27+
}
28+
29+
css = """
30+
@binding-set TabSwitchBindings {}
31+
MeldNotebook { gtk-key-bindings: TabSwitchBindings; }
32+
"""
33+
34+
def __init__(self, *args, **kwargs):
35+
Gtk.Notebook.__init__(self, *args, **kwargs)
36+
37+
provider = Gtk.CssProvider()
38+
provider.load_from_data(self.css)
39+
Gtk.StyleContext.add_provider_for_screen(
40+
Gdk.Screen.get_default(), provider,
41+
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
42+
43+
# Awful hacks because we can't create GtkBindingArg from Python, or
44+
# create a BindingSet from Python, or get a set by class from Python.
45+
bindings = Gtk.BindingSet.find('TabSwitchBindings')
46+
for i in range(10):
47+
key = (i + 1) % 10
48+
Gtk.BindingEntry().add_signal_from_string(
49+
bindings, 'bind "<Alt>%d" { "tab-switch" (%d) };' % (key, i))
50+
self.connect('tab-switch', self.do_tab_switch)
51+
52+
def do_tab_switch(self, notebook, page_num):
53+
notebook.set_current_page(page_num)

0 commit comments

Comments
 (0)