Merge pull request #713 from Vulcalien/bg-image-draw-modes

Background image drawing modes and alignment
This commit is contained in:
Matt Rose 2023-03-01 16:13:23 -05:00 committed by GitHub
commit b0f4463055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 417 additions and 85 deletions

View File

@ -216,6 +216,9 @@ DEFAULTS = {
'background_darkness' : 0.5,
'background_type' : 'solid',
'background_image' : '',
'background_image_mode' : 'stretch_and_fill',
'background_image_align_horiz': 'center',
'background_image_align_vert' : 'middle',
'backspace_binding' : 'ascii-del',
'delete_binding' : 'escape-sequence',
'color_scheme' : 'grey_on_black',

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkListStore" id="BackspaceKeyListStore">
@ -154,6 +154,60 @@
</row>
</data>
</object>
<object class="GtkListStore" id="ImageAlignHorizListStore">
<columns>
<!-- column-name Alignment -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">Left</col>
</row>
<row>
<col id="0" translatable="yes">Center</col>
</row>
<row>
<col id="0" translatable="yes">Right</col>
</row>
</data>
</object>
<object class="GtkListStore" id="ImageAlignVertListStore">
<columns>
<!-- column-name Alignment -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">Top</col>
</row>
<row>
<col id="0" translatable="yes">Middle</col>
</row>
<row>
<col id="0" translatable="yes">Bottom</col>
</row>
</data>
</object>
<object class="GtkListStore" id="ImageDrawingModeListStore">
<columns>
<!-- column-name DrawingMode -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">Stretch and Fill</col>
</row>
<row>
<col id="0" translatable="yes">Scale and Fit</col>
</row>
<row>
<col id="0" translatable="yes">Scale and Crop</col>
</row>
<row>
<col id="0" translatable="yes">Tiling</col>
</row>
</data>
</object>
<object class="GtkListStore" id="KeybindingsListStore">
<columns>
<!-- column-name Name -->
@ -2659,6 +2713,16 @@
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkAlignment">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="left-padding">12</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
@ -2667,7 +2731,7 @@
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Background Image File:</property>
<property name="label" translatable="yes">Image File:</property>
</object>
<packing>
<property name="expand">False</property>
@ -2694,7 +2758,164 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkAccelLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Drawing mode:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">5</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="background_image_mode_combobox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="model">ImageDrawingModeListStore</property>
<property name="active">0</property>
<signal name="changed" handler="on_background_image_mode_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="spacing">12</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Horizontal alignment:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">5</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="background_image_align_horiz_combobox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="model">ImageAlignHorizListStore</property>
<property name="active">1</property>
<signal name="changed" handler="on_background_image_align_horiz_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Vertical alignment:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">5</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="background_image_align_vert_combobox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="model">ImageAlignVertListStore</property>
<property name="active">1</property>
<signal name="changed" handler="on_background_image_align_vert_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
@ -2783,7 +3004,7 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
<property name="position">6</property>
</packing>
</child>
</object>
@ -3721,12 +3942,12 @@
</child>
<child>
<object class="GtkVBox" id="vbox124">
<property name="can-focus">False</property>
<child>
<object class="GtkEntry" id="keybindingsearchentry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="has-focus">False</property>
<property name="placeholder_text">filter keybindings</property>
<property name="placeholder-text">filter keybindings</property>
</object>
<packing>
<property name="expand">False</property>
@ -3794,6 +4015,11 @@
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>

View File

@ -694,11 +694,42 @@ class PrefsEditor:
elif self.config['background_type'] == 'image':
guiget('image_radiobutton').set_active(True)
self.update_background_tab()
# Background image
widget = guiget('background_image_file')
widget.set_filename(self.config['background_image'])
widget = guiget('background_image_mode_combobox')
if self.config['background_image_mode'] == 'scale_and_fit':
widget.set_active(1)
elif self.config['background_image_mode'] == 'scale_and_crop':
widget.set_active(2)
elif self.config['background_image_mode'] == 'tiling':
widget.set_active(3)
else:
# default to stretch_and_fill
widget.set_active(0)
widget = guiget('background_image_align_horiz_combobox')
if self.config['background_image_align_horiz'] == 'center':
widget.set_active(1)
elif self.config['background_image_align_horiz'] == 'right':
widget.set_active(2)
else:
# default to left
widget.set_active(0)
widget = guiget('background_image_align_vert_combobox')
if self.config['background_image_align_vert'] == 'middle':
widget.set_active(1)
elif self.config['background_image_align_vert'] == 'bottom':
widget.set_active(2)
else:
# default to top
widget.set_active(0)
# Background shading
widget = guiget('background_darkness_scale')
widget.set_value(float(self.config['background_darkness']))
widget = guiget('background_image_file')
widget.set_filename(self.config['background_image'])
## Scrolling tab
# Scrollbar position
@ -999,6 +1030,41 @@ class PrefsEditor:
self.config['background_image'] = widget.get_filename()
self.config.save()
def on_background_image_mode_changed(self, widget):
selected = widget.get_active()
if selected == 1:
value = 'scale_and_fit'
elif selected == 2:
value = 'scale_and_crop'
elif selected == 3:
value = 'tiling'
else:
value = 'stretch_and_fill'
self.config['background_image_mode'] = value
self.config.save()
def on_background_image_align_horiz_changed(self, widget):
selected = widget.get_active()
if selected == 1:
value = 'center'
elif selected == 2:
value = 'right'
else:
value = 'left'
self.config['background_image_align_horiz'] = value
self.config.save()
def on_background_image_align_vert_changed(self, widget):
selected = widget.get_active()
if selected == 1:
value = 'middle'
elif selected == 2:
value = 'bottom'
else:
value = 'top'
self.config['background_image_align_vert'] = value
self.config.save()
def on_darken_background_scale_value_changed(self, widget):
"""Background darkness setting changed"""
value = widget.get_value() # This one is rounded according to the UI.
@ -1603,10 +1669,12 @@ class PrefsEditor:
self.config['background_type'] = backtype
self.config.save()
if backtype == 'image':
guiget('background_image_file').set_sensitive(True)
else:
guiget('background_image_file').set_sensitive(False)
# toggle sensitivity of widgets related to background image
for element in ('background_image_file',
'background_image_align_horiz_combobox',
'background_image_align_vert_combobox',
'background_image_mode_combobox'):
guiget(element).set_sensitive(backtype == 'image')
if backtype in ('transparent', 'image'):
guiget('darken_background_scale').set_sensitive(True)

View File

@ -1135,17 +1135,52 @@ class Terminal(Gtk.VBox):
# save cairo context
cr.save()
# draw background image
image_mode = self.config['background_image_mode']
image_align_horiz = self.config['background_image_align_horiz']
image_align_vert = self.config['background_image_align_vert']
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())
if image_mode == 'stretch_and_fill':
# keep stretched ratios
xratio = xratio
yratio = yratio
elif image_mode == 'scale_and_fit':
ratio = min(xratio, yratio)
xratio = yratio = ratio
elif image_mode == 'scale_and_crop':
ratio = max(xratio, yratio)
xratio = yratio = ratio
else:
xratio = yratio = 1
cr.scale(xratio, yratio)
cr.set_source_surface(self.background_image)
xoffset = 0
yoffset = 0
if image_align_horiz == 'center':
xoffset = (rect.width / xratio - self.background_image.get_width()) / 2
elif image_align_horiz == 'right':
xoffset = rect.width / xratio - self.background_image.get_width()
if image_align_vert == 'middle':
yoffset = (rect.height / yratio - self.background_image.get_height()) / 2
elif image_align_vert == 'bottom':
yoffset = rect.height / yratio - self.background_image.get_height()
cr.set_source_surface(self.background_image, xoffset, yoffset)
cr.get_source().set_filter(cairo.Filter.FAST)
if image_mode == 'tiling':
cr.get_source().set_extend(cairo.Extend.REPEAT)
cr.paint()
# draw transparent monochrome layer
Gdk.cairo_set_source_rgba(cr, self.bgcolor)
cr.paint()
# restore cairo context
cr.restore()