From 25902fd73e8002173e9d813185ff2294aa0482d7 Mon Sep 17 00:00:00 2001 From: Vulcalien Date: Wed, 8 Dec 2021 12:05:28 +0100 Subject: [PATCH 1/5] Only set background_image if background_type is 'image' + Fixed code duplication --- terminatorlib/terminal.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/terminatorlib/terminal.py b/terminatorlib/terminal.py index 6c224b94..58f964c8 100644 --- a/terminatorlib/terminal.py +++ b/terminatorlib/terminal.py @@ -140,18 +140,6 @@ def __init__(self): self.pending_on_vte_size_allocate = False self.vte = Vte.Terminal() - self.background_image = None - if self.config['background_image'] != '': - try: - self.background_image = GdkPixbuf.Pixbuf.new_from_file(self.config['background_image']) - self.vte.set_clear_background(False) - self.vte.connect("draw",self.background_draw) - except Exception as e: - self.background_image = None - self.vte.set_clear_background(True) - err('error loading background image: %s, %s' % (type(e).__name__,e)) - - self.background_alpha = self.config['background_darkness'] self.vte.set_allow_hyperlink(True) self.vte._draw_data = None if not hasattr(self.vte, "set_opacity") or \ @@ -161,7 +149,12 @@ def __init__(self): self.composite_support = True dbg('composite_support: %s' % self.composite_support) - + self.background_alpha = self.config['background_darkness'] + if self.config['background_type'] == 'image' and self.config['background_image'] != '': + self.set_background_image(self.config['background_image']) + else: + self.background_image = None + self.vte.show() self.update_url_matches() @@ -1135,7 +1128,7 @@ def on_drag_motion(self, widget, drag_context, x, y, _time, _data): widget._draw_data = None def background_draw(self, widget, cr): - if not self.config['background_type'] == 'image' or not self.background_image: + if self.background_image is None: return False rect = self.vte.get_allocation() From a06e7951f66175336872794fdb4f33a6ff804fb6 Mon Sep 17 00:00:00 2001 From: Vulcalien Date: Wed, 8 Dec 2021 16:05:38 +0100 Subject: [PATCH 2/5] Update background image without restarting --- terminatorlib/config.py | 3 +-- terminatorlib/terminal.py | 13 ++++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index b0888a71..4f7567ec 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -212,6 +212,7 @@ 'background_color' : '#000000', 'background_darkness' : 0.5, 'background_type' : 'solid', + 'background_image' : '', 'backspace_binding' : 'ascii-del', 'delete_binding' : 'escape-sequence', 'color_scheme' : 'grey_on_black', @@ -253,8 +254,6 @@ 'autoclean_groups' : True, 'http_proxy' : '', 'ignore_hosts' : ['localhost','127.0.0.0/8','*.local'], - 'background_image' : '', - 'background_alpha' : 0.0, # Titlebar 'title_hide_sizetext' : False, 'title_transmit_fg_color' : '#ffffff', diff --git a/terminatorlib/terminal.py b/terminatorlib/terminal.py index 58f964c8..2cd7079a 100644 --- a/terminatorlib/terminal.py +++ b/terminatorlib/terminal.py @@ -149,12 +149,6 @@ def __init__(self): self.composite_support = True dbg('composite_support: %s' % self.composite_support) - self.background_alpha = self.config['background_darkness'] - if self.config['background_type'] == 'image' and self.config['background_image'] != '': - self.set_background_image(self.config['background_image']) - else: - self.background_image = None - self.vte.show() self.update_url_matches() @@ -723,11 +717,16 @@ def reconfigure(self, _widget=None): self.bgcolor = Gdk.RGBA() self.bgcolor.parse(self.config['background_color']) - if self.config['background_type'] == 'transparent' or self.config['background_type'] == 'image': + if self.config['background_type'] in ('transparent', 'image'): self.bgcolor.alpha = self.config['background_darkness'] else: self.bgcolor.alpha = 1 + if self.config['background_type'] == 'image' and self.config['background_image'] != '': + self.set_background_image(self.config['background_image']) + else: + self.background_image = None + factor = self.config['inactive_color_offset'] if factor > 1.0: factor = 1.0 From 5a6237cc471b69367d6d31899ea2c19f42be2d6c Mon Sep 17 00:00:00 2001 From: Vulcalien Date: Thu, 9 Dec 2021 23:00:35 +0100 Subject: [PATCH 3/5] Performance: use Surface instead of PixBuf --- terminatorlib/terminal.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/terminatorlib/terminal.py b/terminatorlib/terminal.py index 2cd7079a..bbc842cf 100644 --- a/terminatorlib/terminal.py +++ b/terminatorlib/terminal.py @@ -189,7 +189,8 @@ def __init__(self): def set_background_image(self,image): try: - self.background_image = GdkPixbuf.Pixbuf.new_from_file(image) + bg_pixbuf = GdkPixbuf.Pixbuf.new_from_file(image) + self.background_image = Gdk.cairo_surface_create_from_pixbuf(bg_pixbuf, 1, None) self.vte.set_clear_background(False) self.vte.connect("draw",self.background_draw) except Exception as e: @@ -1135,7 +1136,7 @@ def background_draw(self, widget, cr): yratio = float(rect.height) / float(self.background_image.get_height()) cr.save() cr.scale(xratio,yratio) - Gdk.cairo_set_source_pixbuf(cr, self.background_image, 0, 0) + cr.set_source_surface(self.background_image) cr.paint() Gdk.cairo_set_source_rgba(cr,self.bgcolor) cr.paint() From 122b0fe3ae4de1525a394953f30af15735426a64 Mon Sep 17 00:00:00 2001 From: Vulcalien Date: Thu, 9 Dec 2021 23:01:58 +0100 Subject: [PATCH 4/5] background_draw: organize code --- terminatorlib/terminal.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/terminatorlib/terminal.py b/terminatorlib/terminal.py index bbc842cf..9ffeb980 100644 --- a/terminatorlib/terminal.py +++ b/terminatorlib/terminal.py @@ -192,7 +192,7 @@ def set_background_image(self,image): bg_pixbuf = GdkPixbuf.Pixbuf.new_from_file(image) self.background_image = Gdk.cairo_surface_create_from_pixbuf(bg_pixbuf, 1, None) self.vte.set_clear_background(False) - self.vte.connect("draw",self.background_draw) + self.vte.connect("draw", self.background_draw) except Exception as e: self.background_image = None self.vte.set_clear_background(True) @@ -1131,15 +1131,19 @@ def background_draw(self, widget, cr): if self.background_image is None: return False + # save cairo context + cr.save() + # draw background image rect = self.vte.get_allocation() xratio = float(rect.width) / float(self.background_image.get_width()) yratio = float(rect.height) / float(self.background_image.get_height()) - cr.save() - cr.scale(xratio,yratio) + cr.scale(xratio, yratio) cr.set_source_surface(self.background_image) cr.paint() - Gdk.cairo_set_source_rgba(cr,self.bgcolor) + # draw transparent monochrome layer + Gdk.cairo_set_source_rgba(cr, self.bgcolor) cr.paint() + # restore cairo context cr.restore() def on_draw(self, widget, context): From 1118fb0cfd0c9be8fdfa821c83d5dc6a2b1bd27f Mon Sep 17 00:00:00 2001 From: Vulcalien Date: Fri, 10 Dec 2021 00:35:34 +0100 Subject: [PATCH 5/5] Use FAST as filter for scaling background image --- terminatorlib/terminal.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/terminatorlib/terminal.py b/terminatorlib/terminal.py index 9ffeb980..016ff214 100644 --- a/terminatorlib/terminal.py +++ b/terminatorlib/terminal.py @@ -6,7 +6,7 @@ import os import signal import gi -from gi.repository import GLib, GObject, Pango, Gtk, Gdk, GdkPixbuf +from gi.repository import GLib, GObject, Pango, Gtk, Gdk, GdkPixbuf, cairo gi.require_version('Vte', '2.91') # vte-0.38 (gnome-3.14) from gi.repository import Vte import subprocess @@ -1139,6 +1139,7 @@ def background_draw(self, widget, cr): yratio = float(rect.height) / float(self.background_image.get_height()) cr.scale(xratio, yratio) cr.set_source_surface(self.background_image) + cr.get_source().set_filter(cairo.Filter.FAST) cr.paint() # draw transparent monochrome layer Gdk.cairo_set_source_rgba(cr, self.bgcolor)