changeset 659:8e7d4de56e75 frontends_multi_profiles

browser_side: allow to drop a widget in the "+" tab
author souliane <souliane@mailoo.org>
date Fri, 27 Feb 2015 16:05:28 +0100
parents 476d8d9973d3
children 267761bf7f08
files src/browser/libervia_main.py src/browser/sat_browser/libervia_widget.py
diffstat 2 files changed, 32 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/libervia_main.py	Fri Feb 27 02:42:44 2015 +0100
+++ b/src/browser/libervia_main.py	Fri Feb 27 16:05:28 2015 +0100
@@ -426,7 +426,7 @@
             # TODO: use the bare instead of node when all blogs can be retrieved
             node = jid.JID(data['public_blog']).node
             # FIXME: "/blog/{}" won't work with unicode nodes
-            self.displayWidget(widget.WebWidget, "/blog/{}".format(node), show_url=False, new_tab="{}'s blog".format(unicode(node)))
+            self.displayWidget(widget.WebWidget, "/blog/{}".format(node), show_url=False, new_tab=_(u"{}'s blog").format(unicode(node)))
         else:
             dialog.InfoDialog("Error",
                               "Unmanaged action result", Width="400px").center()
@@ -621,7 +621,7 @@
             kwargs['on_existing_widget'] = C.WIDGET_RECREATE
             wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
             self.tab_panel.addWidgetsTab(new_tab)
-            self.addWidget(wid)
+            self.addWidget(wid, tab_index=self.tab_panel.getWidgetCount() - 1)
             return wid
 
         kwargs['on_existing_widget'] = C.WIDGET_RAISE
--- a/src/browser/sat_browser/libervia_widget.py	Fri Feb 27 02:42:44 2015 +0100
+++ b/src/browser/sat_browser/libervia_widget.py	Fri Feb 27 16:05:28 2015 +0100
@@ -105,6 +105,7 @@
         DragLabel.onDragEnd(self, event)
         LiberviaDragWidget.current = None
 
+
 class DropCell(DropWidget):
     """Cell in the middle grid which replace itself with the dropped widget on DnD"""
     drop_keys = {}
@@ -253,7 +254,6 @@
         self.tab_panel = tab_panel
         self.setStyleName('dropCell')
         self.setWordWrap(False)
-        DOM.setStyleAttribute(self.getElement(), "min-width", "30px")
 
     def _getIndex(self):
         """ get current index of the DropTab """
@@ -276,7 +276,7 @@
         DOM.eventPreventDefault(event)
         self.parent.removeStyleName('dragover')
         if self._getIndex() == self.tab_panel.tabBar.getSelectedTab():
-            # the widget come from the DragTab, so nothing to do, we let it there
+            # the widget comes from the same tab, so nothing to do, we let it there
             return
 
         # FIXME: quite the same stuff as in DropCell, need some factorisation
@@ -293,19 +293,31 @@
             log.debug("no message found")
             item = '&nbsp;'
             item_type = None
+
         if item_type == "WIDGET":
             if not LiberviaDragWidget.current:
                 log.error("No widget registered in LiberviaDragWidget !")
                 return
             _new_panel = LiberviaDragWidget.current
-            _new_panel.getParent(WidgetsPanel, expect=True).removeWidget(_new_panel)
         elif item_type in DropCell.drop_keys:
-            _new_panel = DropCell.drop_keys[item_type](self.tab_panel.host, item)
+            pass  # create the widget when we are sure there's a tab for it
         else:
             log.warning("unmanaged item type")
             return
 
-        widgets_panel = self.tab_panel.getWidget(self._getIndex())
+        # XXX: when needed, new tab creation must be done exactly here to not mess up with LiberviaDragWidget.onDragEnd
+        try:
+            widgets_panel = self.tab_panel.getWidget(self._getIndex())
+        except IndexError:  # widgets panel doesn't exist, e.g. user dropped in "+" tab
+            widgets_panel = self.tab_panel.addWidgetsTab(None)
+            if widgets_panel is None:  # user cancelled
+                return
+
+        if item_type == "WIDGET":
+            _new_panel.getParent(WidgetsPanel, expect=True).removeWidget(_new_panel)
+        else:
+            _new_panel = DropCell.drop_keys[item_type](self.tab_panel.host, item)
+
         widgets_panel.addWidget(_new_panel)
 
 
@@ -749,7 +761,7 @@
         ClickHandler.__init__(self)
         self.host = host
         self.setStyleName('liberviaTabPanel')
-        self.tabBar.addTab(u'✚', asHTML=True)
+        self.tabBar.addTab(DropTab(self, u'✚'), asHTML=False)
         self.tabBar.setVisible(False)  # set to True when profile is logged
         self.tabBar.addStyleDependentName('oneTab')
 
@@ -758,14 +770,7 @@
             TabPanel.onTabSelected(self, sender, tabIndex)
             return
         # user clicked the "+" tab
-        default_label = _(u'new tab')
-        try:
-            label = Window.prompt(_(u'Name of the new tab'), default_label)
-            if not label:
-                return  # empty label or user pressed "cancel"
-        except:  # this happens when the user prevents the page to open the prompt dialog
-            label = default_label
-        self.addWidgetsTab(label, select=True)
+        self.addWidgetsTab(None, select=True)
 
     def getCurrentPanel(self):
         """ Get the panel of the currently selected tab
@@ -791,7 +796,7 @@
     def addWidgetsTab(self, label, select=False, locked=False):
         """Create a new tab for containing LiberviaWidgets.
 
-        @param label (unicode): label of the tab
+        @param label (unicode): label of the tab (None or '' for user prompt)
         @param select (bool): True to select the added tab
         @param locked (bool): If True, the tab will not be removed when there
             are no more widget inside. If False, the tab will be removed with
@@ -799,6 +804,16 @@
         @return: WidgetsPanel
         """
         widgets_panel = WidgetsPanel(self.host, locked=locked)
+
+        if not label:
+            default_label = _(u'new tab')
+            try:
+                label = Window.prompt(_(u'Name of the new tab'), default_label)
+                if not label:  # empty label or user pressed "cancel"
+                    return None
+            except:  # this happens when the user prevents the page to open the prompt dialog
+                label = default_label
+
         self.addTab(widgets_panel, label, select)
         return widgets_panel