diff --git a/schermetto/num_display.py b/schermetto/num_display.py index ecfb56c..cd9c63e 100755 --- a/schermetto/num_display.py +++ b/schermetto/num_display.py @@ -11,7 +11,7 @@ gi.require_version("Gtk", "3.0") gi.require_version("PangoCairo", "1.0") gi.require_version("Gdk", "3.0") gi.require_version("GLib", "2.0") -from gi.repository import Gtk, Gio, cairo, Pango, Gdk, PangoCairo, GLib +from gi.repository import Gtk, Gio, cairo, Pango, Gdk, PangoCairo, GLib, GObject def get_parser(): @@ -59,6 +59,29 @@ class App(Gtk.Application): self.window = None self.cli_args = cli_args + self._text = "" + + self._stdin_buffer = "" + + if self.cli_args.stdin: + os.set_blocking(sys.stdin.fileno(), False) + GLib.io_add_watch(sys.stdin.fileno(), GLib.IO_IN, self._on_stdin_data) + + def _on_stdin_data(self, *args, **kwargs): + max_line_length = 16 + incoming_data = sys.stdin.read(max_line_length) + if not incoming_data: + return True + self._stdin_buffer += incoming_data + if len(self._stdin_buffer) == max_line_length: + self._stdin_buffer += '\n' + if "\n" in self._stdin_buffer: + parts = self._stdin_buffer.split("\n") + self._stdin_buffer = parts[-1] + self.text = parts[-2] + return True + + def do_startup(self): Gtk.Application.do_startup(self) @@ -80,6 +103,16 @@ class App(Gtk.Application): def on_quit(self, action, param): self.quit() + @GObject.Property(type=str, + default="", + flags=GObject.ParamFlags.READWRITE) + def text(self) -> str: + return self._text + + @text.setter + def text(self, new_value: str): + self._text = new_value + def get_color(description: str) -> Gdk.RGBA: rgba = Gdk.RGBA() @@ -93,8 +126,6 @@ class AppWindow(Gtk.ApplicationWindow): def __init__(self, *args, **kwargs): self.app = kwargs["application"] super().__init__(*args, **kwargs) - self.text = "" - self.buffer = "" self.rotation = 0 self.invert_colors = False self.drawing_area = Gtk.DrawingArea() @@ -106,31 +137,16 @@ class AppWindow(Gtk.ApplicationWindow): if self.app.cli_args.fullscreen: self.fullscreen() self.show_all() - if self.app.cli_args.stdin: - os.set_blocking(sys.stdin.fileno(), False) - GLib.io_add_watch(sys.stdin.fileno(), GLib.IO_IN, self._on_stdin_data) + if self.app.cli_args.invert_every > 0: GLib.timeout_add(self.app.cli_args.invert_every * 1000, self.swap_colors) + self.app.connect("notify::text", self.on_text_changed) + def force_redraw(self): self.drawing_area.queue_draw() - def _on_stdin_data(self, *args, **kwargs): - max_line_length = 16 - incoming_data = sys.stdin.read(max_line_length) - if not incoming_data: - return True - self.buffer += incoming_data - if len(self.buffer) == max_line_length: - self.buffer += '\n' - if "\n" in self.buffer: - parts = self.buffer.split("\n") - self.buffer = parts[-1] - self.change_text(parts[-2]) - return True - - def change_text(self, text: str): - self.text = text + def on_text_changed(self, app, text: str): self.force_redraw() def swap_colors(self): @@ -165,7 +181,7 @@ class AppWindow(Gtk.ApplicationWindow): draw = self.drawing_area Gdk.cairo_set_source_rgba(cr, self.bg) cr.paint() - layout = draw.create_pango_layout(self.text) + layout = draw.create_pango_layout(self.app.text) layout.set_font_description(self.font) layout.set_alignment(Pango.Alignment.CENTER) w1, h1 = layout.get_pixel_size()