Added DnD file loading, added button images

This commit is contained in:
itdominator 2023-03-18 05:00:35 -05:00
parent aed28a026c
commit c96b9dbfda
9 changed files with 95 additions and 15 deletions

View File

@ -33,6 +33,6 @@ class BannerControls(Gtk.Box):
def _load_widgets(self): def _load_widgets(self):
self.add(ToggleLineHighlight()) self.add(ToggleLineHighlight())
self.add(ScaleUpButton())
self.add(ScaleDownButton()) self.add(ScaleDownButton())
self.add(ScaleUpButton())
self.pack_end(ThemeButton(), False, False, 0) self.pack_end(ThemeButton(), False, False, 0)

View File

@ -33,6 +33,7 @@ class EditorNotebook(Gtk.Notebook):
def _subscribe_to_events(self): def _subscribe_to_events(self):
# event_system.subscribe("set_buffer_language", self.action_controller, *("set_buffer_language",)) # 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_style", self.action_controller)
event_system.subscribe("set_buffer_language", self.action_controller) event_system.subscribe("set_buffer_language", self.action_controller)
event_system.subscribe("set_buffer_style", self.action_controller) event_system.subscribe("set_buffer_style", self.action_controller)
@ -67,7 +68,7 @@ class EditorNotebook(Gtk.Notebook):
def _load_widgets(self): def _load_widgets(self):
self.create_view() 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) container = SourceViewContainer(self.close_tab)
index = self.append_page(container, container.get_tab_widget()) index = self.append_page(container, container.get_tab_widget())
@ -76,8 +77,12 @@ class EditorNotebook(Gtk.Notebook):
ctx = self.get_style_context() ctx = self.get_style_context()
ctx.add_class("notebook-unselected-focus") ctx.add_class("notebook-unselected-focus")
self.set_tab_reorderable(container, True) 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) self.set_current_page(index)
def close_tab(self, button, scroll_view, source_view, eve = None): def close_tab(self, button, scroll_view, source_view, eve = None):

View File

@ -2,9 +2,13 @@
# Lib imports # Lib imports
import gi import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GtkSource', '4') 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 Gio
from gi.repository import GtkSource
# Application imports # Application imports
@ -16,13 +20,16 @@ class SourceView(GtkSource.View):
self._language_manager = GtkSource.LanguageManager() self._language_manager = GtkSource.LanguageManager()
self._style_scheme_manager = GtkSource.StyleSchemeManager() self._style_scheme_manager = GtkSource.StyleSchemeManager()
self._general_style_tag = None self._general_style_tag = None
self._file_watcher = None self._file_watcher = None
self._is_changed = False
self._buffer = self.get_buffer() self._buffer = self.get_buffer()
self._setup_styling() self._setup_styling()
self._setup_signals() self._setup_signals()
self._set_up_dnd()
self._subscribe_to_events() self._subscribe_to_events()
self._load_widgets() self._load_widgets()
@ -47,6 +54,7 @@ class SourceView(GtkSource.View):
self.set_vexpand(True) self.set_vexpand(True)
def _setup_signals(self): def _setup_signals(self):
self.connect("drag-data-received", self._on_drag_data_received)
... ...
def _subscribe_to_events(self): def _subscribe_to_events(self):
@ -118,3 +126,53 @@ class SourceView(GtkSource.View):
tag.set_property('scale', tag.get_property('scale') - scale_step) tag.set_property('scale', tag.get_property('scale') - scale_step)
self._buffer.apply_tag(tag, start_itr, end_itr) 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

View File

@ -11,6 +11,8 @@ from .source_view import SourceView
# NOTE: https://github.com/Axel-Erfurt/TextEdit/tree/b65f09be945196eb05bef83d81a6abcd129b4eb0
class SourceViewContainer(Gtk.ScrolledWindow): class SourceViewContainer(Gtk.ScrolledWindow):
def __init__(self, close_tab): def __init__(self, close_tab):
super(SourceViewContainer, self).__init__() super(SourceViewContainer, self).__init__()

View File

@ -20,7 +20,11 @@ class ScaleDownButton(Gtk.Button):
def _setup_styling(self): 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): def _setup_signals(self):
self.connect("released", self._emit_scale_eve) self.connect("released", self._emit_scale_eve)

View File

@ -20,7 +20,11 @@ class ScaleUpButton(Gtk.Button):
def _setup_styling(self): 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): def _setup_signals(self):
self.connect("released", self._emit_scale_eve) self.connect("released", self._emit_scale_eve)

View File

@ -62,6 +62,9 @@ class ThemeButton(Gtk.Button):
def _setup_styling(self): def _setup_styling(self):
self.set_label("Themes") 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): def _setup_signals(self):
self.connect("clicked", self._show_popover) self.connect("clicked", self._show_popover)

View File

@ -9,7 +9,7 @@ from gi.repository import Gtk
class ToggleLineHighlight(Gtk.ToggleButton): class ToggleLineHighlight(Gtk.Button):
def __init__(self): def __init__(self):
super(ToggleLineHighlight, self).__init__() super(ToggleLineHighlight, self).__init__()
@ -20,7 +20,11 @@ class ToggleLineHighlight(Gtk.ToggleButton):
def _setup_styling(self): 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): def _setup_signals(self):
self.connect("released", self._emit_toggle_eve) self.connect("released", self._emit_toggle_eve)

View File

@ -13,15 +13,12 @@ class TabHeaderWidget(Gtk.ButtonBox):
"""docstring for TabHeaderWidget""" """docstring for TabHeaderWidget"""
ccount = 0 ccount = 0
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
obj = super(TabHeaderWidget, cls).__new__(cls) obj = super(TabHeaderWidget, cls).__new__(cls)
cls.ccount += 1 cls.ccount += 1
return obj return obj
# def __init__(self, tab, close_tab):
def __init__(self, scroll_view, source_view, close_tab): def __init__(self, scroll_view, source_view, close_tab):
super(TabHeaderWidget, self).__init__() super(TabHeaderWidget, self).__init__()
@ -47,13 +44,16 @@ class TabHeaderWidget(Gtk.ButtonBox):
close = Gtk.Button() close = Gtk.Button()
icon = Gtk.Image(stock=Gtk.STOCK_CLOSE) 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_width_chars(len(self.NAME))
label.set_xalign(0.0) 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.connect("released", self._close_tab, *(self._scroll_view, self._source_view,))
close.add(icon)
self.add(label) self.add(label)
self.add(close) self.add(close)