WindowIcon: Cleanup (#1561)

This commit is contained in:
Leo 2023-02-22 14:28:31 +09:00 committed by GitHub
parent b0b0f8025a
commit 0605f63c49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,87 +1,43 @@
// /*
// Copyright (C) 2012 Tom Beckmann, Rico Tzschichholz * Copyright 2012 Tom Beckmann
// * Copyright 2012 Rico Tzschichholz
// This program is free software: you can redistribute it and/or modify * Copyright 2023 elementary, Inc. <https://elementary.io>
// it under the terms of the GNU General Public License as published by * SPDX-License-Identifier: GPL-3.0-or-later
// the Free Software Foundation, either version 3 of the License, or */
// (at your option) any later version.
// /**
// This program is distributed in the hope that it will be useful, * Creates a new ClutterTexture with an icon for the window at the given size.
// but WITHOUT ANY WARRANTY; without even the implied warranty of * This is recommended way to grab an icon for a window as this method will make
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * sure the icon is updated if it becomes available at a later point.
// GNU General Public License for more details. */
// public class Gala.WindowIcon : Clutter.Actor {
// You should have received a copy of the GNU General Public License public Meta.Window window { get; construct; }
// along with this program. If not, see <http://www.gnu.org/licenses/>. public int icon_size { get; construct; }
// public int scale { get; construct; }
namespace Gala {
/** /**
* Creates a new ClutterTexture with an icon for the window at the given size. * Creates a new WindowIcon
* This is recommended way to grab an icon for a window as this method will make *
* sure the icon is updated if it becomes available at a later point. * @param window The window for which to create the icon
* @param icon_size The size of the icon in pixels
* @param scale The desired scale of the icon
*/ */
public class WindowIcon : Clutter.Actor { public WindowIcon (Meta.Window window, int icon_size, int scale = 1) {
public Meta.Window window { get; construct; } Object (window: window,
public int icon_size { get; construct; } icon_size: icon_size,
public int scale { get; construct; } scale: scale);
}
/** construct {
* If set to true, the SafeWindowClone will destroy itself when the connected width = icon_size * scale;
* window is unmanaged height = icon_size * scale;
*/
public bool destroy_on_unmanaged {
get {
return _destroy_on_unmanaged;
}
construct set {
if (_destroy_on_unmanaged == value)
return;
_destroy_on_unmanaged = value; var pixbuf = Gala.Utils.get_icon_for_window (window, icon_size, scale);
if (_destroy_on_unmanaged) try {
window.unmanaged.connect (unmanaged); var image = new Clutter.Image ();
else Cogl.PixelFormat pixel_format = (pixbuf.get_has_alpha () ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888);
window.unmanaged.disconnect (unmanaged); image.set_data (pixbuf.get_pixels (), pixel_format, pixbuf.width, pixbuf.height, pixbuf.rowstride);
} set_content (image);
} } catch (Error e) {}
private bool _destroy_on_unmanaged = false;
/**
* Creates a new WindowIcon
*
* @param window The window for which to create the icon
* @param icon_size The size of the icon in pixels
* @param scale The desired scale of the icon
* @param destroy_on_unmanaged see destroy_on_unmanaged property
*/
public WindowIcon (Meta.Window window, int icon_size, int scale = 1, bool destroy_on_unmanaged = false) {
Object (window: window,
icon_size: icon_size,
destroy_on_unmanaged: destroy_on_unmanaged,
scale: scale);
}
construct {
width = icon_size * scale;
height = icon_size * scale;
update_texture (true);
}
private void update_texture (bool initial) {
var pixbuf = Gala.Utils.get_icon_for_window (window, icon_size, scale);
try {
var image = new Clutter.Image ();
Cogl.PixelFormat pixel_format = (pixbuf.get_has_alpha () ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888);
image.set_data (pixbuf.get_pixels (), pixel_format, pixbuf.width, pixbuf.height, pixbuf.rowstride);
set_content (image);
} catch (Error e) {}
}
void unmanaged (Meta.Window window) {
destroy ();
}
} }
} }