Skip to content

Commit 89c8daf

Browse files
committed
ChooserButtonWidgets: Fix button and menu images in hidpi.
gtk button: the button image was never being upscaled from its natural size, since the image was 35px and set_picture_from_file only scaled it if the image exceeded that size. icon button: Same as gtk - 48px was being requested and 48 was returned, but it was requesting an unscaled asset (not 48@2x). The cinnamon and pointer thumbnails are larger than the size we want so they were being scaled. None of the menu content was being scaled at all. Simplified image loading a bit also.
1 parent 4b2713d commit 89c8daf

File tree

2 files changed

+45
-76
lines changed

2 files changed

+45
-76
lines changed

files/usr/share/cinnamon/cinnamon-settings/bin/ChooserButtonWidgets.py

Lines changed: 44 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -113,40 +113,26 @@ def reset_loading_progress(self):
113113
self.progress = 0.0
114114
self.queue_draw()
115115

116-
def set_picture_from_file (self, path):
117-
pixbuf = None
118-
message = ""
119-
120-
if os.path.exists(path):
121-
try:
122-
pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
123-
except GLib.Error as e:
124-
message = "Could not load pixbuf from '%s': %s" % (path, e.message)
125-
error = True
126-
127-
if pixbuf is not None:
128-
h = pixbuf.get_height()
129-
w = pixbuf.get_width()
130-
131-
if self.keep_square and (h > self.button_picture_size or w > self.button_picture_size):
132-
try:
133-
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(path, self.button_picture_size * self.scale, self.button_picture_size * self.scale)
134-
except GLib.Error as e:
135-
message = "Could not scale pixbuf from '%s': %s" % (path, e.message)
136-
error = True
137-
elif h > self.button_picture_size:
138-
try:
139-
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(path, -1, self.button_picture_size * self.scale)
140-
except GLib.Error as e:
141-
message = "Could not scale pixbuf from '%s': %s" % (path, e.message)
142-
error = True
116+
def create_scaled_surface(self, path):
117+
w = -1 if not self.keep_square else self.button_picture_size * self.scale
118+
h = self.button_picture_size * self.scale
119+
120+
try:
121+
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(path, w, h)
122+
except GLib.Error as e:
123+
print("Could not load thumbnail file '%s': %s" % (path, e.message))
143124

144125
if pixbuf:
145-
surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, self.scale)
126+
return Gdk.cairo_surface_create_from_pixbuf(pixbuf, self.scale)
127+
else:
128+
return None
129+
130+
def set_picture_from_file (self, path):
131+
surface = self.create_scaled_surface(path)
132+
if surface:
146133
self.button_image.set_from_surface(surface)
147134
else:
148-
print(message)
149-
self.set_picture_from_file("/usr/share/cinnamon/faces/user-generic.png")
135+
self.button_image.set_from_icon_name("user-generic", Gtk.IconSize.BUTTON)
150136

151137
def set_button_label(self, label):
152138
self.button_label.set_markup(label)
@@ -168,51 +154,34 @@ def clear_menu(self):
168154
menu.destroy()
169155

170156
def add_picture(self, path, callback, title=None, id=None):
171-
pixbuf = None
172-
if os.path.exists(path):
173-
try:
174-
pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
175-
except GLib.Error as e:
176-
message = "Could not load pixbuf from '%s': %s" % (path, e.message)
177-
error = True
178-
179-
if pixbuf is not None:
180-
h = pixbuf.get_height()
181-
w = pixbuf.get_width()
182-
183-
try:
184-
if self.menu_pictures_size is None:
185-
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(path, w, h)
186-
else:
187-
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(path, -1, self.menu_pictures_size)
188-
except GLib.Error as e:
189-
print('Caught GLib.Error exception: {}\npath: {}'.format(e, str(path)))
190-
191-
if pixbuf is None:
192-
return
193-
194-
surface = Gdk.cairo_surface_create_from_pixbuf(pixbuf, self.scale)
195-
image = Gtk.Image()
196-
image.set_size_request(self.menu_pictures_size / self.scale, self.menu_pictures_size / self.scale)
197-
image.set_from_surface(surface)
198-
menuitem = Gtk.MenuItem()
199-
if title is not None:
200-
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=2)
201-
vbox.add(image)
202-
label = Gtk.Label()
203-
label.set_text(title)
204-
vbox.add(label)
205-
menuitem.add(vbox)
206-
else:
207-
menuitem.add(image)
208-
if id is not None:
209-
menuitem.connect('activate', self._on_picture_selected, path, callback, id)
210-
else:
211-
menuitem.connect('activate', self._on_picture_selected, path, callback)
212-
self.menu.attach(menuitem, self.col, self.col+1, self.row, self.row+1)
213-
self.col = (self.col+1) % self.num_cols
214-
if self.col == 0:
215-
self.row = self.row + 1
157+
image = Gtk.Image()
158+
image.set_size_request(-1, self.menu_pictures_size / self.scale)
159+
160+
surface = self.create_scaled_surface(path)
161+
162+
if surface:
163+
image.set_from_surface(surface)
164+
else:
165+
image.set_from_icon_name("user-generic", Gtk.IconSize.BUTTON)
166+
167+
menuitem = Gtk.MenuItem()
168+
if title is not None:
169+
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=2)
170+
vbox.add(image)
171+
label = Gtk.Label()
172+
label.set_text(title)
173+
vbox.add(label)
174+
menuitem.add(vbox)
175+
else:
176+
menuitem.add(image)
177+
if id is not None:
178+
menuitem.connect('activate', self._on_picture_selected, path, callback, id)
179+
else:
180+
menuitem.connect('activate', self._on_picture_selected, path, callback)
181+
self.menu.attach(menuitem, self.col, self.col+1, self.row, self.row+1)
182+
self.col = (self.col+1) % self.num_cols
183+
if self.col == 0:
184+
self.row = self.row + 1
216185

217186
def add_separator(self):
218187
self.row = self.row + 1

files/usr/share/cinnamon/cinnamon-settings/modules/cs_themes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def create_button_chooser(self, settings, key, path_prefix, path_suffix, button_
313313
chooser.set_picture_from_file("/usr/share/cinnamon/theme/thumbnail.png")
314314
elif path_suffix == "icons":
315315
current_theme = Gtk.IconTheme.get_default()
316-
folder = current_theme.lookup_icon("folder", button_picture_size, 0)
316+
folder = current_theme.lookup_icon_for_scale("folder", button_picture_size, self.window.get_scale_factor(), 0)
317317
if folder is not None:
318318
path = folder.get_filename()
319319
chooser.set_picture_from_file(path)

0 commit comments

Comments
 (0)