Moving settings out of global space; added editor commands and wired them

This commit is contained in:
2025-12-16 20:25:24 -06:00
parent ce22ed6a53
commit 6acbcb53c2
25 changed files with 288 additions and 53 deletions

View File

@@ -3,7 +3,7 @@
# Lib imports
# Application imports
from .commands import *
from . import commands
@@ -18,12 +18,17 @@ class CommandSystem:
self.data = (args, kwargs)
def exec(self, command: str):
if not command in globals(): return
if not hasattr(commands, command): return
method = getattr(commands, command)
# method = getattr(self, command, None)
method = globals()[command]
args, kwargs = self.data
if kwargs:
method.execute(*args, kwargs)
else:
method.execute(*args)
def exec_with_args(self, command: str, args: list):
if not hasattr(commands, command): return
method = getattr(commands, command)
method.execute(*args)

View File

@@ -2,11 +2,15 @@
Commands Package
"""
import os
import pkgutil
import importlib
__all__ = []
__all__ = [
command.replace(".py", "") for command in os.listdir(
os.path.dirname(__file__)
) if "__init__" not in command
]
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
module = importlib.import_module(f"{__name__}.{module_name}")
globals()[module_name] = module # Add module to package namespace
__all__.append(module_name)
del pkgutil
del importlib

View File

@@ -18,5 +18,6 @@ def execute(
file = editor.files.new()
buffer = editor.get_buffer()
editor.set_buffer(file.buffer)
editor.files.remove_file(buffer)
editor.command.exec("update_info_bar")

View File

@@ -17,3 +17,4 @@ def execute(
logger.debug("Focus Left Sibling Command")
if not editor.sibling_left: return
editor.sibling_left.grab_focus()
editor.sibling_left.command.exec("set_miniview")

View File

@@ -17,3 +17,4 @@ def execute(
logger.debug("Focus Right Sibling Command")
if not editor.sibling_right: return
editor.sibling_right.grab_focus()
editor.sibling_right.command.exec("set_miniview")

View File

@@ -0,0 +1,32 @@
# Python imports
# Lib imports
import gi
gi.require_version('GtkSource', '4')
from gi.repository import GtkSource
from gi.repository import Gio
# Application imports
from ..source_file import SourceFile
def execute(
editor: GtkSource.View,
gfile: Gio.File,
file: SourceFile = None,
):
logger.debug("Load File Command")
if not file:
file = editor.files.new()
file.load_path(gfile)
language = editor.language_manager \
.guess_language(file.fname, None)
file.ftype = language
file.buffer.set_language(language)
file.buffer.set_style_scheme(editor.syntax_theme)

View File

@@ -23,3 +23,4 @@ def execute(
file.buffer.set_style_scheme(editor.syntax_theme)
editor.set_buffer(file.buffer)
editor.exec_command("update_info_bar")

View File

@@ -21,15 +21,8 @@ def execute(
size = len(gfiles)
for i, gfile in enumerate(gfiles):
file = editor.files.new()
file.load_path(gfile)
language = editor.language_manager \
.guess_language(file.fname, None)
file.ftype = language
file.buffer.set_language(language)
file.buffer.set_style_scheme(editor.syntax_theme)
editor.command.exec_with_args("load_file", (editor, gfile, file))
if i == (size - 1):
editor.set_buffer(file.buffer)
editor.command.exec("update_info_bar")

View File

@@ -24,3 +24,5 @@ def execute(
.guess_language(file.fname, None)
file.ftype = language
file.buffer.set_language(language)
editor.exec_command("update_info_bar")

View File

@@ -0,0 +1,26 @@
# Python imports
# Lib imports
import gi
gi.require_version('GtkSource', '4')
from gi.repository import GtkSource
# Application imports
def execute(
editor: GtkSource.View = None
):
logger.debug("Set Focus Border Command")
ctx = editor.get_style_context()
ctx.add_class("source-view-focused")
if editor.sibling_right:
ctx = editor.sibling_right.get_style_context()
elif editor.sibling_left:
ctx = editor.sibling_left.get_style_context()
ctx.remove_class("source-view-focused")

View File

@@ -0,0 +1,21 @@
# Python imports
# Lib imports
import gi
gi.require_version('GtkSource', '4')
from gi.repository import GtkSource
from gi.repository import Gio
# Application imports
from ..source_file import SourceFile
def execute(
editor: GtkSource.View,
):
logger.debug("Set MiniView Command")
event_system.emit("set-mini-view", (editor,))

View File

@@ -0,0 +1,33 @@
# Python imports
# Lib imports
import gi
gi.require_version('GtkSource', '4')
from gi.repository import GtkSource
from gi.repository import Gio
# Application imports
from ..source_file import SourceFile
def execute(
editor: GtkSource.View,
):
logger.debug("Update Info Bar Command")
buffer = editor.get_buffer()
file = editor.files.get_file(buffer)
if not file: return
iter = buffer.get_iter_at_mark( buffer.get_insert() )
line = iter.get_line() + 1
column = iter.get_line_offset()
ftype = file.ftype.get_id() if hasattr(file.ftype, "get_id") else file.ftype
event_system.emit(
"set-info-labels",
(file.fpath, f"{line}:{column}", ftype, file.encoding)
)

View File

@@ -53,6 +53,7 @@ class CompletionManager():
def _start_completion(self):
"""
Note: Use IF NO providers have been added to completion...
print("here")
"""
self._completor.start(
[

View File

@@ -85,7 +85,8 @@ class KeyMapper:
keyname = keyname.replace("<Control>", "") \
.replace("<Shift>", "") \
.replace("<Alt>", "")
.replace("<Alt>", "") \
.lower()
getattr(self.states[state], press_state)[keyname] = command

View File

@@ -0,0 +1,3 @@
"""
Code Mixins Package
"""

View File

@@ -0,0 +1,50 @@
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
# Application imports
class SourceViewDnDMixin:
def _set_up_dnd(self):
PLAIN_TEXT_TARGET_TYPE = 70
URI_TARGET_TYPE = 80
text_target = Gtk.TargetEntry.new('text/plain', Gtk.TargetFlags(0), PLAIN_TEXT_TARGET_TYPE)
uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE)
targets = [ text_target, 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 == 70: return
if info == 80:
uris = data.get_uris()
buffer = self.get_buffer()
file = self.files.get_file(buffer)
if len(uris) == 0:
uris = data.get_text().split("\n")
if file.ftype == "buffer":
gfile = Gio.File.new_for_uri(uris[0])
self.command.exec_with_args(
"load_file",
(self, gfile, file)
)
self.command.exec("update_info_bar")
uris.pop(0)
for uri in uris:
try:
gfile = Gio.File.new_for_uri(uri)
except Exception as e:
gfile = Gio.File.new_for_path(uri)
self.command.exec_with_args("load_file", (self, gfile))

View File

@@ -42,7 +42,7 @@ class SourceFile(GtkSource.File):
if not gfile: return
self.set_location(gfile)
self.fpath = gfile.get_parent().get_path(),
self.fpath = gfile.get_path()
self.fname = gfile.get_basename()
def _set_signals(self):
@@ -93,7 +93,6 @@ class SourceFile(GtkSource.File):
self._write_file( self.get_location() )
def save_as(self):
print("poop")
file = event_system.emit_and_await("save-file-dialog")
if not file: return

View File

@@ -39,10 +39,14 @@ class SourceFilesManager(list):
popped_file = self.pop(i)
sibling_file = None
if len(self) == 0:
size = len(self)
if size == 0:
sibling_file = self.new()
else:
sibling_file = self[ i - 1 if i > 0 else i + 1]
new_i = 0 if size == 1 else i - 1 if i > 1 else i + 1
sibling_file = self[new_i]
return sibling_file, popped_file

View File

@@ -10,6 +10,8 @@ from gi.repository import GLib
from gi.repository import GtkSource
# Application imports
from .mixins.source_view_dnd_mixin import SourceViewDnDMixin
from .source_files_manager import SourceFilesManager
from .completion_manager import CompletionManager
from .command_system import CommandSystem
@@ -17,7 +19,7 @@ from .key_mapper import KeyMapper
class SourceView(GtkSource.View):
class SourceView(SourceViewDnDMixin, GtkSource.View):
def __init__(self):
super(SourceView, self).__init__()
@@ -36,6 +38,7 @@ class SourceView(GtkSource.View):
ctx.add_class("source-view")
self.set_vexpand(True)
self.set_bottom_margin(800)
self.set_show_line_marks(True)
self.set_show_line_numbers(True)
@@ -51,24 +54,20 @@ class SourceView(GtkSource.View):
self.set_highlight_current_line(True)
def _setup_signals(self):
# self.connect("show-completion", self._show_completion)
self.map_id = self.connect("map", self._init_map)
# self.connect("focus", self._on_widget_focus)
# self.connect("focus-in-event", self._focus_in_event)
# self.connect("drag-data-received", self._on_drag_data_received)
self.connect("drag-data-received", self._on_drag_data_received)
self.connect("focus-in-event", self._focus_in_event)
self.connect("key-press-event", self._key_press_event)
self.connect("key-release-event", self._key_release_event)
# self.connect("button-press-event", self._button_press_event)
# self.connect("button-release-event", self._button_release_event)
# self.connect("scroll-event", self._scroll_event)
self.connect("button-press-event", self._button_press_event)
self.connect("button-release-event", self._button_release_event)
def _subscribe_to_events(self):
...
def _load_widgets(self):
...
self._set_up_dnd()
def _init_map(self, view):
def _first_show_init():
@@ -89,19 +88,38 @@ class SourceView(GtkSource.View):
self.files = SourceFilesManager()
self.completion = CompletionManager()
self.command.set_data(self)
self.completion.set_completer( self.get_completion() )
self.style_scheme_manager.append_search_path(
f"{settings_manager.get_home_config_path()}/code_styles"
)
self.syntax_theme = self.style_scheme_manager.get_scheme(
f"{settings.theming.syntax_theme}"
f"{settings_manager.settings.theming.syntax_theme}"
)
self.command.set_data(self)
self.exec_command("new_file")
if self.sibling_right:
self.grab_focus()
def _focus_in_event(self, view, eve):
self.command.exec("set_miniview")
self.command.exec("set_focus_border")
self.command.exec("update_info_bar")
def _move_cursor(self, view, step, count, extend_selection):
self.command.exec("update_info_bar")
def _button_press_event(self, view, eve):
self.command.exec("update_info_bar")
def _button_release_event(self, view, eve):
self.command.exec("update_info_bar")
def _key_press_event(self, view, eve):
self.command.exec("update_info_bar")
command = self.key_mapper._key_press_event(eve)
if not command: return False
@@ -109,6 +127,8 @@ class SourceView(GtkSource.View):
return True
def _key_release_event(self, view, eve):
self.command.exec("update_info_bar")
command = self.key_mapper._key_release_event(eve)
if not command: return False