Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Tabs #7

Closed
wants to merge 9 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 87 additions & 41 deletions mybrowse
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ if len(args.url) > 0:
else:
starturl = startpage

class Tab(Gtk.VBox):
def __init__(self, *args, **kwargs):
super(Tab, self).__init__(*args, **kwargs)


self.context = WebKit2.WebContext.get_default()
self.context.set_web_extensions_directory(extension_dir)

self.view = WebKit2.WebView.new_with_context(self.context)

class Browser(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title='MyBrowse')
Expand All @@ -159,11 +169,6 @@ class Browser(Gtk.Window):
screen = Gdk.Screen.get_default()
style.add_provider_for_screen(screen, styleprovider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

self.context = WebKit2.WebContext.get_default()
self.context.set_web_extensions_directory(extension_dir)

self.view = WebKit2.WebView.new_with_context(self.context)

self.vbox = Gtk.Box(orientation=Gtk.STYLE_CLASS_VERTICAL)
self.vbox.expand = True
self.vbox.set_spacing(10)
Expand Down Expand Up @@ -191,6 +196,14 @@ class Browser(Gtk.Window):
self.bookmarker_symbol = Gtk.Image.new_from_icon_name('bookmark-new', Gtk.IconSize.SMALL_TOOLBAR)
self.bookmarker.add(self.bookmarker_symbol)
self.menu.add(self.bookmarker)
self.new_tab_button = Gtk.Button()
self.new_tab_symbol = Gtk.Image.new_from_icon_name('tab-new', Gtk.IconSize.SMALL_TOOLBAR)
self.new_tab_button.add(self.new_tab_symbol)
self.menu.add(self.new_tab_button)
self.close_tab_button = Gtk.Button()
self.close_tab_icon = Gtk.Image.new_from_icon_name('window-close', Gtk.IconSize.SMALL_TOOLBAR)
self.close_tab_button.add(self.close_tab_icon)
self.menu.add(self.close_tab_button)
self.searchbar = Gtk.SearchEntry()
self.searchbar.set_name('searchbar')
self.menu.add(self.searchbar)
Expand All @@ -201,27 +214,42 @@ class Browser(Gtk.Window):
self.home.connect("clicked", self.go_home)
self.searchbar.connect("activate", self.search)
self.bookmarker.connect("clicked", self.set_bookmark)
self.new_tab_button.connect("clicked", self.new_tab)
self.close_tab_button.connect("clicked", self.close_tab)
self.vbox.add(self.menu)

self.tabbar = Gtk.Notebook()
self.tabbar.set_scrollable(True)
self.vbox.add(self.tabbar)

global current_page
current_page = self.tabbar.get_current_page()

self.tabs = []
self.tabs.append((self.create_tab(), Gtk.Label('Neuer Tab')))
self.tabbar.insert_page(self.tabs[0][0], self.tabs[0][1], 0)

self.tabbar.connect("switch-page", self.change_title)

self.addressbar = Gtk.Entry()
self.addressbar.set_text(starturl)
self.addressbar.set_name('addressbar')
self.vbox.add(self.addressbar)

self.addressbar.connect("activate", self.change_url)

self.findcontroller = WebKit2.FindController(web_view=self.view)
self.findcontroller = WebKit2.FindController(web_view=self.tabs[current_page][0].view)

self.sw = Gtk.ScrolledWindow()
self.sw.add(self.view)
self.sw.add(self.tabs[current_page][0].view)

self.vbox.pack_start(self.sw, True, True, 0)
self.add(self.vbox)
self.view.load_uri(starturl)
self.view.connect("notify::uri", self.change_uri)
self.view.connect("notify::title", self.change_title)
self.view.connect("mouse-target-changed", self.link_hover)
self.view.connect("notify::estimated-load-progress", self.progressbar)
self.tabs[current_page][0].view.load_uri(starturl)
self.tabs[current_page][0].view.connect("notify::uri", self.change_uri)
self.tabs[current_page][0].view.connect("notify::title", self.change_title)
self.tabs[current_page][0].view.connect("mouse-target-changed", self.link_hover)
self.tabs[current_page][0].view.connect("notify::estimated-load-progress", self.progressbar)
self.connect("key-press-event", self.keybinding)

# Load configuration on startup
Expand All @@ -239,7 +267,7 @@ class Browser(Gtk.Window):
raise ValueError(f'Cannot convert string to bool: {str(string)}')

def configuration(self, *args):
settings = self.view.get_settings()
settings = self.tabs[current_page][0].view.get_settings()
try:
settings.set_property('enable-javascript',
self.str_to_bool(config['Browser']['js']))
Expand Down Expand Up @@ -275,7 +303,7 @@ class Browser(Gtk.Window):
with open(conf_file, 'w') as configfile:
config.write(configfile)
try:
self.view.set_zoom_level(float(config['General']['zoom']))
self.tabs[current_page][0].view.set_zoom_level(float(config['General']['zoom']))
except KeyError:
pass
try:
Expand All @@ -284,12 +312,12 @@ class Browser(Gtk.Window):
except KeyError:
pass
try:
self.context.set_preferred_languages(config['General']['language'])
self.tabs[current_page][0].context.set_preferred_languages(config['General']['language'])
except KeyError:
pass

# Cookie accept settings
self.cookie_manager = self.context.get_cookie_manager()
self.cookie_manager = self.tabs[current_page][0].context.get_cookie_manager()
try:
if config['Browser']['cookies'] == 'ALWAYS':
self.cookie_manager.set_accept_policy(WebKit2.CookieAcceptPolicy.ALWAYS)
Expand Down Expand Up @@ -320,26 +348,44 @@ class Browser(Gtk.Window):
except KeyError:
pass

def create_tab(self):
tab = Tab()
tab.view.connect("notify::title", self.change_title)
return tab

def close_tab(self, widget):
if self.tabbar.get_n_pages() != 1:
current = self.tabbar.get_current_page()
self.tabbar.remove(self.tabs.pop(current)[0])

def new_tab(self, widget):
current = self.tabbar.get_current_page()
page_tuple = (self.create_tab(), Gtk.Label(''))
self.tabs.insert(current_page + 1, page_tuple)
self.tabbar.insert_page(page_tuple[0], page_tuple[1], current + 1)
self.tabbar.set_current_page(current + 1)


def change_url(self, widget):
url = self.addressbar.get_text()
if url in (':back', ':undo', ':u'):
self.view.go_back()
self.tabs[current_page][0].view.go_back()
elif url in (':forward', ':redo', ':r'):
self.view.go_forward()
self.tabs[current_page][0].view.go_forward()
elif url in (':reload', ':rl'):
self.view.reload()
self.tabs[current_page][0].view.reload()
elif url.startswith(':/'):
self.search_page(url.split(':/', 1)[1])
else:
try:
self.view.load_uri(links(url))
self.tabs[current_page][0].view.load_uri(links(url))
except TypeError:
pass
self.configuration()


def change_title(self, widget, data, *arg):
title = widget.get_title()
title = self.tabs[current_page][0].view.get_title()
if title == '':
self.set_title('MyBrowse')
else:
Expand All @@ -348,7 +394,7 @@ class Browser(Gtk.Window):
def change_uri(self, widget, data, *arg):
uri = widget.get_uri()
try:
self.view.load_uri(links(uri))
self.tabs[current_page][0].view.load_uri(links(uri))
self.addressbar.set_text(uri)
except TypeError:
pass
Expand All @@ -357,26 +403,26 @@ class Browser(Gtk.Window):
history.close()

def go_back(self, widget):
self.view.go_back()
self.tabs[current_page][0].view.go_back()

def go_forward(self, widget):
self.view.go_forward()
self.tabs[current_page][0].view.go_forward()

def go_reload(self, widget):
self.view.reload()
self.tabs[current_page][0].view.reload()

def go_home(self, widget):
self.addressbar.set_text(startpage)
self.view.load_uri(startpage)
self.tabs[current_page][0].view.load_uri(startpage)

def search(self, searchbar):
searchstring = self.searchbar.get_text()
self.view.load_uri(f'{links(searchengine)}'
self.tabs[current_page][0].view.load_uri(f'{links(searchengine)}'
f'{GLib.uri_escape_string(searchstring,"", True)}')

def set_bookmark(self, widget):
url = self.addressbar.get_text()
title = self.view.get_title()
title = self.tabs[current_page][0].view.get_title()
bm_file_path = f'{conf_dir}bookmarks.html'
bm_file = open(bm_file_path, 'a+')
if open(bm_file_path, 'r').read() == "":
Expand All @@ -399,7 +445,7 @@ class Browser(Gtk.Window):
self.addressbar.set_progress_fraction(amount)
else:
self.addressbar.set_progress_fraction(0)
self.view.grab_focus()
self.tabs[current_page][0].view.grab_focus()

def search_page(self, *args):
keyword = args[0]
Expand All @@ -410,30 +456,30 @@ class Browser(Gtk.Window):
def keybinding(self, widget, event):
if (event.keyval == Gdk.keyval_from_name('d') and
event.get_state() & Gdk.ModifierType.CONTROL_MASK):
self.set_bookmark(self.view)
self.set_bookmark(self.tabs[current_page][0].view)
if (event.keyval == Gdk.keyval_from_name('r') and
event.get_state() & Gdk.ModifierType.CONTROL_MASK):
self.view.reload()
self.tabs[current_page][0].view.reload()
if event.keyval == Gdk.KEY_F5:
self.view.reload()
self.tabs[current_page][0].view.reload()
if (event.keyval == Gdk.KEY_F5 and
event.get_state() & Gdk.ModifierType.SHIFT_MASK):
self.view.reload_bypass_cache()
self.tabs[current_page][0].view.reload_bypass_cache()
if (event.keyval == Gdk.keyval_from_name('z') and
event.get_state() & Gdk.ModifierType.CONTROL_MASK):
self.view.go_back()
self.tabs[current_page][0].view.go_back()
if (event.keyval == Gdk.keyval_from_name('y') and
event.get_state() & Gdk.ModifierType.CONTROL_MASK):
self.view.go_forward()
self.tabs[current_page][0].view.go_forward()
if (event.keyval == Gdk.keyval_from_name('0') and
event.get_state() & Gdk.ModifierType.CONTROL_MASK):
self.view.set_zoom_level(1)
self.tabs[current_page][0].view.set_zoom_level(1)
if (event.keyval == Gdk.unicode_to_keyval(ord('+')) and
event.get_state() & Gdk.ModifierType.CONTROL_MASK):
self.view.set_zoom_level(self.view.get_zoom_level() + 0.1)
self.tabs[current_page][0].view.set_zoom_level(self.tabs[current_page][0].view.get_zoom_level() + 0.1)
if (event.keyval == Gdk.unicode_to_keyval(ord('-')) and
event.get_state() & Gdk.ModifierType.CONTROL_MASK):
self.view.set_zoom_level(self.view.get_zoom_level() - 0.1)
self.tabs[current_page][0].view.set_zoom_level(self.tabs[current_page][0].view.get_zoom_level() - 0.1)
if (event.keyval == Gdk.keyval_from_name('q') and
event.get_state() & Gdk.ModifierType.CONTROL_MASK):
Gtk.main_quit()
Expand All @@ -444,11 +490,11 @@ class Browser(Gtk.Window):
self.addressbar.get_text().startswith(':/')):
self.findcontroller.search_finish()
if (event.keyval == Gdk.KEY_Escape and
(self.addressbar.get_text() is not self.view.get_uri()
(self.addressbar.get_text() is not self.tabs[current_page][0].view.get_uri()
or self.addressbar.get_text().startswith(':/'))):
self.addressbar.set_text(self.view.get_uri())
self.addressbar.set_text(self.tabs[current_page][0].view.get_uri())
if (event.keyval == Gdk.KEY_Escape and
self.addressbar.get_text() == self.view.get_uri()):
self.addressbar.get_text() == self.tabs[current_page][0].view.get_uri()):
self.addressbar.grab_focus()
if (event.keyval == Gdk.keyval_from_name('f') and
event.get_state() & Gdk.ModifierType.CONTROL_MASK):
Expand Down