Commit 0a2f302f authored by Cédric Bellegarde's avatar Cédric Bellegarde

Rework logical, move view management to container

parent 05d0f70e
......@@ -12,6 +12,8 @@
from gi.repository import Gtk, GLib
from gettext import gettext as _
from eolie.view import View
from eolie.popover_webview import WebViewPopover
from eolie.pages_manager import PagesManager
......@@ -139,6 +141,8 @@ class Container(Gtk.Overlay):
window.show()
window.remove(view)
view.set_size_request(-1, -1)
# Needed to force view to resize
view.queue_draw()
self.__stack.add(view)
window.destroy()
# Do not count container views as destroy may be pending on somes
......@@ -203,6 +207,76 @@ class Container(Gtk.Overlay):
self.__window.container.pages_manager.set_filter("")
child.set_filtered(False)
def try_close_view(self, view):
"""
Ask user before closing view if forms filled
@param view as View
"""
page_id = view.webview.get_page_id()
El().helper.call("FormsFilled",
GLib.Variant("(i)", (page_id,)),
self.__on_forms_filled, page_id, view)
def close_view(self, view):
"""
close current view
@param view as View
@param animate as bool
"""
# Get children less view
children = self.__stack.get_children()
children.remove(view)
reversed_children = list(reversed(children))
children_count = len(children)
El().history.set_page_state(view.webview.get_uri())
self.__window.close_popovers()
# Needed to unfocus titlebar
self.__window.set_focus(None)
was_current = view == self.__window.container.current
gtime = view.webview.gtime
El().pages_menu.add_action(view.webview.get_title(),
view.webview.get_uri(),
view.webview.ephemeral,
view.webview.get_session_state())
self.__sites_manager.remove_view(view)
view.destroy()
# Don't show 0 as we are going to open a new one
if children_count:
El().update_unity_badge()
self.__window.toolbar.actions.count_label.set_text(
str(children_count))
# Nothing to do if was not current page
if not was_current:
return False
next_view = None
# First we search a brother ie a paged opened from the same parent page
for view in reversed_children:
if view.webview.gtime == gtime:
next_view = view
break
# Get view with gtime -+ 1
# If closing a parent, go to child
# If closing a child, go to parent
if next_view is None:
for view in reversed_children:
if view.webview.gtime == gtime + 1 or\
view.webview.gtime == gtime - 1:
next_view = view
break
# Get view with higher access time
if next_view is None:
atime = 0
for view in reversed_children:
if view.webview.atime > atime:
next_view = view
atime = view.webview.atime
if next_view is not None:
self.__window.container.set_current(next_view, True)
else:
# We are last row, add a new one
self.__window.container.add_webview(El().start_page,
WindowType.FOREGROUND)
@property
def pages_manager(self):
"""
......@@ -269,3 +343,41 @@ class Container(Gtk.Overlay):
self.__sites_manager.show()
else:
self.__sites_manager.hide()
def __on_forms_filled(self, source, result, view):
"""
Ask user to close view, if ok, close view
@param source as GObject.Object
@param result as Gio.AsyncResult
@param view as View
"""
def on_response_id(dialog, response_id, view, self):
if response_id == Gtk.ResponseType.CLOSE:
self.close_view(view)
dialog.destroy()
def on_close(widget, dialog):
dialog.response(Gtk.ResponseType.CLOSE)
def on_cancel(widget, dialog):
dialog.response(Gtk.ResponseType.CANCEL)
try:
result = source.call_finish(result)[0]
if result:
builder = Gtk.Builder()
builder.add_from_resource("/org/gnome/Eolie/QuitDialog.ui")
dialog = builder.get_object("dialog")
label = builder.get_object("label")
close = builder.get_object("close")
cancel = builder.get_object("cancel")
label.set_text(_("Do you really want to close this page?"))
dialog.set_transient_for(self.__window)
dialog.connect("response", on_response_id, view, self)
close.connect("clicked", on_close, dialog)
cancel.connect("clicked", on_cancel, dialog)
dialog.run()
else:
self.close_view(view)
except:
self.close_view(view)
......@@ -12,8 +12,6 @@
from gi.repository import Gtk, Gdk, GLib
from gettext import gettext as _
from eolie.pages_manager_child import PagesManagerChild
from eolie.define import El, WindowType
......@@ -149,77 +147,6 @@ class PagesManager(Gtk.EventBox):
else:
self.__previous()
def try_close_view(self, view):
"""
Ask user before closing view if forms filled
@param view as View
"""
page_id = view.webview.get_page_id()
El().helper.call("FormsFilled",
GLib.Variant("(i)", (page_id,)),
self.__on_forms_filled, page_id, view)
def close_view(self, view):
"""
close current view
@param view as View
@param animate as bool
"""
El().history.set_page_state(view.webview.get_uri())
self.__window.close_popovers()
# Needed to unfocus titlebar
self.__window.set_focus(None)
was_current = view == self.__window.container.current
child_index = self.__get_index(view)
child = self.__box.get_child_at_index(child_index)
if child is None:
return
gtime = child.view.webview.gtime
El().pages_menu.add_action(view.webview.get_title(),
view.webview.get_uri(),
view.webview.ephemeral,
view.webview.get_session_state())
child.destroy()
children_count = len(self.__box.get_children())
# Don't show 0 as we are going to open a new one
if children_count:
El().update_unity_badge()
self.__window.toolbar.actions.count_label.set_text(
str(children_count))
# Nothing to do if was not current page
if not was_current:
return False
next_view = None
reversed_children = reversed(self.__box.get_children())
# First we search a brother ie a paged opened from the same parent page
for child in reversed_children:
if child.view.webview.gtime == gtime:
next_view = child.view
break
# Get view with gtime -+ 1
# If closing a parent, go to child
# If closing a child, go to parent
if next_view is None:
for child in reversed_children:
if child.view.webview.gtime == gtime + 1 or\
child.view.webview.gtime == gtime - 1:
next_view = child.view
break
# Get view with higher access time
if next_view is None:
atime = 0
for child in self.__box.get_children():
if child.view.webview.atime > atime:
next_view = child.view
atime = child.view.webview.atime
if next_view is not None:
self.__window.container.set_current(next_view, True)
else:
# We are last row, add a new one
self.__window.container.add_webview(El().start_page,
WindowType.FOREGROUND)
def ctrl_released(self):
"""
Disable any pending expose
......@@ -351,21 +278,6 @@ class PagesManager(Gtk.EventBox):
index += 1
return index
def __get_index_for_string(self, view_str):
"""
Get view index for str
@param view_str as str
@return int
"""
# Search current index
children = self.__box.get_children()
index = 0
for child in children:
if str(child.view) == view_str:
break
index += 1
return index
def __sort_func(self, row1, row2):
"""
Sort listbox
......@@ -397,44 +309,6 @@ class PagesManager(Gtk.EventBox):
"""
self.__box.invalidate_filter()
def __on_forms_filled(self, source, result, view):
"""
Ask user to close view, if ok, close view
@param source as GObject.Object
@param result as Gio.AsyncResult
@param view as View
"""
def on_response_id(dialog, response_id, view, self):
if response_id == Gtk.ResponseType.CLOSE:
self.close_view(view)
dialog.destroy()
def on_close(widget, dialog):
dialog.response(Gtk.ResponseType.CLOSE)
def on_cancel(widget, dialog):
dialog.response(Gtk.ResponseType.CANCEL)
try:
result = source.call_finish(result)[0]
if result:
builder = Gtk.Builder()
builder.add_from_resource("/org/gnome/Eolie/QuitDialog.ui")
dialog = builder.get_object("dialog")
label = builder.get_object("label")
close = builder.get_object("close")
cancel = builder.get_object("cancel")
label.set_text(_("Do you really want to close this page?"))
dialog.set_transient_for(self.__window)
dialog.connect("response", on_response_id, view, self)
close.connect("clicked", on_close, dialog)
cancel.connect("clicked", on_cancel, dialog)
dialog.run()
else:
self.close_view(view)
except:
self.close_view(view)
def __on_button_press(self, widget, event):
"""
Hide popover if visible
......
......@@ -72,37 +72,21 @@ class PagesManagerChild(Gtk.FlowBoxChild):
ArtSize.START_HEIGHT +
ArtSize.PREVIEW_WIDTH_MARGIN)
self.connect("query-tooltip", self.__on_query_tooltip)
self.connect("destroy", self.__on_destroy)
self.__view_destroy_id = view.connect("destroy",
self.__on_view_destroy)
self.__connected_ids.append(
self.__view.webview.connect(
"favicon-changed",
self.__on_webview_favicon_changed))
self.__connected_ids.append(
self.__view.webview.connect(
"notify::is-playing-audio",
self.__on_webview_notify_is_playing_audio))
self.__connected_ids.append(
self.__view.webview.connect(
"uri-changed",
self.__on_webview_uri_changed))
self.__connected_ids.append(
self.__view.webview.connect(
"title-changed",
self.__on_webview_title_changed))
self.__connected_ids.append(
self.__view.webview.connect(
"scroll-event",
self.__on_webview_scroll_event))
self.__connected_ids.append(
self.__view.webview.connect(
"load-changed",
self.__on_webview_load_changed))
self.__connected_ids.append(
self.__view.webview.connect(
"shown",
self.__on_webview_shown))
view.connect("destroying", self.__on_view_destroying)
self.__view.webview.connect("favicon-changed",
self.__on_webview_favicon_changed)
self.__view.webview.connect("notify::is-playing-audio",
self.__on_webview_notify_is_playing_audio)
self.__view.webview.connect("uri-changed",
self.__on_webview_uri_changed)
self.__view.webview.connect("title-changed",
self.__on_webview_title_changed)
self.__view.webview.connect("scroll-event",
self.__on_webview_scroll_event)
self.__view.webview.connect("load-changed",
self.__on_webview_load_changed)
self.__view.webview.connect("shown",
self.__on_webview_shown)
@property
def view(self):
......@@ -228,25 +212,11 @@ class PagesManagerChild(Gtk.FlowBoxChild):
GLib.markup_escape_text(uri))
widget.set_tooltip_markup(text)
def __on_destroy(self, widget):
def __on_view_destroying(self, view):
"""
Disconnect signals and destroy view
@param widget as Gtk.Widget
"""
while self.__connected_ids:
connected_id = self.__connected_ids.pop(0)
self.__view.webview.disconnect(connected_id)
if self.__view_destroy_id is not None:
self.__view.disconnect(self.__view_destroy_id)
self.__view.destroy()
def __on_view_destroy(self, view):
"""
Destroy self as view has been destroyed
Destroy self
@param view as View
"""
self.__connected_ids = []
self.__view_destroy_id = None
self.destroy()
def __on_webview_favicon_changed(self, webview, favicon,
......
......@@ -10,7 +10,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from gi.repository import Gtk, Gdk, GLib, Gio, WebKit2
from gi.repository import GObject, Gtk, Gdk, GLib, Gio, WebKit2
from eolie.widget_find import FindWidget
from eolie.webview import WebView
......@@ -23,6 +23,10 @@ class View(Gtk.Overlay):
An overlay with a webview and a find widget
"""
__gsignals__ = {
'destroying': (GObject.SignalFlags.RUN_FIRST, None, ())
}
def get_new_webview(ephemeral, window):
"""
Get a new webview
......@@ -119,7 +123,7 @@ class View(Gtk.Overlay):
"""
Delayed destroy
"""
self.__window.container.sites_manager.remove_view(self)
self.emit("destroying")
GLib.timeout_add(1000, self.__destroy)
@property
......
......@@ -423,7 +423,7 @@ class Window(Gtk.ApplicationWindow):
if self.is_fullscreen:
self.container.current.webview.emit("leave-fullscreen")
Gtk.ApplicationWindow.unfullscreen(self)
self.__container.pages_manager.try_close_view(
self.__container.try_close_view(
self.container.current)
elif string == "reload":
self.container.current.webview.reload()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment