From c96b9dbfda5346610ef65916f46115440e0b56bb Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sat, 18 Mar 2023 05:00:35 -0500 Subject: [PATCH] Added DnD file loading, added button images --- src/core/widgets/base/banner_controls.py | 2 +- src/core/widgets/base/editer_notebook.py | 9 ++- src/core/widgets/base/source_view.py | 60 ++++++++++++++++++- src/core/widgets/base/sourceview_container.py | 2 + .../widgets/controls/scale_down_button.py | 6 +- src/core/widgets/controls/scale_up_button.py | 6 +- src/core/widgets/controls/theme_button.py | 3 + .../widgets/controls/toggle_line_highlight.py | 8 ++- src/core/widgets/tab_header_widget.py | 14 ++--- 9 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src/core/widgets/base/banner_controls.py b/src/core/widgets/base/banner_controls.py index bd2f0e1..29f03ed 100644 --- a/src/core/widgets/base/banner_controls.py +++ b/src/core/widgets/base/banner_controls.py @@ -33,6 +33,6 @@ class BannerControls(Gtk.Box): def _load_widgets(self): self.add(ToggleLineHighlight()) - self.add(ScaleUpButton()) self.add(ScaleDownButton()) + self.add(ScaleUpButton()) self.pack_end(ThemeButton(), False, False, 0) diff --git a/src/core/widgets/base/editer_notebook.py b/src/core/widgets/base/editer_notebook.py index f283669..9c8572c 100644 --- a/src/core/widgets/base/editer_notebook.py +++ b/src/core/widgets/base/editer_notebook.py @@ -33,6 +33,7 @@ class EditorNotebook(Gtk.Notebook): def _subscribe_to_events(self): # event_system.subscribe("set_buffer_language", self.action_controller, *("set_buffer_language",)) + event_system.subscribe("create_view", self.create_view) event_system.subscribe("set_buffer_style", self.action_controller) event_system.subscribe("set_buffer_language", self.action_controller) event_system.subscribe("set_buffer_style", self.action_controller) @@ -67,7 +68,7 @@ class EditorNotebook(Gtk.Notebook): def _load_widgets(self): self.create_view() - def create_view(self, widget = None, eve = None): + def create_view(self, widget = None, eve = None, gfile = None): container = SourceViewContainer(self.close_tab) index = self.append_page(container, container.get_tab_widget()) @@ -76,8 +77,12 @@ class EditorNotebook(Gtk.Notebook): ctx = self.get_style_context() ctx.add_class("notebook-unselected-focus") self.set_tab_reorderable(container, True) - self.show_all() + if gfile: + source_view = container.get_source_view() + source_view.open_file(gfile) + + self.show_all() self.set_current_page(index) def close_tab(self, button, scroll_view, source_view, eve = None): diff --git a/src/core/widgets/base/source_view.py b/src/core/widgets/base/source_view.py index 8e18147..688bcff 100644 --- a/src/core/widgets/base/source_view.py +++ b/src/core/widgets/base/source_view.py @@ -2,9 +2,13 @@ # Lib imports import gi +gi.require_version('Gtk', '3.0') gi.require_version('GtkSource', '4') -from gi.repository import GtkSource +gi.require_version('Gdk', '3.0') +from gi.repository import Gtk +from gi.repository import Gdk from gi.repository import Gio +from gi.repository import GtkSource # Application imports @@ -16,13 +20,16 @@ class SourceView(GtkSource.View): self._language_manager = GtkSource.LanguageManager() self._style_scheme_manager = GtkSource.StyleSchemeManager() + self._general_style_tag = None self._file_watcher = None + self._is_changed = False self._buffer = self.get_buffer() self._setup_styling() self._setup_signals() + self._set_up_dnd() self._subscribe_to_events() self._load_widgets() @@ -47,6 +54,7 @@ class SourceView(GtkSource.View): self.set_vexpand(True) def _setup_signals(self): + self.connect("drag-data-received", self._on_drag_data_received) ... def _subscribe_to_events(self): @@ -118,3 +126,53 @@ class SourceView(GtkSource.View): tag.set_property('scale', tag.get_property('scale') - scale_step) self._buffer.apply_tag(tag, start_itr, end_itr) + + + def _set_up_dnd(self): + URI_TARGET_TYPE = 80 + uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE) + targets = [ uri_target ] + self.drag_dest_set_target_list(targets) + + def _on_drag_data_received(self, widget, drag_context, x, y, data, info, time): + if info == 80: + uris = data.get_uris() + + if len(uris) == 0: + uris = data.get_text().split("\n") + + if self._is_changed: + # self.maybe_saved() + ... + + gfile = Gio.File.new_for_uri(uris[0]) + self.open_file(gfile) + + uris.pop(0) + for uri in uris: + gfile = Gio.File.new_for_uri(uri) + event_system.emit('create_view', (None, None, gfile,)) + + + def open_file(self, gfile, *args): + info = gfile.query_info("standard::content-type", 0, cancellable=None) + content_type = info.get_content_type() + lm = self._language_manager.guess_language(None, content_type) + + if settings.is_debug(): + logger.debug(f"Detected Content Type: {content_type}") + + with open(gfile.get_path(), 'r') as f: + data = f.read() + self._buffer.set_text(data) + self.set_buffer_language( lm.get_id() ) + + # self.current_file = myfile + # self.current_filename = myfile.rpartition("/")[2] + # self.current_folder = path.dirname(myfile) + f.close() + # self.headerbar.set_subtitle(myfile) + # self.status_label.set_text(f"'{myfile}' loaded") + # self.headerbar.set_title("TextEdit") + self.grab_focus() + # self.is_changed = False diff --git a/src/core/widgets/base/sourceview_container.py b/src/core/widgets/base/sourceview_container.py index 73a4031..ee9e2c3 100644 --- a/src/core/widgets/base/sourceview_container.py +++ b/src/core/widgets/base/sourceview_container.py @@ -11,6 +11,8 @@ from .source_view import SourceView +# NOTE: https://github.com/Axel-Erfurt/TextEdit/tree/b65f09be945196eb05bef83d81a6abcd129b4eb0 + class SourceViewContainer(Gtk.ScrolledWindow): def __init__(self, close_tab): super(SourceViewContainer, self).__init__() diff --git a/src/core/widgets/controls/scale_down_button.py b/src/core/widgets/controls/scale_down_button.py index 672d1a1..b734769 100644 --- a/src/core/widgets/controls/scale_down_button.py +++ b/src/core/widgets/controls/scale_down_button.py @@ -20,7 +20,11 @@ class ScaleDownButton(Gtk.Button): def _setup_styling(self): - self.set_label("Zoom Out (-)") + self.set_label("Zoom") + self.set_image( Gtk.Image.new_from_icon_name("gtk-remove", 4) ) + self.set_always_show_image(True) + self.set_image_position(1) # Left - 0, Right = 1 + self.set_hexpand(False) def _setup_signals(self): self.connect("released", self._emit_scale_eve) diff --git a/src/core/widgets/controls/scale_up_button.py b/src/core/widgets/controls/scale_up_button.py index 67e2b83..0ae383f 100644 --- a/src/core/widgets/controls/scale_up_button.py +++ b/src/core/widgets/controls/scale_up_button.py @@ -20,7 +20,11 @@ class ScaleUpButton(Gtk.Button): def _setup_styling(self): - self.set_label("Zoom In (+)") + self.set_label("Zoom") + self.set_image( Gtk.Image.new_from_icon_name("gtk-add", 4) ) + self.set_always_show_image(True) + self.set_image_position(1) # Left - 0, Right = 1 + self.set_hexpand(False) def _setup_signals(self): self.connect("released", self._emit_scale_eve) diff --git a/src/core/widgets/controls/theme_button.py b/src/core/widgets/controls/theme_button.py index 293c714..70b22de 100644 --- a/src/core/widgets/controls/theme_button.py +++ b/src/core/widgets/controls/theme_button.py @@ -62,6 +62,9 @@ class ThemeButton(Gtk.Button): def _setup_styling(self): self.set_label("Themes") + self.set_image( Gtk.Image.new_from_icon_name("gtk-page-setup", 4) ) + self.set_always_show_image(True) + self.set_image_position(1) # Left - 0, Right = 1 def _setup_signals(self): self.connect("clicked", self._show_popover) diff --git a/src/core/widgets/controls/toggle_line_highlight.py b/src/core/widgets/controls/toggle_line_highlight.py index a6033cb..cc2339e 100644 --- a/src/core/widgets/controls/toggle_line_highlight.py +++ b/src/core/widgets/controls/toggle_line_highlight.py @@ -9,7 +9,7 @@ from gi.repository import Gtk -class ToggleLineHighlight(Gtk.ToggleButton): +class ToggleLineHighlight(Gtk.Button): def __init__(self): super(ToggleLineHighlight, self).__init__() @@ -20,7 +20,11 @@ class ToggleLineHighlight(Gtk.ToggleButton): def _setup_styling(self): - self.set_label("Toggle Line Highlight") + self.set_label("Line Highlight") + # self.set_image( Gtk.Image.new_from_icon_name("gtk-add", 4) ) + self.set_always_show_image(True) + self.set_image_position(1) # Left - 0, Right = 1 + self.set_hexpand(False) def _setup_signals(self): self.connect("released", self._emit_toggle_eve) diff --git a/src/core/widgets/tab_header_widget.py b/src/core/widgets/tab_header_widget.py index ad58854..274cbf8 100644 --- a/src/core/widgets/tab_header_widget.py +++ b/src/core/widgets/tab_header_widget.py @@ -13,20 +13,17 @@ class TabHeaderWidget(Gtk.ButtonBox): """docstring for TabHeaderWidget""" ccount = 0 - def __new__(cls, *args, **kwargs): obj = super(TabHeaderWidget, cls).__new__(cls) cls.ccount += 1 - return obj - # def __init__(self, tab, close_tab): def __init__(self, scroll_view, source_view, close_tab): super(TabHeaderWidget, self).__init__() - self.INDEX = self.ccount - self.NAME = f"tab_{self.INDEX}" + self.INDEX = self.ccount + self.NAME = f"tab_{self.INDEX}" self._scroll_view = scroll_view self._source_view = source_view self._close_tab = close_tab # NOTE: Close method in tab_mixin @@ -47,13 +44,16 @@ class TabHeaderWidget(Gtk.ButtonBox): close = Gtk.Button() icon = Gtk.Image(stock=Gtk.STOCK_CLOSE) - label.set_label(f"{self.NAME}") + label.set_label("untitled") label.set_width_chars(len(self.NAME)) label.set_xalign(0.0) + close.set_always_show_image(True) + close.set_hexpand(False) + close.set_size_request(32, 32) + close.set_image( Gtk.Image.new_from_icon_name("gtk-close", 4) ) close.connect("released", self._close_tab, *(self._scroll_view, self._source_view,)) - close.add(icon) self.add(label) self.add(close)