Factor the badge color and size data into fields - so it can be shared more easily

Yeah, this is skanky, but we're passing so many parameters its getting silly.
This commit is contained in:
Daniel Johnson 2023-07-09 06:59:59 -04:00
parent cc32b06bde
commit 8d26b0a3a1

View file

@ -100,6 +100,10 @@ class GridViewCellRendererImage(Gtk.CellRenderer):
self.cached_surfaces_loaded = 0
self.cycle_cache_idle_id = None
self.cached_surface_generation = 0
self.badge_size = 0, 0
self.badge_alpha = 0.6
self.badge_fore_color = 1, 1, 1
self.badge_back_color = 0, 0, 0
@GObject.Property(type=int, default=0)
def media_width(self):
@ -174,10 +178,12 @@ class GridViewCellRendererImage(Gtk.CellRenderer):
if surface:
x, y = self.get_media_position(surface, cell_area)
self.select_badge_metrics(surface)
if alpha >= 1:
self.render_media(cr, widget, surface, x, y)
self.render_platforms(cr, widget, surface, x, cell_area)
else:
cr.push_group()
self.render_media(cr, widget, surface, x, y)
@ -190,6 +196,30 @@ class GridViewCellRendererImage(Gtk.CellRenderer):
if not self.cycle_cache_idle_id:
self.cycle_cache_idle_id = GLib.idle_add(self.cycle_cache)
def select_badge_metrics(self, surface):
"""Updates fields holding data about the appearance of the badges;
this sets self.badge_size to None if no badges should be shown at all."""
def get_badge_icon_size():
"""Returns the size of the badge icons to render, or None to hide them. We check
width for the smallest size because Dolphin has very thin banners, but we only hide
badges for icons, not banners."""
if self.media_width < 64:
return None
if self.media_height < 128:
return 16, 16
if self.media_height < 256:
return 24, 24
return 32, 32
self.badge_size = get_badge_icon_size()
on_bright_surface = self.badge_size and GridViewCellRendererImage.is_bright_corner(surface, self.badge_size)
bright_color = 0.8, 0.8, 0.8
dark_color = 0.2, 0.2, 0.2
self.badge_fore_color = dark_color if on_bright_surface else bright_color
self.badge_back_color = bright_color if on_bright_surface else dark_color
@staticmethod
def is_bright_corner(surface, corner_size):
"""Tests several pixels near the corner of the surface where the badges
@ -242,18 +272,6 @@ class GridViewCellRendererImage(Gtk.CellRenderer):
y = round(cell_area.y + cell_area.height - height) # at bottom of cell
return x, y
def get_badge_icon_size(self):
"""Returns the size of the badge icons to render, or None to hide them. We check
width for the smallest size because Dolphin has very thin banners, but we only hide
badges for icons, not banners."""
if self.media_width < 64:
return None
if self.media_height < 128:
return 16, 16
if self.media_height < 256:
return 24, 24
return 32, 32
def render_media(self, cr, widget, surface, x, y):
"""Renders the media itself, given the surface containing it
and the position."""
@ -268,8 +286,7 @@ class GridViewCellRendererImage(Gtk.CellRenderer):
"""Renders the stack of platform icons. They appear lined up vertically to the
right of 'media_right', if that will fit in 'cell_area'."""
platform = self.platform
icon_size = self.get_badge_icon_size()
if platform and icon_size:
if platform and self.badge_size:
if "," in platform:
platforms = platform.split(",") # pylint:disable=no-member
else:
@ -278,29 +295,25 @@ class GridViewCellRendererImage(Gtk.CellRenderer):
icon_paths = [get_runtime_icon_path(p + "-symbolic") for p in platforms]
icon_paths = [path for path in icon_paths if path]
if icon_paths:
self.render_badge_stack(cr, widget, surface, surface_x, icon_paths, icon_size, cell_area)
self.render_badge_stack(cr, widget, surface, surface_x, icon_paths, cell_area)
def render_badge_stack(self, cr, widget, surface, surface_x, icon_paths, icon_size, cell_area):
def render_badge_stack(self, cr, widget, surface, surface_x, icon_paths, cell_area):
"""Renders a vertical stack of badges, placed at the edge of the media, off to the right
of 'media_right' if this will fit in the 'cell_area'. The icons in icon_paths are drawn from
top to bottom, and spaced to fit in 'cell_area', even if they overlap because of this."""
badge_width = icon_size[0]
badge_height = icon_size[1]
on_bright_surface = GridViewCellRendererImage.is_bright_corner(surface, (badge_width, badge_height))
alpha = 0.6
bright_color = 0.8, 0.8, 0.8
dark_color = 0.2, 0.2, 0.2
back_color = bright_color if on_bright_surface else dark_color
fore_color = dark_color if on_bright_surface else bright_color
badge_width = self.badge_size[0]
badge_height = self.badge_size[1]
alpha = self.badge_alpha
fore_color = self.badge_fore_color
back_color = self.badge_back_color
def render_badge(badge_x, badge_y, path):
cr.rectangle(badge_x, badge_y, icon_size[0], icon_size[0])
cr.rectangle(badge_x, badge_y, badge_width, badge_height)
cr.set_source_rgba(back_color[0], back_color[1], back_color[2], alpha)
cr.fill()
icon = self.get_cached_surface_by_path(widget, path, size=icon_size)
icon = self.get_cached_surface_by_path(widget, path, size=self.badge_size)
cr.set_source_rgba(fore_color[0], fore_color[1], fore_color[2], alpha)
cr.mask_surface(icon, badge_x, badge_y)