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

PiP: Mask window #2143

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
184 changes: 184 additions & 0 deletions data/confirm.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions data/gala.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<gresource prefix="/org/pantheon/desktop/gala">
<file alias="buttons/close.svg" compressed="true" preprocess="xml-stripblanks">close.svg</file>
<file alias="buttons/resize.svg" compressed="true" preprocess="xml-stripblanks">resize.svg</file>
<file alias="buttons/confirm.svg" compressed="true" preprocess="xml-stripblanks">confirm.svg</file>
</gresource>
<gresource prefix="/io/elementary/desktop/gala">
<file compressed="true">shaders/colorblindness-correction.vert</file>
Expand Down
31 changes: 31 additions & 0 deletions lib/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Gala {
}

private static Gee.HashMap<int, Gdk.Pixbuf?>? resize_pixbufs = null;
private static Gee.HashMap<int, Gdk.Pixbuf?>? confirm_pixbufs = null;

private static Gee.HashMultiMap<DesktopAppInfo, CachedIcon?> icon_cache;
private static Gee.HashMap<Meta.Window, DesktopAppInfo> window_to_desktop_cache;
Expand Down Expand Up @@ -370,6 +371,36 @@ namespace Gala {
return resize_pixbufs[height];
}

/**
* Returns the pixbuf that is used for confirm buttons throughout gala at a
* size of 36px
*
* @return the confirm button pixbuf or null if it failed to load
*/
public static Gdk.Pixbuf? get_confirm_button_pixbuf (float scale) {
var height = scale_to_int (36, scale);

if (confirm_pixbufs == null) {
confirm_pixbufs = new Gee.HashMap<int, Gdk.Pixbuf?> ();
}

if (confirm_pixbufs[height] == null) {
try {
confirm_pixbufs[height] = new Gdk.Pixbuf.from_resource_at_scale (
Config.RESOURCEPATH + "/buttons/resize.svg",
-1,
height,
true
);
} catch (Error e) {
warning (e.message);
return null;
}
}

return confirm_pixbufs[height];
}

/**
* Creates a new reactive ClutterActor at 36px with the resize pixbuf
*
Expand Down
106 changes: 24 additions & 82 deletions plugins/pip/Main.vala
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
//
// Copyright (C) 2017 Adam Bieńkowski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// 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,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2017 Adam Bieńkowski
* 2024 elementary, Inc. (https://elementary.io)
*/

public class Gala.Plugins.PIP.Plugin : Gala.Plugin {
private const int MIN_SELECTION_SIZE = 30;
Expand All @@ -22,15 +11,6 @@ public class Gala.Plugins.PIP.Plugin : Gala.Plugin {
private Gala.WindowManager? wm = null;
private SelectionArea? selection_area;

#if HAS_MUTTER45
private static inline bool meta_rectangle_contains (Mtk.Rectangle rect, int x, int y) {
#else
private static inline bool meta_rectangle_contains (Meta.Rectangle rect, int x, int y) {
#endif
return x >= rect.x && x < rect.x + rect.width
&& y >= rect.y && y < rect.y + rect.height;
}

construct {
windows = new Gee.ArrayList<PopupWindow> ();
}
Expand All @@ -56,8 +36,12 @@ public class Gala.Plugins.PIP.Plugin : Gala.Plugin {
[CCode (instance_pos = -1)]
private void on_initiate (Meta.Display display, Meta.Window? window, Clutter.KeyEvent event,
Meta.KeyBinding binding) {
selection_area = new SelectionArea (wm);
selection_area.selected.connect (on_selection_actor_selected);
var target_actor = get_active_window_actor ();
if (target_actor == null) {
return;
}

selection_area = new SelectionArea (wm, target_actor);
selection_area.captured.connect (on_selection_actor_captured);
selection_area.closed.connect (clear_selection_area);

Expand All @@ -67,37 +51,29 @@ public class Gala.Plugins.PIP.Plugin : Gala.Plugin {
selection_area.start_selection ();
}

private void on_selection_actor_selected (int x, int y) {
clear_selection_area ();
select_window_at (x, y);
}

private void on_selection_actor_captured (int x, int y, int width, int height) {
clear_selection_area ();

if (width < MIN_SELECTION_SIZE || height < MIN_SELECTION_SIZE) {
select_window_at (x, y);
} else {
var active = get_active_window_actor ();
if (active != null) {
int point_x = x - (int)active.x;
int point_y = y - (int)active.y;
var active = get_active_window_actor ();
if (active != null) {
var window = active.get_meta_window ();
var frame = window.get_frame_rect ();
var popup_window = new PopupWindow (wm.get_display (), active);

// Compensate for server-side window decorations
var input_rect = active.get_meta_window ().get_buffer_rect ();
var outer_rect = active.get_meta_window ().get_frame_rect ();
point_x -= outer_rect.x - input_rect.x;
point_y -= outer_rect.y - input_rect.y;
// Don't clip if the entire window was selected
if (frame.x != x || frame.y != y || frame.width != width || frame.height != height) {
int point_x = x - (int)frame.x;
int point_y = y - (int)frame.y;

var rect = Graphene.Rect.alloc ();
rect.init (point_x, point_y, width, height);

var popup_window = new PopupWindow (wm.get_display (), active);
popup_window.set_container_clip (rect);
popup_window.show.connect (on_popup_window_show);
popup_window.hide.connect (on_popup_window_hide);
add_window (popup_window);
}

popup_window.show.connect (on_popup_window_show);
popup_window.hide.connect (on_popup_window_hide);
add_window (popup_window);
}
}

Expand All @@ -111,16 +87,6 @@ public class Gala.Plugins.PIP.Plugin : Gala.Plugin {
update_region ();
}

private void select_window_at (int x, int y) {
var selected = get_window_actor_at (x, y);
if (selected != null) {
var popup_window = new PopupWindow (wm.get_display (), selected);
popup_window.show.connect (on_popup_window_show);
popup_window.hide.connect (on_popup_window_hide);
add_window (popup_window);
}
}

private void clear_selection_area () {
if (selection_area != null) {
untrack_actor (selection_area);
Expand All @@ -131,30 +97,6 @@ public class Gala.Plugins.PIP.Plugin : Gala.Plugin {
}
}

private Meta.WindowActor? get_window_actor_at (int x, int y) {
unowned Meta.Display display = wm.get_display ();
unowned List<Meta.WindowActor> actors = display.get_window_actors ();

var copy = actors.copy ();
copy.reverse ();

weak Meta.WindowActor? selected = null;
copy.@foreach ((actor) => {
if (selected != null) {
return;
}

var window = actor.get_meta_window ();
var rect = window.get_frame_rect ();

if (!actor.is_destroyed () && !window.is_hidden () && !window.is_skip_taskbar () && meta_rectangle_contains (rect, x, y)) {
selected = actor;
}
});

return selected;
}

private Meta.WindowActor? get_active_window_actor () {
unowned Meta.Display display = wm.get_display ();
unowned List<Meta.WindowActor> actors = display.get_window_actors ();
Expand Down
Loading
Loading