import gi
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import Gtk, WebKit2

class ImprovedBrowser(Gtk.Window):
    def __init__(self):
        super(ImprovedBrowser, self).__init__()

        self.set_title("Improved Web Browser")
        self.set_default_size(1200, 800)

        # Create a vertical box to hold the components
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.add(vbox)

        # Create the web view
        self.webview = WebKit2.WebView()
        self.webview.connect("load_changed", self.on_load_changed)
        vbox.pack_start(self.webview, True, True, 0)

        # Create a toolbar for navigation
        toolbar = Gtk.Toolbar()
        vbox.pack_start(toolbar, False, False, 0)

        # Add navigation buttons
        self.create_toolbar_button("go-back", self.go_back, toolbar, 0)
        self.create_toolbar_button("go-forward", self.go_forward, toolbar, 1)
        self.create_toolbar_button("process-stop", self.stop_loading, toolbar, 2)
        self.create_toolbar_button("view-refresh", self.reload_page, toolbar, 3)

        # URL entry field
        self.url_entry = Gtk.Entry()
        self.url_entry.set_placeholder_text("Enter URL...")
        self.url_entry.connect("activate", self.load_url)

        # Create a ToolItem for the URL entry and make it expand
        url_item = Gtk.ToolItem()
        url_item.add(self.url_entry)
        url_item.set_expand(True)
        toolbar.insert(url_item, 4)

        # Status bar at the bottom
        self.status_bar = Gtk.Statusbar()
        vbox.pack_start(self.status_bar, False, False, 0)

        # Load a default URL
        self.load_url_default()

        self.show_all()

    def create_toolbar_button(self, icon_name, callback, toolbar, position):
        button = Gtk.ToolButton.new(None, None)
        button.set_icon_name(icon_name)
        button.connect("clicked", callback)
        toolbar.insert(button, position)
        
    def load_url_default(self):
        self.webview.load_uri("https://www.example.com")

    def load_url(self, entry):
        url = self.url_entry.get_text().strip()
        if not url.startswith(("http://", "https://")):
            url = "http://" + url
        try:
            self.webview.load_uri(url)
        except Exception as e:
            self.status_bar.push(self.status_bar.get_context_id("Error"), f"Failed to load URL: {e}")

    def go_back(self, widget):
        if self.webview.can_go_back():
            self.webview.go_back()

    def go_forward(self, widget):
        if self.webview.can_go_forward():
            self.webview.go_forward()

    def reload_page(self, widget):
        self.webview.reload()

    def stop_loading(self, widget):
        self.webview.stop_loading()

    def on_load_changed(self, webview, load_event):
        if load_event == WebKit2.LoadEvent.COMMITTED:
            current_url = webview.get_uri()
            self.url_entry.set_text(current_url)

        # Update status bar
        context_id = self.status_bar.get_context_id("Loading")
        if load_event == WebKit2.LoadEvent.STARTED:
            self.status_bar.push(context_id, "Loading...")
        elif load_event == WebKit2.LoadEvent.FINISHED:
            self.status_bar.pop(context_id)
            self.status_bar.push(self.status_bar.get_context_id("Loaded"), "Page Loaded")

if __name__ == "__main__":
    win = ImprovedBrowser()
    win.connect("destroy", Gtk.main_quit)
    Gtk.main()



add download manager