Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force scaling icon #812

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 111 additions & 16 deletions src/modules/tray/icon.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::image::ImageProvider;
use crate::modules::tray::interface::TrayMenu;
use cairo::ImageSurface;
use color_eyre::{Report, Result};
use glib::ffi::g_strfreev;
use glib::translate::ToGlibPtr;
use gtk::ffi::gtk_icon_theme_get_search_path;
use gtk::gdk_pixbuf::{Colorspace, InterpType, Pixbuf};
use gtk::prelude::IconThemeExt;
use gtk::gdk_pixbuf::{Colorspace, Pixbuf};
use gtk::prelude::{GdkContextExt, IconThemeExt};
use gtk::{IconLookupFlags, IconTheme, Image};
use std::collections::HashSet;
use std::ffi::CStr;
use std::os::raw::{c_char, c_int};
use std::ptr;
use system_tray::item::IconPixmap;

/// Gets the GTK icon theme search paths by calling the FFI function.
/// Conveniently returns the result as a `HashSet`.
Expand Down Expand Up @@ -44,17 +46,42 @@ pub fn get_image(
size: u32,
prefer_icons: bool,
) -> Result<Image> {
if !prefer_icons && item.icon_pixmap.is_some() {
let pixbuf = if !prefer_icons && item.icon_pixmap.is_some() {
get_image_from_pixmap(item, size)
} else {
get_image_from_icon_name(item, icon_theme, size)
.or_else(|_| get_image_from_pixmap(item, size))
}
}?;

let image = if pixbuf.height() == size as i32 {
let image = Image::new();
ImageProvider::create_and_load_surface(&pixbuf, &image)?;
image
} else {
Image::from_surface(Some(&scale_image_to_height(pixbuf, size as i32)))
};

Ok(image)
}

fn scale_image_to_height(pixbuf: Pixbuf, size: i32) -> ImageSurface {
let scale = size as f64 / pixbuf.height() as f64;
let width = (pixbuf.width() as f64 * scale).ceil() as i32;
let height = (pixbuf.height() as f64 * scale).ceil() as i32;

let surf = ImageSurface::create(cairo::Format::ARgb32, width, height).unwrap();
let context = cairo::Context::new(&surf).unwrap();

context.scale(scale, scale);
context.set_source_pixbuf(&pixbuf, 0., 0.);
context.paint().unwrap();

surf
}

/// Attempts to get a GTK `Image` component
/// for the status notifier item's icon.
fn get_image_from_icon_name(item: &TrayMenu, icon_theme: &IconTheme, size: u32) -> Result<Image> {
fn get_image_from_icon_name(item: &TrayMenu, icon_theme: &IconTheme, size: u32) -> Result<Pixbuf> {
if let Some(path) = item.icon_theme_path.as_ref() {
if !path.is_empty() && !get_icon_theme_search_paths(icon_theme).contains(path) {
icon_theme.append_search_path(path);
Expand All @@ -67,9 +94,7 @@ fn get_image_from_icon_name(item: &TrayMenu, icon_theme: &IconTheme, size: u32)

if let Some(icon_info) = icon_info {
let pixbuf = icon_info.load_icon()?;
let image = Image::new();
ImageProvider::create_and_load_surface(&pixbuf, &image)?;
Ok(image)
Ok(pixbuf)
} else {
Err(Report::msg("could not find icon"))
}
Expand All @@ -81,13 +106,15 @@ fn get_image_from_icon_name(item: &TrayMenu, icon_theme: &IconTheme, size: u32)
/// which has 8 bits per sample and a bit stride of `4*width`.
/// The Pixbuf expects RGBA32 format, so some channel shuffling
/// is required.
fn get_image_from_pixmap(item: &TrayMenu, size: u32) -> Result<Image> {
fn get_image_from_pixmap(item: &TrayMenu, size: u32) -> Result<Pixbuf> {
const BITS_PER_SAMPLE: i32 = 8;

let pixmap = item
.icon_pixmap
.as_ref()
.and_then(|pixmap| pixmap.first())
// The vec is sorted(ASC) with size(width==height) most of the time,
// but we can not be sure that it'll always sorted by `height`
.and_then(|pixmap| find_approx_height(pixmap, size as i32))
.ok_or_else(|| Report::msg("Failed to get pixmap from tray icon"))?;

if pixmap.width == 0 || pixmap.height == 0 {
Expand Down Expand Up @@ -117,11 +144,79 @@ fn get_image_from_pixmap(item: &TrayMenu, size: u32) -> Result<Image> {
row_stride,
);

let pixbuf = pixbuf
.scale_simple(size as i32, size as i32, InterpType::Bilinear)
.unwrap_or(pixbuf);
Ok(pixbuf)
}

let image = Image::new();
ImageProvider::create_and_load_surface(&pixbuf, &image)?;
Ok(image)
/// finds the pixmap
/// which is the smallest but bigger than wanted
/// or
/// the biggest of all if no bigger than wanted
///
/// O(n)
fn find_approx_height(v: &[IconPixmap], size: i32) -> Option<&IconPixmap> {
if v.is_empty() {
return None;
}
if v.len() == 1 {
return v.first();
}

let mut approx = &v[0];

for p in &v[1..] {
// p bigger than wanted size
// and then we check for
// `approx` is smaller than wanted || p smaller than `approx`
if (p.height >= size && (approx.height < size || p.height < approx.height))
// or p smaller than wanted
// but bigger than `approx`
|| (p.height < size && p.height > approx.height)
{
approx = p;
}
}

Some(approx)
}

mod tests {

#[test]
fn test_find_approx_height() {
use super::{find_approx_height, IconPixmap};

macro_rules! make_list {
($heights:expr) => {
$heights
.iter()
.map(|height| IconPixmap {
width: 0,
height: *height,
pixels: vec![],
})
.collect::<Vec<IconPixmap>>()
};
}
macro_rules! assert_correct {
($list:expr, $height:expr, $index:expr) => {
assert_eq!(
find_approx_height(&$list, $height).unwrap().height,
$list[$index].height
);
};
}

let list = make_list!([10, 20, 50, 40, 30]);
assert_correct!(list, 1, 0);
assert_correct!(list, 10, 0);
assert_correct!(list, 11, 1);
assert_correct!(list, 20, 1);
assert_correct!(list, 21, 4);
assert_correct!(list, 30, 4);
assert_correct!(list, 31, 3);
assert_correct!(list, 40, 3);
assert_correct!(list, 41, 2);
assert_correct!(list, 50, 2);
assert_correct!(list, 51, 2);
}
}
Loading